xref: /onnv-gate/usr/src/lib/libresolv2/common/inet/inet_net_pton.c (revision 11038:74b12212b8a2)
10Sstevel@tonic-gate /*
2*11038SRao.Shoaib@Sun.COM  * Copyright (C) 2004, 2005, 2008  Internet Systems Consortium, Inc. ("ISC")
3*11038SRao.Shoaib@Sun.COM  * Copyright (C) 1996, 1998, 1999, 2001, 2003  Internet Software Consortium.
40Sstevel@tonic-gate  *
5*11038SRao.Shoaib@Sun.COM  * Permission to use, copy, modify, and/or 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 WITH
10*11038SRao.Shoaib@Sun.COM  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*11038SRao.Shoaib@Sun.COM  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*11038SRao.Shoaib@Sun.COM  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*11038SRao.Shoaib@Sun.COM  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*11038SRao.Shoaib@Sun.COM  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*11038SRao.Shoaib@Sun.COM  * 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_net_pton.c,v 1.10 2008/11/14 02:36:51 marka 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 
45*11038SRao.Shoaib@Sun.COM /*%
460Sstevel@tonic-gate  * static int
470Sstevel@tonic-gate  * inet_net_pton_ipv4(src, dst, size)
480Sstevel@tonic-gate  *	convert IPv4 network number from presentation to network format.
490Sstevel@tonic-gate  *	accepts hex octets, hex strings, decimal octets, and /CIDR.
500Sstevel@tonic-gate  *	"size" is in bytes and describes "dst".
510Sstevel@tonic-gate  * return:
520Sstevel@tonic-gate  *	number of bits, either imputed classfully or specified with /CIDR,
530Sstevel@tonic-gate  *	or -1 if some failure occurred (check errno).  ENOENT means it was
540Sstevel@tonic-gate  *	not an IPv4 network specification.
550Sstevel@tonic-gate  * note:
560Sstevel@tonic-gate  *	network byte order assumed.  this means 192.5.5.240/28 has
570Sstevel@tonic-gate  *	0b11110000 in its fourth octet.
580Sstevel@tonic-gate  * author:
590Sstevel@tonic-gate  *	Paul Vixie (ISC), June 1996
600Sstevel@tonic-gate  */
610Sstevel@tonic-gate static int
inet_net_pton_ipv4(const char * src,u_char * dst,size_t size)62*11038SRao.Shoaib@Sun.COM inet_net_pton_ipv4(const char *src, u_char *dst, size_t size) {
630Sstevel@tonic-gate 	static const char xdigits[] = "0123456789abcdef";
640Sstevel@tonic-gate 	static const char digits[] = "0123456789";
650Sstevel@tonic-gate 	int n, ch, tmp = 0, dirty, bits;
660Sstevel@tonic-gate 	const u_char *odst = dst;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	ch = *src++;
690Sstevel@tonic-gate 	if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
700Sstevel@tonic-gate 	    && isascii((unsigned char)(src[1]))
710Sstevel@tonic-gate 	    && isxdigit((unsigned char)(src[1]))) {
720Sstevel@tonic-gate 		/* Hexadecimal: Eat nybble string. */
73*11038SRao.Shoaib@Sun.COM 		if (size <= 0U)
740Sstevel@tonic-gate 			goto emsgsize;
750Sstevel@tonic-gate 		dirty = 0;
76*11038SRao.Shoaib@Sun.COM 		src++;	/*%< skip x or X. */
770Sstevel@tonic-gate 		while ((ch = *src++) != '\0' && isascii(ch) && isxdigit(ch)) {
780Sstevel@tonic-gate 			if (isupper(ch))
790Sstevel@tonic-gate 				ch = tolower(ch);
800Sstevel@tonic-gate 			n = strchr(xdigits, ch) - xdigits;
810Sstevel@tonic-gate 			INSIST(n >= 0 && n <= 15);
820Sstevel@tonic-gate 			if (dirty == 0)
830Sstevel@tonic-gate 				tmp = n;
840Sstevel@tonic-gate 			else
850Sstevel@tonic-gate 				tmp = (tmp << 4) | n;
860Sstevel@tonic-gate 			if (++dirty == 2) {
87*11038SRao.Shoaib@Sun.COM 				if (size-- <= 0U)
880Sstevel@tonic-gate 					goto emsgsize;
890Sstevel@tonic-gate 				*dst++ = (u_char) tmp;
900Sstevel@tonic-gate 				dirty = 0;
910Sstevel@tonic-gate 			}
920Sstevel@tonic-gate 		}
93*11038SRao.Shoaib@Sun.COM 		if (dirty) {  /*%< Odd trailing nybble? */
94*11038SRao.Shoaib@Sun.COM 			if (size-- <= 0U)
950Sstevel@tonic-gate 				goto emsgsize;
960Sstevel@tonic-gate 			*dst++ = (u_char) (tmp << 4);
970Sstevel@tonic-gate 		}
980Sstevel@tonic-gate 	} else if (isascii(ch) && isdigit(ch)) {
990Sstevel@tonic-gate 		/* Decimal: eat dotted digit string. */
1000Sstevel@tonic-gate 		for (;;) {
1010Sstevel@tonic-gate 			tmp = 0;
1020Sstevel@tonic-gate 			do {
1030Sstevel@tonic-gate 				n = strchr(digits, ch) - digits;
1040Sstevel@tonic-gate 				INSIST(n >= 0 && n <= 9);
1050Sstevel@tonic-gate 				tmp *= 10;
1060Sstevel@tonic-gate 				tmp += n;
1070Sstevel@tonic-gate 				if (tmp > 255)
1080Sstevel@tonic-gate 					goto enoent;
1090Sstevel@tonic-gate 			} while ((ch = *src++) != '\0' &&
1100Sstevel@tonic-gate 				 isascii(ch) && isdigit(ch));
111*11038SRao.Shoaib@Sun.COM 			if (size-- <= 0U)
1120Sstevel@tonic-gate 				goto emsgsize;
1130Sstevel@tonic-gate 			*dst++ = (u_char) tmp;
1140Sstevel@tonic-gate 			if (ch == '\0' || ch == '/')
1150Sstevel@tonic-gate 				break;
1160Sstevel@tonic-gate 			if (ch != '.')
1170Sstevel@tonic-gate 				goto enoent;
1180Sstevel@tonic-gate 			ch = *src++;
1190Sstevel@tonic-gate 			if (!isascii(ch) || !isdigit(ch))
1200Sstevel@tonic-gate 				goto enoent;
1210Sstevel@tonic-gate 		}
1220Sstevel@tonic-gate 	} else
1230Sstevel@tonic-gate 		goto enoent;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	bits = -1;
1260Sstevel@tonic-gate 	if (ch == '/' && isascii((unsigned char)(src[0])) &&
1270Sstevel@tonic-gate 	    isdigit((unsigned char)(src[0])) && dst > odst) {
1280Sstevel@tonic-gate 		/* CIDR width specifier.  Nothing can follow it. */
129*11038SRao.Shoaib@Sun.COM 		ch = *src++;	/*%< Skip over the /. */
1300Sstevel@tonic-gate 		bits = 0;
1310Sstevel@tonic-gate 		do {
1320Sstevel@tonic-gate 			n = strchr(digits, ch) - digits;
1330Sstevel@tonic-gate 			INSIST(n >= 0 && n <= 9);
1340Sstevel@tonic-gate 			bits *= 10;
1350Sstevel@tonic-gate 			bits += n;
136*11038SRao.Shoaib@Sun.COM 			if (bits > 32)
137*11038SRao.Shoaib@Sun.COM 				goto enoent;
1380Sstevel@tonic-gate 		} while ((ch = *src++) != '\0' && isascii(ch) && isdigit(ch));
1390Sstevel@tonic-gate 		if (ch != '\0')
1400Sstevel@tonic-gate 			goto enoent;
1410Sstevel@tonic-gate 	}
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/* Firey death and destruction unless we prefetched EOS. */
1440Sstevel@tonic-gate 	if (ch != '\0')
1450Sstevel@tonic-gate 		goto enoent;
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 	/* If nothing was written to the destination, we found no address. */
1480Sstevel@tonic-gate 	if (dst == odst)
1490Sstevel@tonic-gate 		goto enoent;
1500Sstevel@tonic-gate 	/* If no CIDR spec was given, infer width from net class. */
1510Sstevel@tonic-gate 	if (bits == -1) {
152*11038SRao.Shoaib@Sun.COM 		if (*odst >= 240)	/*%< Class E */
1530Sstevel@tonic-gate 			bits = 32;
154*11038SRao.Shoaib@Sun.COM 		else if (*odst >= 224)	/*%< Class D */
155*11038SRao.Shoaib@Sun.COM 			bits = 8;
156*11038SRao.Shoaib@Sun.COM 		else if (*odst >= 192)	/*%< Class C */
1570Sstevel@tonic-gate 			bits = 24;
158*11038SRao.Shoaib@Sun.COM 		else if (*odst >= 128)	/*%< Class B */
1590Sstevel@tonic-gate 			bits = 16;
160*11038SRao.Shoaib@Sun.COM 		else			/*%< Class A */
1610Sstevel@tonic-gate 			bits = 8;
1620Sstevel@tonic-gate 		/* If imputed mask is narrower than specified octets, widen. */
163*11038SRao.Shoaib@Sun.COM 		if (bits < ((dst - odst) * 8))
1640Sstevel@tonic-gate 			bits = (dst - odst) * 8;
165*11038SRao.Shoaib@Sun.COM 		/*
166*11038SRao.Shoaib@Sun.COM 		 * If there are no additional bits specified for a class D
167*11038SRao.Shoaib@Sun.COM 		 * address adjust bits to 4.
168*11038SRao.Shoaib@Sun.COM 		 */
169*11038SRao.Shoaib@Sun.COM 		if (bits == 8 && *odst == 224)
170*11038SRao.Shoaib@Sun.COM 			bits = 4;
1710Sstevel@tonic-gate 	}
1720Sstevel@tonic-gate 	/* Extend network to cover the actual mask. */
1730Sstevel@tonic-gate 	while (bits > ((dst - odst) * 8)) {
174*11038SRao.Shoaib@Sun.COM 		if (size-- <= 0U)
1750Sstevel@tonic-gate 			goto emsgsize;
1760Sstevel@tonic-gate 		*dst++ = '\0';
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 	return (bits);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate  enoent:
1810Sstevel@tonic-gate 	errno = ENOENT;
1820Sstevel@tonic-gate 	return (-1);
1830Sstevel@tonic-gate 
1840Sstevel@tonic-gate  emsgsize:
1850Sstevel@tonic-gate 	errno = EMSGSIZE;
1860Sstevel@tonic-gate 	return (-1);
1870Sstevel@tonic-gate }
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate static int
getbits(const char * src,int * bitsp)1900Sstevel@tonic-gate getbits(const char *src, int *bitsp) {
1910Sstevel@tonic-gate 	static const char digits[] = "0123456789";
1920Sstevel@tonic-gate 	int n;
1930Sstevel@tonic-gate 	int val;
1940Sstevel@tonic-gate 	char ch;
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	val = 0;
1970Sstevel@tonic-gate 	n = 0;
1980Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
1990Sstevel@tonic-gate 		const char *pch;
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		pch = strchr(digits, ch);
2020Sstevel@tonic-gate 		if (pch != NULL) {
203*11038SRao.Shoaib@Sun.COM 			if (n++ != 0 && val == 0)	/*%< no leading zeros */
2040Sstevel@tonic-gate 				return (0);
2050Sstevel@tonic-gate 			val *= 10;
2060Sstevel@tonic-gate 			val += (pch - digits);
207*11038SRao.Shoaib@Sun.COM 			if (val > 128)			/*%< range */
2080Sstevel@tonic-gate 				return (0);
2090Sstevel@tonic-gate 			continue;
2100Sstevel@tonic-gate 		}
2110Sstevel@tonic-gate 		return (0);
2120Sstevel@tonic-gate 	}
2130Sstevel@tonic-gate 	if (n == 0)
2140Sstevel@tonic-gate 		return (0);
2150Sstevel@tonic-gate 	*bitsp = val;
2160Sstevel@tonic-gate 	return (1);
2170Sstevel@tonic-gate }
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate static int
getv4(const char * src,u_char * dst,int * bitsp)2200Sstevel@tonic-gate getv4(const char *src, u_char *dst, int *bitsp) {
2210Sstevel@tonic-gate 	static const char digits[] = "0123456789";
2220Sstevel@tonic-gate 	u_char *odst = dst;
2230Sstevel@tonic-gate 	int n;
2240Sstevel@tonic-gate 	u_int val;
2250Sstevel@tonic-gate 	char ch;
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	val = 0;
2280Sstevel@tonic-gate 	n = 0;
2290Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
2300Sstevel@tonic-gate 		const char *pch;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 		pch = strchr(digits, ch);
2330Sstevel@tonic-gate 		if (pch != NULL) {
234*11038SRao.Shoaib@Sun.COM 			if (n++ != 0 && val == 0)	/*%< no leading zeros */
2350Sstevel@tonic-gate 				return (0);
2360Sstevel@tonic-gate 			val *= 10;
2370Sstevel@tonic-gate 			val += (pch - digits);
238*11038SRao.Shoaib@Sun.COM 			if (val > 255)			/*%< range */
2390Sstevel@tonic-gate 				return (0);
2400Sstevel@tonic-gate 			continue;
2410Sstevel@tonic-gate 		}
2420Sstevel@tonic-gate 		if (ch == '.' || ch == '/') {
243*11038SRao.Shoaib@Sun.COM 			if (dst - odst > 3)		/*%< too many octets? */
2440Sstevel@tonic-gate 				return (0);
2450Sstevel@tonic-gate 			*dst++ = val;
2460Sstevel@tonic-gate 			if (ch == '/')
2470Sstevel@tonic-gate 				return (getbits(src, bitsp));
2480Sstevel@tonic-gate 			val = 0;
2490Sstevel@tonic-gate 			n = 0;
2500Sstevel@tonic-gate 			continue;
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 		return (0);
2530Sstevel@tonic-gate 	}
2540Sstevel@tonic-gate 	if (n == 0)
2550Sstevel@tonic-gate 		return (0);
256*11038SRao.Shoaib@Sun.COM 	if (dst - odst > 3)		/*%< too many octets? */
2570Sstevel@tonic-gate 		return (0);
2580Sstevel@tonic-gate 	*dst++ = val;
2590Sstevel@tonic-gate 	return (1);
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate 
2620Sstevel@tonic-gate static int
inet_net_pton_ipv6(const char * src,u_char * dst,size_t size)2630Sstevel@tonic-gate inet_net_pton_ipv6(const char *src, u_char *dst, size_t size) {
2640Sstevel@tonic-gate 	static const char xdigits_l[] = "0123456789abcdef",
2650Sstevel@tonic-gate 			  xdigits_u[] = "0123456789ABCDEF";
2660Sstevel@tonic-gate 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
2670Sstevel@tonic-gate 	const char *xdigits, *curtok;
2680Sstevel@tonic-gate 	int ch, saw_xdigit;
2690Sstevel@tonic-gate 	u_int val;
2700Sstevel@tonic-gate 	int digits;
2710Sstevel@tonic-gate 	int bits;
2720Sstevel@tonic-gate 	size_t bytes;
2730Sstevel@tonic-gate 	int words;
2740Sstevel@tonic-gate 	int ipv4;
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
2770Sstevel@tonic-gate 	endp = tp + NS_IN6ADDRSZ;
2780Sstevel@tonic-gate 	colonp = NULL;
2790Sstevel@tonic-gate 	/* Leading :: requires some special handling. */
2800Sstevel@tonic-gate 	if (*src == ':')
2810Sstevel@tonic-gate 		if (*++src != ':')
2820Sstevel@tonic-gate 			goto enoent;
2830Sstevel@tonic-gate 	curtok = src;
2840Sstevel@tonic-gate 	saw_xdigit = 0;
2850Sstevel@tonic-gate 	val = 0;
2860Sstevel@tonic-gate 	digits = 0;
2870Sstevel@tonic-gate 	bits = -1;
2880Sstevel@tonic-gate 	ipv4 = 0;
2890Sstevel@tonic-gate 	while ((ch = *src++) != '\0') {
2900Sstevel@tonic-gate 		const char *pch;
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
2930Sstevel@tonic-gate 			pch = strchr((xdigits = xdigits_u), ch);
2940Sstevel@tonic-gate 		if (pch != NULL) {
2950Sstevel@tonic-gate 			val <<= 4;
2960Sstevel@tonic-gate 			val |= (pch - xdigits);
2970Sstevel@tonic-gate 			if (++digits > 4)
2980Sstevel@tonic-gate 				goto enoent;
2990Sstevel@tonic-gate 			saw_xdigit = 1;
3000Sstevel@tonic-gate 			continue;
3010Sstevel@tonic-gate 		}
3020Sstevel@tonic-gate 		if (ch == ':') {
3030Sstevel@tonic-gate 			curtok = src;
3040Sstevel@tonic-gate 			if (!saw_xdigit) {
3050Sstevel@tonic-gate 				if (colonp)
3060Sstevel@tonic-gate 					goto enoent;
3070Sstevel@tonic-gate 				colonp = tp;
3080Sstevel@tonic-gate 				continue;
3090Sstevel@tonic-gate 			} else if (*src == '\0')
3100Sstevel@tonic-gate 				goto enoent;
3110Sstevel@tonic-gate 			if (tp + NS_INT16SZ > endp)
3120Sstevel@tonic-gate 				return (0);
3130Sstevel@tonic-gate 			*tp++ = (u_char) (val >> 8) & 0xff;
3140Sstevel@tonic-gate 			*tp++ = (u_char) val & 0xff;
3150Sstevel@tonic-gate 			saw_xdigit = 0;
3160Sstevel@tonic-gate 			digits = 0;
3170Sstevel@tonic-gate 			val = 0;
3180Sstevel@tonic-gate 			continue;
3190Sstevel@tonic-gate 		}
3200Sstevel@tonic-gate 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
3210Sstevel@tonic-gate 		     getv4(curtok, tp, &bits) > 0) {
3220Sstevel@tonic-gate 			tp += NS_INADDRSZ;
3230Sstevel@tonic-gate 			saw_xdigit = 0;
3240Sstevel@tonic-gate 			ipv4 = 1;
325*11038SRao.Shoaib@Sun.COM 			break;	/*%< '\\0' was seen by inet_pton4(). */
3260Sstevel@tonic-gate 		}
3270Sstevel@tonic-gate 		if (ch == '/' && getbits(src, &bits) > 0)
3280Sstevel@tonic-gate 			break;
3290Sstevel@tonic-gate 		goto enoent;
3300Sstevel@tonic-gate 	}
3310Sstevel@tonic-gate 	if (saw_xdigit) {
3320Sstevel@tonic-gate 		if (tp + NS_INT16SZ > endp)
3330Sstevel@tonic-gate 			goto enoent;
3340Sstevel@tonic-gate 		*tp++ = (u_char) (val >> 8) & 0xff;
3350Sstevel@tonic-gate 		*tp++ = (u_char) val & 0xff;
3360Sstevel@tonic-gate 	}
3370Sstevel@tonic-gate 	if (bits == -1)
3380Sstevel@tonic-gate 		bits = 128;
3390Sstevel@tonic-gate 
3400Sstevel@tonic-gate 	words = (bits + 15) / 16;
3410Sstevel@tonic-gate 	if (words < 2)
3420Sstevel@tonic-gate 		words = 2;
3430Sstevel@tonic-gate 	if (ipv4)
3440Sstevel@tonic-gate 		words = 8;
3450Sstevel@tonic-gate 	endp =  tmp + 2 * words;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	if (colonp != NULL) {
3480Sstevel@tonic-gate 		/*
3490Sstevel@tonic-gate 		 * Since some memmove()'s erroneously fail to handle
3500Sstevel@tonic-gate 		 * overlapping regions, we'll do the shift by hand.
3510Sstevel@tonic-gate 		 */
3520Sstevel@tonic-gate 		const int n = tp - colonp;
3530Sstevel@tonic-gate 		int i;
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 		if (tp == endp)
3560Sstevel@tonic-gate 			goto enoent;
3570Sstevel@tonic-gate 		for (i = 1; i <= n; i++) {
3580Sstevel@tonic-gate 			endp[- i] = colonp[n - i];
3590Sstevel@tonic-gate 			colonp[n - i] = 0;
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 		tp = endp;
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 	if (tp != endp)
3640Sstevel@tonic-gate 		goto enoent;
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	bytes = (bits + 7) / 8;
3670Sstevel@tonic-gate 	if (bytes > size)
3680Sstevel@tonic-gate 		goto emsgsize;
3690Sstevel@tonic-gate 	memcpy(dst, tmp, bytes);
3700Sstevel@tonic-gate 	return (bits);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate  enoent:
3730Sstevel@tonic-gate 	errno = ENOENT;
3740Sstevel@tonic-gate 	return (-1);
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate  emsgsize:
3770Sstevel@tonic-gate 	errno = EMSGSIZE;
3780Sstevel@tonic-gate 	return (-1);
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate 
381*11038SRao.Shoaib@Sun.COM /*%
3820Sstevel@tonic-gate  * int
3830Sstevel@tonic-gate  * inet_net_pton(af, src, dst, size)
3840Sstevel@tonic-gate  *	convert network number from presentation to network format.
3850Sstevel@tonic-gate  *	accepts hex octets, hex strings, decimal octets, and /CIDR.
3860Sstevel@tonic-gate  *	"size" is in bytes and describes "dst".
3870Sstevel@tonic-gate  * return:
3880Sstevel@tonic-gate  *	number of bits, either imputed classfully or specified with /CIDR,
3890Sstevel@tonic-gate  *	or -1 if some failure occurred (check errno).  ENOENT means it was
3900Sstevel@tonic-gate  *	not a valid network specification.
3910Sstevel@tonic-gate  * author:
3920Sstevel@tonic-gate  *	Paul Vixie (ISC), June 1996
3930Sstevel@tonic-gate  */
3940Sstevel@tonic-gate int
inet_net_pton(int af,const char * src,void * dst,size_t size)3950Sstevel@tonic-gate inet_net_pton(int af, const char *src, void *dst, size_t size) {
3960Sstevel@tonic-gate 	switch (af) {
3970Sstevel@tonic-gate 	case AF_INET:
3980Sstevel@tonic-gate 		return (inet_net_pton_ipv4(src, dst, size));
3990Sstevel@tonic-gate 	case AF_INET6:
4000Sstevel@tonic-gate 		return (inet_net_pton_ipv6(src, dst, size));
4010Sstevel@tonic-gate 	default:
4020Sstevel@tonic-gate 		errno = EAFNOSUPPORT;
4030Sstevel@tonic-gate 		return (-1);
4040Sstevel@tonic-gate 	}
4050Sstevel@tonic-gate }
406*11038SRao.Shoaib@Sun.COM 
407*11038SRao.Shoaib@Sun.COM /*! \file */
408