xref: /xnu-8796.121.2/libkern/net/inet_pton.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3*c54f35caSApple OSS Distributions  * Copyright (c) 1996,1999 by Internet Software Consortium.
4*c54f35caSApple OSS Distributions  *
5*c54f35caSApple OSS Distributions  * Permission to use, copy, modify, and distribute this software for any
6*c54f35caSApple OSS Distributions  * purpose with or without fee is hereby granted, provided that the above
7*c54f35caSApple OSS Distributions  * copyright notice and this permission notice appear in all copies.
8*c54f35caSApple OSS Distributions  *
9*c54f35caSApple OSS Distributions  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*c54f35caSApple OSS Distributions  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*c54f35caSApple OSS Distributions  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*c54f35caSApple OSS Distributions  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*c54f35caSApple OSS Distributions  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*c54f35caSApple OSS Distributions  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*c54f35caSApple OSS Distributions  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16*c54f35caSApple OSS Distributions  */
17*c54f35caSApple OSS Distributions #if defined(LIBC_SCCS) && !defined(lint)
18*c54f35caSApple OSS Distributions static const char rcsid[] = "$Id: inet_pton.c,v 1.3.18.2 2005/07/28 07:38:07 marka Exp $";
19*c54f35caSApple OSS Distributions #endif /* LIBC_SCCS and not lint */
20*c54f35caSApple OSS Distributions 
21*c54f35caSApple OSS Distributions #include <string.h>
22*c54f35caSApple OSS Distributions 
23*c54f35caSApple OSS Distributions #include <sys/param.h>
24*c54f35caSApple OSS Distributions #include <sys/socket.h>
25*c54f35caSApple OSS Distributions #include <sys/systm.h>
26*c54f35caSApple OSS Distributions 
27*c54f35caSApple OSS Distributions #include <netinet/in.h>
28*c54f35caSApple OSS Distributions 
29*c54f35caSApple OSS Distributions /*%
30*c54f35caSApple OSS Distributions  * WARNING: Don't even consider trying to compile this on a system where
31*c54f35caSApple OSS Distributions  * sizeof(int) < 4.  sizeof(int) > 4 is fine; all the world's not a VAX.
32*c54f35caSApple OSS Distributions  */
33*c54f35caSApple OSS Distributions 
34*c54f35caSApple OSS Distributions static int      inet_pton4(const char *src, u_char *dst);
35*c54f35caSApple OSS Distributions static int      inet_pton6(const char *src, u_char *dst);
36*c54f35caSApple OSS Distributions 
37*c54f35caSApple OSS Distributions /* int
38*c54f35caSApple OSS Distributions  * inet_pton(af, src, dst)
39*c54f35caSApple OSS Distributions  *	convert from presentation format (which usually means ASCII printable)
40*c54f35caSApple OSS Distributions  *	to network format (which is usually some kind of binary format).
41*c54f35caSApple OSS Distributions  * return:
42*c54f35caSApple OSS Distributions  *	1 if the address was valid for the specified address family
43*c54f35caSApple OSS Distributions  *	0 if the address wasn't valid (`dst' is untouched in this case)
44*c54f35caSApple OSS Distributions  *	-1 if some other error occurred (`dst' is untouched in this case, too)
45*c54f35caSApple OSS Distributions  * author:
46*c54f35caSApple OSS Distributions  *	Paul Vixie, 1996.
47*c54f35caSApple OSS Distributions  */
48*c54f35caSApple OSS Distributions int
inet_pton(int af,const char * src,void * dst)49*c54f35caSApple OSS Distributions inet_pton(int af, const char *src, void *dst)
50*c54f35caSApple OSS Distributions {
51*c54f35caSApple OSS Distributions 	switch (af) {
52*c54f35caSApple OSS Distributions 	case AF_INET:
53*c54f35caSApple OSS Distributions 		return inet_pton4(src, dst);
54*c54f35caSApple OSS Distributions 	case AF_INET6:
55*c54f35caSApple OSS Distributions 		return inet_pton6(src, dst);
56*c54f35caSApple OSS Distributions 	default:
57*c54f35caSApple OSS Distributions 		return -1;
58*c54f35caSApple OSS Distributions 	}
59*c54f35caSApple OSS Distributions 	/* NOTREACHED */
60*c54f35caSApple OSS Distributions }
61*c54f35caSApple OSS Distributions 
62*c54f35caSApple OSS Distributions /* int
63*c54f35caSApple OSS Distributions  * inet_pton4(src, dst)
64*c54f35caSApple OSS Distributions  *	like inet_aton() but without all the hexadecimal and shorthand.
65*c54f35caSApple OSS Distributions  * return:
66*c54f35caSApple OSS Distributions  *	1 if `src' is a valid dotted quad, else 0.
67*c54f35caSApple OSS Distributions  * notice:
68*c54f35caSApple OSS Distributions  *	does not touch `dst' unless it's returning 1.
69*c54f35caSApple OSS Distributions  * author:
70*c54f35caSApple OSS Distributions  *	Paul Vixie, 1996.
71*c54f35caSApple OSS Distributions  */
72*c54f35caSApple OSS Distributions static int
inet_pton4(const char * src,u_char * dst)73*c54f35caSApple OSS Distributions inet_pton4(const char *src, u_char *dst)
74*c54f35caSApple OSS Distributions {
75*c54f35caSApple OSS Distributions 	static const char digits[] = "0123456789";
76*c54f35caSApple OSS Distributions 	int saw_digit, octets, ch;
77*c54f35caSApple OSS Distributions #define NS_INADDRSZ     4
78*c54f35caSApple OSS Distributions 	u_char tmp[NS_INADDRSZ], *tp;
79*c54f35caSApple OSS Distributions 
80*c54f35caSApple OSS Distributions 	saw_digit = 0;
81*c54f35caSApple OSS Distributions 	octets = 0;
82*c54f35caSApple OSS Distributions 	*(tp = tmp) = 0;
83*c54f35caSApple OSS Distributions 	while ((ch = *src++) != '\0') {
84*c54f35caSApple OSS Distributions 		const char *pch;
85*c54f35caSApple OSS Distributions 
86*c54f35caSApple OSS Distributions 		if ((pch = strchr(digits, ch)) != NULL) {
87*c54f35caSApple OSS Distributions 			u_int new = *tp * 10 + (u_int)(pch - digits);
88*c54f35caSApple OSS Distributions 
89*c54f35caSApple OSS Distributions 			if (saw_digit && *tp == 0) {
90*c54f35caSApple OSS Distributions 				return 0;
91*c54f35caSApple OSS Distributions 			}
92*c54f35caSApple OSS Distributions 			if (new > 255) {
93*c54f35caSApple OSS Distributions 				return 0;
94*c54f35caSApple OSS Distributions 			}
95*c54f35caSApple OSS Distributions 			*tp = (u_char)new;
96*c54f35caSApple OSS Distributions 			if (!saw_digit) {
97*c54f35caSApple OSS Distributions 				if (++octets > 4) {
98*c54f35caSApple OSS Distributions 					return 0;
99*c54f35caSApple OSS Distributions 				}
100*c54f35caSApple OSS Distributions 				saw_digit = 1;
101*c54f35caSApple OSS Distributions 			}
102*c54f35caSApple OSS Distributions 		} else if (ch == '.' && saw_digit) {
103*c54f35caSApple OSS Distributions 			if (octets == 4) {
104*c54f35caSApple OSS Distributions 				return 0;
105*c54f35caSApple OSS Distributions 			}
106*c54f35caSApple OSS Distributions 			*++tp = 0;
107*c54f35caSApple OSS Distributions 			saw_digit = 0;
108*c54f35caSApple OSS Distributions 		} else {
109*c54f35caSApple OSS Distributions 			return 0;
110*c54f35caSApple OSS Distributions 		}
111*c54f35caSApple OSS Distributions 	}
112*c54f35caSApple OSS Distributions 	if (octets < 4) {
113*c54f35caSApple OSS Distributions 		return 0;
114*c54f35caSApple OSS Distributions 	}
115*c54f35caSApple OSS Distributions 	memcpy(dst, tmp, NS_INADDRSZ);
116*c54f35caSApple OSS Distributions 	return 1;
117*c54f35caSApple OSS Distributions }
118*c54f35caSApple OSS Distributions 
119*c54f35caSApple OSS Distributions /* int
120*c54f35caSApple OSS Distributions  * inet_pton6(src, dst)
121*c54f35caSApple OSS Distributions  *	convert presentation level address to network order binary form.
122*c54f35caSApple OSS Distributions  * return:
123*c54f35caSApple OSS Distributions  *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
124*c54f35caSApple OSS Distributions  * notice:
125*c54f35caSApple OSS Distributions  *	(1) does not touch `dst' unless it's returning 1.
126*c54f35caSApple OSS Distributions  *	(2) :: in a full address is silently ignored.
127*c54f35caSApple OSS Distributions  * credit:
128*c54f35caSApple OSS Distributions  *	inspired by Mark Andrews.
129*c54f35caSApple OSS Distributions  * author:
130*c54f35caSApple OSS Distributions  *	Paul Vixie, 1996.
131*c54f35caSApple OSS Distributions  */
132*c54f35caSApple OSS Distributions static int
inet_pton6(const char * src,u_char * dst)133*c54f35caSApple OSS Distributions inet_pton6(const char *src, u_char *dst)
134*c54f35caSApple OSS Distributions {
135*c54f35caSApple OSS Distributions 	static const char xdigits_l[] = "0123456789abcdef",
136*c54f35caSApple OSS Distributions 	    xdigits_u[] = "0123456789ABCDEF";
137*c54f35caSApple OSS Distributions #define NS_IN6ADDRSZ    16
138*c54f35caSApple OSS Distributions #define NS_INT16SZ      2
139*c54f35caSApple OSS Distributions 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
140*c54f35caSApple OSS Distributions 	const char *xdigits, *curtok;
141*c54f35caSApple OSS Distributions 	int ch, seen_xdigits;
142*c54f35caSApple OSS Distributions 	u_int val;
143*c54f35caSApple OSS Distributions 
144*c54f35caSApple OSS Distributions 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
145*c54f35caSApple OSS Distributions 	endp = tp + NS_IN6ADDRSZ;
146*c54f35caSApple OSS Distributions 	colonp = NULL;
147*c54f35caSApple OSS Distributions 	/* Leading :: requires some special handling. */
148*c54f35caSApple OSS Distributions 	if (*src == ':') {
149*c54f35caSApple OSS Distributions 		if (*++src != ':') {
150*c54f35caSApple OSS Distributions 			return 0;
151*c54f35caSApple OSS Distributions 		}
152*c54f35caSApple OSS Distributions 	}
153*c54f35caSApple OSS Distributions 	curtok = src;
154*c54f35caSApple OSS Distributions 	seen_xdigits = 0;
155*c54f35caSApple OSS Distributions 	val = 0;
156*c54f35caSApple OSS Distributions 	while ((ch = *src++) != '\0') {
157*c54f35caSApple OSS Distributions 		const char *pch;
158*c54f35caSApple OSS Distributions 
159*c54f35caSApple OSS Distributions 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL) {
160*c54f35caSApple OSS Distributions 			pch = strchr((xdigits = xdigits_u), ch);
161*c54f35caSApple OSS Distributions 		}
162*c54f35caSApple OSS Distributions 		if (pch != NULL) {
163*c54f35caSApple OSS Distributions 			val <<= 4;
164*c54f35caSApple OSS Distributions 			val |= (pch - xdigits);
165*c54f35caSApple OSS Distributions 			if (++seen_xdigits > 4) {
166*c54f35caSApple OSS Distributions 				return 0;
167*c54f35caSApple OSS Distributions 			}
168*c54f35caSApple OSS Distributions 			continue;
169*c54f35caSApple OSS Distributions 		}
170*c54f35caSApple OSS Distributions 		if (ch == ':') {
171*c54f35caSApple OSS Distributions 			curtok = src;
172*c54f35caSApple OSS Distributions 			if (!seen_xdigits) {
173*c54f35caSApple OSS Distributions 				if (colonp) {
174*c54f35caSApple OSS Distributions 					return 0;
175*c54f35caSApple OSS Distributions 				}
176*c54f35caSApple OSS Distributions 				colonp = tp;
177*c54f35caSApple OSS Distributions 				continue;
178*c54f35caSApple OSS Distributions 			} else if (*src == '\0') {
179*c54f35caSApple OSS Distributions 				return 0;
180*c54f35caSApple OSS Distributions 			}
181*c54f35caSApple OSS Distributions 			if (tp + NS_INT16SZ > endp) {
182*c54f35caSApple OSS Distributions 				return 0;
183*c54f35caSApple OSS Distributions 			}
184*c54f35caSApple OSS Distributions 			*tp++ = (u_char) (val >> 8) & 0xff;
185*c54f35caSApple OSS Distributions 			*tp++ = (u_char) val & 0xff;
186*c54f35caSApple OSS Distributions 			seen_xdigits = 0;
187*c54f35caSApple OSS Distributions 			val = 0;
188*c54f35caSApple OSS Distributions 			continue;
189*c54f35caSApple OSS Distributions 		}
190*c54f35caSApple OSS Distributions 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
191*c54f35caSApple OSS Distributions 		    inet_pton4(curtok, tp) > 0) {
192*c54f35caSApple OSS Distributions 			tp += NS_INADDRSZ;
193*c54f35caSApple OSS Distributions 			seen_xdigits = 0;
194*c54f35caSApple OSS Distributions 			break;  /*%< '\\0' was seen by inet_pton4(). */
195*c54f35caSApple OSS Distributions 		}
196*c54f35caSApple OSS Distributions 		return 0;
197*c54f35caSApple OSS Distributions 	}
198*c54f35caSApple OSS Distributions 	if (seen_xdigits) {
199*c54f35caSApple OSS Distributions 		if (tp + NS_INT16SZ > endp) {
200*c54f35caSApple OSS Distributions 			return 0;
201*c54f35caSApple OSS Distributions 		}
202*c54f35caSApple OSS Distributions 		*tp++ = (u_char) (val >> 8) & 0xff;
203*c54f35caSApple OSS Distributions 		*tp++ = (u_char) val & 0xff;
204*c54f35caSApple OSS Distributions 	}
205*c54f35caSApple OSS Distributions 	if (colonp != NULL) {
206*c54f35caSApple OSS Distributions 		/*
207*c54f35caSApple OSS Distributions 		 * Since some memmove()'s erroneously fail to handle
208*c54f35caSApple OSS Distributions 		 * overlapping regions, we'll do the shift by hand.
209*c54f35caSApple OSS Distributions 		 */
210*c54f35caSApple OSS Distributions 		const long n = tp - colonp;
211*c54f35caSApple OSS Distributions 		int i;
212*c54f35caSApple OSS Distributions 
213*c54f35caSApple OSS Distributions 		if (tp == endp) {
214*c54f35caSApple OSS Distributions 			return 0;
215*c54f35caSApple OSS Distributions 		}
216*c54f35caSApple OSS Distributions 		for (i = 1; i <= n; i++) {
217*c54f35caSApple OSS Distributions 			endp[-i] = colonp[n - i];
218*c54f35caSApple OSS Distributions 			colonp[n - i] = 0;
219*c54f35caSApple OSS Distributions 		}
220*c54f35caSApple OSS Distributions 		tp = endp;
221*c54f35caSApple OSS Distributions 	}
222*c54f35caSApple OSS Distributions 	if (tp != endp) {
223*c54f35caSApple OSS Distributions 		return 0;
224*c54f35caSApple OSS Distributions 	}
225*c54f35caSApple OSS Distributions 	memcpy(dst, tmp, NS_IN6ADDRSZ);
226*c54f35caSApple OSS Distributions 	return 1;
227*c54f35caSApple OSS Distributions }
228