xref: /onnv-gate/usr/src/lib/libresolv2/common/inet/inet_cidr_pton.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
30Sstevel@tonic-gate  * Copyright (c) 1998,1999 by Internet Software Consortium.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
60Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
70Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies.
80Sstevel@tonic-gate  *
9*11038SRao.Shoaib@Sun.COM  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10*11038SRao.Shoaib@Sun.COM  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11*11038SRao.Shoaib@Sun.COM  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12*11038SRao.Shoaib@Sun.COM  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13*11038SRao.Shoaib@Sun.COM  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14*11038SRao.Shoaib@Sun.COM  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15*11038SRao.Shoaib@Sun.COM  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
160Sstevel@tonic-gate  */
170Sstevel@tonic-gate 
180Sstevel@tonic-gate #if defined(LIBC_SCCS) && !defined(lint)
19*11038SRao.Shoaib@Sun.COM static const char rcsid[] = "$Id: inet_cidr_pton.c,v 1.6 2005/04/27 04:56:19 sra Exp $";
200Sstevel@tonic-gate #endif
210Sstevel@tonic-gate 
220Sstevel@tonic-gate #include "port_before.h"
230Sstevel@tonic-gate 
240Sstevel@tonic-gate #include <sys/types.h>
250Sstevel@tonic-gate #include <sys/socket.h>
260Sstevel@tonic-gate #include <netinet/in.h>
270Sstevel@tonic-gate #include <arpa/nameser.h>
280Sstevel@tonic-gate #include <arpa/inet.h>
290Sstevel@tonic-gate 
300Sstevel@tonic-gate #include <isc/assertions.h>
310Sstevel@tonic-gate #include <ctype.h>
320Sstevel@tonic-gate #include <errno.h>
330Sstevel@tonic-gate #include <stdio.h>
340Sstevel@tonic-gate #include <string.h>
350Sstevel@tonic-gate #include <stdlib.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include "port_after.h"
380Sstevel@tonic-gate 
390Sstevel@tonic-gate #ifdef SPRINTF_CHAR
400Sstevel@tonic-gate # define SPRINTF(x) strlen(sprintf/**/x)
410Sstevel@tonic-gate #else
420Sstevel@tonic-gate # define SPRINTF(x) ((size_t)sprintf x)
430Sstevel@tonic-gate #endif
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static int	inet_cidr_pton_ipv4 __P((const char *src, u_char *dst,
460Sstevel@tonic-gate 					 int *bits, int ipv6));
470Sstevel@tonic-gate static int	inet_cidr_pton_ipv6 __P((const char *src, u_char *dst,
480Sstevel@tonic-gate 					 int *bits));
490Sstevel@tonic-gate 
500Sstevel@tonic-gate static int	getbits(const char *, int ipv6);
510Sstevel@tonic-gate 
52*11038SRao.Shoaib@Sun.COM /*%
530Sstevel@tonic-gate  * int
540Sstevel@tonic-gate  * inet_cidr_pton(af, src, dst, *bits)
550Sstevel@tonic-gate  *	convert network address from presentation to network format.
560Sstevel@tonic-gate  *	accepts inet_pton()'s input for this "af" plus trailing "/CIDR".
570Sstevel@tonic-gate  *	"dst" is assumed large enough for its "af".  "bits" is set to the
580Sstevel@tonic-gate  *	/CIDR prefix length, which can have defaults (like /32 for IPv4).
590Sstevel@tonic-gate  * return:
600Sstevel@tonic-gate  *	-1 if an error occurred (inspect errno; ENOENT means bad format).
610Sstevel@tonic-gate  *	0 if successful conversion occurred.
620Sstevel@tonic-gate  * note:
630Sstevel@tonic-gate  *	192.5.5.1/28 has a nonzero host part, which means it isn't a network
640Sstevel@tonic-gate  *	as called for by inet_net_pton() but it can be a host address with
650Sstevel@tonic-gate  *	an included netmask.
660Sstevel@tonic-gate  * author:
670Sstevel@tonic-gate  *	Paul Vixie (ISC), October 1998
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate int
inet_cidr_pton(int af,const char * src,void * dst,int * bits)700Sstevel@tonic-gate inet_cidr_pton(int af, const char *src, void *dst, int *bits) {
710Sstevel@tonic-gate 	switch (af) {
720Sstevel@tonic-gate 	case AF_INET:
730Sstevel@tonic-gate 		return (inet_cidr_pton_ipv4(src, dst, bits, 0));
740Sstevel@tonic-gate 	case AF_INET6:
750Sstevel@tonic-gate 		return (inet_cidr_pton_ipv6(src, dst, bits));
760Sstevel@tonic-gate 	default:
770Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
780Sstevel@tonic-gate 		return (-1);
790Sstevel@tonic-gate 	}
800Sstevel@tonic-gate }
810Sstevel@tonic-gate 
820Sstevel@tonic-gate static const char digits[] = "0123456789";
830Sstevel@tonic-gate 
840Sstevel@tonic-gate static int
inet_cidr_pton_ipv4(const char * src,u_char * dst,int * pbits,int ipv6)850Sstevel@tonic-gate inet_cidr_pton_ipv4(const char *src, u_char *dst, int *pbits, int ipv6) {
860Sstevel@tonic-gate 	const u_char *odst = dst;
870Sstevel@tonic-gate 	int n, ch, tmp, bits;
880Sstevel@tonic-gate 	size_t size = 4;
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	/* Get the mantissa. */
910Sstevel@tonic-gate 	while (ch = *src++, (isascii(ch) && isdigit(ch))) {
920Sstevel@tonic-gate 		tmp = 0;
930Sstevel@tonic-gate 		do {
940Sstevel@tonic-gate 			n = strchr(digits, ch) - digits;
950Sstevel@tonic-gate 			INSIST(n >= 0 && n <= 9);
960Sstevel@tonic-gate 			tmp *= 10;
970Sstevel@tonic-gate 			tmp += n;
980Sstevel@tonic-gate 			if (tmp > 255)
990Sstevel@tonic-gate 				goto enoent;
1000Sstevel@tonic-gate 		} while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
101*11038SRao.Shoaib@Sun.COM 		if (size-- == 0U)
1020Sstevel@tonic-gate 			goto emsgsize;
1030Sstevel@tonic-gate 		*dst++ = (u_char) tmp;
1040Sstevel@tonic-gate 		if (ch == '\0' || ch == '/')
1050Sstevel@tonic-gate 			break;
1060Sstevel@tonic-gate 		if (ch != '.')
1070Sstevel@tonic-gate 			goto enoent;
1080Sstevel@tonic-gate 	}
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/* Get the prefix length if any. */
1110Sstevel@tonic-gate 	bits = -1;
1120Sstevel@tonic-gate 	if (ch == '/' && dst > odst) {
1130Sstevel@tonic-gate 		bits = getbits(src, ipv6);
1140Sstevel@tonic-gate 		if (bits == -2)
1150Sstevel@tonic-gate 			goto enoent;
1160Sstevel@tonic-gate 	} else if (ch != '\0')
1170Sstevel@tonic-gate 		goto enoent;
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate 	/* Prefix length can default to /32 only if all four octets spec'd. */
1200Sstevel@tonic-gate 	if (bits == -1) {
1210Sstevel@tonic-gate 		if (dst - odst == 4)
1220Sstevel@tonic-gate 			bits = ipv6 ? 128 : 32;
1230Sstevel@tonic-gate 		else
1240Sstevel@tonic-gate 			goto enoent;
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/* If nothing was written to the destination, we found no address. */
1280Sstevel@tonic-gate 	if (dst == odst)
1290Sstevel@tonic-gate 		goto enoent;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	/* If prefix length overspecifies mantissa, life is bad. */
1320Sstevel@tonic-gate 	if (((bits - (ipv6 ? 96 : 0)) / 8) > (dst - odst))
1330Sstevel@tonic-gate 		goto enoent;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	/* Extend address to four octets. */
136*11038SRao.Shoaib@Sun.COM 	while (size-- > 0U)
1370Sstevel@tonic-gate 		*dst++ = 0;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	*pbits = bits;
1400Sstevel@tonic-gate 	return (0);
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate  enoent:
1430Sstevel@tonic-gate 	errno = ENOENT;
1440Sstevel@tonic-gate 	return (-1);
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate  emsgsize:
1470Sstevel@tonic-gate 	errno = EMSGSIZE;
1480Sstevel@tonic-gate 	return (-1);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate static int
inet_cidr_pton_ipv6(const char * src,u_char * dst,int * pbits)1520Sstevel@tonic-gate inet_cidr_pton_ipv6(const char *src, u_char *dst, int *pbits) {
1530Sstevel@tonic-gate 	static const char xdigits_l[] = "0123456789abcdef",
1540Sstevel@tonic-gate 			  xdigits_u[] = "0123456789ABCDEF";
1550Sstevel@tonic-gate 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
1560Sstevel@tonic-gate 	const char *xdigits, *curtok;
1570Sstevel@tonic-gate 	int ch, saw_xdigit;
1580Sstevel@tonic-gate 	u_int val;
1590Sstevel@tonic-gate 	int bits;
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
1620Sstevel@tonic-gate 	endp = tp + NS_IN6ADDRSZ;
1630Sstevel@tonic-gate 	colonp = NULL;
1640Sstevel@tonic-gate 	/* Leading :: requires some special handling. */
1650Sstevel@tonic-gate 	if (*src == ':')
1660Sstevel@tonic-gate 		if (*++src != ':')
1670Sstevel@tonic-gate 			return (0);
1680Sstevel@tonic-gate 	curtok = src;
1690Sstevel@tonic-gate 	saw_xdigit = 0;
1700Sstevel@tonic-gate 	val = 0;
1710Sstevel@tonic-gate 	bits = -1;
1720Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
1730Sstevel@tonic-gate 		const char *pch;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
1760Sstevel@tonic-gate 			pch = strchr((xdigits = xdigits_u), ch);
1770Sstevel@tonic-gate 		if (pch != NULL) {
1780Sstevel@tonic-gate 			val <<= 4;
1790Sstevel@tonic-gate 			val |= (pch - xdigits);
1800Sstevel@tonic-gate 			if (val > 0xffff)
1810Sstevel@tonic-gate 				return (0);
1820Sstevel@tonic-gate 			saw_xdigit = 1;
1830Sstevel@tonic-gate 			continue;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 		if (ch == ':') {
1860Sstevel@tonic-gate 			curtok = src;
1870Sstevel@tonic-gate 			if (!saw_xdigit) {
1880Sstevel@tonic-gate 				if (colonp)
1890Sstevel@tonic-gate 					return (0);
1900Sstevel@tonic-gate 				colonp = tp;
1910Sstevel@tonic-gate 				continue;
1920Sstevel@tonic-gate 			} else if (*src == '\0') {
1930Sstevel@tonic-gate 				return (0);
1940Sstevel@tonic-gate 			}
1950Sstevel@tonic-gate 			if (tp + NS_INT16SZ > endp)
1960Sstevel@tonic-gate 				return (0);
1970Sstevel@tonic-gate 			*tp++ = (u_char) (val >> 8) & 0xff;
1980Sstevel@tonic-gate 			*tp++ = (u_char) val & 0xff;
1990Sstevel@tonic-gate 			saw_xdigit = 0;
2000Sstevel@tonic-gate 			val = 0;
2010Sstevel@tonic-gate 			continue;
2020Sstevel@tonic-gate 		}
2030Sstevel@tonic-gate 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
2040Sstevel@tonic-gate 		    inet_cidr_pton_ipv4(curtok, tp, &bits, 1) == 0) {
2050Sstevel@tonic-gate 			tp += NS_INADDRSZ;
2060Sstevel@tonic-gate 			saw_xdigit = 0;
207*11038SRao.Shoaib@Sun.COM 			break;	/*%< '\\0' was seen by inet_pton4(). */
2080Sstevel@tonic-gate 		}
2090Sstevel@tonic-gate 		if (ch == '/') {
2100Sstevel@tonic-gate 			bits = getbits(src, 1);
2110Sstevel@tonic-gate 			if (bits == -2)
2120Sstevel@tonic-gate 				goto enoent;
2130Sstevel@tonic-gate 			break;
2140Sstevel@tonic-gate 		}
2150Sstevel@tonic-gate 		goto enoent;
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 	if (saw_xdigit) {
2180Sstevel@tonic-gate 		if (tp + NS_INT16SZ > endp)
2190Sstevel@tonic-gate 			goto emsgsize;
2200Sstevel@tonic-gate 		*tp++ = (u_char) (val >> 8) & 0xff;
2210Sstevel@tonic-gate 		*tp++ = (u_char) val & 0xff;
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 	if (colonp != NULL) {
2240Sstevel@tonic-gate 		/*
2250Sstevel@tonic-gate 		 * Since some memmove()'s erroneously fail to handle
2260Sstevel@tonic-gate 		 * overlapping regions, we'll do the shift by hand.
2270Sstevel@tonic-gate 		 */
2280Sstevel@tonic-gate 		const int n = tp - colonp;
2290Sstevel@tonic-gate 		int i;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 		if (tp == endp)
2320Sstevel@tonic-gate 			goto enoent;
2330Sstevel@tonic-gate 		for (i = 1; i <= n; i++) {
2340Sstevel@tonic-gate 			endp[- i] = colonp[n - i];
2350Sstevel@tonic-gate 			colonp[n - i] = 0;
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 		tp = endp;
2380Sstevel@tonic-gate 	}
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	memcpy(dst, tmp, NS_IN6ADDRSZ);
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	*pbits = bits;
2430Sstevel@tonic-gate 	return (0);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate  enoent:
2460Sstevel@tonic-gate 	errno = ENOENT;
2470Sstevel@tonic-gate 	return (-1);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate  emsgsize:
2500Sstevel@tonic-gate 	errno = EMSGSIZE;
2510Sstevel@tonic-gate 	return (-1);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
254*11038SRao.Shoaib@Sun.COM static int
getbits(const char * src,int ipv6)2550Sstevel@tonic-gate getbits(const char *src, int ipv6) {
2560Sstevel@tonic-gate 	int bits = 0;
2570Sstevel@tonic-gate 	char *cp, ch;
2580Sstevel@tonic-gate 
259*11038SRao.Shoaib@Sun.COM 	if (*src == '\0')			/*%< syntax */
2600Sstevel@tonic-gate 		return (-2);
2610Sstevel@tonic-gate 	do {
2620Sstevel@tonic-gate 		ch = *src++;
2630Sstevel@tonic-gate 		cp = strchr(digits, ch);
264*11038SRao.Shoaib@Sun.COM 		if (cp == NULL)			/*%< syntax */
2650Sstevel@tonic-gate 			return (-2);
2660Sstevel@tonic-gate 		bits *= 10;
2670Sstevel@tonic-gate 		bits += cp - digits;
268*11038SRao.Shoaib@Sun.COM 		if (bits == 0 && *src != '\0')	/*%< no leading zeros */
2690Sstevel@tonic-gate 			return (-2);
270*11038SRao.Shoaib@Sun.COM 		if (bits > (ipv6 ? 128 : 32))	/*%< range error */
2710Sstevel@tonic-gate 			return (-2);
2720Sstevel@tonic-gate 	} while (*src != '\0');
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	return (bits);
2750Sstevel@tonic-gate }
276*11038SRao.Shoaib@Sun.COM 
277*11038SRao.Shoaib@Sun.COM /*! \file */
278