ng_ipaddr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * (c) 2008 Florian Westphal <fw@strlen.de>, public domain.
  3. */
  4. #include "portab.h"
  5. /**
  6. * @file
  7. * Functions for AF_ agnostic ipv4/ipv6 handling.
  8. */
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12. #ifdef HAVE_GETADDRINFO
  13. #include <netdb.h>
  14. #include <sys/types.h>
  15. #endif
  16. #include "ng_ipaddr.h"
  17. GLOBAL bool
  18. ng_ipaddr_init(ng_ipaddr_t *addr, const char *ip_str, UINT16 port)
  19. {
  20. #ifdef HAVE_GETADDRINFO
  21. int ret;
  22. char portstr[64];
  23. struct addrinfo *res0;
  24. struct addrinfo hints;
  25. assert(ip_str);
  26. memset(&hints, 0, sizeof(hints));
  27. #ifdef AI_NUMERICHOST
  28. hints.ai_flags = AI_NUMERICHOST;
  29. #endif
  30. #ifndef WANT_IPV6 /* do not convert ipv6 addresses */
  31. hints.ai_family = AF_INET;
  32. #endif
  33. /* some getaddrinfo implementations require that ai_socktype is set. */
  34. hints.ai_socktype = SOCK_STREAM;
  35. /* silly, but ngircd stores UINT16 in server config, not string */
  36. snprintf(portstr, sizeof(portstr), "%u", (unsigned int) port);
  37. ret = getaddrinfo(ip_str, portstr, &hints, &res0);
  38. if (ret != 0)
  39. return false;
  40. assert(sizeof(*addr) >= (size_t)res0->ai_addrlen);
  41. if (sizeof(*addr) >= (size_t)res0->ai_addrlen)
  42. memcpy(addr, res0->ai_addr, res0->ai_addrlen);
  43. else
  44. ret = -1;
  45. freeaddrinfo(res0);
  46. return ret == 0;
  47. #else /* HAVE_GETADDRINFO */
  48. assert(ip_str);
  49. memset(addr, 0, sizeof *addr);
  50. #ifdef HAVE_sockaddr_in_len
  51. addr->sin4.sin_len = sizeof(addr->sin4);
  52. #endif
  53. addr->sin4.sin_family = AF_INET;
  54. # ifdef HAVE_INET_ATON
  55. if (inet_aton(ip_str, &addr->sin4.sin_addr) == 0)
  56. return false;
  57. # else
  58. addr->sin4.sin_addr.s_addr = inet_addr(ip_str);
  59. if (addr->sin4.sin_addr.s_addr == (unsigned) -1)
  60. return false;
  61. # endif
  62. ng_ipaddr_setport(addr, port);
  63. return true;
  64. #endif /* HAVE_GETADDRINFO */
  65. }
  66. GLOBAL void
  67. ng_ipaddr_setport(ng_ipaddr_t *a, UINT16 port)
  68. {
  69. #ifdef WANT_IPV6
  70. int af;
  71. assert(a != NULL);
  72. af = a->sa.sa_family;
  73. assert(af == AF_INET || af == AF_INET6);
  74. switch (af) {
  75. case AF_INET:
  76. a->sin4.sin_port = htons(port);
  77. break;
  78. case AF_INET6:
  79. a->sin6.sin6_port = htons(port);
  80. break;
  81. }
  82. #else /* WANT_IPV6 */
  83. assert(a != NULL);
  84. assert(a->sin4.sin_family == AF_INET);
  85. a->sin4.sin_port = htons(port);
  86. #endif /* WANT_IPV6 */
  87. }
  88. GLOBAL bool
  89. ng_ipaddr_ipequal(const ng_ipaddr_t *a, const ng_ipaddr_t *b)
  90. {
  91. assert(a != NULL);
  92. assert(b != NULL);
  93. #ifdef WANT_IPV6
  94. if (a->sa.sa_family != b->sa.sa_family)
  95. return false;
  96. assert(ng_ipaddr_salen(a) == ng_ipaddr_salen(b));
  97. switch (a->sa.sa_family) {
  98. case AF_INET6:
  99. return IN6_ARE_ADDR_EQUAL(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
  100. case AF_INET:
  101. return memcmp(&a->sin4.sin_addr, &b->sin4.sin_addr, sizeof(a->sin4.sin_addr)) == 0;
  102. }
  103. return false;
  104. #else
  105. assert(a->sin4.sin_family == AF_INET);
  106. assert(b->sin4.sin_family == AF_INET);
  107. return memcmp(&a->sin4.sin_addr, &b->sin4.sin_addr, sizeof(a->sin4.sin_addr)) == 0;
  108. #endif
  109. }
  110. #ifdef WANT_IPV6
  111. GLOBAL const char *
  112. ng_ipaddr_tostr(const ng_ipaddr_t *addr)
  113. {
  114. static char strbuf[NG_INET_ADDRSTRLEN];
  115. strbuf[0] = 0;
  116. ng_ipaddr_tostr_r(addr, strbuf);
  117. return strbuf;
  118. }
  119. /* str must be at least NG_INET_ADDRSTRLEN bytes long */
  120. GLOBAL bool
  121. ng_ipaddr_tostr_r(const ng_ipaddr_t *addr, char *str)
  122. {
  123. #ifdef HAVE_GETNAMEINFO
  124. const struct sockaddr *sa = (const struct sockaddr *) addr;
  125. int ret;
  126. *str = 0;
  127. ret = getnameinfo(sa, ng_ipaddr_salen(addr),
  128. str, NG_INET_ADDRSTRLEN, NULL, 0, NI_NUMERICHOST);
  129. /*
  130. * avoid leading ':'.
  131. * causes mis-interpretation of client host in e.g. /WHOIS
  132. */
  133. if (*str == ':') {
  134. char tmp[NG_INET_ADDRSTRLEN] = "0";
  135. ret = getnameinfo(sa, ng_ipaddr_salen(addr),
  136. tmp + 1, (socklen_t)sizeof(tmp) - 1,
  137. NULL, 0, NI_NUMERICHOST);
  138. if (ret == 0)
  139. strlcpy(str, tmp, NG_INET_ADDRSTRLEN);
  140. }
  141. assert (ret == 0);
  142. return ret == 0;
  143. #else
  144. abort(); /* WANT_IPV6 depends on HAVE_GETNAMEINFO */
  145. #endif
  146. }
  147. #endif /* WANT_IPV6 */
  148. /* -eof- */