inet_ntop.c 6.0 KB

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