2 * Original code by Paul Vixie. "curlified" by Gisle Vanem.
9 #ifdef HAVE_SYS_PARAM_H
10 #include <sys/param.h>
12 #ifdef HAVE_SYS_TYPES_H
13 #include <sys/types.h>
15 #ifdef HAVE_SYS_SOCKET_H
16 #include <sys/socket.h>
18 #ifdef HAVE_NETINET_IN_H
19 #include <netinet/in.h>
21 #ifdef HAVE_ARPA_INET_H
22 #include <arpa/inet.h>
27 #include "inet_ntop.h"
34 #define EAFNOSUPPORT WSAEAFNOSUPPORT
35 #define SET_ERRNO(e) WSASetLastError(errno = (e))
37 #define SET_ERRNO(e) errno = e
41 * Format an IPv4 address, more or less like inet_ntoa().
43 * Returns `dst' (as a const)
46 * - takes a u_char* not an in_addr as input
48 static const char *inet_ntop4 (const u_char *src, char *dst, size_t size)
50 #ifdef HAVE_INET_NTOA_R
51 return inet_ntoa_r(*(struct in_addr*)src, dst, size);
53 const char *addr = inet_ntoa(*(struct in_addr*)src);
55 if (strlen(addr) >= size)
60 return strcpy(dst, addr);
66 * Convert IPv6 binary address into presentation (printable) format.
68 static const char *inet_ntop6 (const u_char *src, char *dst, size_t size)
71 * Note that int32_t and int16_t need only be "at least" large enough
72 * to contain a value of the specified size. On some systems, like
73 * Crays, there is no such thing as an integer variable with 16 bits.
74 * Keep this in mind if you think this function should have been coded
75 * to use pointer overlays. All the world's not a VAX.
77 char tmp [sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
83 u_long words [IN6ADDRSZ / INT16SZ];
87 * Copy the input (bytewise) array into a wordwise array.
88 * Find the longest run of 0x00's in src[] for :: shorthanding.
90 memset(words, 0, sizeof(words));
91 for (i = 0; i < IN6ADDRSZ; i++)
92 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
96 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
101 cur.base = i, cur.len = 1;
105 else if (cur.base != -1)
107 if (best.base == -1 || cur.len > best.len)
112 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
114 if (best.base != -1 && best.len < 2)
117 /* Format the result.
120 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
122 /* Are we inside the best run of 0x00's?
124 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
131 /* Are we following an initial run of 0x00s or any real hex?
136 /* Is this address an encapsulated IPv4?
138 if (i == 6 && best.base == 0 &&
139 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
141 if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
149 tp += sprintf (tp, "%lx", words[i]);
152 /* Was it a trailing run of 0x00's?
154 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
158 /* Check for overflow, copy, and we're done.
160 if ((size_t)(tp - tmp) > size)
165 return strcpy (dst, tmp);
167 #endif /* ENABLE_IPV6 */
170 * Convert a network format address to presentation format.
172 * Returns pointer to presentation format address (`dst'),
173 * Returns NULL on error (see errno).
175 const char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
179 return inet_ntop4((const u_char*)src, buf, size);
182 return inet_ntop6((const u_char*)src, buf, size);
185 SET_ERRNO(EAFNOSUPPORT);
189 #endif /* HAVE_INET_NTOP */