inet_ntop.h 6.3 KB

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