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