inet_ntop.c 5.2 KB

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