Resync
[CMakeLuaTailorHgBridge.git] / CMakeLua / Utilities / cmcurl / inet_ntop.c
blob9381963f5069526e8396cbd8f3bf16224c8c46a4
1 /*
2 * Copyright (C) 1996-2001 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 "setup.h"
23 #ifndef HAVE_INET_NTOP
25 #ifdef HAVE_SYS_PARAM_H
26 #include <sys/param.h>
27 #endif
28 #ifdef HAVE_SYS_TYPES_H
29 #include <sys/types.h>
30 #endif
31 #ifdef HAVE_SYS_SOCKET_H
32 #include <sys/socket.h>
33 #endif
34 #ifdef HAVE_NETINET_IN_H
35 #include <netinet/in.h>
36 #endif
37 #ifdef HAVE_ARPA_INET_H
38 #include <arpa/inet.h>
39 #endif
40 #include <string.h>
41 #include <errno.h>
43 #define _MPRINTF_REPLACE /* use our functions only */
44 #include <curl/mprintf.h>
46 #include "inet_ntop.h"
48 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
49 /* this platform has a inet_ntoa_r() function, but no proto declared anywhere
50 so we include our own proto to make compilers happy */
51 #include "inet_ntoa_r.h"
52 #endif
54 #define IN6ADDRSZ 16
55 #define INADDRSZ 4
56 #define INT16SZ 2
58 #ifdef USE_WINSOCK
59 #define EAFNOSUPPORT WSAEAFNOSUPPORT
60 #define SET_ERRNO(e) WSASetLastError(errno = (e))
61 #else
62 #define SET_ERRNO(e) errno = e
63 #endif
66 * Format an IPv4 address, more or less like inet_ntoa().
68 * Returns `dst' (as a const)
69 * Note:
70 * - uses no statics
71 * - takes a unsigned char* not an in_addr as input
73 static char *inet_ntop4 (const unsigned char *src, char *dst, size_t size)
75 #if defined(HAVE_INET_NTOA_R_2_ARGS)
76 const char *ptr;
77 curlassert(size >= 16);
78 ptr = inet_ntoa_r(*(struct in_addr*)src, dst);
79 return (char *)memmove(dst, ptr, strlen(ptr)+1);
81 #elif defined(HAVE_INET_NTOA_R)
82 return inet_ntoa_r(*(struct in_addr*)src, dst, size);
84 #else
85 const char *addr = inet_ntoa(*(struct in_addr*)src);
87 if (strlen(addr) >= size)
89 SET_ERRNO(ENOSPC);
90 return (NULL);
92 return strcpy(dst, addr);
93 #endif
96 #ifdef ENABLE_IPV6
98 * Convert IPv6 binary address into presentation (printable) format.
100 static char *inet_ntop6 (const unsigned char *src, char *dst, size_t size)
103 * Note that int32_t and int16_t need only be "at least" large enough
104 * to contain a value of the specified size. On some systems, like
105 * Crays, there is no such thing as an integer variable with 16 bits.
106 * Keep this in mind if you think this function should have been coded
107 * to use pointer overlays. All the world's not a VAX.
109 char tmp[sizeof("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")];
110 char *tp;
111 struct {
112 long base;
113 long len;
114 } best, cur;
115 unsigned long words[IN6ADDRSZ / INT16SZ];
116 int i;
118 /* Preprocess:
119 * Copy the input (bytewise) array into a wordwise array.
120 * Find the longest run of 0x00's in src[] for :: shorthanding.
122 memset(words, '\0', sizeof(words));
123 for (i = 0; i < IN6ADDRSZ; i++)
124 words[i/2] |= (src[i] << ((1 - (i % 2)) << 3));
126 best.base = -1;
127 cur.base = -1;
128 best.len = 0;
129 cur.len = 0;
131 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
133 if (words[i] == 0)
135 if (cur.base == -1)
136 cur.base = i, cur.len = 1;
137 else
138 cur.len++;
140 else if (cur.base != -1)
142 if (best.base == -1 || cur.len > best.len)
143 best = cur;
144 cur.base = -1;
147 if ((cur.base != -1) && (best.base == -1 || cur.len > best.len))
148 best = cur;
149 if (best.base != -1 && best.len < 2)
150 best.base = -1;
152 /* Format the result.
154 tp = tmp;
155 for (i = 0; i < (IN6ADDRSZ / INT16SZ); i++)
157 /* Are we inside the best run of 0x00's?
159 if (best.base != -1 && i >= best.base && i < (best.base + best.len))
161 if (i == best.base)
162 *tp++ = ':';
163 continue;
166 /* Are we following an initial run of 0x00s or any real hex?
168 if (i != 0)
169 *tp++ = ':';
171 /* Is this address an encapsulated IPv4?
173 if (i == 6 && best.base == 0 &&
174 (best.len == 6 || (best.len == 5 && words[5] == 0xffff)))
176 if (!inet_ntop4(src+12, tp, sizeof(tmp) - (tp - tmp)))
178 SET_ERRNO(ENOSPC);
179 return (NULL);
181 tp += strlen(tp);
182 break;
184 tp += snprintf(tp, 5, "%lx", words[i]);
187 /* Was it a trailing run of 0x00's?
189 if (best.base != -1 && (best.base + best.len) == (IN6ADDRSZ / INT16SZ))
190 *tp++ = ':';
191 *tp++ = '\0';
193 /* Check for overflow, copy, and we're done.
195 if ((size_t)(tp - tmp) > size)
197 SET_ERRNO(ENOSPC);
198 return (NULL);
200 return strcpy (dst, tmp);
202 #endif /* ENABLE_IPV6 */
205 * Convert a network format address to presentation format.
207 * Returns pointer to presentation format address (`buf'),
208 * Returns NULL on error (see errno).
210 char *Curl_inet_ntop(int af, const void *src, char *buf, size_t size)
212 switch (af) {
213 case AF_INET:
214 return inet_ntop4((const unsigned char*)src, buf, size);
215 #ifdef ENABLE_IPV6
216 case AF_INET6:
217 return inet_ntop6((const unsigned char*)src, buf, size);
218 #endif
219 default:
220 SET_ERRNO(EAFNOSUPPORT);
221 return NULL;
224 #endif /* HAVE_INET_NTOP */