xref: /illumos-gate/usr/src/lib/libresolv2/common/inet/inet_cidr_pton.c (revision 55fea89dcaa64928bed4327112404dcb3e07b79f)
17c478bd9Sstevel@tonic-gate /*
2*9525b14bSRao Shoaib  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
37c478bd9Sstevel@tonic-gate  * Copyright (c) 1998,1999 by Internet Software Consortium.
47c478bd9Sstevel@tonic-gate  *
57c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
67c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
77c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
87c478bd9Sstevel@tonic-gate  *
9*9525b14bSRao Shoaib  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*9525b14bSRao Shoaib  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*9525b14bSRao Shoaib  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*9525b14bSRao Shoaib  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*9525b14bSRao Shoaib  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*9525b14bSRao Shoaib  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*9525b14bSRao Shoaib  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
167c478bd9Sstevel@tonic-gate  */
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*9525b14bSRao Shoaib static const char rcsid[] = "$Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp $";
207c478bd9Sstevel@tonic-gate #endif
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate #include "port_before.h"
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #include <sys/types.h>
257c478bd9Sstevel@tonic-gate #include <sys/socket.h>
267c478bd9Sstevel@tonic-gate #include <netinet/in.h>
277c478bd9Sstevel@tonic-gate #include <arpa/nameser.h>
287c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <isc/assertions.h>
317c478bd9Sstevel@tonic-gate #include <ctype.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <string.h>
357c478bd9Sstevel@tonic-gate #include <stdlib.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include "port_after.h"
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #ifdef SPRINTF_CHAR
407c478bd9Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
417c478bd9Sstevel@tonic-gate #else
427c478bd9Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
437c478bd9Sstevel@tonic-gate #endif
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static int	inet_cidr_pton_ipv4 __P((const char *src, u_char *dst,
467c478bd9Sstevel@tonic-gate 					 int *bits, int ipv6));
477c478bd9Sstevel@tonic-gate static int	inet_cidr_pton_ipv6 __P((const char *src, u_char *dst,
487c478bd9Sstevel@tonic-gate 					 int *bits));
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate static int	getbits(const char *, int ipv6);
517c478bd9Sstevel@tonic-gate 
52*9525b14bSRao Shoaib /*%
537c478bd9Sstevel@tonic-gate  * int
547c478bd9Sstevel@tonic-gate  * inet_cidr_pton(af, src, dst, *bits)
557c478bd9Sstevel@tonic-gate  *	convert network address from presentation to network format.
567c478bd9Sstevel@tonic-gate  *	accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
577c478bd9Sstevel@tonic-gate  *	"dst" is assumed large enough for its "af".  "bits" is set to the
587c478bd9Sstevel@tonic-gate  *	/CIDR prefix length, which can have defaults (like /32 for IPv4).
597c478bd9Sstevel@tonic-gate  * return:
607c478bd9Sstevel@tonic-gate  *	-1 if an error occurred (inspect errno; ENOENT means bad format).
617c478bd9Sstevel@tonic-gate  *	0 if successful conversion occurred.
627c478bd9Sstevel@tonic-gate  * note:
637c478bd9Sstevel@tonic-gate  *	192.5.5.1/28 has a nonzero host part, which means it isn't a network
647c478bd9Sstevel@tonic-gate  *	as called for by inet_net_pton() but it can be a host address with
657c478bd9Sstevel@tonic-gate  *	an included netmask.
667c478bd9Sstevel@tonic-gate  * author:
677c478bd9Sstevel@tonic-gate  *	Paul Vixie (ISC), October 1998
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate int
inet_cidr_pton(int af,const char * src,void * dst,int * bits)707c478bd9Sstevel@tonic-gate inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
717c478bd9Sstevel@tonic-gate 	switch (af) {
727c478bd9Sstevel@tonic-gate 	case AF_INET:
737c478bd9Sstevel@tonic-gate 		return (inet_cidr_pton_ipv4(src, dst, bits, 0));
747c478bd9Sstevel@tonic-gate 	case AF_INET6:
757c478bd9Sstevel@tonic-gate 		return (inet_cidr_pton_ipv6(src, dst, bits));
767c478bd9Sstevel@tonic-gate 	default:
777c478bd9Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
787c478bd9Sstevel@tonic-gate 		return (-1);
797c478bd9Sstevel@tonic-gate 	}
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static const char digits[] = "0123456789";
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate static int
inet_cidr_pton_ipv4(const char * src,u_char * dst,int * pbits,int ipv6)857c478bd9Sstevel@tonic-gate inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
867c478bd9Sstevel@tonic-gate 	const u_char *odst = dst;
877c478bd9Sstevel@tonic-gate 	int n, ch, tmp, bits;
887c478bd9Sstevel@tonic-gate 	size_t size = 4;
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/* Get the mantissa. */
917c478bd9Sstevel@tonic-gate 	while (ch = *src++, (isascii(ch) && isdigit(ch))) {
927c478bd9Sstevel@tonic-gate 		tmp = 0;
937c478bd9Sstevel@tonic-gate 		do {
947c478bd9Sstevel@tonic-gate 			n = strchr(digits, ch) - digits;
957c478bd9Sstevel@tonic-gate 			INSIST(n >= 0 && n <= 9);
967c478bd9Sstevel@tonic-gate 			tmp *= 10;
977c478bd9Sstevel@tonic-gate 			tmp += n;
987c478bd9Sstevel@tonic-gate 			if (tmp > 255)
997c478bd9Sstevel@tonic-gate 				goto enoent;
1007c478bd9Sstevel@tonic-gate 		} while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
101*9525b14bSRao Shoaib 		if (size-- == 0U)
1027c478bd9Sstevel@tonic-gate 			goto emsgsize;
1037c478bd9Sstevel@tonic-gate 		*dst++ = (u_char) tmp;
1047c478bd9Sstevel@tonic-gate 		if (ch == '\0' || ch == '/')
1057c478bd9Sstevel@tonic-gate 			break;
1067c478bd9Sstevel@tonic-gate 		if (ch != '.')
1077c478bd9Sstevel@tonic-gate 			goto enoent;
1087c478bd9Sstevel@tonic-gate 	}
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/* Get the prefix length if any. */
1117c478bd9Sstevel@tonic-gate 	bits = -1;
1127c478bd9Sstevel@tonic-gate 	if (ch == '/' && dst > odst) {
1137c478bd9Sstevel@tonic-gate 		bits = getbits(src, ipv6);
1147c478bd9Sstevel@tonic-gate 		if (bits == -2)
1157c478bd9Sstevel@tonic-gate 			goto enoent;
1167c478bd9Sstevel@tonic-gate 	} else if (ch != '\0')
1177c478bd9Sstevel@tonic-gate 		goto enoent;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	/* Prefix length can default to /32 only if all four octets spec'd. */
1207c478bd9Sstevel@tonic-gate 	if (bits == -1) {
1217c478bd9Sstevel@tonic-gate 		if (dst - odst == 4)
1227c478bd9Sstevel@tonic-gate 			bits = ipv6 ? 128 : 32;
1237c478bd9Sstevel@tonic-gate 		else
1247c478bd9Sstevel@tonic-gate 			goto enoent;
1257c478bd9Sstevel@tonic-gate 	}
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	/* If nothing was written to the destination, we found no address. */
1287c478bd9Sstevel@tonic-gate 	if (dst == odst)
1297c478bd9Sstevel@tonic-gate 		goto enoent;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* If prefix length overspecifies mantissa, life is bad. */
1327c478bd9Sstevel@tonic-gate 	if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
1337c478bd9Sstevel@tonic-gate 		goto enoent;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	/* Extend address to four octets. */
136*9525b14bSRao Shoaib 	while (size-- > 0U)
1377c478bd9Sstevel@tonic-gate 		*dst++ = 0;
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	*pbits = bits;
1407c478bd9Sstevel@tonic-gate 	return (0);
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate  enoent:
1437c478bd9Sstevel@tonic-gate 	errno = ENOENT;
1447c478bd9Sstevel@tonic-gate 	return (-1);
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate  emsgsize:
1477c478bd9Sstevel@tonic-gate 	errno = EMSGSIZE;
1487c478bd9Sstevel@tonic-gate 	return (-1);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate static int
inet_cidr_pton_ipv6(const char * src,u_char * dst,int * pbits)1527c478bd9Sstevel@tonic-gate inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
1537c478bd9Sstevel@tonic-gate 	static const char xdigits_l[] = "0123456789abcdef",
1547c478bd9Sstevel@tonic-gate 			  xdigits_u[] = "0123456789ABCDEF";
1557c478bd9Sstevel@tonic-gate 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
1567c478bd9Sstevel@tonic-gate 	const char *xdigits, *curtok;
1577c478bd9Sstevel@tonic-gate 	int ch, saw_xdigit;
1587c478bd9Sstevel@tonic-gate 	u_int val;
1597c478bd9Sstevel@tonic-gate 	int bits;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
1627c478bd9Sstevel@tonic-gate 	endp = tp + NS_IN6ADDRSZ;
1637c478bd9Sstevel@tonic-gate 	colonp = NULL;
1647c478bd9Sstevel@tonic-gate 	/* Leading :: requires some special handling. */
1657c478bd9Sstevel@tonic-gate 	if (*src == ':')
1667c478bd9Sstevel@tonic-gate 		if (*++src != ':')
1677c478bd9Sstevel@tonic-gate 			return (0);
1687c478bd9Sstevel@tonic-gate 	curtok = src;
1697c478bd9Sstevel@tonic-gate 	saw_xdigit = 0;
1707c478bd9Sstevel@tonic-gate 	val = 0;
1717c478bd9Sstevel@tonic-gate 	bits = -1;
1727c478bd9Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
1737c478bd9Sstevel@tonic-gate 		const char *pch;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
1767c478bd9Sstevel@tonic-gate 			pch = strchr((xdigits = xdigits_u), ch);
1777c478bd9Sstevel@tonic-gate 		if (pch != NULL) {
1787c478bd9Sstevel@tonic-gate 			val <<= 4;
1797c478bd9Sstevel@tonic-gate 			val |= (pch - xdigits);
1807c478bd9Sstevel@tonic-gate 			if (val > 0xffff)
1817c478bd9Sstevel@tonic-gate 				return (0);
1827c478bd9Sstevel@tonic-gate 			saw_xdigit = 1;
1837c478bd9Sstevel@tonic-gate 			continue;
1847c478bd9Sstevel@tonic-gate 		}
1857c478bd9Sstevel@tonic-gate 		if (ch == ':') {
1867c478bd9Sstevel@tonic-gate 			curtok = src;
1877c478bd9Sstevel@tonic-gate 			if (!saw_xdigit) {
1887c478bd9Sstevel@tonic-gate 				if (colonp)
1897c478bd9Sstevel@tonic-gate 					return (0);
1907c478bd9Sstevel@tonic-gate 				colonp = tp;
1917c478bd9Sstevel@tonic-gate 				continue;
1927c478bd9Sstevel@tonic-gate 			} else if (*src == '\0') {
1937c478bd9Sstevel@tonic-gate 				return (0);
1947c478bd9Sstevel@tonic-gate 			}
1957c478bd9Sstevel@tonic-gate 			if (tp + NS_INT16SZ > endp)
1967c478bd9Sstevel@tonic-gate 				return (0);
1977c478bd9Sstevel@tonic-gate 			*tp++ = (u_char) (val >> 8) & 0xff;
1987c478bd9Sstevel@tonic-gate 			*tp++ = (u_char) val & 0xff;
1997c478bd9Sstevel@tonic-gate 			saw_xdigit = 0;
2007c478bd9Sstevel@tonic-gate 			val = 0;
2017c478bd9Sstevel@tonic-gate 			continue;
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
2047c478bd9Sstevel@tonic-gate 		    inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
2057c478bd9Sstevel@tonic-gate 			tp += NS_INADDRSZ;
2067c478bd9Sstevel@tonic-gate 			saw_xdigit = 0;
207*9525b14bSRao Shoaib 			break;	/*%< '\\0' was seen by inet_pton4(). */
2087c478bd9Sstevel@tonic-gate 		}
2097c478bd9Sstevel@tonic-gate 		if (ch == '/') {
2107c478bd9Sstevel@tonic-gate 			bits = getbits(src, 1);
2117c478bd9Sstevel@tonic-gate 			if (bits == -2)
2127c478bd9Sstevel@tonic-gate 				goto enoent;
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		goto enoent;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate 	if (saw_xdigit) {
2187c478bd9Sstevel@tonic-gate 		if (tp + NS_INT16SZ > endp)
2197c478bd9Sstevel@tonic-gate 			goto emsgsize;
2207c478bd9Sstevel@tonic-gate 		*tp++ = (u_char) (val >> 8) & 0xff;
2217c478bd9Sstevel@tonic-gate 		*tp++ = (u_char) val & 0xff;
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 	if (colonp != NULL) {
2247c478bd9Sstevel@tonic-gate 		/*
2257c478bd9Sstevel@tonic-gate 		 * Since some memmove()'s erroneously fail to handle
2267c478bd9Sstevel@tonic-gate 		 * overlapping regions, we'll do the shift by hand.
2277c478bd9Sstevel@tonic-gate 		 */
2287c478bd9Sstevel@tonic-gate 		const int n = tp - colonp;
2297c478bd9Sstevel@tonic-gate 		int i;
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		if (tp == endp)
2327c478bd9Sstevel@tonic-gate 			goto enoent;
2337c478bd9Sstevel@tonic-gate 		for (i = 1; i <= n; i++) {
2347c478bd9Sstevel@tonic-gate 			endp[- i] = colonp[n - i];
2357c478bd9Sstevel@tonic-gate 			colonp[n - i] = 0;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 		tp = endp;
2387c478bd9Sstevel@tonic-gate 	}
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	memcpy(dst, tmp, NS_IN6ADDRSZ);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	*pbits = bits;
2437c478bd9Sstevel@tonic-gate 	return (0);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate  enoent:
2467c478bd9Sstevel@tonic-gate 	errno = ENOENT;
2477c478bd9Sstevel@tonic-gate 	return (-1);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate  emsgsize:
2507c478bd9Sstevel@tonic-gate 	errno = EMSGSIZE;
2517c478bd9Sstevel@tonic-gate 	return (-1);
2527c478bd9Sstevel@tonic-gate }
2537c478bd9Sstevel@tonic-gate 
254*9525b14bSRao Shoaib static int
getbits(const char * src,int ipv6)2557c478bd9Sstevel@tonic-gate getbits(const char *src, int ipv6) {
2567c478bd9Sstevel@tonic-gate 	int bits = 0;
2577c478bd9Sstevel@tonic-gate 	char *cp, ch;
2587c478bd9Sstevel@tonic-gate 
259*9525b14bSRao Shoaib 	if (*src == '\0')			/*%< syntax */
2607c478bd9Sstevel@tonic-gate 		return (-2);
2617c478bd9Sstevel@tonic-gate 	do {
2627c478bd9Sstevel@tonic-gate 		ch = *src++;
2637c478bd9Sstevel@tonic-gate 		cp = strchr(digits, ch);
264*9525b14bSRao Shoaib 		if (cp == NULL)			/*%< syntax */
2657c478bd9Sstevel@tonic-gate 			return (-2);
2667c478bd9Sstevel@tonic-gate 		bits *= 10;
2677c478bd9Sstevel@tonic-gate 		bits += cp - digits;
268*9525b14bSRao Shoaib 		if (bits == 0 && *src != '\0')	/*%< no leading zeros */
2697c478bd9Sstevel@tonic-gate 			return (-2);
270*9525b14bSRao Shoaib 		if (bits > (ipv6 ? 128 : 32))	/*%< range error */
2717c478bd9Sstevel@tonic-gate 			return (-2);
2727c478bd9Sstevel@tonic-gate 	} while (*src != '\0');
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	return (bits);
2757c478bd9Sstevel@tonic-gate }
276*9525b14bSRao Shoaib 
277*9525b14bSRao Shoaib /*! \file */
278