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 #define _MPRINTF_REPLACE /* use our functions only */
28 #include <curl/mprintf.h>
30 #include "inet_ntop.h"
32 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
33 /* this platform has a inet_ntoa_r() function, but no proto declared anywhere
34 so we include our own proto to make compilers happy */
35 #include "inet_ntoa_r.h"
43 #define EAFNOSUPPORT WSAEAFNOSUPPORT
44 #define SET_ERRNO(e) WSASetLastError(errno = (e))
46 #define SET_ERRNO(e) errno = e
50 * Format an IPv4 address, more or less like inet_ntoa().
52 * Returns `dst' (as a const)
55 * - takes a u_char* not an in_addr as input
57 static const char *inet_ntop4 (const u_char *src, char *dst, size_t size)
59 #if defined(HAVE_INET_NTOA_R_2_ARGS)
62 curlassert(size >= 16);
63 ptr = inet_ntoa_r(*(struct in_addr*)src, dst);
65 memmove(dst, ptr, len);
69 #elif defined(HAVE_INET_NTOA_R)
70 return inet_ntoa_r(*(struct in_addr*)src, dst, size);
73 const char *addr = inet_ntoa(*(struct in_addr*)src);
75 if (strlen(addr) >= size)
80 return strcpy(dst, addr);
86 * Convert IPv6 binary address into presentation (printable) format.
88 static const char *inet_ntop6 (const u_char *src, char *dst, size_t size)
91 * Note that int32_t and int16_t need only be "at least" large enough
92 * to contain a value of the specified size. On some systems, like
93 * Crays, there is no such thing as an integer variable with 16 bits.
94 * Keep this in mind if you think this function should have been coded
95 * to use pointer overlays. All the world's not a VAX.
97 char tmp [sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
103 u_long words [IN6ADDRSZ / INT16SZ];
107 * Copy the input (bytewise) array into a wordwise array.
108 * Find the longest run of 0x00's in src[] for :: shorthanding.
110 memset(words, 0, sizeof(words));
111 for (i = 0; i < IN6ADDRSZ; i++)
112 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
116 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
121 cur.base = i, cur.len = 1;
125 else if (cur.base != -1)
127 if (best.base == -1 || cur.len > best.len)
132 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
134 if (best.base != -1 && best.len < 2)
137 /* Format the result.
140 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
142 /* Are we inside the best run of 0x00's?
144 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
151 /* Are we following an initial run of 0x00s or any real hex?
156 /* Is this address an encapsulated IPv4?
158 if (i == 6 && best.base == 0 &&
159 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
161 if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
169 tp += snprintf(tp, 5, "%lx", words[i]);
172 /* Was it a trailing run of 0x00's?
174 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
178 /* Check for overflow, copy, and we're done.
180 if ((size_t)(tp - tmp) > size)
185 return strcpy (dst, tmp);
187 #endif /* ENABLE_IPV6 */
190 * Convert a network format address to presentation format.
192 * Returns pointer to presentation format address (`dst'),
193 * Returns NULL on error (see errno).
195 const char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
199 return inet_ntop4((const u_char*)src, buf, size);
202 return inet_ntop6((const u_char*)src, buf, size);
205 SET_ERRNO(EAFNOSUPPORT);
209 #endif /* HAVE_INET_NTOP */