inet_ntop.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * Original code by Paul Vixie. "curlified" by Gisle Vanem.
  3. */
  4. #include "setup.h"
  5. #ifndef HAVE_INET_NTOP
  6. #ifdef HAVE_SYS_PARAM_H
  7. #include <sys/param.h>
  8. #endif
  9. #ifdef HAVE_SYS_TYPES_H
  10. #include <sys/types.h>
  11. #endif
  12. #ifdef HAVE_SYS_SOCKET_H
  13. #include <sys/socket.h>
  14. #endif
  15. #ifdef HAVE_NETINET_IN_H
  16. #include <netinet/in.h>
  17. #endif
  18. #ifdef HAVE_ARPA_INET_H
  19. #include <arpa/inet.h>
  20. #endif
  21. #include <string.h>
  22. #include <errno.h>
  23. #define _MPRINTF_REPLACE /* use our functions only */
  24. #include <curl/mprintf.h>
  25. #include "inet_ntop.h"
  26. #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
  27. /* this platform has a inet_ntoa_r() function, but no proto declared anywhere
  28. so we include our own proto to make compilers happy */
  29. #include "inet_ntoa_r.h"
  30. #endif
  31. #define IN6ADDRSZ 16
  32. #define INADDRSZ 4
  33. #define INT16SZ 2
  34. #ifdef WIN32
  35. #define EAFNOSUPPORT WSAEAFNOSUPPORT
  36. #define SET_ERRNO(e) WSASetLastError(errno = (e))
  37. #else
  38. #define SET_ERRNO(e) errno = e
  39. #endif
  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 u_char* not an in_addr as input
  47. */
  48. static const char *inet_ntop4 (const u_char *src, char *dst, size_t size)
  49. {
  50. #ifdef HAVE_INET_NTOA_R
  51. return inet_ntoa_r(*(struct in_addr*)src, dst, size);
  52. #else
  53. union {
  54. const u_char* uch;
  55. const struct in_addr* iad;
  56. } srcaddr;
  57. const char *addr;
  58. srcaddr.uch = src;
  59. addr = inet_ntoa(*srcaddr.iad);
  60. if (strlen(addr) >= size)
  61. {
  62. SET_ERRNO(ENOSPC);
  63. return (NULL);
  64. }
  65. return strcpy(dst, addr);
  66. #endif
  67. }
  68. #ifdef ENABLE_IPV6
  69. /*
  70. * Convert IPv6 binary address into presentation (printable) format.
  71. */
  72. static const char *inet_ntop6 (const u_char *src, char *dst, size_t size)
  73. {
  74. /*
  75. * Note that int32_t and int16_t need only be "at least" large enough
  76. * to contain a value of the specified size. On some systems, like
  77. * Crays, there is no such thing as an integer variable with 16 bits.
  78. * Keep this in mind if you think this function should have been coded
  79. * to use pointer overlays. All the world's not a VAX.
  80. */
  81. char tmp [sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
  82. char *tp;
  83. struct {
  84. long base;
  85. long len;
  86. } best, cur;
  87. u_long words [IN6ADDRSZ / INT16SZ];
  88. int i;
  89. /* Preprocess:
  90. * Copy the input (bytewise) array into a wordwise array.
  91. * Find the longest run of 0x00's in src[] for :: shorthanding.
  92. */
  93. memset(words, 0, sizeof(words));
  94. for (i = 0; i < IN6ADDRSZ; i++)
  95. words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
  96. best.base = -1;
  97. cur.base = -1;
  98. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
  99. {
  100. if (words[i] == 0)
  101. {
  102. if (cur.base == -1)
  103. cur.base = i, cur.len = 1;
  104. else
  105. cur.len++;
  106. }
  107. else if (cur.base != -1)
  108. {
  109. if (best.base == -1 || cur.len > best.len)
  110. best = cur;
  111. cur.base = -1;
  112. }
  113. }
  114. if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
  115. best = cur;
  116. if (best.base != -1 && best.len < 2)
  117. best.base = -1;
  118. /* Format the result.
  119. */
  120. tp = tmp;
  121. for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
  122. {
  123. /* Are we inside the best run of 0x00's?
  124. */
  125. if (best.base != -1 && i >= best.base && i < (best.base + best.len))
  126. {
  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 != 0)
  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. {
  140. if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
  141. {
  142. SET_ERRNO(ENOSPC);
  143. return (NULL);
  144. }
  145. tp += strlen(tp);
  146. break;
  147. }
  148. tp += snprintf(tp, 5, "%lx", words[i]);
  149. }
  150. /* Was it a trailing run of 0x00's?
  151. */
  152. if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
  153. *tp++ = ':';
  154. *tp++ = '\0';
  155. /* Check for overflow, copy, and we're done.
  156. */
  157. if ((size_t)(tp - tmp) > size)
  158. {
  159. SET_ERRNO(ENOSPC);
  160. return (NULL);
  161. }
  162. return strcpy (dst, tmp);
  163. }
  164. #endif /* ENABLE_IPV6 */
  165. /*
  166. * Convert a network format address to presentation format.
  167. *
  168. * Returns pointer to presentation format address (`dst'),
  169. * Returns NULL on error (see errno).
  170. */
  171. const char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
  172. {
  173. switch (af) {
  174. case AF_INET:
  175. return inet_ntop4((const u_char*)src, buf, size);
  176. #ifdef ENABLE_IPV6
  177. case AF_INET6:
  178. return inet_ntop6((const u_char*)src, buf, size);
  179. #endif
  180. default:
  181. SET_ERRNO(EAFNOSUPPORT);
  182. return NULL;
  183. }
  184. }
  185. #endif /* HAVE_INET_NTOP */