2
0

inet_ntop.c 5.5 KB

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