hurd: Make send/recv more posixish
[glibc.git] / resolv / inet_pton.c
blobc0a7d54c303af82b9a563fcf97bec51928cad6c8
1 /*
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
15 * SOFTWARE.
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>
24 #include <ctype.h>
25 #include <string.h>
26 #include <errno.h>
29 * WARNING: Don't even consider trying to compile this on a system where
30 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
33 static int inet_pton4 (const char *src, u_char *dst) internal_function;
34 static int inet_pton6 (const char *src, u_char *dst) internal_function;
36 /* int
37 * inet_pton(af, src, dst)
38 * convert from presentation format (which usually means ASCII printable)
39 * to network format (which is usually some kind of binary format).
40 * return:
41 * 1 if the address was valid for the specified address family
42 * 0 if the address wasn't valid (`dst' is untouched in this case)
43 * -1 if some other error occurred (`dst' is untouched in this case, too)
44 * author:
45 * Paul Vixie, 1996.
47 int
48 __inet_pton (int af, const char *src, void *dst)
50 switch (af) {
51 case AF_INET:
52 return (inet_pton4(src, dst));
53 case AF_INET6:
54 return (inet_pton6(src, dst));
55 default:
56 __set_errno (EAFNOSUPPORT);
57 return (-1);
59 /* NOTREACHED */
61 libc_hidden_def (__inet_pton)
62 weak_alias (__inet_pton, inet_pton)
63 libc_hidden_weak (inet_pton)
65 /* int
66 * inet_pton4(src, dst)
67 * like inet_aton() but without all the hexadecimal, octal (with the
68 * exception of 0) and shorthand.
69 * return:
70 * 1 if `src' is a valid dotted quad, else 0.
71 * notice:
72 * does not touch `dst' unless it's returning 1.
73 * author:
74 * Paul Vixie, 1996.
76 static int
77 internal_function
78 inet_pton4 (const char *src, u_char *dst)
80 int saw_digit, octets, ch;
81 u_char tmp[NS_INADDRSZ], *tp;
83 saw_digit = 0;
84 octets = 0;
85 *(tp = tmp) = 0;
86 while ((ch = *src++) != '\0') {
88 if (ch >= '0' && ch <= '9') {
89 u_int new = *tp * 10 + (ch - '0');
91 if (saw_digit && *tp == 0)
92 return (0);
93 if (new > 255)
94 return (0);
95 *tp = new;
96 if (! saw_digit) {
97 if (++octets > 4)
98 return (0);
99 saw_digit = 1;
101 } else if (ch == '.' && saw_digit) {
102 if (octets == 4)
103 return (0);
104 *++tp = 0;
105 saw_digit = 0;
106 } else
107 return (0);
109 if (octets < 4)
110 return (0);
111 memcpy(dst, tmp, NS_INADDRSZ);
112 return (1);
115 /* int
116 * inet_pton6(src, dst)
117 * convert presentation level address to network order binary form.
118 * return:
119 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
120 * notice:
121 * (1) does not touch `dst' unless it's returning 1.
122 * (2) :: in a full address is silently ignored.
123 * credit:
124 * inspired by Mark Andrews.
125 * author:
126 * Paul Vixie, 1996.
128 static int
129 internal_function
130 inet_pton6 (const char *src, u_char *dst)
132 static const char xdigits[] = "0123456789abcdef";
133 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
134 const char *curtok;
135 int ch, saw_xdigit;
136 u_int val;
138 tp = memset(tmp, '\0', NS_IN6ADDRSZ);
139 endp = tp + NS_IN6ADDRSZ;
140 colonp = NULL;
141 /* Leading :: requires some special handling. */
142 if (*src == ':')
143 if (*++src != ':')
144 return (0);
145 curtok = src;
146 saw_xdigit = 0;
147 val = 0;
148 while ((ch = tolower (*src++)) != '\0') {
149 const char *pch;
151 pch = strchr(xdigits, ch);
152 if (pch != NULL) {
153 val <<= 4;
154 val |= (pch - xdigits);
155 if (val > 0xffff)
156 return (0);
157 saw_xdigit = 1;
158 continue;
160 if (ch == ':') {
161 curtok = src;
162 if (!saw_xdigit) {
163 if (colonp)
164 return (0);
165 colonp = tp;
166 continue;
167 } else if (*src == '\0') {
168 return (0);
170 if (tp + NS_INT16SZ > endp)
171 return (0);
172 *tp++ = (u_char) (val >> 8) & 0xff;
173 *tp++ = (u_char) val & 0xff;
174 saw_xdigit = 0;
175 val = 0;
176 continue;
178 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
179 inet_pton4(curtok, tp) > 0) {
180 tp += NS_INADDRSZ;
181 saw_xdigit = 0;
182 break; /* '\0' was seen by inet_pton4(). */
184 return (0);
186 if (saw_xdigit) {
187 if (tp + NS_INT16SZ > endp)
188 return (0);
189 *tp++ = (u_char) (val >> 8) & 0xff;
190 *tp++ = (u_char) val & 0xff;
192 if (colonp != NULL) {
194 * Since some memmove()'s erroneously fail to handle
195 * overlapping regions, we'll do the shift by hand.
197 const int n = tp - colonp;
198 int i;
200 if (tp == endp)
201 return (0);
202 for (i = 1; i <= n; i++) {
203 endp[- i] = colonp[n - i];
204 colonp[n - i] = 0;
206 tp = endp;
208 if (tp != endp)
209 return (0);
210 memcpy(dst, tmp, NS_IN6ADDRSZ);
211 return (1);