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