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