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