inet_ntop.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. /* $OpenBSD: inet_ntop.c,v 1.8 2008/12/09 19:38:38 otto Exp $ */
  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. /* OPENBSD ORIGINAL: lib/libc/net/inet_ntop.c */
  18. #include "includes.h"
  19. #ifndef HAVE_INET_NTOP
  20. #include <sys/param.h>
  21. #include <sys/types.h>
  22. #include <sys/socket.h>
  23. #include <netinet/in.h>
  24. #include <arpa/inet.h>
  25. #include <arpa/nameser.h>
  26. #include <string.h>
  27. #include <errno.h>
  28. #include <stdio.h>
  29. #ifndef IN6ADDRSZ
  30. #define IN6ADDRSZ 16 /* IPv6 T_AAAA */
  31. #endif
  32. #ifndef INT16SZ
  33. #define INT16SZ 2 /* for systems without 16-bit ints */
  34. #endif
  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 u_char *src, char *dst, size_t size);
  40. static const char *inet_ntop6(const u_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. * author:
  47. * Paul Vixie, 1996.
  48. */
  49. const char *
  50. inet_ntop(int af, const void *src, char *dst, socklen_t size)
  51. {
  52. switch (af) {
  53. case AF_INET:
  54. return (inet_ntop4(src, dst, (size_t)size));
  55. case AF_INET6:
  56. return (inet_ntop6(src, dst, (size_t)size));
  57. default:
  58. errno = EAFNOSUPPORT;
  59. return (NULL);
  60. }
  61. /* NOTREACHED */
  62. }
  63. /* const char *
  64. * inet_ntop4(src, dst, size)
  65. * format an IPv4 address, more or less like inet_ntoa()
  66. * return:
  67. * `dst' (as a const)
  68. * notes:
  69. * (1) uses no statics
  70. * (2) takes a u_char* not an in_addr as input
  71. * author:
  72. * Paul Vixie, 1996.
  73. */
  74. static const char *
  75. inet_ntop4(const u_char *src, char *dst, size_t size)
  76. {
  77. static const char fmt[] = "%u.%u.%u.%u";
  78. char tmp[sizeof "255.255.255.255"];
  79. int l;
  80. l = snprintf(tmp, size, fmt, src[0], src[1], src[2], src[3]);
  81. if (l <= 0 || l >= size) {
  82. errno = ENOSPC;
  83. return (NULL);
  84. }
  85. strlcpy(dst, tmp, size);
  86. return (dst);
  87. }
  88. /* const char *
  89. * inet_ntop6(src, dst, size)
  90. * convert IPv6 binary address into presentation (printable) format
  91. * author:
  92. * Paul Vixie, 1996.
  93. */
  94. static const char *
  95. inet_ntop6(const u_char *src, char *dst, size_t size)
  96. {
  97. /*
  98. * Note that int32_t and int16_t need only be "at least" large enough
  99. * to contain a value of the specified size. On some systems, like
  100. * Crays, there is no such thing as an integer variable with 16 bits.
  101. * Keep this in mind if you think this function should have been coded
  102. * to use pointer overlays. All the world's not a VAX.
  103. */
  104. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"];
  105. char *tp, *ep;
  106. struct { int base, len; } best, cur;
  107. u_int words[IN6ADDRSZ / INT16SZ];
  108. int i;
  109. int advance;
  110. /*
  111. * Preprocess:
  112. * Copy the input (bytewise) array into a wordwise array.
  113. * Find the longest run of 0x00's in src[] for :: shorthanding.
  114. */
  115. memset(words, '\0', sizeof words);
  116. for (i = 0; i < IN6ADDRSZ; i++)
  117. words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  118. best.base = -1;
  119. cur.base = -1;
  120. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
  121. if (words[i] == 0) {
  122. if (cur.base == -1)
  123. cur.base = i, cur.len = 1;
  124. else
  125. cur.len++;
  126. } else {
  127. if (cur.base != -1) {
  128. if (best.base == -1 || cur.len > best.len)
  129. best = cur;
  130. cur.base = -1;
  131. }
  132. }
  133. }
  134. if (cur.base != -1) {
  135. if (best.base == -1 || cur.len > best.len)
  136. best = cur;
  137. }
  138. if (best.base != -1 && best.len < 2)
  139. best.base = -1;
  140. /*
  141. * Format the result.
  142. */
  143. tp = tmp;
  144. ep = tmp + sizeof(tmp);
  145. for (i = 0; i < (IN6ADDRSZ / INT16SZ) && tp < ep; i++) {
  146. /* Are we inside the best run of 0x00's? */
  147. if (best.base != -1 && i >= best.base &&
  148. i < (best.base + best.len)) {
  149. if (i == best.base) {
  150. if (tp + 1 >= ep)
  151. return (NULL);
  152. *tp++ = ':';
  153. }
  154. continue;
  155. }
  156. /* Are we following an initial run of 0x00s or any real hex? */
  157. if (i != 0) {
  158. if (tp + 1 >= ep)
  159. return (NULL);
  160. *tp++ = ':';
  161. }
  162. /* Is this address an encapsulated IPv4? */
  163. if (i == 6 && best.base == 0 &&
  164. (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
  165. if (!inet_ntop4(src+12, tp, (size_t)(ep - tp)))
  166. return (NULL);
  167. tp += strlen(tp);
  168. break;
  169. }
  170. advance = snprintf(tp, ep - tp, "%x", words[i]);
  171. if (advance <= 0 || advance >= ep - tp)
  172. return (NULL);
  173. tp += advance;
  174. }
  175. /* Was it a trailing run of 0x00's? */
  176. if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
  177. if (tp + 1 >= ep)
  178. return (NULL);
  179. *tp++ = ':';
  180. }
  181. if (tp + 1 >= ep)
  182. return (NULL);
  183. *tp++ = '\0';
  184. /*
  185. * Check for overflow, copy, and we're done.
  186. */
  187. if ((size_t)(tp - tmp) > size) {
  188. errno = ENOSPC;
  189. return (NULL);
  190. }
  191. strlcpy(dst, tmp, size);
  192. return (dst);
  193. }
  194. #endif /* !HAVE_INET_NTOP */