xref: /freebsd/lib/libc/inet/inet_cidr_pton.c (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: ISC
3  *
4  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5  * Copyright (c) 1998,1999 by Internet Software Consortium.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #if defined(LIBC_SCCS) && !defined(lint)
21 static const char rcsid[] = "$Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp $";
22 #endif
23 #include <sys/cdefs.h>
24 __FBSDID("$FreeBSD$");
25 
26 #include "port_before.h"
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/nameser.h>
32 #include <arpa/inet.h>
33 
34 #include <assert.h>
35 #include <ctype.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 
41 #include "port_after.h"
42 
43 #ifdef SPRINTF_CHAR
44 # define SPRINTF(x) strlen(sprintf/**/x)
45 #else
46 # define SPRINTF(x) ((size_t)sprintf x)
47 #endif
48 
49 static int	inet_cidr_pton_ipv4 (const char *src, u_char *dst, int *bits,
50 				     int ipv6);
51 static int	inet_cidr_pton_ipv6 (const char *src, u_char *dst, int *bits);
52 
53 static int	getbits(const char *, int ipv6);
54 
55 /*%
56  * int
57  * inet_cidr_pton(af, src, dst, *bits)
58  *	convert network address from presentation to network format.
59  *	accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
60  *	"dst" is assumed large enough for its "af".  "bits" is set to the
61  *	/CIDR prefix length, which can have defaults (like /32 for IPv4).
62  * return:
63  *	-1 if an error occurred (inspect errno; ENOENT means bad format).
64  *	0 if successful conversion occurred.
65  * note:
66  *	192.5.5.1/28 has a nonzero host part, which means it isn't a network
67  *	as called for by inet_net_pton() but it can be a host address with
68  *	an included netmask.
69  * author:
70  *	Paul Vixie (ISC), October 1998
71  */
72 int
73 inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
74 	switch (af) {
75 	case AF_INET:
76 		return (inet_cidr_pton_ipv4(src, dst, bits, 0));
77 	case AF_INET6:
78 		return (inet_cidr_pton_ipv6(src, dst, bits));
79 	default:
80 		errno = EAFNOSUPPORT;
81 		return (-1);
82 	}
83 }
84 
85 static const char digits[] = "0123456789";
86 
87 static int
88 inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
89 	const u_char *odst = dst;
90 	int n, ch, tmp, bits;
91 	size_t size = 4;
92 
93 	/* Get the mantissa. */
94 	while (ch = *src++, (isascii(ch) && isdigit(ch))) {
95 		tmp = 0;
96 		do {
97 			n = strchr(digits, ch) - digits;
98 			assert(n >= 0 && n <= 9);
99 			tmp *= 10;
100 			tmp += n;
101 			if (tmp > 255)
102 				goto enoent;
103 		} while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
104 		if (size-- == 0U)
105 			goto emsgsize;
106 		*dst++ = (u_char) tmp;
107 		if (ch == '\0' || ch == '/')
108 			break;
109 		if (ch != '.')
110 			goto enoent;
111 	}
112 
113 	/* Get the prefix length if any. */
114 	bits = -1;
115 	if (ch == '/' && dst > odst) {
116 		bits = getbits(src, ipv6);
117 		if (bits == -2)
118 			goto enoent;
119 	} else if (ch != '\0')
120 		goto enoent;
121 
122 	/* Prefix length can default to /32 only if all four octets spec'd. */
123 	if (bits == -1) {
124 		if (dst - odst == 4)
125 			bits = ipv6 ? 128 : 32;
126 		else
127 			goto enoent;
128 	}
129 
130 	/* If nothing was written to the destination, we found no address. */
131 	if (dst == odst)
132 		goto enoent;
133 
134 	/* If prefix length overspecifies mantissa, life is bad. */
135 	if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
136 		goto enoent;
137 
138 	/* Extend address to four octets. */
139 	while (size-- > 0U)
140 		*dst++ = 0;
141 
142 	*pbits = bits;
143 	return (0);
144 
145  enoent:
146 	errno = ENOENT;
147 	return (-1);
148 
149  emsgsize:
150 	errno = EMSGSIZE;
151 	return (-1);
152 }
153 
154 static int
155 inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
156 	static const char xdigits_l[] = "0123456789abcdef",
157 			  xdigits_u[] = "0123456789ABCDEF";
158 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
159 	const char *xdigits, *curtok;
160 	int ch, saw_xdigit;
161 	u_int val;
162 	int bits;
163 
164 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
165 	endp = tp + NS_IN6ADDRSZ;
166 	colonp = NULL;
167 	/* Leading :: requires some special handling. */
168 	if (*src == ':')
169 		if (*++src != ':')
170 			return (0);
171 	curtok = src;
172 	saw_xdigit = 0;
173 	val = 0;
174 	bits = -1;
175 	while ((ch = *src++) != '\0') {
176 		const char *pch;
177 
178 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
179 			pch = strchr((xdigits = xdigits_u), ch);
180 		if (pch != NULL) {
181 			val <<= 4;
182 			val |= (pch - xdigits);
183 			if (val > 0xffff)
184 				return (0);
185 			saw_xdigit = 1;
186 			continue;
187 		}
188 		if (ch == ':') {
189 			curtok = src;
190 			if (!saw_xdigit) {
191 				if (colonp)
192 					return (0);
193 				colonp = tp;
194 				continue;
195 			} else if (*src == '\0') {
196 				return (0);
197 			}
198 			if (tp + NS_INT16SZ > endp)
199 				return (0);
200 			*tp++ = (u_char) (val >> 8) & 0xff;
201 			*tp++ = (u_char) val & 0xff;
202 			saw_xdigit = 0;
203 			val = 0;
204 			continue;
205 		}
206 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
207 		    inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
208 			tp += NS_INADDRSZ;
209 			saw_xdigit = 0;
210 			break;	/*%< '\\0' was seen by inet_pton4(). */
211 		}
212 		if (ch == '/') {
213 			bits = getbits(src, 1);
214 			if (bits == -2)
215 				goto enoent;
216 			break;
217 		}
218 		goto enoent;
219 	}
220 	if (saw_xdigit) {
221 		if (tp + NS_INT16SZ > endp)
222 			goto emsgsize;
223 		*tp++ = (u_char) (val >> 8) & 0xff;
224 		*tp++ = (u_char) val & 0xff;
225 	}
226 	if (colonp != NULL) {
227 		/*
228 		 * Since some memmove()'s erroneously fail to handle
229 		 * overlapping regions, we'll do the shift by hand.
230 		 */
231 		const int n = tp - colonp;
232 		int i;
233 
234 		if (tp == endp)
235 			goto enoent;
236 		for (i = 1; i <= n; i++) {
237 			endp[- i] = colonp[n - i];
238 			colonp[n - i] = 0;
239 		}
240 		tp = endp;
241 	}
242 
243 	memcpy(dst, tmp, NS_IN6ADDRSZ);
244 
245 	*pbits = bits;
246 	return (0);
247 
248  enoent:
249 	errno = ENOENT;
250 	return (-1);
251 
252  emsgsize:
253 	errno = EMSGSIZE;
254 	return (-1);
255 }
256 
257 static int
258 getbits(const char *src, int ipv6) {
259 	int bits = 0;
260 	char *cp, ch;
261 
262 	if (*src == '\0')			/*%< syntax */
263 		return (-2);
264 	do {
265 		ch = *src++;
266 		cp = strchr(digits, ch);
267 		if (cp == NULL)			/*%< syntax */
268 			return (-2);
269 		bits *= 10;
270 		bits += cp - digits;
271 		if (bits == 0 && *src != '\0')	/*%< no leading zeros */
272 			return (-2);
273 		if (bits > (ipv6 ? 128 : 32))	/*%< range error */
274 			return (-2);
275 	} while (*src != '\0');
276 
277 	return (bits);
278 }
279 
280 /*! \file */
281