inet_ntop.c 4.9 KB

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