/contrib/ntp/libisc/inet_ntop.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 198 lines · 113 code · 17 blank · 68 comment · 45 complexity · 57579a857951888a435712100133b469 MD5 · raw file

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