2 * Copyright (c) 1996-1999 by 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 DISCLAIMS
9 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 #include <sys/param.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
24 #include <netinet/in.h>
25 #include <arpa/inet.h>
26 #include <arpa/nameser.h>
32 #ifdef __UCLIBC_HAS_IPV6__
34 #define SPRINTF(a) sprintf a
40 * WARNING: Don't even consider trying to compile this on a system where
41 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
46 * inet_ntop4(src, dst, size)
47 * format an IPv4 address
52 * (2) takes a u_char* not an in_addr as input
57 inet_ntop4(src, dst, size)
62 char tmp[sizeof ("255.255.255.255") + 1] = "\0";
67 for (octet = 0; octet <= 3; octet++) {
69 if (src[octet] > 255) {
73 tmp[i++] = '0' + src[octet] / 100;
74 if (tmp[i - 1] == '0') {
75 tmp[i - 1] = '0' + (src[octet] / 10 % 10);
76 if (tmp[i - 1] == '0') i--;
78 tmp[i++] = '0' + (src[octet] / 10 % 10);
80 tmp[i++] = '0' + src[octet] % 10;
85 if (strlen (tmp) > size) {
90 return strcpy(dst, tmp);
95 * inet_ntop6(src, dst, size)
96 * convert IPv6 binary address into presentation (printable) format
104 inet_ntop6(src, dst, size)
110 * Note that int32_t and int16_t need only be "at least" large enough
111 * to contain a value of the specified size. On some systems, like
112 * Crays, there is no such thing as an integer variable with 16 bits.
113 * Keep this in mind if you think this function should have been coded
114 * to use pointer overlays. All the world's not a VAX.
116 char tmp[sizeof ("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
117 struct { int base, len; } best, cur;
123 * Copy the input (bytewise) array into a wordwise array.
124 * Find the longest run of 0x00's in src[] for :: shorthanding.
126 memset(words, '\0', sizeof words);
127 for (i = 0; i < 16; i += 2)
128 words[i / 2] = (src[i] << 8) | src[i + 1];
131 for (i = 0; i < 8; i++) {
134 cur.base = i, cur.len = 1;
138 if (cur.base != -1) {
139 if (best.base == -1 || cur.len > best.len)
145 if (cur.base != -1) {
146 if (best.base == -1 || cur.len > best.len)
149 if (best.base != -1 && best.len < 2)
156 for (i = 0; i < 8; i++) {
157 /* Are we inside the best run of 0x00's? */
158 if (best.base != -1 && i >= best.base &&
159 i < (best.base + best.len)) {
164 /* Are we following an initial run of 0x00s or any real hex? */
167 /* Is this address an encapsulated IPv4? */
168 if (i == 6 && best.base == 0 &&
169 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
170 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
175 tp += SPRINTF((tp, "%x", words[i]));
177 /* Was it a trailing run of 0x00's? */
178 if (best.base != -1 && (best.base + best.len) == 8)
183 * Check for overflow, copy, and we're done.
185 if ((size_t)(tp - tmp) > size) {
186 __set_errno (ENOSPC);
189 return strcpy(dst, tmp);
191 #endif /* INET_IPV6 */
195 * inet_pton4(src, dst)
196 * like inet_aton() but without all the hexadecimal and shorthand.
198 * 1 if `src' is a valid dotted quad, else 0.
200 * does not touch `dst' unless it's returning 1.
209 int saw_digit, octets, ch;
215 while ((ch = *src++) != '\0') {
217 if (ch >= '0' && ch <= '9') {
218 u_int new = *tp * 10 + (ch - '0');
228 } else if (ch == '.' && saw_digit) {
243 * inet_pton6(src, dst)
244 * convert presentation level address to network order binary form.
246 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
248 * (1) does not touch `dst' unless it's returning 1.
249 * (2) :: in a full address is silently ignored.
251 * inspired by Mark Andrews.
263 static const char xdigits[] = "0123456789abcdef";
264 u_char tmp[16], *tp, *endp, *colonp;
270 tp = memset(tmp, '\0', 16);
273 /* Leading :: requires some special handling. */
280 while ((ch = tolower (*src++)) != '\0') {
283 pch = strchr(xdigits, ch);
286 val |= (pch - xdigits);
299 } else if (*src == '\0') {
304 *tp++ = (u_char) (val >> 8) & 0xff;
305 *tp++ = (u_char) val & 0xff;
310 if (ch == '.' && ((tp + 4) <= endp) &&
311 inet_pton4(curtok, tp) > 0) {
314 break; /* '\0' was seen by inet_pton4(). */
321 *tp++ = (u_char) (val >> 8) & 0xff;
322 *tp++ = (u_char) val & 0xff;
324 if (colonp != NULL) {
326 * Since some memmove()'s erroneously fail to handle
327 * overlapping regions, we'll do the shift by hand.
329 const int n = tp - colonp;
334 for (i = 1; i <= n; i++) {
335 endp[- i] = colonp[n - i];
342 memcpy(dst, tmp, 16);
346 #endif /* INET_IPV6 */
351 * inet_ntop(af, src, dst, size)
352 * convert a network format address to presentation format.
354 * pointer to presentation format address (`dst'), or NULL (see errno).
359 inet_ntop(af, src, dst, size)
367 return (inet_ntop4(src, dst, size));
370 return (inet_ntop6(src, dst, size));
373 __set_errno (EAFNOSUPPORT);
381 * inet_pton(af, src, dst)
382 * convert from presentation format (which usually means ASCII printable)
383 * to network format (which is usually some kind of binary format).
385 * 1 if the address was valid for the specified address family
386 * 0 if the address wasn't valid (`dst' is untouched in this case)
387 * -1 if some other error occurred (`dst' is untouched in this case, too)
392 inet_pton(af, src, dst)
399 return (inet_pton4(src, dst));
402 return (inet_pton6(src, dst));
405 __set_errno (EAFNOSUPPORT);