inet_ntop.c 4.3 KB

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