inet_ntop.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 1996-2001 Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
  9. * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. #include "rsync.h"
  18. #define NS_INT16SZ 2
  19. #define NS_IN6ADDRSZ 16
  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 unsigned char *src, char *dst,
  25. size_t size);
  26. #ifdef AF_INET6
  27. static const char *inet_ntop6(const unsigned char *src, char *dst,
  28. size_t size);
  29. #endif
  30. /* char *
  31. * isc_net_ntop(af, src, dst, size)
  32. * convert a network format address to presentation format.
  33. * return:
  34. * pointer to presentation format address (`dst'), or NULL (see errno).
  35. * author:
  36. * Paul Vixie, 1996.
  37. */
  38. const char *
  39. inet_ntop(int af, const void *src, char *dst, size_t size)
  40. {
  41. switch (af) {
  42. case AF_INET:
  43. return (inet_ntop4(src, dst, size));
  44. #ifdef AF_INET6
  45. case AF_INET6:
  46. return (inet_ntop6(src, dst, size));
  47. #endif
  48. default:
  49. errno = EAFNOSUPPORT;
  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 unsigned char* not an in_addr as input
  62. * author:
  63. * Paul Vixie, 1996.
  64. */
  65. static const char *
  66. inet_ntop4(const unsigned char *src, char *dst, size_t size)
  67. {
  68. static const char *fmt = "%u.%u.%u.%u";
  69. char tmp[sizeof "255.255.255.255"];
  70. size_t len;
  71. len = snprintf(tmp, sizeof tmp, fmt, src[0], src[1], src[2], src[3]);
  72. if (len >= size) {
  73. errno = ENOSPC;
  74. return (NULL);
  75. }
  76. memcpy(dst, tmp, len + 1);
  77. return (dst);
  78. }
  79. /* const char *
  80. * isc_inet_ntop6(src, dst, size)
  81. * convert IPv6 binary address into presentation (printable) format
  82. * author:
  83. * Paul Vixie, 1996.
  84. */
  85. #ifdef AF_INET6
  86. static const char *
  87. inet_ntop6(const unsigned char *src, char *dst, size_t size)
  88. {
  89. /*
  90. * Note that int32_t and int16_t need only be "at least" large enough
  91. * to contain a value of the specified size. On some systems, like
  92. * Crays, there is no such thing as an integer variable with 16 bits.
  93. * Keep this in mind if you think this function should have been coded
  94. * to use pointer overlays. All the world's not a VAX.
  95. */
  96. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  97. struct { int base, len; } best, cur;
  98. unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
  99. int i, inc;
  100. /*
  101. * Preprocess:
  102. * Copy the input (bytewise) array into a wordwise array.
  103. * Find the longest run of 0x00's in src[] for :: shorthanding.
  104. */
  105. memset(words, '\0', sizeof words);
  106. for (i = 0; i < NS_IN6ADDRSZ; i++)
  107. words[i / 2] |= (src[i] << ((1 - (i % 2)) << 3));
  108. best.base = -1;
  109. cur.base = -1;
  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. inc = snprintf(tp, 5, "%x", words[i]);
  154. assert(inc < 5);
  155. tp += inc;
  156. }
  157. /* Was it a trailing run of 0x00's? */
  158. if (best.base != -1 && (best.base + best.len) ==
  159. (NS_IN6ADDRSZ / NS_INT16SZ))
  160. *tp++ = ':';
  161. *tp++ = '\0';
  162. /*
  163. * Check for overflow, copy, and we're done.
  164. */
  165. if ((size_t)(tp - tmp) > size) {
  166. errno = ENOSPC;
  167. return (NULL);
  168. }
  169. memcpy(dst, tmp, tp - tmp);
  170. return (dst);
  171. }
  172. #endif /* AF_INET6 */