inet_ntop.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * inet_ntop.c --
  3. *
  4. * provides inet_ntop()
  5. */
  6. #include "inet_ntop.h"
  7. #include "sprintf.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. #define NS_INADDRSZ 4 /* IPv4 T_A */
  18. #define NS_IN6ADDRSZ 16 /* IPv6 T_AAAA */
  19. #define NS_INT16SZ 2 /* #/bytes of data in a u_int16_t */
  20. /*
  21. * WARNING: Don't even consider trying to compile this on a system where
  22. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  23. */
  24. static const char *inet_ntop4 (const u_char *src, char *dst, socklen_t size);
  25. #ifdef USE_IPV6
  26. static const char *inet_ntop6 (const u_char *src, char *dst, socklen_t size);
  27. #endif /* USE_IPV6 */
  28. /* char *
  29. * inet_ntop(af, src, dst, size)
  30. * convert a network format address to presentation format.
  31. * return:
  32. * pointer to presentation format address (`dst'), or NULL (see errno).
  33. * author:
  34. * Paul Vixie, 1996.
  35. */
  36. const char *
  37. inet_ntop(int af, const void *src, char *dst, socklen_t size)
  38. {
  39. switch (af) {
  40. case AF_INET:
  41. return (inet_ntop4((const u_char *) src, dst, size));
  42. #ifdef USE_IPV6
  43. case AF_INET6:
  44. return (inet_ntop6((const u_char *) src, dst, size));
  45. #endif /* USE_IPV6 */
  46. default:
  47. return (NULL);
  48. }
  49. /* NOTREACHED */
  50. }
  51. /* const char *
  52. * inet_ntop4(src, dst, size)
  53. * format an IPv4 address
  54. * return:
  55. * `dst' (as a const)
  56. * notes:
  57. * (1) uses no statics
  58. * (2) takes a u_char* not an in_addr as input
  59. * author:
  60. * Paul Vixie, 1996.
  61. */
  62. static const char *
  63. inet_ntop4(const u_char *src, char *dst, socklen_t size)
  64. {
  65. static const char fmt[] = "%u.%u.%u.%u";
  66. char tmp[sizeof "255.255.255.255"];
  67. if (simple_snprintf(tmp, sizeof(tmp), fmt, src[0], src[1], src[2], src[3]) > size) {
  68. return (NULL);
  69. }
  70. return strcpy(dst, tmp);
  71. }
  72. #ifdef USE_IPV6
  73. /* const char *
  74. * inet_ntop6(src, dst, size)
  75. * convert IPv6 binary address into presentation (printable) format
  76. * author:
  77. * Paul Vixie, 1996.
  78. */
  79. static const char *
  80. inet_ntop6(const u_char *src, char *dst, socklen_t size)
  81. {
  82. /*
  83. * Note that int32_t and int16_t need only be "at least" large enough
  84. * to contain a value of the specified size. On some systems, like
  85. * Crays, there is no such thing as an integer variable with 16 bits.
  86. * Keep this in mind if you think this function should have been coded
  87. * to use pointer overlays. All the world's not a VAX.
  88. */
  89. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  90. struct { int base, len; } best, cur;
  91. u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
  92. int i;
  93. /*
  94. * Preprocess:
  95. * Copy the input (bytewise) array into a wordwise array.
  96. * Find the longest run of 0x00's in src[] for :: shorthanding.
  97. */
  98. memset(words, '\0', sizeof words);
  99. for (i = 0; i < NS_IN6ADDRSZ; i += 2)
  100. words[i >> 1] = (src[i] << 8) | src[i + 1];
  101. best.base = -1;
  102. best.len = 0;
  103. cur.base = -1;
  104. cur.len = 0;
  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 (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
  144. return (NULL);
  145. tp += strlen(tp);
  146. break;
  147. }
  148. tp += simple_snprintf(tp, sizeof(tmp), "%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 /* USE_IPV6 */
  164. #endif /* !HAVE_INET_NTOP */