inet_ntop.c 6.1 KB

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