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 char *inet_ntop4 (const u_char *src, char *dst, size_t size)
59 #if defined(HAVE_INET_NTOA_R_2_ARGS)
61 curlassert(size >= 16);
62 ptr = inet_ntoa_r(*(struct in_addr*)src, dst);
63 return (char *)memmove(dst, ptr, strlen(ptr)+1);
65 #elif defined(HAVE_INET_NTOA_R)
66 return inet_ntoa_r(*(struct in_addr*)src, dst, size);
69 const char *addr = inet_ntoa(*(struct in_addr*)src);
71 if (strlen(addr) >= size)
76 return strcpy(dst, addr);
82 * Convert IPv6 binary address into presentation (printable) format.
84 static char *inet_ntop6 (const u_char *src, char *dst, size_t size)
87 * Note that int32_t and int16_t need only be "at least" large enough
88 * to contain a value of the specified size. On some systems, like
89 * Crays, there is no such thing as an integer variable with 16 bits.
90 * Keep this in mind if you think this function should have been coded
91 * to use pointer overlays. All the world's not a VAX.
93 char tmp [sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
99 u_long words [IN6ADDRSZ / INT16SZ];
103 * Copy the input (bytewise) array into a wordwise array.
104 * Find the longest run of 0x00's in src[] for :: shorthanding.
106 memset(words, 0, sizeof(words));
107 for (i = 0; i < IN6ADDRSZ; i++)
108 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
112 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
117 cur.base = i, cur.len = 1;
121 else if (cur.base != -1)
123 if (best.base == -1 || cur.len > best.len)
128 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
130 if (best.base != -1 && best.len < 2)
133 /* Format the result.
136 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
138 /* Are we inside the best run of 0x00's?
140 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
147 /* Are we following an initial run of 0x00s or any real hex?
152 /* Is this address an encapsulated IPv4?
154 if (i == 6 && best.base == 0 &&
155 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
157 if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
165 tp += snprintf(tp, 5, "%lx", words[i]);
168 /* Was it a trailing run of 0x00's?
170 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
174 /* Check for overflow, copy, and we're done.
176 if ((size_t)(tp - tmp) > size)
181 return strcpy (dst, tmp);
183 #endif /* ENABLE_IPV6 */
186 * Convert a network format address to presentation format.
188 * Returns pointer to presentation format address (`buf'),
189 * Returns NULL on error (see errno).
191 char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
195 return inet_ntop4((const u_char*)src, buf, size);
198 return inet_ntop6((const u_char*)src, buf, size);
201 SET_ERRNO(EAFNOSUPPORT);
205 #endif /* HAVE_INET_NTOP */