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