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>
35 * WARNING: Don't even consider trying to compile this on a system where
36 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
41 * inet_ntop4(src, dst, size)
42 * format an IPv4 address
47 * (2) takes a u_char* not an in_addr as input
52 inet_ntop4(const u_char *src, char *dst, size_t size)
54 char tmp[sizeof ("255.255.255.255") + 1] = "\0";
59 for (octet = 0; octet <= 3; octet++) {
61 #if 0 /* since src is unsigned char, it will never be > 255 ... */
62 if (src[octet] > 255) {
67 tmp[i++] = '0' + src[octet] / 100;
68 if (tmp[i - 1] == '0') {
69 tmp[i - 1] = '0' + (src[octet] / 10 % 10);
70 if (tmp[i - 1] == '0') i--;
72 tmp[i++] = '0' + (src[octet] / 10 % 10);
74 tmp[i++] = '0' + src[octet] % 10;
79 if (strlen (tmp) > size) {
84 return strcpy(dst, tmp);
89 * inet_ntop6(src, dst, size)
90 * convert IPv6 binary address into presentation (printable) format
95 #ifdef __UCLIBC_HAS_IPV6__
98 inet_ntop6(const u_char *src, char *dst, size_t size)
101 * Note that int32_t and int16_t need only be "at least" large enough
102 * to contain a value of the specified size. On some systems, like
103 * Crays, there is no such thing as an integer variable with 16 bits.
104 * Keep this in mind if you think this function should have been coded
105 * to use pointer overlays. All the world's not a VAX.
107 char tmp[sizeof ("ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255")], *tp;
108 struct { int base, len; } best, cur;
114 * Copy the input (bytewise) array into a wordwise array.
115 * Find the longest run of 0x00's in src[] for :: shorthanding.
117 memset(words, '\0', sizeof words);
118 for (i = 0; i < 16; i += 2)
119 words[i / 2] = (src[i] << 8) | src[i + 1];
122 for (i = 0; i < 8; i++) {
125 cur.base = i, cur.len = 1;
129 if (cur.base != -1) {
130 if (best.base == -1 || cur.len > best.len)
136 if (cur.base != -1) {
137 if (best.base == -1 || cur.len > best.len)
140 if (best.base != -1 && best.len < 2)
147 for (i = 0; i < 8; i++) {
148 /* Are we inside the best run of 0x00's? */
149 if (best.base != -1 && i >= best.base &&
150 i < (best.base + best.len)) {
155 /* Are we following an initial run of 0x00s or any real hex? */
158 /* Is this address an encapsulated IPv4? */
159 if (i == 6 && best.base == 0 &&
160 (best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
161 if (!inet_ntop4(src+12, tp, sizeof tmp - (tp - tmp)))
166 tp += sprintf(tp, "%x", words[i]);
168 /* Was it a trailing run of 0x00's? */
169 if (best.base != -1 && (best.base + best.len) == 8)
174 * Check for overflow, copy, and we're done.
176 if ((size_t)(tp - tmp) > size) {
177 __set_errno (ENOSPC);
180 return strcpy(dst, tmp);
182 #endif /* __UCLIBC_HAS_IPV6__ */
186 * inet_pton4(src, dst)
187 * like inet_aton() but without all the hexadecimal and shorthand.
189 * 1 if `src' is a valid dotted quad, else 0.
191 * does not touch `dst' unless it's returning 1.
196 inet_pton4(const char *src, u_char *dst)
198 int saw_digit, octets, ch;
204 while ((ch = *src++) != '\0') {
206 if (ch >= '0' && ch <= '9') {
207 u_int new = *tp * 10 + (ch - '0');
217 } else if (ch == '.' && saw_digit) {
232 * inet_pton6(src, dst)
233 * convert presentation level address to network order binary form.
235 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
237 * (1) does not touch `dst' unless it's returning 1.
238 * (2) :: in a full address is silently ignored.
240 * inspired by Mark Andrews.
245 #ifdef __UCLIBC_HAS_IPV6__
247 /* We cannot use the macro version of tolower() or very bad
248 * things happen when '*src++' gets evaluated multiple times.
249 * So * undef it here so we get the function version of tolower
255 inet_pton6(const char *src, u_char *dst)
257 static const char xdigits[] = "0123456789abcdef";
258 u_char tmp[16], *tp, *endp, *colonp;
264 tp = memset(tmp, '\0', 16);
267 /* Leading :: requires some special handling. */
274 while ((ch = tolower (*src++)) != '\0') {
277 pch = strchr(xdigits, ch);
280 val |= (pch - xdigits);
293 } else if (*src == '\0') {
298 *tp++ = (u_char) (val >> 8) & 0xff;
299 *tp++ = (u_char) val & 0xff;
304 if (ch == '.' && ((tp + 4) <= endp) &&
305 inet_pton4(curtok, tp) > 0) {
308 break; /* '\0' was seen by inet_pton4(). */
315 *tp++ = (u_char) (val >> 8) & 0xff;
316 *tp++ = (u_char) val & 0xff;
318 if (colonp != NULL) {
320 * Since some memmove()'s erroneously fail to handle
321 * overlapping regions, we'll do the shift by hand.
323 const int n = tp - colonp;
328 for (i = 1; i <= n; i++) {
329 endp[- i] = colonp[n - i];
336 memcpy(dst, tmp, 16);
340 #endif /* __UCLIBC_HAS_IPV6__ */
345 * inet_ntop(af, src, dst, size)
346 * convert a network format address to presentation format.
348 * pointer to presentation format address (`dst'), or NULL (see errno).
353 inet_ntop(af, src, dst, size)
361 return (inet_ntop4(src, dst, size));
362 #ifdef __UCLIBC_HAS_IPV6__
364 return (inet_ntop6(src, dst, size));
367 __set_errno (EAFNOSUPPORT);
375 * inet_pton(af, src, dst)
376 * convert from presentation format (which usually means ASCII printable)
377 * to network format (which is usually some kind of binary format).
379 * 1 if the address was valid for the specified address family
380 * 0 if the address wasn't valid (`dst' is untouched in this case)
381 * -1 if some other error occurred (`dst' is untouched in this case, too)
386 inet_pton(af, src, dst)
393 return (inet_pton4(src, dst));
394 #ifdef __UCLIBC_HAS_IPV6__
396 return (inet_pton6(src, dst));
399 __set_errno (EAFNOSUPPORT);