inet_ntop.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* inet_ntop.c -- convert IPv4 and IPv6 addresses from binary to text form
  2. Copyright (C) 2005-2006, 2008-2012 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 Lesser General Public License as published by
  5. the Free Software Foundation; either version 2, 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 Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser 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. /* Use this to suppress gcc's "...may be used before initialized" warnings.
  34. Beware: The Code argument must not contain commas. */
  35. #ifndef IF_LINT
  36. # ifdef lint
  37. # define IF_LINT(Code) Code
  38. # else
  39. # define IF_LINT(Code) /* empty */
  40. # endif
  41. #endif
  42. #if HAVE_DECL_INET_NTOP
  43. # undef inet_ntop
  44. const char *
  45. rpl_inet_ntop (int af, const void *restrict src,
  46. char *restrict dst, socklen_t cnt)
  47. {
  48. return inet_ntop (af, src, dst, cnt);
  49. }
  50. #else
  51. # include <stdio.h>
  52. # include <string.h>
  53. # include <errno.h>
  54. # define NS_IN6ADDRSZ 16
  55. # define NS_INT16SZ 2
  56. /*
  57. * WARNING: Don't even consider trying to compile this on a system where
  58. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  59. */
  60. typedef int verify_int_size[4 <= sizeof (int) ? 1 : -1];
  61. static const char *inet_ntop4 (const unsigned char *src, char *dst, socklen_t size);
  62. # if HAVE_IPV6
  63. static const char *inet_ntop6 (const unsigned char *src, char *dst, socklen_t size);
  64. # endif
  65. /* char *
  66. * inet_ntop(af, src, dst, size)
  67. * convert a network format address to presentation format.
  68. * return:
  69. * pointer to presentation format address ('dst'), or NULL (see errno).
  70. * author:
  71. * Paul Vixie, 1996.
  72. */
  73. const char *
  74. inet_ntop (int af, const void *restrict src,
  75. char *restrict dst, socklen_t cnt)
  76. {
  77. switch (af)
  78. {
  79. # if HAVE_IPV4
  80. case AF_INET:
  81. return (inet_ntop4 (src, dst, cnt));
  82. # endif
  83. # if HAVE_IPV6
  84. case AF_INET6:
  85. return (inet_ntop6 (src, dst, cnt));
  86. # endif
  87. default:
  88. errno = EAFNOSUPPORT;
  89. return (NULL);
  90. }
  91. /* NOTREACHED */
  92. }
  93. /* const char *
  94. * inet_ntop4(src, dst, size)
  95. * format an IPv4 address
  96. * return:
  97. * 'dst' (as a const)
  98. * notes:
  99. * (1) uses no statics
  100. * (2) takes a u_char* not an in_addr as input
  101. * author:
  102. * Paul Vixie, 1996.
  103. */
  104. static const char *
  105. inet_ntop4 (const unsigned char *src, char *dst, socklen_t size)
  106. {
  107. char tmp[sizeof "255.255.255.255"];
  108. int len;
  109. len = sprintf (tmp, "%u.%u.%u.%u", src[0], src[1], src[2], src[3]);
  110. if (len < 0)
  111. return NULL;
  112. if (len > size)
  113. {
  114. errno = ENOSPC;
  115. return NULL;
  116. }
  117. return strcpy (dst, tmp);
  118. }
  119. # if HAVE_IPV6
  120. /* const char *
  121. * inet_ntop6(src, dst, size)
  122. * convert IPv6 binary address into presentation (printable) format
  123. * author:
  124. * Paul Vixie, 1996.
  125. */
  126. static const char *
  127. inet_ntop6 (const unsigned char *src, char *dst, socklen_t size)
  128. {
  129. /*
  130. * Note that int32_t and int16_t need only be "at least" large enough
  131. * to contain a value of the specified size. On some systems, like
  132. * Crays, there is no such thing as an integer variable with 16 bits.
  133. * Keep this in mind if you think this function should have been coded
  134. * to use pointer overlays. All the world's not a VAX.
  135. */
  136. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  137. struct
  138. {
  139. int base, len;
  140. } best, cur;
  141. unsigned int words[NS_IN6ADDRSZ / NS_INT16SZ];
  142. int i;
  143. /*
  144. * Preprocess:
  145. * Copy the input (bytewise) array into a wordwise array.
  146. * Find the longest run of 0x00's in src[] for :: shorthanding.
  147. */
  148. memset (words, '\0', sizeof words);
  149. for (i = 0; i < NS_IN6ADDRSZ; i += 2)
  150. words[i / 2] = (src[i] << 8) | src[i + 1];
  151. best.base = -1;
  152. cur.base = -1;
  153. IF_LINT(best.len = 0);
  154. IF_LINT(cur.len = 0);
  155. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
  156. {
  157. if (words[i] == 0)
  158. {
  159. if (cur.base == -1)
  160. cur.base = i, cur.len = 1;
  161. else
  162. cur.len++;
  163. }
  164. else
  165. {
  166. if (cur.base != -1)
  167. {
  168. if (best.base == -1 || cur.len > best.len)
  169. best = cur;
  170. cur.base = -1;
  171. }
  172. }
  173. }
  174. if (cur.base != -1)
  175. {
  176. if (best.base == -1 || cur.len > best.len)
  177. best = cur;
  178. }
  179. if (best.base != -1 && best.len < 2)
  180. best.base = -1;
  181. /*
  182. * Format the result.
  183. */
  184. tp = tmp;
  185. for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++)
  186. {
  187. /* Are we inside the best run of 0x00's? */
  188. if (best.base != -1 && i >= best.base && i < (best.base + best.len))
  189. {
  190. if (i == best.base)
  191. *tp++ = ':';
  192. continue;
  193. }
  194. /* Are we following an initial run of 0x00s or any real hex? */
  195. if (i != 0)
  196. *tp++ = ':';
  197. /* Is this address an encapsulated IPv4? */
  198. if (i == 6 && best.base == 0 &&
  199. (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
  200. {
  201. if (!inet_ntop4 (src + 12, tp, sizeof tmp - (tp - tmp)))
  202. return (NULL);
  203. tp += strlen (tp);
  204. break;
  205. }
  206. {
  207. int len = sprintf (tp, "%x", words[i]);
  208. if (len < 0)
  209. return NULL;
  210. tp += len;
  211. }
  212. }
  213. /* Was it a trailing run of 0x00's? */
  214. if (best.base != -1 && (best.base + best.len) ==
  215. (NS_IN6ADDRSZ / NS_INT16SZ))
  216. *tp++ = ':';
  217. *tp++ = '\0';
  218. /*
  219. * Check for overflow, copy, and we're done.
  220. */
  221. if ((socklen_t) (tp - tmp) > size)
  222. {
  223. errno = ENOSPC;
  224. return NULL;
  225. }
  226. return strcpy (dst, tmp);
  227. }
  228. # endif
  229. #endif