1
0

inet_ntop.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 1996-2001 Internet Software Consortium.
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
  9. * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
  10. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
  11. * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  13. * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
  14. * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  15. * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /*
  18. * Original code by Paul Vixie. "curlified" by Gisle Vanem.
  19. */
  20. #include "setup.h"
  21. #ifndef HAVE_INET_NTOP
  22. #ifdef HAVE_SYS_PARAM_H
  23. #include <sys/param.h>
  24. #endif
  25. #ifdef HAVE_SYS_TYPES_H
  26. #include <sys/types.h>
  27. #endif
  28. #ifdef HAVE_SYS_SOCKET_H
  29. #include <sys/socket.h>
  30. #endif
  31. #ifdef HAVE_NETINET_IN_H
  32. #include <netinet/in.h>
  33. #endif
  34. #ifdef HAVE_ARPA_INET_H
  35. #include <arpa/inet.h>
  36. #endif
  37. #include <string.h>
  38. #include <errno.h>
  39. #define _MPRINTF_REPLACE /* use our functions only */
  40. #include <curl/mprintf.h>
  41. #include "inet_ntop.h"
  42. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  43. /* this platform has a inet_ntoa_r() function, but no proto declared anywhere
  44. so we include our own proto to make compilers happy */
  45. #include "inet_ntoa_r.h"
  46. #endif
  47. #define IN6ADDRSZ 16
  48. #define INADDRSZ 4
  49. #define INT16SZ 2
  50. #ifdef USE_WINSOCK
  51. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  52. #define SET_ERRNO(e) WSASetLastError(errno = (e))
  53. #else
  54. #define SET_ERRNO(e) errno = e
  55. #endif
  56. /*
  57. * Format an IPv4 address, more or less like inet_ntoa().
  58. *
  59. * Returns `dst' (as a const)
  60. * Note:
  61. * - uses no statics
  62. * - takes a unsigned char* not an in_addr as input
  63. */
  64. static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
  65. {
  66. #if defined(HAVE_INET_NTOA_R_2_ARGS)
  67. const char *ptr;
  68. curlassert(size >= 16);
  69. ptr = inet_ntoa_r(*(struct in_addr*)src, dst);
  70. return (char *)memmove(dst, ptr, strlen(ptr)+1);
  71. #elif defined(HAVE_INET_NTOA_R)
  72. return inet_ntoa_r(*(struct in_addr*)src, dst, size);
  73. #else
  74. const char *addr = inet_ntoa(*(struct in_addr*)src);
  75. if (strlen(addr) >= size)
  76. {
  77. SET_ERRNO(ENOSPC);
  78. return (NULL);
  79. }
  80. return strcpy(dst, addr);
  81. #endif
  82. }
  83. #ifdef ENABLE_IPV6
  84. /*
  85. * Convert IPv6 binary address into presentation (printable) format.
  86. */
  87. static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
  88. {
  89. /*
  90. * Note that int32_t and int16_t need only be "at least" large enough
  91. * to contain a value of the specified size. On some systems, like
  92. * Crays, there is no such thing as an integer variable with 16 bits.
  93. * Keep this in mind if you think this function should have been coded
  94. * to use pointer overlays. All the world's not a VAX.
  95. */
  96. char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  97. char *tp;
  98. struct {
  99. long base;
  100. long len;
  101. } best, cur;
  102. unsigned long words[IN6ADDRSZ / INT16SZ];
  103. int i;
  104. /* Preprocess:
  105. * Copy the input (bytewise) array into a wordwise array.
  106. * Find the longest run of 0x00's in src[] for :: shorthanding.
  107. */
  108. memset(words, '\0', sizeof(words));
  109. for (i = 0; i < IN6ADDRSZ; i++)
  110. words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
  111. best.base = -1;
  112. cur.base = -1;
  113. best.len = 0;
  114. cur.len = 0;
  115. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
  116. {
  117. if (words[i] == 0)
  118. {
  119. if (cur.base == -1)
  120. cur.base = i, cur.len = 1;
  121. else
  122. cur.len++;
  123. }
  124. else if (cur.base != -1)
  125. {
  126. if (best.base == -1 || cur.len > best.len)
  127. best = cur;
  128. cur.base = -1;
  129. }
  130. }
  131. if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
  132. best = cur;
  133. if (best.base != -1 && best.len < 2)
  134. best.base = -1;
  135. /* Format the result.
  136. */
  137. tp = tmp;
  138. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
  139. {
  140. /* Are we inside the best run of 0x00's?
  141. */
  142. if (best.base != -1 && i >= best.base && i < (best.base + best.len))
  143. {
  144. if (i == best.base)
  145. *tp++ = ':';
  146. continue;
  147. }
  148. /* Are we following an initial run of 0x00s or any real hex?
  149. */
  150. if (i != 0)
  151. *tp++ = ':';
  152. /* Is this address an encapsulated IPv4?
  153. */
  154. if (i == 6 && best.base == 0 &&
  155. (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
  156. {
  157. if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
  158. {
  159. SET_ERRNO(ENOSPC);
  160. return (NULL);
  161. }
  162. tp += strlen(tp);
  163. break;
  164. }
  165. tp += snprintf(tp, 5, "%lx", words[i]);
  166. }
  167. /* Was it a trailing run of 0x00's?
  168. */
  169. if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
  170. *tp++ = ':';
  171. *tp++ = '\0';
  172. /* Check for overflow, copy, and we're done.
  173. */
  174. if ((size_t)(tp - tmp) > size)
  175. {
  176. SET_ERRNO(ENOSPC);
  177. return (NULL);
  178. }
  179. return strcpy (dst, tmp);
  180. }
  181. #endif /* ENABLE_IPV6 */
  182. /*
  183. * Convert a network format address to presentation format.
  184. *
  185. * Returns pointer to presentation format address (`buf'),
  186. * Returns NULL on error (see errno).
  187. */
  188. char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
  189. {
  190. switch (af) {
  191. case AF_INET:
  192. return inet_ntop4((const unsigned char*)src, buf, size);
  193. #ifdef ENABLE_IPV6
  194. case AF_INET6:
  195. return inet_ntop6((const unsigned char*)src, buf, size);
  196. #endif
  197. default:
  198. SET_ERRNO(EAFNOSUPPORT);
  199. return NULL;
  200. }
  201. }
  202. #endif /* HAVE_INET_NTOP */