inet_ntop.c 4.7 KB

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