inet_ntop.c 4.3 KB

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