2 * Copyright (C) 1996-2019 Internet Software Consortium.
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
9 * DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
10 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
11 * INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
13 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
14 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
15 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 * Original code by Paul Vixie. "curlified" by Gisle Vanem.
21 #include "curl_setup.h"
23 #ifndef HAVE_INET_NTOP
25 #ifdef HAVE_SYS_PARAM_H
26 #include <sys/param.h>
28 #ifdef HAVE_NETINET_IN_H
29 #include <netinet/in.h>
31 #ifdef HAVE_ARPA_INET_H
32 #include <arpa/inet.h>
35 #include "inet_ntop.h"
36 #include "curl_printf.h"
43 * Format an IPv4 address, more or less like inet_ntoa().
45 * Returns `dst' (as a const)
48 * - takes a unsigned char* not an in_addr as input
50 static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
52 char tmp[sizeof("255.255.255.255")];
55 DEBUGASSERT(size >= 16);
58 (void)msnprintf(tmp, sizeof(tmp), "%d.%d.%d.%d",
59 ((int)((unsigned char)src[0])) & 0xff,
60 ((int)((unsigned char)src[1])) & 0xff,
61 ((int)((unsigned char)src[2])) & 0xff,
62 ((int)((unsigned char)src[3])) & 0xff);
65 if(len == 0 || len >= size) {
75 * Convert IPv6 binary address into presentation (printable) format.
77 static char *inet_ntop6 (const unsigned 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 unsigned 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));
108 for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
111 cur.base = i, cur.len = 1;
115 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)
125 /* Format the result. */
127 for(i = 0; i < (IN6ADDRSZ / INT16SZ); i++) {
128 /* Are we inside the best run of 0x00's? */
129 if(best.base != -1 && i >= best.base && i < (best.base + best.len)) {
135 /* Are we following an initial run of 0x00s or any real hex?
140 /* Is this address an encapsulated IPv4?
142 if(i == 6 && best.base == 0 &&
143 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
144 if(!inet_ntop4(src + 12, tp, sizeof(tmp) - (tp - tmp))) {
151 tp += msnprintf(tp, 5, "%lx", words[i]);
154 /* Was it a trailing run of 0x00's?
156 if(best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
160 /* Check for overflow, copy, and we're done.
162 if((size_t)(tp - tmp) > size) {
169 #endif /* ENABLE_IPV6 */
172 * Convert a network format address to presentation format.
174 * Returns pointer to presentation format address (`buf').
175 * Returns NULL on error and errno set with the specific
176 * error, EAFNOSUPPORT or ENOSPC.
178 * On Windows we store the error in the thread errno, not
179 * in the winsock error code. This is to avoid losing the
180 * actual last winsock error. So when this function returns
181 * NULL, check errno not SOCKERRNO.
183 char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
187 return inet_ntop4((const unsigned char *)src, buf, size);
190 return inet_ntop6((const unsigned char *)src, buf, size);
193 errno = EAFNOSUPPORT;
197 #endif /* HAVE_INET_NTOP */