1 /* $KAME: inet_pton.c,v 1.0 2005/11/14 15:49:40 SONIC Exp $ */
3 /* Copyright (c) 1996 by Internet Software Consortium.
4 * Copyright (c) 2005 by Pavel Fedin.
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
20 #include <amitcp/socketbasetags.h>
21 #include <emul/emulregs.h>
22 #include <proto/socket.h>
23 #include <sys/cdefs.h>
24 #include <sys/param.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <arpa/nameser.h>
34 * WARNING: Don't even consider trying to compile this on a system where
35 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
38 static int inet_pton4(const char *src
, u_char
*dst
);
39 static int inet_pton6(const char *src
, u_char
*dst
);
42 * inet_pton(af, src, dst)
43 * convert from presentation format (which usually means ASCII printable)
44 * to network format (which is usually some kind of binary format).
46 * 1 if the address was valid for the specified address family
47 * 0 if the address wasn't valid (`dst' is untouched in this case)
48 * -1 if some other error occurred (`dst' is untouched in this case, too)
53 __inet_pton(int af
, const char * __restrict src
, void * __restrict dst
)
57 return (inet_pton4(src
, dst
));
59 return (inet_pton6(src
, dst
));
61 SocketBaseTags(SBTM_SETVAL(SBTC_ERRNO
),EAFNOSUPPORT
,TAG_DONE
);
70 const char *src
= (const char *)REG_A0
;
71 void *dst
= (void *)REG_A1
;
73 return __inet_pton(af
, src
, dst
);
77 * inet_pton4(src, dst)
78 * like inet_aton() but without all the hexadecimal and shorthand.
80 * 1 if `src' is a valid dotted quad, else 0.
82 * does not touch `dst' unless it's returning 1.
87 inet_pton4(const char *src
, u_char
*dst
)
89 static const char digits
[] = "0123456789";
90 int saw_digit
, octets
, ch
;
91 u_char tmp
[NS_INADDRSZ
], *tp
;
96 while ((ch
= *src
++) != '\0') {
99 if ((pch
= strchr(digits
, ch
)) != NULL
) {
100 u_int
new = *tp
* 10 + (pch
- digits
);
110 } else if (ch
== '.' && saw_digit
) {
121 memcpy(dst
, tmp
, NS_INADDRSZ
);
126 * inet_pton6(src, dst)
127 * convert presentation level address to network order binary form.
129 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
131 * (1) does not touch `dst' unless it's returning 1.
132 * (2) :: in a full address is silently ignored.
134 * inspired by Mark Andrews.
139 inet_pton6(const char *src
, u_char
*dst
)
141 static const char xdigits_l
[] = "0123456789abcdef",
142 xdigits_u
[] = "0123456789ABCDEF";
143 u_char tmp
[NS_IN6ADDRSZ
], *tp
, *endp
, *colonp
;
144 const char *xdigits
, *curtok
;
148 memset((tp
= tmp
), '\0', NS_IN6ADDRSZ
);
149 endp
= tp
+ NS_IN6ADDRSZ
;
151 /* Leading :: requires some special handling. */
158 while ((ch
= *src
++) != '\0') {
161 if ((pch
= strchr((xdigits
= xdigits_l
), ch
)) == NULL
)
162 pch
= strchr((xdigits
= xdigits_u
), ch
);
165 val
|= (pch
- xdigits
);
179 if (tp
+ NS_INT16SZ
> endp
)
181 *tp
++ = (u_char
) (val
>> 8) & 0xff;
182 *tp
++ = (u_char
) val
& 0xff;
187 if (ch
== '.' && ((tp
+ NS_INADDRSZ
) <= endp
) &&
188 inet_pton4(curtok
, tp
) > 0) {
191 break; /* '\0' was seen by inet_pton4(). */
196 if (tp
+ NS_INT16SZ
> endp
)
198 *tp
++ = (u_char
) (val
>> 8) & 0xff;
199 *tp
++ = (u_char
) val
& 0xff;
201 if (colonp
!= NULL
) {
203 * Since some memmove()'s erroneously fail to handle
204 * overlapping regions, we'll do the shift by hand.
206 const int n
= tp
- colonp
;
209 for (i
= 1; i
<= n
; i
++) {
210 endp
[- i
] = colonp
[n
- i
];
217 memcpy(dst
, tmp
, NS_IN6ADDRSZ
);