inet_ntop.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /* Copyright (c) 1996 by Internet Software Consortium.
  2. *
  3. * Permission to use, copy, modify, and distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  8. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  9. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  10. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  12. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  13. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  14. * SOFTWARE.
  15. */
  16. #include "fspr_private.h"
  17. #include "fspr_arch_networkio.h"
  18. #include "fspr_strings.h"
  19. #if APR_HAVE_SYS_TYPES_H
  20. #include <sys/types.h>
  21. #endif
  22. #if APR_HAVE_SYS_SOCKET_H
  23. #include <sys/socket.h>
  24. #endif
  25. #if APR_HAVE_NETINET_IN_H
  26. #include <netinet/in.h>
  27. #endif
  28. #if APR_HAVE_ARPA_INET_H
  29. #include <arpa/inet.h>
  30. #endif
  31. #include <string.h>
  32. #if APR_HAVE_ERRNO_H
  33. #include <errno.h>
  34. #endif
  35. #include <stdio.h>
  36. #ifndef IN6ADDRSZ
  37. #define IN6ADDRSZ 16
  38. #endif
  39. #ifndef INT16SZ
  40. #define INT16SZ sizeof(fspr_int16_t)
  41. #endif
  42. #ifndef __P
  43. #define __P(x) x
  44. #endif
  45. #if !defined(EAFNOSUPPORT) && defined(WSAEAFNOSUPPORT)
  46. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  47. #endif
  48. /*
  49. * WARNING: Don't even consider trying to compile this on a system where
  50. * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
  51. */
  52. static const char *inet_ntop4 __P((const unsigned char *src, char *dst, fspr_size_t size));
  53. #if APR_HAVE_IPV6
  54. static const char *inet_ntop6 __P((const unsigned char *src, char *dst, fspr_size_t size));
  55. #endif
  56. /* char *
  57. * inet_ntop(af, src, dst, size)
  58. * convert a network format address to presentation format.
  59. * return:
  60. * pointer to presentation format address (`dst'), or NULL (see errno).
  61. * author:
  62. * Paul Vixie, 1996.
  63. */
  64. const char *
  65. fspr_inet_ntop(int af, const void *src, char *dst, fspr_size_t size)
  66. {
  67. switch (af) {
  68. case AF_INET:
  69. return (inet_ntop4(src, dst, size));
  70. #if APR_HAVE_IPV6
  71. case AF_INET6:
  72. return (inet_ntop6(src, dst, size));
  73. #endif
  74. default:
  75. errno = EAFNOSUPPORT;
  76. return (NULL);
  77. }
  78. /* NOTREACHED */
  79. }
  80. /* const char *
  81. * inet_ntop4(src, dst, size)
  82. * format an IPv4 address, more or less like inet_ntoa()
  83. * return:
  84. * `dst' (as a const)
  85. * notes:
  86. * (1) uses no statics
  87. * (2) takes a u_char* not an in_addr as input
  88. * author:
  89. * Paul Vixie, 1996.
  90. */
  91. static const char *
  92. inet_ntop4(const unsigned char *src, char *dst, fspr_size_t size)
  93. {
  94. const fspr_size_t MIN_SIZE = 16; /* space for 255.255.255.255\0 */
  95. int n = 0;
  96. char *next = dst;
  97. if (size < MIN_SIZE) {
  98. errno = ENOSPC;
  99. return NULL;
  100. }
  101. do {
  102. unsigned char u = *src++;
  103. if (u > 99) {
  104. *next++ = '0' + u/100;
  105. u %= 100;
  106. *next++ = '0' + u/10;
  107. u %= 10;
  108. }
  109. else if (u > 9) {
  110. *next++ = '0' + u/10;
  111. u %= 10;
  112. }
  113. *next++ = '0' + u;
  114. *next++ = '.';
  115. n++;
  116. } while (n < 4);
  117. *--next = 0;
  118. return dst;
  119. }
  120. #if APR_HAVE_IPV6
  121. /* const char *
  122. * inet_ntop6(src, dst, size)
  123. * convert IPv6 binary address into presentation (printable) format
  124. * author:
  125. * Paul Vixie, 1996.
  126. */
  127. static const char *
  128. inet_ntop6(const unsigned char *src, char *dst, fspr_size_t size)
  129. {
  130. /*
  131. * Note that int32_t and int16_t need only be "at least" large enough
  132. * to contain a value of the specified size. On some systems, like
  133. * Crays, there is no such thing as an integer variable with 16 bits.
  134. * Keep this in mind if you think this function should have been coded
  135. * to use pointer overlays. All the world's not a VAX.
  136. */
  137. char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
  138. struct { int base, len; } best, cur;
  139. unsigned int words[IN6ADDRSZ / INT16SZ];
  140. int i;
  141. const unsigned char *next_src, *src_end;
  142. unsigned int *next_dest;
  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. next_src = src;
  149. src_end = src + IN6ADDRSZ;
  150. next_dest = words;
  151. best.base = -1;
  152. cur.base = -1;
  153. cur.len = best.len = 0; /* silence gcc4 warning */
  154. i = 0;
  155. do {
  156. unsigned int next_word = (unsigned int)*next_src++;
  157. next_word <<= 8;
  158. next_word |= (unsigned int)*next_src++;
  159. *next_dest++ = next_word;
  160. if (next_word == 0) {
  161. if (cur.base == -1) {
  162. cur.base = i;
  163. cur.len = 1;
  164. }
  165. else {
  166. cur.len++;
  167. }
  168. } else {
  169. if (cur.base != -1) {
  170. if (best.base == -1 || cur.len > best.len) {
  171. best = cur;
  172. }
  173. cur.base = -1;
  174. }
  175. }
  176. i++;
  177. } while (next_src < src_end);
  178. if (cur.base != -1) {
  179. if (best.base == -1 || cur.len > best.len) {
  180. best = cur;
  181. }
  182. }
  183. if (best.base != -1 && best.len < 2) {
  184. best.base = -1;
  185. }
  186. /*
  187. * Format the result.
  188. */
  189. tp = tmp;
  190. for (i = 0; i < (IN6ADDRSZ / INT16SZ);) {
  191. /* Are we inside the best run of 0x00's? */
  192. if (i == best.base) {
  193. *tp++ = ':';
  194. i += best.len;
  195. continue;
  196. }
  197. /* Are we following an initial run of 0x00s or any real hex? */
  198. if (i != 0) {
  199. *tp++ = ':';
  200. }
  201. /* Is this address an encapsulated IPv4? */
  202. if (i == 6 && best.base == 0 &&
  203. (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
  204. if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp))) {
  205. return (NULL);
  206. }
  207. tp += strlen(tp);
  208. break;
  209. }
  210. tp += fspr_snprintf(tp, sizeof tmp - (tp - tmp), "%x", words[i]);
  211. i++;
  212. }
  213. /* Was it a trailing run of 0x00's? */
  214. if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ)) {
  215. *tp++ = ':';
  216. }
  217. *tp++ = '\0';
  218. /*
  219. * Check for overflow, copy, and we're done.
  220. */
  221. if ((fspr_size_t)(tp - tmp) > size) {
  222. errno = ENOSPC;
  223. return (NULL);
  224. }
  225. strcpy(dst, tmp);
  226. return (dst);
  227. }
  228. #endif