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"
29 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
30 /* this platform has a inet_ntoa_r() function, but no proto declared anywhere
31 so we include our own proto to make compilers happy */
32 #include "inet_ntoa_r.h"
40 #define EAFNOSUPPORT WSAEAFNOSUPPORT
41 #define SET_ERRNO(e) WSASetLastError(errno = (e))
43 #define SET_ERRNO(e) errno = e
47 * Format an IPv4 address, more or less like inet_ntoa().
49 * Returns `dst' (as a const)
52 * - takes a u_char* not an in_addr as input
54 static const char *inet_ntop4 (const u_char *src, char *dst, size_t size)
56 #ifdef HAVE_INET_NTOA_R
57 return inet_ntoa_r(*(struct in_addr*)src, dst, size);
59 const char *addr = inet_ntoa(*(struct in_addr*)src);
61 if (strlen(addr) >= size)
66 return strcpy(dst, addr);
72 * Convert IPv6 binary address into presentation (printable) format.
74 static const char *inet_ntop6 (const u_char *src, char *dst, size_t size)
77 * Note that int32_t and int16_t need only be "at least" large enough
78 * to contain a value of the specified size. On some systems, like
79 * Crays, there is no such thing as an integer variable with 16 bits.
80 * Keep this in mind if you think this function should have been coded
81 * to use pointer overlays. All the world's not a VAX.
83 char tmp [sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
89 u_long words [IN6ADDRSZ / INT16SZ];
93 * Copy the input (bytewise) array into a wordwise array.
94 * Find the longest run of 0x00's in src[] for :: shorthanding.
96 memset(words, 0, sizeof(words));
97 for (i = 0; i < IN6ADDRSZ; i++)
98 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
102 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
107 cur.base = i, cur.len = 1;
111 else if (cur.base != -1)
113 if (best.base == -1 || cur.len > best.len)
118 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
120 if (best.base != -1 && best.len < 2)
123 /* Format the result.
126 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
128 /* Are we inside the best run of 0x00's?
130 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
137 /* Are we following an initial run of 0x00s or any real hex?
142 /* Is this address an encapsulated IPv4?
144 if (i == 6 && best.base == 0 &&
145 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
147 if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
155 tp += sprintf (tp, "%lx", words[i]);
158 /* Was it a trailing run of 0x00's?
160 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
164 /* Check for overflow, copy, and we're done.
166 if ((size_t)(tp - tmp) > size)
171 return strcpy (dst, tmp);
173 #endif /* ENABLE_IPV6 */
176 * Convert a network format address to presentation format.
178 * Returns pointer to presentation format address (`dst'),
179 * Returns NULL on error (see errno).
181 const char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
185 return inet_ntop4((const u_char*)src, buf, size);
188 return inet_ntop6((const u_char*)src, buf, size);
191 SET_ERRNO(EAFNOSUPPORT);
195 #endif /* HAVE_INET_NTOP */