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 #ifdef HAVE_INET_NTOA_R
60 return inet_ntoa_r(*(struct in_addr*)src, dst, size);
62 const char *addr = inet_ntoa(*(struct in_addr*)src);
64 if (strlen(addr) >= size)
69 return strcpy(dst, addr);
75 * Convert IPv6 binary address into presentation (printable) format.
77 static const char *inet_ntop6 (const u_char *src, char *dst, size_t size)
80 * Note that int32_t and int16_t need only be "at least" large enough
81 * to contain a value of the specified size. On some systems, like
82 * Crays, there is no such thing as an integer variable with 16 bits.
83 * Keep this in mind if you think this function should have been coded
84 * to use pointer overlays. All the world's not a VAX.
86 char tmp [sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
92 u_long words [IN6ADDRSZ / INT16SZ];
96 * Copy the input (bytewise) array into a wordwise array.
97 * Find the longest run of 0x00's in src[] for :: shorthanding.
99 memset(words, 0, sizeof(words));
100 for (i = 0; i < IN6ADDRSZ; i++)
101 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
105 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
110 cur.base = i, cur.len = 1;
114 else if (cur.base != -1)
116 if (best.base == -1 || cur.len > best.len)
121 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
123 if (best.base != -1 && best.len < 2)
126 /* Format the result.
129 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
131 /* Are we inside the best run of 0x00's?
133 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
140 /* Are we following an initial run of 0x00s or any real hex?
145 /* Is this address an encapsulated IPv4?
147 if (i == 6 && best.base == 0 &&
148 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
150 if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
158 tp += snprintf(tp, 5, "%lx", words[i]);
161 /* Was it a trailing run of 0x00's?
163 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
167 /* Check for overflow, copy, and we're done.
169 if ((size_t)(tp - tmp) > size)
174 return strcpy (dst, tmp);
176 #endif /* ENABLE_IPV6 */
179 * Convert a network format address to presentation format.
181 * Returns pointer to presentation format address (`dst'),
182 * Returns NULL on error (see errno).
184 const char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
188 return inet_ntop4((const u_char*)src, buf, size);
191 return inet_ntop6((const u_char*)src, buf, size);
194 SET_ERRNO(EAFNOSUPPORT);
198 #endif /* HAVE_INET_NTOP */