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