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
18 #include <sys/param.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <arpa/nameser.h>
28 static int inet_pton4 (const char *src
, unsigned char *dst
);
29 static int inet_pton6 (const char *src
, unsigned char *dst
);
31 /* Convert from presentation format (which usually means ASCII printable)
32 * to network format (which is usually some kind of binary format).
35 * 1 if the address was valid for the specified address family
36 * 0 if the address wasn't valid (`dst' is untouched in this case)
37 * -1 if some other error occurred (`dst' is untouched in this case, too)
42 __inet_pton (int af
, const char *src
, void *dst
)
47 return inet_pton4 (src
, dst
);
49 return inet_pton6 (src
, dst
);
51 __set_errno (EAFNOSUPPORT
);
55 libc_hidden_def (__inet_pton
)
56 weak_alias (__inet_pton
, inet_pton
)
57 libc_hidden_weak (inet_pton
)
59 /* Like inet_atonbut without all the hexadecimal, octal and shorthand.
62 * 1 if `src' is a valid dotted quad, else 0.
64 * does not touch `dst' unless it's returning 1.
69 inet_pton4 (const char *src
, unsigned char *dst
)
71 int saw_digit
, octets
, ch
;
72 unsigned char tmp
[NS_INADDRSZ
], *tp
;
77 while ((ch
= *src
++) != '\0')
79 if (ch
>= '0' && ch
<= '9')
81 unsigned int new = *tp
* 10 + (ch
- '0');
83 if (saw_digit
&& *tp
== 0)
95 else if (ch
== '.' && saw_digit
)
107 memcpy (dst
, tmp
, NS_INADDRSZ
);
111 /* Cconvert presentation level address to network order binary form.
114 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
116 * (1) does not touch `dst' unless it's returning 1.
117 * (2) :: in a full address is silently ignored.
119 * inspired by Mark Andrews.
124 inet_pton6 (const char *src
, unsigned char *dst
)
126 static const char xdigits
[] = "0123456789abcdef";
127 unsigned char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
132 tp
= memset (tmp
, '\0', NS_IN6ADDRSZ
);
133 endp
= tp
+ NS_IN6ADDRSZ
;
135 /* Leading :: requires some special handling. */
142 while ((ch
= tolower (*src
++)) != '\0')
146 pch
= strchr (xdigits
, ch
);
150 val
|= (pch
- xdigits
);
166 else if (*src
== '\0')
170 if (tp
+ NS_INT16SZ
> endp
)
172 *tp
++ = (unsigned char) (val
>> 8) & 0xff;
173 *tp
++ = (unsigned char) val
& 0xff;
178 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
179 inet_pton4 (curtok
, tp
) > 0)
183 break; /* '\0' was seen by inet_pton4. */
189 if (tp
+ NS_INT16SZ
> endp
)
191 *tp
++ = (unsigned char) (val
>> 8) & 0xff;
192 *tp
++ = (unsigned char) val
& 0xff;
197 * Since some memmove's erroneously fail to handle
198 * overlapping regions, we'll do the shift by hand.
200 const int n
= tp
- colonp
;
205 for (i
= 1; i
<= n
; i
++)
207 endp
[- i
] = colonp
[n
- i
];
214 memcpy (dst
, tmp
, NS_IN6ADDRSZ
);