xref: /freebsd/sys/libkern/inet_pton.c (revision edf8578117e8844e02c0121147f45e4609b30680)
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 #if defined(LIBC_SCCS) && !defined(lint)
19 static const char rcsid[] = "$Id: inet_pton.c,v 1.3.18.2 2005/07/28 07:38:07 marka Exp $";
20 #endif /* LIBC_SCCS and not lint */
21 #include <sys/cdefs.h>
22 #include <sys/param.h>
23 #include <sys/socket.h>
24 #include <sys/systm.h>
25 
26 #include <netinet/in.h>
27 
28 /*%
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.
31  */
32 
33 static int	inet_pton4(const char *src, u_char *dst);
34 static int	inet_pton6(const char *src, u_char *dst);
35 
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.
46  */
47 int
48 inet_pton(int af, const char *src, void *dst)
49 {
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 		return (-1);
57 	}
58 	/* NOTREACHED */
59 }
60 
61 /* int
62  * inet_pton4(src, dst)
63  *	like inet_aton() but without all the hexadecimal and shorthand.
64  * return:
65  *	1 if `src' is a valid dotted quad, else 0.
66  * notice:
67  *	does not touch `dst' unless it's returning 1.
68  * author:
69  *	Paul Vixie, 1996.
70  */
71 static int
72 inet_pton4(const char *src, u_char *dst)
73 {
74 	static const char digits[] = "0123456789";
75 	int saw_digit, octets, ch;
76 #define NS_INADDRSZ	4
77 	u_char tmp[NS_INADDRSZ], *tp;
78 
79 	saw_digit = 0;
80 	octets = 0;
81 	*(tp = tmp) = 0;
82 	while ((ch = *src++) != '\0') {
83 		const char *pch;
84 
85 		if ((pch = strchr(digits, ch)) != NULL) {
86 			u_int new = *tp * 10 + (pch - digits);
87 
88 			if (saw_digit && *tp == 0)
89 				return (0);
90 			if (new > 255)
91 				return (0);
92 			*tp = new;
93 			if (!saw_digit) {
94 				if (++octets > 4)
95 					return (0);
96 				saw_digit = 1;
97 			}
98 		} else if (ch == '.' && saw_digit) {
99 			if (octets == 4)
100 				return (0);
101 			*++tp = 0;
102 			saw_digit = 0;
103 		} else
104 			return (0);
105 	}
106 	if (octets < 4)
107 		return (0);
108 	memcpy(dst, tmp, NS_INADDRSZ);
109 	return (1);
110 }
111 
112 /* int
113  * inet_pton6(src, dst)
114  *	convert presentation level address to network order binary form.
115  * return:
116  *	1 if `src' is a valid [RFC1884 2.2] address, else 0.
117  * notice:
118  *	(1) does not touch `dst' unless it's returning 1.
119  *	(2) :: in a full address is silently ignored.
120  * credit:
121  *	inspired by Mark Andrews.
122  * author:
123  *	Paul Vixie, 1996.
124  */
125 static int
126 inet_pton6(const char *src, u_char *dst)
127 {
128 	static const char xdigits_l[] = "0123456789abcdef",
129 			  xdigits_u[] = "0123456789ABCDEF";
130 #define NS_IN6ADDRSZ	16
131 #define NS_INT16SZ	2
132 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
133 	const char *xdigits, *curtok;
134 	int ch, seen_xdigits;
135 	u_int val;
136 
137 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
138 	endp = tp + NS_IN6ADDRSZ;
139 	colonp = NULL;
140 	/* Leading :: requires some special handling. */
141 	if (*src == ':')
142 		if (*++src != ':')
143 			return (0);
144 	curtok = src;
145 	seen_xdigits = 0;
146 	val = 0;
147 	while ((ch = *src++) != '\0') {
148 		const char *pch;
149 
150 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
151 			pch = strchr((xdigits = xdigits_u), ch);
152 		if (pch != NULL) {
153 			val <<= 4;
154 			val |= (pch - xdigits);
155 			if (++seen_xdigits > 4)
156 				return (0);
157 			continue;
158 		}
159 		if (ch == ':') {
160 			curtok = src;
161 			if (!seen_xdigits) {
162 				if (colonp)
163 					return (0);
164 				colonp = tp;
165 				continue;
166 			} else if (*src == '\0') {
167 				return (0);
168 			}
169 			if (tp + NS_INT16SZ > endp)
170 				return (0);
171 			*tp++ = (u_char) (val >> 8) & 0xff;
172 			*tp++ = (u_char) val & 0xff;
173 			seen_xdigits = 0;
174 			val = 0;
175 			continue;
176 		}
177 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
178 		    inet_pton4(curtok, tp) > 0) {
179 			tp += NS_INADDRSZ;
180 			seen_xdigits = 0;
181 			break;	/*%< '\\0' was seen by inet_pton4(). */
182 		}
183 		return (0);
184 	}
185 	if (seen_xdigits) {
186 		if (tp + NS_INT16SZ > endp)
187 			return (0);
188 		*tp++ = (u_char) (val >> 8) & 0xff;
189 		*tp++ = (u_char) val & 0xff;
190 	}
191 	if (colonp != NULL) {
192 		/*
193 		 * Since some memmove()'s erroneously fail to handle
194 		 * overlapping regions, we'll do the shift by hand.
195 		 */
196 		const int n = tp - colonp;
197 		int i;
198 
199 		if (tp == endp)
200 			return (0);
201 		for (i = 1; i <= n; i++) {
202 			endp[- i] = colonp[n - i];
203 			colonp[n - i] = 0;
204 		}
205 		tp = endp;
206 	}
207 	if (tp != endp)
208 		return (0);
209 	memcpy(dst, tmp, NS_IN6ADDRSZ);
210 	return (1);
211 }
212 
213 /*! \file */
214