inet_ntop.c 5.5 KB

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