]> git.saurik.com Git - apple/libc.git/blob - net/inet_pton.c
Libc-1353.100.2.tar.gz
[apple/libc.git] / net / inet_pton.c
1 /*
2 * Copyright (c) 1996,1999 by Internet Software Consortium.
3 *
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.
7 *
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.
16 */
17
18 #if defined(LIBC_SCCS) && !defined(lint)
19 static char rcsid[] = "$Id: inet_pton.c,v 1.3 2003/04/10 18:53:29 majka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <sys/param.h>
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
31 #include <net/if.h>
32 #include <arpa/nameser.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <errno.h>
36
37 #define INET6 1
38
39 #define IN6ADDRSZ 16
40
41 #if 0
42 #ifndef HAVE_PORTABLE_PROTOTYPE
43 #include "cdecl_ext.h"
44 #endif
45
46 #ifndef HAVE_U_INT16_T
47 #include "bittypes.h"
48 #endif
49 #if !(defined(HAVE_INADDRSZ) && defined(HAVE_IN6ADDRSZ))
50 #include "addrsize.h"
51 #endif
52 #endif
53 #ifndef NS_INADDRSZ
54 #define NS_INADDRSZ INADDRSZ
55 #endif
56 #ifndef NS_IN6ADDRSZ
57 #define NS_IN6ADDRSZ IN6ADDRSZ
58 #endif
59 #ifndef NS_INT16SZ
60 #define NS_INT16SZ sizeof(u_int16_t)
61 #endif
62
63 /*
64 * WARNING: Don't even consider trying to compile this on a system where
65 * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX.
66 */
67
68 static int inet_pton4 __P((const char *src, u_char *dst));
69 #ifdef INET6
70 static int inet_pton6 __P((const char *src, u_char *dst));
71 #endif
72
73 /* int
74 * inet_pton(af, src, dst)
75 * convert from presentation format (which usually means ASCII printable)
76 * to network format (which is usually some kind of binary format).
77 * return:
78 * 1 if the address was valid for the specified address family
79 * 0 if the address wasn't valid (`dst' is untouched in this case)
80 * -1 if some other error occurred (`dst' is untouched in this case, too)
81 * author:
82 * Paul Vixie, 1996.
83 */
84 int
85 inet_pton(int af, const char *src, void *dst)
86 {
87 int status;
88 unsigned short ifnum;
89 char *p, *s;
90
91 switch (af)
92 {
93 case AF_INET:
94 {
95 return (inet_pton4(src, dst));
96 }
97
98 #ifdef INET6
99 case AF_INET6:
100 {
101 ifnum = 0;
102 p = NULL;
103 s = (char *)src;
104
105 if (src != NULL) p = strrchr(src, '%');
106 if (p != NULL)
107 {
108 s = strdup(src);
109 if (s == NULL)
110 {
111 errno = ENOMEM;
112 return -1;
113 }
114
115 s[p - src] = '\0';
116 }
117
118 status = inet_pton6(s, dst);
119 if (p != NULL) free(s);
120 if (status != 1) return status;
121
122 if ((p != NULL) && IN6_IS_ADDR_LINKLOCAL((struct in6_addr *)dst))
123 {
124 ifnum = if_nametoindex(++p);
125 ifnum = htons(ifnum);
126 ((struct in6_addr *)dst)->__u6_addr.__u6_addr16[1] = ifnum;
127 }
128
129 return 1;
130 }
131 #endif
132
133 default:
134 {
135 errno = EAFNOSUPPORT;
136 return -1;
137 }
138 }
139
140 /* NOTREACHED */
141 return -1;
142 }
143
144 /* int
145 * inet_pton4(src, dst)
146 * like inet_aton() but without all the hexadecimal and shorthand.
147 * return:
148 * 1 if `src' is a valid dotted quad, else 0.
149 * notice:
150 * does not touch `dst' unless it's returning 1.
151 * author:
152 * Paul Vixie, 1996.
153 */
154 static int
155 inet_pton4(const char *src, u_char *dst)
156 {
157 static const char digits[] = "0123456789";
158 int saw_digit, octets, ch;
159 u_char tmp[NS_INADDRSZ], *tp;
160
161 saw_digit = 0;
162 octets = 0;
163 *(tp = tmp) = 0;
164 while ((ch = *src++) != '\0') {
165 const char *pch;
166
167 if ((pch = strchr(digits, ch)) != NULL) {
168 u_int new = *tp * 10 + (pch - digits);
169
170 if (new > 255)
171 return (0);
172 *tp = new;
173 if (! saw_digit) {
174 if (++octets > 4)
175 return (0);
176 saw_digit = 1;
177 }
178 } else if (ch == '.' && saw_digit) {
179 if (octets == 4)
180 return (0);
181 *++tp = 0;
182 saw_digit = 0;
183 } else
184 return (0);
185 }
186 if (octets < 4)
187 return (0);
188 memcpy(dst, tmp, NS_INADDRSZ);
189 return (1);
190 }
191
192 #ifdef INET6
193 /* int
194 * inet_pton6(src, dst)
195 * convert presentation level address to network order binary form.
196 * return:
197 * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
198 * notice:
199 * (1) does not touch `dst' unless it's returning 1.
200 * (2) :: in a full address is silently ignored.
201 * credit:
202 * inspired by Mark Andrews.
203 * author:
204 * Paul Vixie, 1996.
205 */
206 static int
207 inet_pton6(const char *src, u_char *dst)
208 {
209 static const char xdigits_l[] = "0123456789abcdef",
210 xdigits_u[] = "0123456789ABCDEF";
211 u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
212 const char *xdigits, *curtok;
213 int ch, saw_xdigit;
214 u_int val;
215
216 memset((tp = tmp), '\0', NS_IN6ADDRSZ);
217 endp = tp + NS_IN6ADDRSZ;
218 colonp = NULL;
219 /* Leading :: requires some special handling. */
220 if (*src == ':')
221 if (*++src != ':')
222 return (0);
223 curtok = src;
224 saw_xdigit = 0;
225 val = 0;
226 while ((ch = *src++) != '\0') {
227 const char *pch;
228
229 if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
230 pch = strchr((xdigits = xdigits_u), ch);
231 if (pch != NULL) {
232 val <<= 4;
233 val |= (pch - xdigits);
234 if (val > 0xffff)
235 return (0);
236 saw_xdigit = 1;
237 continue;
238 }
239 if (ch == ':') {
240 curtok = src;
241 if (!saw_xdigit) {
242 if (colonp)
243 return (0);
244 colonp = tp;
245 continue;
246 } else if (*src == '\0') {
247 return (0);
248 }
249 if (tp + NS_INT16SZ > endp)
250 return (0);
251 *tp++ = (u_char) (val >> 8) & 0xff;
252 *tp++ = (u_char) val & 0xff;
253 saw_xdigit = 0;
254 val = 0;
255 continue;
256 }
257 if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
258 inet_pton4(curtok, tp) > 0) {
259 tp += NS_INADDRSZ;
260 saw_xdigit = 0;
261 break; /* '\0' was seen by inet_pton4(). */
262 }
263 return (0);
264 }
265 if (saw_xdigit) {
266 if (tp + NS_INT16SZ > endp)
267 return (0);
268 *tp++ = (u_char) (val >> 8) & 0xff;
269 *tp++ = (u_char) val & 0xff;
270 }
271 if (colonp != NULL) {
272 /*
273 * Since some memmove()'s erroneously fail to handle
274 * overlapping regions, we'll do the shift by hand.
275 */
276 const int n = tp - colonp;
277 int i;
278
279 if (tp == endp)
280 return (0);
281 for (i = 1; i <= n; i++) {
282 endp[- i] = colonp[n - i];
283 colonp[n - i] = 0;
284 }
285 tp = endp;
286 }
287 if (tp != endp)
288 return (0);
289 memcpy(dst, tmp, NS_IN6ADDRSZ);
290 return (1);
291 }
292 #endif /*INET6*/