inet_ntop.c 5.3 KB

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