inet_ntop.c 4.4 KB

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