inet_ntop.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /*
  2. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (c) 1996-1999 by Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  10. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  11. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  12. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  13. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  14. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  15. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "ares_setup.h"
  18. #ifdef HAVE_NETINET_IN_H
  19. # include <netinet/in.h>
  20. #endif
  21. #ifdef HAVE_ARPA_INET_H
  22. # include <arpa/inet.h>
  23. #endif
  24. #ifdef HAVE_ARPA_NAMESER_H
  25. # include <arpa/nameser.h>
  26. #else
  27. # include "nameser.h"
  28. #endif
  29. #ifdef HAVE_ARPA_NAMESER_COMPAT_H
  30. # include <arpa/nameser_compat.h>
  31. #endif
  32. #include "ares.h"
  33. #include "ares_ipv6.h"
  34. #ifndef HAVE_INET_NTOP
  35. /*
  36. * WARNING: Don't even consider trying to compile this on a system where
  37. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  38. */
  39. static const char *inet_ntop4(const unsigned char *src, char *dst, size_t size);
  40. static const char *inet_ntop6(const unsigned char *src, char *dst, size_t size);
  41. /* char *
  42. * inet_ntop(af, src, dst, size)
  43. * convert a network format address to presentation format.
  44. * return:
  45. * pointer to presentation format address (`dst'), or NULL (see errno).
  46. * note:
  47. * On Windows we store the error in the thread errno, not
  48. * in the winsock error code. This is to avoid loosing the
  49. * actual last winsock error. So use macro ERRNO to fetch the
  50. * errno this function sets when returning NULL, not SOCKERRNO.
  51. * author:
  52. * Paul Vixie, 1996.
  53. */
  54. const char *
  55. ares_inet_ntop(int af, const void *src, char *dst, ares_socklen_t size)
  56. {
  57. switch (af) {
  58. case AF_INET:
  59. return (inet_ntop4(src, dst, (size_t)size));
  60. case AF_INET6:
  61. return (inet_ntop6(src, dst, (size_t)size));
  62. default:
  63. SET_ERRNO(EAFNOSUPPORT);
  64. return (NULL);
  65. }
  66. /* NOTREACHED */
  67. }
  68. /* const char *
  69. * inet_ntop4(src, dst, size)
  70. * format an IPv4 address
  71. * return:
  72. * `dst' (as a const)
  73. * notes:
  74. * (1) uses no statics
  75. * (2) takes a unsigned char* not an in_addr as input
  76. * author:
  77. * Paul Vixie, 1996.
  78. */
  79. static const char *
  80. inet_ntop4(const unsigned char *src, char *dst, size_t size)
  81. {
  82. static const char fmt[] = "%u.%u.%u.%u";
  83. char tmp[sizeof("255.255.255.255")];
  84. if ((size_t)sprintf(tmp, fmt, src[0], src[1], src[2], src[3]) >= size) {
  85. SET_ERRNO(ENOSPC);
  86. return (NULL);
  87. }
  88. strcpy(dst, tmp);
  89. return (dst);
  90. }
  91. /* const char *
  92. * inet_ntop6(src, dst, size)
  93. * convert IPv6 binary address into presentation (printable) format
  94. * author:
  95. * Paul Vixie, 1996.
  96. */
  97. static const char *
  98. inet_ntop6(const unsigned char *src, char *dst, size_t size)
  99. {
  100. /*
  101. * Note that int32_t and int16_t need only be "at least" large enough
  102. * to contain a value of the specified size. On some systems, like
  103. * Crays, there is no such thing as an integer variable with 16 bits.
  104. * Keep this in mind if you think this function should have been coded
  105. * to use pointer overlays. All the world's not a VAX.
  106. */
  107. char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  108. char *tp;
  109. struct { int base, len; } best, cur;
  110. unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
  111. int i;
  112. /*
  113. * Preprocess:
  114. * Copy the input (bytewise) array into a wordwise array.
  115. * Find the longest run of 0x00's in src[] for :: shorthanding.
  116. */
  117. memset(words, '\0', sizeof(words));
  118. for (i = 0; i < NS_IN6ADDRSZ; i++)
  119. words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  120. best.base = -1;
  121. best.len = 0;
  122. cur.base = -1;
  123. cur.len = 0;
  124. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  125. if (words[i] == 0) {
  126. if (cur.base == -1)
  127. cur.base = i, cur.len = 1;
  128. else
  129. cur.len++;
  130. } else {
  131. if (cur.base != -1) {
  132. if (best.base == -1 || cur.len > best.len)
  133. best = cur;
  134. cur.base = -1;
  135. }
  136. }
  137. }
  138. if (cur.base != -1) {
  139. if (best.base == -1 || cur.len > best.len)
  140. best = cur;
  141. }
  142. if (best.base != -1 && best.len < 2)
  143. best.base = -1;
  144. /*
  145. * Format the result.
  146. */
  147. tp = tmp;
  148. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  149. /* Are we inside the best run of 0x00's? */
  150. if (best.base != -1 && i >= best.base &&
  151. i < (best.base + best.len)) {
  152. if (i == best.base)
  153. *tp++ = ':';
  154. continue;
  155. }
  156. /* Are we following an initial run of 0x00s or any real hex? */
  157. if (i != 0)
  158. *tp++ = ':';
  159. /* Is this address an encapsulated IPv4? */
  160. if (i == 6 && best.base == 0 && (best.len == 6 ||
  161. (best.len == 7 && words[7] != 0x0001) ||
  162. (best.len == 5 && words[5] == 0xffff))) {
  163. if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
  164. return (NULL);
  165. tp += strlen(tp);
  166. break;
  167. }
  168. tp += sprintf(tp, "%x", words[i]);
  169. }
  170. /* Was it a trailing run of 0x00's? */
  171. if (best.base != -1 && (best.base + best.len) == (NS_IN6ADDRSZ / NS_INT16SZ))
  172. *tp++ = ':';
  173. *tp++ = '\0';
  174. /*
  175. * Check for overflow, copy, and we're done.
  176. */
  177. if ((size_t)(tp - tmp) > size) {
  178. SET_ERRNO(ENOSPC);
  179. return (NULL);
  180. }
  181. strcpy(dst, tmp);
  182. return (dst);
  183. }
  184. #else /* HAVE_INET_NTOP */
  185. const char *
  186. ares_inet_ntop(int af, const void *src, char *dst, ares_socklen_t size)
  187. {
  188. /* just relay this to the underlying function */
  189. return inet_ntop(af, src, dst, size);
  190. }
  191. #endif /* HAVE_INET_NTOP */