revert between 56095 -> 55830 in arch
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / inet_pton.c
blobce92a1c57119bc0d0116a7a23cf8ba9f65a99d4f
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
17 * SOFTWARE.
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>
30 #include <string.h>
31 #include <errno.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);
41 /* int
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).
45 * return:
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)
49 * author:
50 * Paul Vixie, 1996.
52 int
53 __inet_pton(int af, const char * __restrict src, void * __restrict dst)
55 switch (af) {
56 case AF_INET:
57 return (inet_pton4(src, dst));
58 case AF_INET6:
59 return (inet_pton6(src, dst));
60 default:
61 SocketBaseTags(SBTM_SETVAL(SBTC_ERRNO),EAFNOSUPPORT,TAG_DONE);
62 return (-1);
64 /* NOTREACHED */
67 int inet_pton(void)
69 int af = REG_D0;
70 const char *src = (const char *)REG_A0;
71 void *dst = (void *)REG_A1;
73 return __inet_pton(af, src, dst);
76 /* int
77 * inet_pton4(src, dst)
78 * like inet_aton() but without all the hexadecimal and shorthand.
79 * return:
80 * 1 if `src' is a valid dotted quad, else 0.
81 * notice:
82 * does not touch `dst' unless it's returning 1.
83 * author:
84 * Paul Vixie, 1996.
86 static int
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;
93 saw_digit = 0;
94 octets = 0;
95 *(tp = tmp) = 0;
96 while ((ch = *src++) != '\0') {
97 const char *pch;
99 if ((pch = strchr(digits, ch)) != NULL) {
100 u_int new = *tp * 10 + (pch - digits);
102 if (new > 255)
103 return (0);
104 *tp = new;
105 if (! saw_digit) {
106 if (++octets > 4)
107 return (0);
108 saw_digit = 1;
110 } else if (ch == '.' && saw_digit) {
111 if (octets == 4)
112 return (0);
113 *++tp = 0;
114 saw_digit = 0;
115 } else
116 return (0);
118 if (octets < 4)
119 return (0);
121 memcpy(dst, tmp, NS_INADDRSZ);
122 return (1);
125 /* int
126 * inet_pton6(src, dst)
127 * convert presentation level address to network order binary form.
128 * return:
129 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
130 * notice:
131 * (1) does not touch `dst' unless it's returning 1.
132 * (2) :: in a full address is silently ignored.
133 * credit:
134 * inspired by Mark Andrews.
135 * author:
136 * Paul Vixie, 1996.
138 static int
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;
145 int ch, saw_xdigit;
146 u_int val;
148 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
149 endp = tp + NS_IN6ADDRSZ;
150 colonp = NULL;
151 /* Leading :: requires some special handling. */
152 if (*src == ':')
153 if (*++src != ':')
154 return (0);
155 curtok = src;
156 saw_xdigit = 0;
157 val = 0;
158 while ((ch = *src++) != '\0') {
159 const char *pch;
161 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
162 pch = strchr((xdigits = xdigits_u), ch);
163 if (pch != NULL) {
164 val <<= 4;
165 val |= (pch - xdigits);
166 if (val > 0xffff)
167 return (0);
168 saw_xdigit = 1;
169 continue;
171 if (ch == ':') {
172 curtok = src;
173 if (!saw_xdigit) {
174 if (colonp)
175 return (0);
176 colonp = tp;
177 continue;
179 if (tp + NS_INT16SZ > endp)
180 return (0);
181 *tp++ = (u_char) (val >> 8) & 0xff;
182 *tp++ = (u_char) val & 0xff;
183 saw_xdigit = 0;
184 val = 0;
185 continue;
187 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
188 inet_pton4(curtok, tp) > 0) {
189 tp += NS_INADDRSZ;
190 saw_xdigit = 0;
191 break; /* '\0' was seen by inet_pton4(). */
193 return (0);
195 if (saw_xdigit) {
196 if (tp + NS_INT16SZ > endp)
197 return (0);
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;
207 int i;
209 for (i = 1; i <= n; i++) {
210 endp[- i] = colonp[n - i];
211 colonp[n - i] = 0;
213 tp = endp;
215 if (tp != endp)
216 return (0);
217 memcpy(dst, tmp, NS_IN6ADDRSZ);
218 return (1);