inet_ntop.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (C) 1996-2022 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. * SPDX-License-Identifier: ISC
  18. */
  19. /*
  20. * Original code by Paul Vixie. "curlified" by Gisle Vanem.
  21. */
  22. #include "../curl_setup.h"
  23. #ifndef HAVE_INET_NTOP
  24. #ifdef HAVE_SYS_PARAM_H
  25. #include <sys/param.h>
  26. #endif
  27. #ifdef HAVE_NETINET_IN_H
  28. #include <netinet/in.h>
  29. #endif
  30. #ifdef HAVE_ARPA_INET_H
  31. #include <arpa/inet.h>
  32. #endif
  33. #include "inet_ntop.h"
  34. #define IN6ADDRSZ 16
  35. /* #define INADDRSZ 4 */
  36. #define INT16SZ 2
  37. /*
  38. * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make
  39. * sure we have _some_ value for AF_INET6 without polluting our fake value
  40. * everywhere.
  41. */
  42. #if !defined(USE_IPV6) && !defined(AF_INET6)
  43. #define AF_INET6 (AF_INET + 1)
  44. #endif
  45. /*
  46. * Format an IPv4 address, more or less like inet_ntop().
  47. *
  48. * Returns `dst' (as a const)
  49. * Note:
  50. * - uses no static variables
  51. * - takes an unsigned char* not an in_addr as input
  52. */
  53. static char *inet_ntop4(const unsigned char *src, char *dst, size_t size)
  54. {
  55. char tmp[sizeof("255.255.255.255")];
  56. size_t len;
  57. DEBUGASSERT(size >= 16);
  58. /* this sprintf() does not overflow the buffer. Avoids snprintf to work more
  59. widely. Avoids the msnprintf family to work as a curlx function. */
  60. (void)(sprintf)(tmp, "%d.%d.%d.%d",
  61. ((int)((unsigned char)src[0])) & 0xff,
  62. ((int)((unsigned char)src[1])) & 0xff,
  63. ((int)((unsigned char)src[2])) & 0xff,
  64. ((int)((unsigned char)src[3])) & 0xff);
  65. len = strlen(tmp);
  66. if(len == 0 || len >= size) {
  67. #ifdef USE_WINSOCK
  68. CURL_SETERRNO(WSAEINVAL);
  69. #else
  70. CURL_SETERRNO(ENOSPC);
  71. #endif
  72. return NULL;
  73. }
  74. strcpy(dst, tmp);
  75. return dst;
  76. }
  77. /*
  78. * Convert IPv6 binary address into presentation (printable) format.
  79. */
  80. static char *inet_ntop6(const unsigned char *src, char *dst, size_t size)
  81. {
  82. /*
  83. * Note that int32_t and int16_t need only be "at least" large enough
  84. * to contain a value of the specified size. On some systems, like
  85. * Crays, there is no such thing as an integer variable with 16 bits.
  86. * Keep this in mind if you think this function should have been coded
  87. * to use pointer overlays. All the world's not a VAX.
  88. */
  89. char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  90. char *tp;
  91. struct {
  92. int base;
  93. int len;
  94. } best, cur;
  95. unsigned int words[IN6ADDRSZ / INT16SZ];
  96. int i;
  97. /* Preprocess:
  98. * Copy the input (bytewise) array into a wordwise array.
  99. * Find the longest run of 0x00's in src[] for :: shorthanding.
  100. */
  101. memset(words, '\0', sizeof(words));
  102. for(i = 0; i < IN6ADDRSZ; i++)
  103. words[i/2] |= ((unsigned int)src[i] << ((1 - (i % 2)) << 3));
  104. best.base = -1;
  105. cur.base = -1;
  106. best.len = 0;
  107. cur.len = 0;
  108. for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
  109. if(words[i] == 0) {
  110. if(cur.base == -1) {
  111. cur.base = i; cur.len = 1;
  112. }
  113. else
  114. cur.len++;
  115. }
  116. else if(cur.base != -1) {
  117. if(best.base == -1 || cur.len > best.len)
  118. best = cur;
  119. cur.base = -1;
  120. }
  121. }
  122. if((cur.base != -1) && (best.base == -1 || cur.len > best.len))
  123. best = cur;
  124. if(best.base != -1 && best.len < 2)
  125. best.base = -1;
  126. /* Format the result. */
  127. tp = tmp;
  128. for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
  129. /* Are we inside the best run of 0x00's? */
  130. if(best.base != -1 && i >= best.base && i < (best.base + best.len)) {
  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)
  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. if(!inet_ntop4(src + 12, tp, sizeof(tmp) - (tp - tmp))) {
  144. return NULL;
  145. }
  146. tp += strlen(tp);
  147. break;
  148. }
  149. else {
  150. /* Lower-case digits. Can't use the set from mprintf.c since this
  151. needs to work as a curlx function */
  152. static const unsigned char ldigits[] = "0123456789abcdef";
  153. unsigned int w = words[i];
  154. /* output lowercase 16bit hex number but ignore leading zeroes */
  155. if(w & 0xf000)
  156. *tp++ = ldigits[(w & 0xf000) >> 12];
  157. if(w & 0xff00)
  158. *tp++ = ldigits[(w & 0x0f00) >> 8];
  159. if(w & 0xfff0)
  160. *tp++ = ldigits[(w & 0x00f0) >> 4];
  161. *tp++ = ldigits[(w & 0x000f)];
  162. }
  163. }
  164. /* Was it a trailing run of 0x00's?
  165. */
  166. if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
  167. *tp++ = ':';
  168. *tp++ = '\0';
  169. /* Check for overflow, copy, and we are done.
  170. */
  171. if((size_t)(tp - tmp) > size) {
  172. #ifdef USE_WINSOCK
  173. CURL_SETERRNO(WSAEINVAL);
  174. #else
  175. CURL_SETERRNO(ENOSPC);
  176. #endif
  177. return NULL;
  178. }
  179. strcpy(dst, tmp);
  180. return dst;
  181. }
  182. /*
  183. * Convert a network format address to presentation format.
  184. *
  185. * Returns pointer to presentation format address (`buf').
  186. * Returns NULL on error and errno set with the specific
  187. * error, EAFNOSUPPORT or ENOSPC.
  188. *
  189. * On Windows we store the error in the thread errno, not in the Winsock error
  190. * code. This is to avoid losing the actual last Winsock error. When this
  191. * function returns NULL, check errno not SOCKERRNO.
  192. */
  193. char *curlx_inet_ntop(int af, const void *src, char *buf, size_t size)
  194. {
  195. switch(af) {
  196. case AF_INET:
  197. return inet_ntop4((const unsigned char *)src, buf, size);
  198. case AF_INET6:
  199. return inet_ntop6((const unsigned char *)src, buf, size);
  200. default:
  201. CURL_SETERRNO(SOCKEAFNOSUPPORT);
  202. return NULL;
  203. }
  204. }
  205. #endif /* HAVE_INET_NTOP */