inet_ntop.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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_SOCKET_H
  26. #include <sys/socket.h>
  27. #endif
  28. #ifdef HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #ifdef HAVE_ARPA_INET_H
  32. #include <arpa/inet.h>
  33. #endif
  34. #include <string.h>
  35. #include <errno.h>
  36. #define _MPRINTF_REPLACE /* use our functions only */
  37. #include <curl/mprintf.h>
  38. #include "inet_ntop.h"
  39. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  40. /* this platform has a inet_ntoa_r() function, but no proto declared anywhere
  41. so we include our own proto to make compilers happy */
  42. #include "inet_ntoa_r.h"
  43. #endif
  44. #define IN6ADDRSZ 16
  45. #define INADDRSZ 4
  46. #define INT16SZ 2
  47. /*
  48. * Format an IPv4 address, more or less like inet_ntoa().
  49. *
  50. * Returns `dst' (as a const)
  51. * Note:
  52. * - uses no statics
  53. * - takes a unsigned char* not an in_addr as input
  54. */
  55. static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
  56. {
  57. #if defined(HAVE_INET_NTOA_R_2_ARGS)
  58. const char *ptr;
  59. DEBUGASSERT(size >= 16);
  60. ptr = inet_ntoa_r(*(struct in_addr*)src, dst);
  61. return (char *)memmove(dst, ptr, strlen(ptr)+1);
  62. #elif defined(HAVE_INET_NTOA_R)
  63. #if defined(HAVE_INT_INET_NTOA_R)
  64. return inet_ntoa_r(*(struct in_addr*)src, dst, size)? NULL: dst;
  65. #else
  66. return inet_ntoa_r(*(struct in_addr*)src, dst, size);
  67. #endif
  68. #else
  69. const char *addr = inet_ntoa(*(struct in_addr*)src);
  70. if(strlen(addr) >= size)
  71. {
  72. SET_ERRNO(ENOSPC);
  73. return (NULL);
  74. }
  75. return strcpy(dst, addr);
  76. #endif
  77. }
  78. #ifdef ENABLE_IPV6
  79. /*
  80. * Convert IPv6 binary address into presentation (printable) format.
  81. */
  82. static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
  83. {
  84. /*
  85. * Note that int32_t and int16_t need only be "at least" large enough
  86. * to contain a value of the specified size. On some systems, like
  87. * Crays, there is no such thing as an integer variable with 16 bits.
  88. * Keep this in mind if you think this function should have been coded
  89. * to use pointer overlays. All the world's not a VAX.
  90. */
  91. char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  92. char *tp;
  93. struct {
  94. long base;
  95. long len;
  96. } best, cur;
  97. unsigned long words[IN6ADDRSZ / INT16SZ];
  98. int i;
  99. /* Preprocess:
  100. * Copy the input (bytewise) array into a wordwise array.
  101. * Find the longest run of 0x00's in src[] for :: shorthanding.
  102. */
  103. memset(words, '\0', sizeof(words));
  104. for (i = 0; i < IN6ADDRSZ; i++)
  105. words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
  106. best.base = -1;
  107. cur.base = -1;
  108. best.len = 0;
  109. cur.len = 0;
  110. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
  111. {
  112. if(words[i] == 0)
  113. {
  114. if(cur.base == -1)
  115. cur.base = i, cur.len = 1;
  116. else
  117. cur.len++;
  118. }
  119. else if(cur.base != -1)
  120. {
  121. if(best.base == -1 || cur.len > best.len)
  122. best = cur;
  123. cur.base = -1;
  124. }
  125. }
  126. if((cur.base != -1) && (best.base == -1 || cur.len > best.len))
  127. best = cur;
  128. if(best.base != -1 && best.len < 2)
  129. best.base = -1;
  130. /* Format the result.
  131. */
  132. tp = tmp;
  133. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
  134. {
  135. /* Are we inside the best run of 0x00's?
  136. */
  137. if(best.base != -1 && i >= best.base && i < (best.base + best.len))
  138. {
  139. if(i == best.base)
  140. *tp++ = ':';
  141. continue;
  142. }
  143. /* Are we following an initial run of 0x00s or any real hex?
  144. */
  145. if(i != 0)
  146. *tp++ = ':';
  147. /* Is this address an encapsulated IPv4?
  148. */
  149. if(i == 6 && best.base == 0 &&
  150. (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
  151. {
  152. if(!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
  153. {
  154. SET_ERRNO(ENOSPC);
  155. return (NULL);
  156. }
  157. tp += strlen(tp);
  158. break;
  159. }
  160. tp += snprintf(tp, 5, "%lx", words[i]);
  161. }
  162. /* Was it a trailing run of 0x00's?
  163. */
  164. if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
  165. *tp++ = ':';
  166. *tp++ = '\0';
  167. /* Check for overflow, copy, and we're done.
  168. */
  169. if((size_t)(tp - tmp) > size)
  170. {
  171. SET_ERRNO(ENOSPC);
  172. return (NULL);
  173. }
  174. return strcpy (dst, tmp);
  175. }
  176. #endif /* ENABLE_IPV6 */
  177. /*
  178. * Convert a network format address to presentation format.
  179. *
  180. * Returns pointer to presentation format address (`buf').
  181. * Returns NULL on error and errno set with the specific
  182. * error, EAFNOSUPPORT or ENOSPC.
  183. *
  184. * On Windows we store the error in the thread errno, not
  185. * in the winsock error code. This is to avoid loosing the
  186. * actual last winsock error. So use macro ERRNO to fetch the
  187. * errno this funtion sets when returning NULL, not SOCKERRNO.
  188. */
  189. char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
  190. {
  191. switch (af) {
  192. case AF_INET:
  193. return inet_ntop4((const unsigned char*)src, buf, size);
  194. #ifdef ENABLE_IPV6
  195. case AF_INET6:
  196. return inet_ntop6((const unsigned char*)src, buf, size);
  197. #endif
  198. default:
  199. SET_ERRNO(EAFNOSUPPORT);
  200. return NULL;
  201. }
  202. }
  203. #endif /* HAVE_INET_NTOP */