inet_ntop.c 6.0 KB

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