1*e6e30c83Schristos /*	$NetBSD: inet_net_pton.c,v 1.1.1.2 2012/09/09 16:07:50 christos Exp $	*/
2b5677b36Schristos 
3b5677b36Schristos /*
4b5677b36Schristos  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos  * Copyright (C) 1996, 1998, 1999, 2001, 2003  Internet Software Consortium.
6b5677b36Schristos  *
7b5677b36Schristos  * Permission to use, copy, modify, and/or distribute this software for any
8b5677b36Schristos  * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos  * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos  *
11b5677b36Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12b5677b36Schristos  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13b5677b36Schristos  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14b5677b36Schristos  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15b5677b36Schristos  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16b5677b36Schristos  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17b5677b36Schristos  * PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos  */
19b5677b36Schristos 
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: inet_net_pton.c,v 1.10 2008/11/14 02:36:51 marka Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos 
24b5677b36Schristos #include "port_before.h"
25b5677b36Schristos 
26b5677b36Schristos #include <sys/types.h>
27b5677b36Schristos #include <sys/socket.h>
28b5677b36Schristos #include <netinet/in.h>
29b5677b36Schristos #include <arpa/nameser.h>
30b5677b36Schristos #include <arpa/inet.h>
31b5677b36Schristos 
32b5677b36Schristos #include <isc/assertions.h>
33b5677b36Schristos #include <ctype.h>
34b5677b36Schristos #include <errno.h>
35b5677b36Schristos #include <stdio.h>
36b5677b36Schristos #include <string.h>
37b5677b36Schristos #include <stdlib.h>
38b5677b36Schristos 
39b5677b36Schristos #include "port_after.h"
40b5677b36Schristos 
41b5677b36Schristos #ifdef SPRINTF_CHAR
42b5677b36Schristos # define SPRINTF(x) strlen(sprintf/**/x)
43b5677b36Schristos #else
44b5677b36Schristos # define SPRINTF(x) ((size_t)sprintf x)
45b5677b36Schristos #endif
46b5677b36Schristos 
47b5677b36Schristos /*%
48b5677b36Schristos  * static int
49b5677b36Schristos  * inet_net_pton_ipv4(src, dst, size)
50b5677b36Schristos  *	convert IPv4 network number from presentation to network format.
51b5677b36Schristos  *	accepts hex octets, hex strings, decimal octets, and /CIDR.
52b5677b36Schristos  *	"size" is in bytes and describes "dst".
53b5677b36Schristos  * return:
54b5677b36Schristos  *	number of bits, either imputed classfully or specified with /CIDR,
55b5677b36Schristos  *	or -1 if some failure occurred (check errno).  ENOENT means it was
56b5677b36Schristos  *	not an IPv4 network specification.
57b5677b36Schristos  * note:
58b5677b36Schristos  *	network byte order assumed.  this means 192.5.5.240/28 has
59b5677b36Schristos  *	0b11110000 in its fourth octet.
60b5677b36Schristos  * author:
61b5677b36Schristos  *	Paul Vixie (ISC), June 1996
62b5677b36Schristos  */
63b5677b36Schristos static int
inet_net_pton_ipv4(const char * src,u_char * dst,size_t size)64b5677b36Schristos inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
65b5677b36Schristos 	static const char xdigits[] = "0123456789abcdef";
66b5677b36Schristos 	static const char digits[] = "0123456789";
67b5677b36Schristos 	int n, ch, tmp = 0, dirty, bits;
68b5677b36Schristos 	const u_char *odst = dst;
69b5677b36Schristos 
70b5677b36Schristos 	ch = *src++;
71b5677b36Schristos 	if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
72b5677b36Schristos 	    && isascii((unsigned char)(src[1]))
73b5677b36Schristos 	    && isxdigit((unsigned char)(src[1]))) {
74b5677b36Schristos 		/* Hexadecimal: Eat nybble string. */
75b5677b36Schristos 		if (size <= 0U)
76b5677b36Schristos 			goto emsgsize;
77b5677b36Schristos 		dirty = 0;
78b5677b36Schristos 		src++;	/*%< skip x or X. */
79b5677b36Schristos 		while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
80b5677b36Schristos 			if (isupper(ch))
81b5677b36Schristos 				ch = tolower(ch);
82b5677b36Schristos 			n = strchr(xdigits, ch) - xdigits;
83b5677b36Schristos 			INSIST(n >= 0 && n <= 15);
84b5677b36Schristos 			if (dirty == 0)
85b5677b36Schristos 				tmp = n;
86b5677b36Schristos 			else
87b5677b36Schristos 				tmp = (tmp << 4) | n;
88b5677b36Schristos 			if (++dirty == 2) {
89b5677b36Schristos 				if (size-- <= 0U)
90b5677b36Schristos 					goto emsgsize;
91b5677b36Schristos 				*dst++ = (u_char) tmp;
92b5677b36Schristos 				dirty = 0;
93b5677b36Schristos 			}
94b5677b36Schristos 		}
95b5677b36Schristos 		if (dirty) {  /*%< Odd trailing nybble? */
96b5677b36Schristos 			if (size-- <= 0U)
97b5677b36Schristos 				goto emsgsize;
98b5677b36Schristos 			*dst++ = (u_char) (tmp << 4);
99b5677b36Schristos 		}
100b5677b36Schristos 	} else if (isascii(ch) && isdigit(ch)) {
101b5677b36Schristos 		/* Decimal: eat dotted digit string. */
102b5677b36Schristos 		for (;;) {
103b5677b36Schristos 			tmp = 0;
104b5677b36Schristos 			do {
105b5677b36Schristos 				n = strchr(digits, ch) - digits;
106b5677b36Schristos 				INSIST(n >= 0 && n <= 9);
107b5677b36Schristos 				tmp *= 10;
108b5677b36Schristos 				tmp += n;
109b5677b36Schristos 				if (tmp > 255)
110b5677b36Schristos 					goto enoent;
111b5677b36Schristos 			} while ((ch = *src++) != '\0' &&
112b5677b36Schristos 				 isascii(ch) && isdigit(ch));
113b5677b36Schristos 			if (size-- <= 0U)
114b5677b36Schristos 				goto emsgsize;
115b5677b36Schristos 			*dst++ = (u_char) tmp;
116b5677b36Schristos 			if (ch == '\0' || ch == '/')
117b5677b36Schristos 				break;
118b5677b36Schristos 			if (ch != '.')
119b5677b36Schristos 				goto enoent;
120b5677b36Schristos 			ch = *src++;
121b5677b36Schristos 			if (!isascii(ch) || !isdigit(ch))
122b5677b36Schristos 				goto enoent;
123b5677b36Schristos 		}
124b5677b36Schristos 	} else
125b5677b36Schristos 		goto enoent;
126b5677b36Schristos 
127b5677b36Schristos 	bits = -1;
128b5677b36Schristos 	if (ch == '/' && isascii((unsigned char)(src[0])) &&
129b5677b36Schristos 	    isdigit((unsigned char)(src[0])) && dst > odst) {
130b5677b36Schristos 		/* CIDR width specifier.  Nothing can follow it. */
131b5677b36Schristos 		ch = *src++;	/*%< Skip over the /. */
132b5677b36Schristos 		bits = 0;
133b5677b36Schristos 		do {
134b5677b36Schristos 			n = strchr(digits, ch) - digits;
135b5677b36Schristos 			INSIST(n >= 0 && n <= 9);
136b5677b36Schristos 			bits *= 10;
137b5677b36Schristos 			bits += n;
138b5677b36Schristos 			if (bits > 32)
139b5677b36Schristos 				goto enoent;
140b5677b36Schristos 		} while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
141b5677b36Schristos 		if (ch != '\0')
142b5677b36Schristos 			goto enoent;
143b5677b36Schristos 	}
144b5677b36Schristos 
145b5677b36Schristos 	/* Firey death and destruction unless we prefetched EOS. */
146b5677b36Schristos 	if (ch != '\0')
147b5677b36Schristos 		goto enoent;
148b5677b36Schristos 
149b5677b36Schristos 	/* If nothing was written to the destination, we found no address. */
150b5677b36Schristos 	if (dst == odst)
151b5677b36Schristos 		goto enoent;
152b5677b36Schristos 	/* If no CIDR spec was given, infer width from net class. */
153b5677b36Schristos 	if (bits == -1) {
154b5677b36Schristos 		if (*odst >= 240)	/*%< Class E */
155b5677b36Schristos 			bits = 32;
156b5677b36Schristos 		else if (*odst >= 224)	/*%< Class D */
157b5677b36Schristos 			bits = 8;
158b5677b36Schristos 		else if (*odst >= 192)	/*%< Class C */
159b5677b36Schristos 			bits = 24;
160b5677b36Schristos 		else if (*odst >= 128)	/*%< Class B */
161b5677b36Schristos 			bits = 16;
162b5677b36Schristos 		else			/*%< Class A */
163b5677b36Schristos 			bits = 8;
164b5677b36Schristos 		/* If imputed mask is narrower than specified octets, widen. */
165b5677b36Schristos 		if (bits < ((dst - odst) * 8))
166b5677b36Schristos 			bits = (dst - odst) * 8;
167b5677b36Schristos 		/*
168b5677b36Schristos 		 * If there are no additional bits specified for a class D
169b5677b36Schristos 		 * address adjust bits to 4.
170b5677b36Schristos 		 */
171b5677b36Schristos 		if (bits == 8 && *odst == 224)
172b5677b36Schristos 			bits = 4;
173b5677b36Schristos 	}
174b5677b36Schristos 	/* Extend network to cover the actual mask. */
175b5677b36Schristos 	while (bits > ((dst - odst) * 8)) {
176b5677b36Schristos 		if (size-- <= 0U)
177b5677b36Schristos 			goto emsgsize;
178b5677b36Schristos 		*dst++ = '\0';
179b5677b36Schristos 	}
180b5677b36Schristos 	return (bits);
181b5677b36Schristos 
182b5677b36Schristos  enoent:
183b5677b36Schristos 	errno = ENOENT;
184b5677b36Schristos 	return (-1);
185b5677b36Schristos 
186b5677b36Schristos  emsgsize:
187b5677b36Schristos 	errno = EMSGSIZE;
188b5677b36Schristos 	return (-1);
189b5677b36Schristos }
190b5677b36Schristos 
191b5677b36Schristos static int
getbits(const char * src,int * bitsp)192b5677b36Schristos getbits(const char *src, int *bitsp) {
193b5677b36Schristos 	static const char digits[] = "0123456789";
194b5677b36Schristos 	int n;
195b5677b36Schristos 	int val;
196b5677b36Schristos 	char ch;
197b5677b36Schristos 
198b5677b36Schristos 	val = 0;
199b5677b36Schristos 	n = 0;
200b5677b36Schristos 	while ((ch = *src++) != '\0') {
201b5677b36Schristos 		const char *pch;
202b5677b36Schristos 
203b5677b36Schristos 		pch = strchr(digits, ch);
204b5677b36Schristos 		if (pch != NULL) {
205b5677b36Schristos 			if (n++ != 0 && val == 0)	/*%< no leading zeros */
206b5677b36Schristos 				return (0);
207b5677b36Schristos 			val *= 10;
208b5677b36Schristos 			val += (pch - digits);
209b5677b36Schristos 			if (val > 128)			/*%< range */
210b5677b36Schristos 				return (0);
211b5677b36Schristos 			continue;
212b5677b36Schristos 		}
213b5677b36Schristos 		return (0);
214b5677b36Schristos 	}
215b5677b36Schristos 	if (n == 0)
216b5677b36Schristos 		return (0);
217b5677b36Schristos 	*bitsp = val;
218b5677b36Schristos 	return (1);
219b5677b36Schristos }
220b5677b36Schristos 
221b5677b36Schristos static int
getv4(const char * src,u_char * dst,int * bitsp)222b5677b36Schristos getv4(const char *src, u_char *dst, int *bitsp) {
223b5677b36Schristos 	static const char digits[] = "0123456789";
224b5677b36Schristos 	u_char *odst = dst;
225b5677b36Schristos 	int n;
226b5677b36Schristos 	u_int val;
227b5677b36Schristos 	char ch;
228b5677b36Schristos 
229b5677b36Schristos 	val = 0;
230b5677b36Schristos 	n = 0;
231b5677b36Schristos 	while ((ch = *src++) != '\0') {
232b5677b36Schristos 		const char *pch;
233b5677b36Schristos 
234b5677b36Schristos 		pch = strchr(digits, ch);
235b5677b36Schristos 		if (pch != NULL) {
236b5677b36Schristos 			if (n++ != 0 && val == 0)	/*%< no leading zeros */
237b5677b36Schristos 				return (0);
238b5677b36Schristos 			val *= 10;
239b5677b36Schristos 			val += (pch - digits);
240b5677b36Schristos 			if (val > 255)			/*%< range */
241b5677b36Schristos 				return (0);
242b5677b36Schristos 			continue;
243b5677b36Schristos 		}
244b5677b36Schristos 		if (ch == '.' || ch == '/') {
245b5677b36Schristos 			if (dst - odst > 3)		/*%< too many octets? */
246b5677b36Schristos 				return (0);
247b5677b36Schristos 			*dst++ = val;
248b5677b36Schristos 			if (ch == '/')
249b5677b36Schristos 				return (getbits(src, bitsp));
250b5677b36Schristos 			val = 0;
251b5677b36Schristos 			n = 0;
252b5677b36Schristos 			continue;
253b5677b36Schristos 		}
254b5677b36Schristos 		return (0);
255b5677b36Schristos 	}
256b5677b36Schristos 	if (n == 0)
257b5677b36Schristos 		return (0);
258b5677b36Schristos 	if (dst - odst > 3)		/*%< too many octets? */
259b5677b36Schristos 		return (0);
260b5677b36Schristos 	*dst++ = val;
261b5677b36Schristos 	return (1);
262b5677b36Schristos }
263b5677b36Schristos 
264b5677b36Schristos static int
inet_net_pton_ipv6(const char * src,u_char * dst,size_t size)265b5677b36Schristos inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
266b5677b36Schristos 	static const char xdigits_l[] = "0123456789abcdef",
267b5677b36Schristos 			  xdigits_u[] = "0123456789ABCDEF";
268b5677b36Schristos 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
269b5677b36Schristos 	const char *xdigits, *curtok;
270b5677b36Schristos 	int ch, saw_xdigit;
271b5677b36Schristos 	u_int val;
272b5677b36Schristos 	int digits;
273b5677b36Schristos 	int bits;
274b5677b36Schristos 	size_t bytes;
275b5677b36Schristos 	int words;
276b5677b36Schristos 	int ipv4;
277b5677b36Schristos 
278b5677b36Schristos 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
279b5677b36Schristos 	endp = tp + NS_IN6ADDRSZ;
280b5677b36Schristos 	colonp = NULL;
281b5677b36Schristos 	/* Leading :: requires some special handling. */
282b5677b36Schristos 	if (*src == ':')
283b5677b36Schristos 		if (*++src != ':')
284b5677b36Schristos 			goto enoent;
285b5677b36Schristos 	curtok = src;
286b5677b36Schristos 	saw_xdigit = 0;
287b5677b36Schristos 	val = 0;
288b5677b36Schristos 	digits = 0;
289b5677b36Schristos 	bits = -1;
290b5677b36Schristos 	ipv4 = 0;
291b5677b36Schristos 	while ((ch = *src++) != '\0') {
292b5677b36Schristos 		const char *pch;
293b5677b36Schristos 
294b5677b36Schristos 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
295b5677b36Schristos 			pch = strchr((xdigits = xdigits_u), ch);
296b5677b36Schristos 		if (pch != NULL) {
297b5677b36Schristos 			val <<= 4;
298b5677b36Schristos 			val |= (pch - xdigits);
299b5677b36Schristos 			if (++digits > 4)
300b5677b36Schristos 				goto enoent;
301b5677b36Schristos 			saw_xdigit = 1;
302b5677b36Schristos 			continue;
303b5677b36Schristos 		}
304b5677b36Schristos 		if (ch == ':') {
305b5677b36Schristos 			curtok = src;
306b5677b36Schristos 			if (!saw_xdigit) {
307b5677b36Schristos 				if (colonp)
308b5677b36Schristos 					goto enoent;
309b5677b36Schristos 				colonp = tp;
310b5677b36Schristos 				continue;
311b5677b36Schristos 			} else if (*src == '\0')
312b5677b36Schristos 				goto enoent;
313b5677b36Schristos 			if (tp + NS_INT16SZ > endp)
314b5677b36Schristos 				return (0);
315b5677b36Schristos 			*tp++ = (u_char) (val >> 8) & 0xff;
316b5677b36Schristos 			*tp++ = (u_char) val & 0xff;
317b5677b36Schristos 			saw_xdigit = 0;
318b5677b36Schristos 			digits = 0;
319b5677b36Schristos 			val = 0;
320b5677b36Schristos 			continue;
321b5677b36Schristos 		}
322b5677b36Schristos 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
323b5677b36Schristos 		     getv4(curtok, tp, &bits) > 0) {
324b5677b36Schristos 			tp += NS_INADDRSZ;
325b5677b36Schristos 			saw_xdigit = 0;
326b5677b36Schristos 			ipv4 = 1;
327b5677b36Schristos 			break;	/*%< '\\0' was seen by inet_pton4(). */
328b5677b36Schristos 		}
329b5677b36Schristos 		if (ch == '/' && getbits(src, &bits) > 0)
330b5677b36Schristos 			break;
331b5677b36Schristos 		goto enoent;
332b5677b36Schristos 	}
333b5677b36Schristos 	if (saw_xdigit) {
334b5677b36Schristos 		if (tp + NS_INT16SZ > endp)
335b5677b36Schristos 			goto enoent;
336b5677b36Schristos 		*tp++ = (u_char) (val >> 8) & 0xff;
337b5677b36Schristos 		*tp++ = (u_char) val & 0xff;
338b5677b36Schristos 	}
339b5677b36Schristos 	if (bits == -1)
340b5677b36Schristos 		bits = 128;
341b5677b36Schristos 
342b5677b36Schristos 	words = (bits + 15) / 16;
343b5677b36Schristos 	if (words < 2)
344b5677b36Schristos 		words = 2;
345b5677b36Schristos 	if (ipv4)
346b5677b36Schristos 		words = 8;
347b5677b36Schristos 	endp =  tmp + 2 * words;
348b5677b36Schristos 
349b5677b36Schristos 	if (colonp != NULL) {
350b5677b36Schristos 		/*
351b5677b36Schristos 		 * Since some memmove()'s erroneously fail to handle
352b5677b36Schristos 		 * overlapping regions, we'll do the shift by hand.
353b5677b36Schristos 		 */
354b5677b36Schristos 		const int n = tp - colonp;
355b5677b36Schristos 		int i;
356b5677b36Schristos 
357b5677b36Schristos 		if (tp == endp)
358b5677b36Schristos 			goto enoent;
359b5677b36Schristos 		for (i = 1; i <= n; i++) {
360b5677b36Schristos 			endp[- i] = colonp[n - i];
361b5677b36Schristos 			colonp[n - i] = 0;
362b5677b36Schristos 		}
363b5677b36Schristos 		tp = endp;
364b5677b36Schristos 	}
365b5677b36Schristos 	if (tp != endp)
366b5677b36Schristos 		goto enoent;
367b5677b36Schristos 
368b5677b36Schristos 	bytes = (bits + 7) / 8;
369b5677b36Schristos 	if (bytes > size)
370b5677b36Schristos 		goto emsgsize;
371b5677b36Schristos 	memcpy(dst, tmp, bytes);
372b5677b36Schristos 	return (bits);
373b5677b36Schristos 
374b5677b36Schristos  enoent:
375b5677b36Schristos 	errno = ENOENT;
376b5677b36Schristos 	return (-1);
377b5677b36Schristos 
378b5677b36Schristos  emsgsize:
379b5677b36Schristos 	errno = EMSGSIZE;
380b5677b36Schristos 	return (-1);
381b5677b36Schristos }
382b5677b36Schristos 
383b5677b36Schristos /*%
384b5677b36Schristos  * int
385b5677b36Schristos  * inet_net_pton(af, src, dst, size)
386b5677b36Schristos  *	convert network number from presentation to network format.
387b5677b36Schristos  *	accepts hex octets, hex strings, decimal octets, and /CIDR.
388b5677b36Schristos  *	"size" is in bytes and describes "dst".
389b5677b36Schristos  * return:
390b5677b36Schristos  *	number of bits, either imputed classfully or specified with /CIDR,
391b5677b36Schristos  *	or -1 if some failure occurred (check errno).  ENOENT means it was
392b5677b36Schristos  *	not a valid network specification.
393b5677b36Schristos  * author:
394b5677b36Schristos  *	Paul Vixie (ISC), June 1996
395b5677b36Schristos  */
396b5677b36Schristos int
inet_net_pton(int af,const char * src,void * dst,size_t size)397b5677b36Schristos inet_net_pton(int af, const char *src, void *dst, size_t size) {
398b5677b36Schristos 	switch (af) {
399b5677b36Schristos 	case AF_INET:
400b5677b36Schristos 		return (inet_net_pton_ipv4(src, dst, size));
401b5677b36Schristos 	case AF_INET6:
402b5677b36Schristos 		return (inet_net_pton_ipv6(src, dst, size));
403b5677b36Schristos 	default:
404b5677b36Schristos 		errno = EAFNOSUPPORT;
405b5677b36Schristos 		return (-1);
406b5677b36Schristos 	}
407b5677b36Schristos }
408b5677b36Schristos 
409b5677b36Schristos /*! \file */
410