inet_ntop.c 5.4 KB

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