inet_ntop.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #if defined(LIBC_SCCS) && !defined(lint)
  18. static const char rcsid[] = "$Id: inet_ntop.c,v 1.3.18.2 2005/11/03 23:02:22 marka Exp $";
  19. #endif /* LIBC_SCCS and not lint */
  20. #include <sys/cdefs.h>
  21. __FBSDID("$FreeBSD$");
  22. #include <sys/param.h>
  23. #include <sys/socket.h>
  24. #include <sys/systm.h>
  25. #include <netinet/in.h>
  26. /*%
  27. * WARNING: Don't even consider trying to compile this on a system where
  28. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  29. */
  30. static char *inet_ntop4(const u_char *src, char *dst, socklen_t size);
  31. static char *inet_ntop6(const u_char *src, char *dst, socklen_t size);
  32. /* char *
  33. * inet_ntop(af, src, dst, size)
  34. * convert a network format address to presentation format.
  35. * return:
  36. * pointer to presentation format address (`dst'), or NULL (see errno).
  37. * author:
  38. * Paul Vixie, 1996.
  39. */
  40. char *
  41. inet_ntop(int af, const void *src, char *dst, socklen_t size)
  42. {
  43. switch (af) {
  44. case AF_INET:
  45. return (inet_ntop4(src, dst, size));
  46. case AF_INET6:
  47. return (inet_ntop6(src, dst, size));
  48. default:
  49. return (NULL);
  50. }
  51. /* NOTREACHED */
  52. }
  53. /* const char *
  54. * inet_ntop4(src, dst, size)
  55. * format an IPv4 address
  56. * return:
  57. * `dst' (as a const)
  58. * notes:
  59. * (1) uses no statics
  60. * (2) takes a u_char* not an in_addr as input
  61. * author:
  62. * Paul Vixie, 1996.
  63. */
  64. static char *
  65. inet_ntop4(const u_char *src, char *dst, socklen_t size)
  66. {
  67. static const char fmt[] = "%u.%u.%u.%u";
  68. char tmp[sizeof "255.255.255.255"];
  69. int l;
  70. l = snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]);
  71. if (l <= 0 || (socklen_t) l >= size) {
  72. return (NULL);
  73. }
  74. strlcpy(dst, tmp, size);
  75. return (dst);
  76. }
  77. /* const char *
  78. * inet_ntop6(src, dst, size)
  79. * convert IPv6 binary address into presentation (printable) format
  80. * author:
  81. * Paul Vixie, 1996.
  82. */
  83. static char *
  84. inet_ntop6(const u_char *src, char *dst, socklen_t size)
  85. {
  86. /*
  87. * Note that int32_t and int16_t need only be "at least" large enough
  88. * to contain a value of the specified size. On some systems, like
  89. * Crays, there is no such thing as an integer variable with 16 bits.
  90. * Keep this in mind if you think this function should have been coded
  91. * to use pointer overlays. All the world's not a VAX.
  92. */
  93. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  94. struct { int base, len; } best, cur;
  95. #define NS_IN6ADDRSZ 16
  96. #define NS_INT16SZ 2
  97. u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
  98. int i;
  99. /*
  100. * Preprocess:
  101. * Copy the input (bytewise) array into a wordwise array.
  102. * Find the longest run of 0x00's in src[] for :: shorthanding.
  103. */
  104. memset(words, '\0', sizeof words);
  105. for (i = 0; i < NS_IN6ADDRSZ; i++)
  106. words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  107. best.base = -1;
  108. best.len = 0;
  109. cur.base = -1;
  110. cur.len = 0;
  111. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  112. if (words[i] == 0) {
  113. if (cur.base == -1)
  114. cur.base = i, cur.len = 1;
  115. else
  116. cur.len++;
  117. } else {
  118. if (cur.base != -1) {
  119. if (best.base == -1 || cur.len > best.len)
  120. best = cur;
  121. cur.base = -1;
  122. }
  123. }
  124. }
  125. if (cur.base != -1) {
  126. if (best.base == -1 || cur.len > best.len)
  127. best = cur;
  128. }
  129. if (best.base != -1 && best.len < 2)
  130. best.base = -1;
  131. /*
  132. * Format the result.
  133. */
  134. tp = tmp;
  135. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
  136. /* Are we inside the best run of 0x00's? */
  137. if (best.base != -1 && i >= best.base &&
  138. i < (best.base + best.len)) {
  139. if (i == best.base)
  140. *tp++ = ':';
  141. continue;
  142. }
  143. /* Are we following an initial run of 0x00s or any real hex? */
  144. if (i != 0)
  145. *tp++ = ':';
  146. /* Is this address an encapsulated IPv4? */
  147. if (i == 6 && best.base == 0 && (best.len == 6 ||
  148. (best.len == 7 && words[7] != 0x0001) ||
  149. (best.len == 5 && words[5] == 0xffff))) {
  150. if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
  151. return (NULL);
  152. tp += strlen(tp);
  153. break;
  154. }
  155. tp += sprintf(tp, "%x", words[i]);
  156. }
  157. /* Was it a trailing run of 0x00's? */
  158. if (best.base != -1 && (best.base + best.len) ==
  159. (NS_IN6ADDRSZ / NS_INT16SZ))
  160. *tp++ = ':';
  161. *tp++ = '\0';
  162. /*
  163. * Check for overflow, copy, and we're done.
  164. */
  165. if ((socklen_t)(tp - tmp) > size) {
  166. return (NULL);
  167. }
  168. strcpy(dst, tmp);
  169. return (dst);
  170. }
  171. /*! \file */