inet_ntop.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
  2. Copyright (C) 2005-2006, 2008-2010 Free Software Foundation, Inc.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. /*
  15. * Copyright (c) 1996-1999 by Internet Software Consortium.
  16. *
  17. * Permission to use, copy, modify, and distribute this software for any
  18. * purpose with or without fee is hereby granted, provided that the above
  19. * copyright notice and this permission notice appear in all copies.
  20. *
  21. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  22. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  23. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  24. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  25. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  27. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  28. * SOFTWARE.
  29. */
  30. #include <config.h>
  31. /* Specification. */
  32. #include <arpa/inet.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35. #include <errno.h>
  36. #define NS_IN6ADDRSZ 16
  37. #define NS_INT16SZ 2
  38. /*
  39. * WARNING: Don't even consider trying to compile this on a system where
  40. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  41. */
  42. typedef int verify_int_size[2 * sizeof (int) - 7];
  43. static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
  44. #if HAVE_IPV6
  45. static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
  46. #endif
  47. /* char *
  48. * inet_ntop(af, src, dst, size)
  49. * convert a network format address to presentation format.
  50. * return:
  51. * pointer to presentation format address (`dst'), or NULL (see errno).
  52. * author:
  53. * Paul Vixie, 1996.
  54. */
  55. const char *
  56. inet_ntop (int af, const void *restrict src,
  57. char *restrict dst, socklen_t cnt)
  58. {
  59. switch (af)
  60. {
  61. #if HAVE_IPV4
  62. case AF_INET:
  63. return (inet_ntop4 (src, dst, cnt));
  64. #endif
  65. #if HAVE_IPV6
  66. case AF_INET6:
  67. return (inet_ntop6 (src, dst, cnt));
  68. #endif
  69. default:
  70. errno = EAFNOSUPPORT;
  71. return (NULL);
  72. }
  73. /* NOTREACHED */
  74. }
  75. /* const char *
  76. * inet_ntop4(src, dst, size)
  77. * format an IPv4 address
  78. * return:
  79. * `dst' (as a const)
  80. * notes:
  81. * (1) uses no statics
  82. * (2) takes a u_char* not an in_addr as input
  83. * author:
  84. * Paul Vixie, 1996.
  85. */
  86. static const char *
  87. inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
  88. {
  89. char tmp[sizeof "255.255.255.255"];
  90. int len;
  91. len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
  92. if (len < 0)
  93. return NULL;
  94. if (len > size)
  95. {
  96. errno = ENOSPC;
  97. return NULL;
  98. }
  99. return strcpy (dst, tmp);
  100. }
  101. #if HAVE_IPV6
  102. /* const char *
  103. * inet_ntop6(src, dst, size)
  104. * convert IPv6 binary address into presentation (printable) format
  105. * author:
  106. * Paul Vixie, 1996.
  107. */
  108. static const char *
  109. inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
  110. {
  111. /*
  112. * Note that int32_t and int16_t need only be "at least" large enough
  113. * to contain a value of the specified size. On some systems, like
  114. * Crays, there is no such thing as an integer variable with 16 bits.
  115. * Keep this in mind if you think this function should have been coded
  116. * to use pointer overlays. All the world's not a VAX.
  117. */
  118. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  119. struct
  120. {
  121. int base, len;
  122. } best, cur;
  123. unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
  124. int i;
  125. /*
  126. * Preprocess:
  127. * Copy the input (bytewise) array into a wordwise array.
  128. * Find the longest run of 0x00's in src[] for :: shorthanding.
  129. */
  130. memset (words, '\0', sizeof words);
  131. for (i = 0; i < NS_IN6ADDRSZ; i += 2)
  132. words[i / 2] = (src[i] << 8) | src[i + 1];
  133. best.base = -1;
  134. cur.base = -1;
  135. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
  136. {
  137. if (words[i] == 0)
  138. {
  139. if (cur.base == -1)
  140. cur.base = i, cur.len = 1;
  141. else
  142. cur.len++;
  143. }
  144. else
  145. {
  146. if (cur.base != -1)
  147. {
  148. if (best.base == -1 || cur.len > best.len)
  149. best = cur;
  150. cur.base = -1;
  151. }
  152. }
  153. }
  154. if (cur.base != -1)
  155. {
  156. if (best.base == -1 || cur.len > best.len)
  157. best = cur;
  158. }
  159. if (best.base != -1 && best.len < 2)
  160. best.base = -1;
  161. /*
  162. * Format the result.
  163. */
  164. tp = tmp;
  165. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
  166. {
  167. /* Are we inside the best run of 0x00's? */
  168. if (best.base != -1 && i >= best.base && i < (best.base + best.len))
  169. {
  170. if (i == best.base)
  171. *tp++ = ':';
  172. continue;
  173. }
  174. /* Are we following an initial run of 0x00s or any real hex? */
  175. if (i != 0)
  176. *tp++ = ':';
  177. /* Is this address an encapsulated IPv4? */
  178. if (i == 6 && best.base == 0 &&
  179. (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
  180. {
  181. if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
  182. return (NULL);
  183. tp += strlen (tp);
  184. break;
  185. }
  186. {
  187. int len = sprintf (tp, "%x", words[i]);
  188. if (len < 0)
  189. return NULL;
  190. tp += len;
  191. }
  192. }
  193. /* Was it a trailing run of 0x00's? */
  194. if (best.base != -1 && (best.base + best.len) ==
  195. (NS_IN6ADDRSZ / NS_INT16SZ))
  196. *tp++ = ':';
  197. *tp++ = '\0';
  198. /*
  199. * Check for overflow, copy, and we're done.
  200. */
  201. if ((socklen_t) (tp - tmp) > size)
  202. {
  203. errno = ENOSPC;
  204. return NULL;
  205. }
  206. return strcpy (dst, tmp);
  207. }
  208. #endif