Home | History | Annotate | Line # | Download | only in inet
      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.7   msaitoh __RCSID("$NetBSD: inet_net_pton.c,v 1.7 2024/05/12 23:58:18 msaitoh 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 /*
     51  1.1  christos  * static int
     52  1.1  christos  * inet_net_pton_ipv4(src, dst, size)
     53  1.1  christos  *	convert IPv4 network number from presentation to network format.
     54  1.1  christos  *	accepts hex octets, hex strings, decimal octets, and /CIDR.
     55  1.1  christos  *	"size" is in bytes and describes "dst".
     56  1.1  christos  * return:
     57  1.6    andvar  *	number of bits, either inputed classfully or specified with /CIDR,
     58  1.1  christos  *	or -1 if some failure occurred (check errno).  ENOENT means it was
     59  1.1  christos  *	not an IPv4 network specification.
     60  1.1  christos  * note:
     61  1.1  christos  *	network byte order assumed.  this means 192.5.5.240/28 has
     62  1.1  christos  *	0b11110000 in its fourth octet.
     63  1.1  christos  * author:
     64  1.1  christos  *	Paul Vixie (ISC), June 1996
     65  1.1  christos  */
     66  1.1  christos static int
     67  1.4      matt inet_net_pton_ipv4(const char *src, u_char *dst, size_t size)
     68  1.4      matt {
     69  1.1  christos 	static const char xdigits[] = "0123456789abcdef";
     70  1.1  christos 	static const char digits[] = "0123456789";
     71  1.3  christos 	int ch, dirty, bits;
     72  1.3  christos 	ptrdiff_t n, tmp;
     73  1.1  christos 	const u_char *odst = dst;
     74  1.1  christos 
     75  1.3  christos 	tmp = 0;
     76  1.1  christos 	ch = *src++;
     77  1.1  christos 	if (ch == '0' && (src[0] == 'x' || src[0] == 'X')
     78  1.1  christos 	    && isascii((u_char)(src[1]))
     79  1.1  christos 	    && isxdigit((u_char)(src[1]))) {
     80  1.1  christos 		/* Hexadecimal: Eat nybble string. */
     81  1.1  christos 		if (size == 0)
     82  1.1  christos 			goto emsgsize;
     83  1.1  christos 		dirty = 0;
     84  1.1  christos 		src++;	/* skip x or X. */
     85  1.1  christos 		while ((ch = *src++) != '\0' && isascii((u_char)ch)
     86  1.1  christos 		    && isxdigit((u_char)ch)) {
     87  1.1  christos 			if (isupper((u_char)ch))
     88  1.1  christos 				ch = tolower((u_char)ch);
     89  1.1  christos 			n = strchr(xdigits, ch) - xdigits;
     90  1.1  christos 			INSIST(n >= 0 && n <= 15);
     91  1.1  christos 			if (dirty == 0)
     92  1.1  christos 				tmp = n;
     93  1.1  christos 			else
     94  1.1  christos 				tmp = (tmp << 4) | n;
     95  1.1  christos 			if (++dirty == 2) {
     96  1.1  christos 				if (size-- == 0)
     97  1.1  christos 					goto emsgsize;
     98  1.1  christos 				*dst++ = (u_char) tmp;
     99  1.1  christos 				dirty = 0;
    100  1.1  christos 			}
    101  1.1  christos 		}
    102  1.1  christos 		if (dirty) {  /* Odd trailing nybble? */
    103  1.1  christos 			if (size-- == 0)
    104  1.1  christos 				goto emsgsize;
    105  1.1  christos 			*dst++ = (u_char) (tmp << 4);
    106  1.1  christos 		}
    107  1.1  christos 	} else if (isascii((u_char)ch) && isdigit((u_char)ch)) {
    108  1.1  christos 		/* Decimal: eat dotted digit string. */
    109  1.1  christos 		for (;;) {
    110  1.1  christos 			tmp = 0;
    111  1.1  christos 			do {
    112  1.1  christos 				n = strchr(digits, ch) - digits;
    113  1.1  christos 				INSIST(n >= 0 && n <= 9);
    114  1.1  christos 				tmp *= 10;
    115  1.1  christos 				tmp += n;
    116  1.1  christos 				if (tmp > 255)
    117  1.1  christos 					goto enoent;
    118  1.1  christos 			} while ((ch = *src++) != '\0' &&
    119  1.1  christos 				 isascii((u_char)ch) && isdigit((u_char)ch));
    120  1.1  christos 			if (size-- == 0)
    121  1.1  christos 				goto emsgsize;
    122  1.1  christos 			*dst++ = (u_char) tmp;
    123  1.1  christos 			if (ch == '\0' || ch == '/')
    124  1.1  christos 				break;
    125  1.1  christos 			if (ch != '.')
    126  1.1  christos 				goto enoent;
    127  1.1  christos 			ch = *src++;
    128  1.1  christos 			if (!isascii((u_char)ch) || !isdigit((u_char)ch))
    129  1.1  christos 				goto enoent;
    130  1.1  christos 		}
    131  1.1  christos 	} else
    132  1.1  christos 		goto enoent;
    133  1.1  christos 
    134  1.1  christos 	bits = -1;
    135  1.1  christos 	if (ch == '/' && isascii((u_char)(src[0])) &&
    136  1.1  christos 	    isdigit((u_char)(src[0])) && dst > odst) {
    137  1.1  christos 		/* CIDR width specifier.  Nothing can follow it. */
    138  1.1  christos 		ch = *src++;	/* Skip over the /. */
    139  1.1  christos 		bits = 0;
    140  1.1  christos 		do {
    141  1.1  christos 			n = strchr(digits, ch) - digits;
    142  1.1  christos 			INSIST(n >= 0 && n <= 9);
    143  1.1  christos 			bits *= 10;
    144  1.3  christos 			bits += (int)n;
    145  1.2  christos 			if (bits > 32)
    146  1.2  christos 				goto emsgsize;
    147  1.1  christos 		} while ((ch = *src++) != '\0' && isascii((u_char)ch)
    148  1.1  christos 		    && isdigit((u_char)ch));
    149  1.1  christos 		if (ch != '\0')
    150  1.1  christos 			goto enoent;
    151  1.1  christos 	}
    152  1.1  christos 
    153  1.7   msaitoh 	/* Fiery death and destruction unless we prefetched EOS. */
    154  1.1  christos 	if (ch != '\0')
    155  1.1  christos 		goto enoent;
    156  1.1  christos 
    157  1.1  christos 	/* If nothing was written to the destination, we found no address. */
    158  1.1  christos 	if (dst == odst)
    159  1.1  christos 		goto enoent;
    160  1.1  christos 	/* If no CIDR spec was given, infer width from net class. */
    161  1.1  christos 	if (bits == -1) {
    162  1.1  christos 		if (*odst >= 240)	/* Class E */
    163  1.1  christos 			bits = 32;
    164  1.1  christos 		else if (*odst >= 224)	/* Class D */
    165  1.1  christos 			bits = 4;
    166  1.1  christos 		else if (*odst >= 192)	/* Class C */
    167  1.1  christos 			bits = 24;
    168  1.1  christos 		else if (*odst >= 128)	/* Class B */
    169  1.1  christos 			bits = 16;
    170  1.1  christos 		else			/* Class A */
    171  1.1  christos 			bits = 8;
    172  1.6    andvar 		/* If inputed mask is narrower than specified octets, widen. */
    173  1.1  christos 		if (bits >= 8 && bits < ((dst - odst) * 8))
    174  1.3  christos 			bits = (int)(dst - odst) * 8;
    175  1.1  christos 	}
    176  1.1  christos 	/* Extend network to cover the actual mask. */
    177  1.1  christos 	while (bits > ((dst - odst) * 8)) {
    178  1.1  christos 		if (size-- == 0)
    179  1.1  christos 			goto emsgsize;
    180  1.1  christos 		*dst++ = '\0';
    181  1.1  christos 	}
    182  1.1  christos 	return (bits);
    183  1.1  christos 
    184  1.1  christos  enoent:
    185  1.1  christos 	errno = ENOENT;
    186  1.1  christos 	return (-1);
    187  1.1  christos 
    188  1.1  christos  emsgsize:
    189  1.1  christos 	errno = EMSGSIZE;
    190  1.1  christos 	return (-1);
    191  1.1  christos }
    192  1.1  christos 
    193  1.1  christos static int
    194  1.4      matt getbits(const char *src, int *bitsp)
    195  1.4      matt {
    196  1.1  christos 	static const char digits[] = "0123456789";
    197  1.1  christos 	int n;
    198  1.1  christos 	int val;
    199  1.1  christos 	char ch;
    200  1.1  christos 
    201  1.1  christos 	val = 0;
    202  1.1  christos 	n = 0;
    203  1.1  christos 	while ((ch = *src++) != '\0') {
    204  1.1  christos 		const char *pch;
    205  1.1  christos 
    206  1.1  christos 		pch = strchr(digits, ch);
    207  1.1  christos 		if (pch != NULL) {
    208  1.1  christos 			if (n++ != 0 && val == 0)	/* no leading zeros */
    209  1.1  christos 				return (0);
    210  1.1  christos 			val *= 10;
    211  1.3  christos 			val += (int)(pch - digits);
    212  1.1  christos 			if (val > 128)			/* range */
    213  1.1  christos 				return (0);
    214  1.1  christos 			continue;
    215  1.1  christos 		}
    216  1.1  christos 		return (0);
    217  1.1  christos 	}
    218  1.1  christos 	if (n == 0)
    219  1.1  christos 		return (0);
    220  1.1  christos 	*bitsp = val;
    221  1.1  christos 	return (1);
    222  1.1  christos }
    223  1.1  christos 
    224  1.1  christos static int
    225  1.4      matt getv4(const char *src, u_char *dst, int *bitsp)
    226  1.4      matt {
    227  1.1  christos 	static const char digits[] = "0123456789";
    228  1.1  christos 	u_char *odst = dst;
    229  1.1  christos 	int n;
    230  1.1  christos 	u_int val;
    231  1.1  christos 	char ch;
    232  1.1  christos 
    233  1.1  christos 	val = 0;
    234  1.1  christos 	n = 0;
    235  1.1  christos 	while ((ch = *src++) != '\0') {
    236  1.1  christos 		const char *pch;
    237  1.1  christos 
    238  1.1  christos 		pch = strchr(digits, ch);
    239  1.1  christos 		if (pch != NULL) {
    240  1.1  christos 			if (n++ != 0 && val == 0)	/* no leading zeros */
    241  1.1  christos 				return (0);
    242  1.1  christos 			val *= 10;
    243  1.3  christos 			val += (int)(pch - digits);
    244  1.1  christos 			if (val > 255)			/* range */
    245  1.1  christos 				return (0);
    246  1.1  christos 			continue;
    247  1.1  christos 		}
    248  1.1  christos 		if (ch == '.' || ch == '/') {
    249  1.1  christos 			if (dst - odst > 3)		/* too many octets? */
    250  1.1  christos 				return (0);
    251  1.1  christos 			*dst++ = val;
    252  1.1  christos 			if (ch == '/')
    253  1.1  christos 				return (getbits(src, bitsp));
    254  1.1  christos 			val = 0;
    255  1.1  christos 			n = 0;
    256  1.1  christos 			continue;
    257  1.1  christos 		}
    258  1.1  christos 		return (0);
    259  1.1  christos 	}
    260  1.1  christos 	if (n == 0)
    261  1.1  christos 		return (0);
    262  1.1  christos 	if (dst - odst > 3)		/* too many octets? */
    263  1.1  christos 		return (0);
    264  1.1  christos 	*dst++ = val;
    265  1.1  christos 	return (1);
    266  1.1  christos }
    267  1.1  christos 
    268  1.1  christos static int
    269  1.4      matt inet_net_pton_ipv6(const char *src, u_char *dst, size_t size)
    270  1.4      matt {
    271  1.1  christos 	static const char xdigits_l[] = "0123456789abcdef",
    272  1.1  christos 			  xdigits_u[] = "0123456789ABCDEF";
    273  1.1  christos 	u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
    274  1.1  christos 	const char *xdigits, *curtok;
    275  1.1  christos 	int ch, saw_xdigit;
    276  1.1  christos 	u_int val;
    277  1.1  christos 	int digits;
    278  1.1  christos 	int bits;
    279  1.1  christos 	size_t bytes;
    280  1.1  christos 	int words;
    281  1.1  christos 	int ipv4;
    282  1.1  christos 
    283  1.1  christos 	memset((tp = tmp), '\0', NS_IN6ADDRSZ);
    284  1.1  christos 	endp = tp + NS_IN6ADDRSZ;
    285  1.1  christos 	colonp = NULL;
    286  1.1  christos 	/* Leading :: requires some special handling. */
    287  1.1  christos 	if (*src == ':')
    288  1.1  christos 		if (*++src != ':')
    289  1.1  christos 			goto enoent;
    290  1.1  christos 	curtok = src;
    291  1.1  christos 	saw_xdigit = 0;
    292  1.1  christos 	val = 0;
    293  1.1  christos 	digits = 0;
    294  1.1  christos 	bits = -1;
    295  1.1  christos 	ipv4 = 0;
    296  1.1  christos 	while ((ch = *src++) != '\0') {
    297  1.1  christos 		const char *pch;
    298  1.1  christos 
    299  1.1  christos 		if ((pch = strchr((xdigits = xdigits_l), ch)) == NULL)
    300  1.1  christos 			pch = strchr((xdigits = xdigits_u), ch);
    301  1.1  christos 		if (pch != NULL) {
    302  1.1  christos 			val <<= 4;
    303  1.3  christos 			val |= (int)(pch - xdigits);
    304  1.1  christos 			if (++digits > 4)
    305  1.1  christos 				goto enoent;
    306  1.1  christos 			saw_xdigit = 1;
    307  1.1  christos 			continue;
    308  1.1  christos 		}
    309  1.1  christos 		if (ch == ':') {
    310  1.1  christos 			curtok = src;
    311  1.1  christos 			if (!saw_xdigit) {
    312  1.1  christos 				if (colonp)
    313  1.1  christos 					goto enoent;
    314  1.1  christos 				colonp = tp;
    315  1.1  christos 				continue;
    316  1.1  christos 			} else if (*src == '\0')
    317  1.1  christos 				goto enoent;
    318  1.1  christos 			if (tp + NS_INT16SZ > endp)
    319  1.1  christos 				return (0);
    320  1.1  christos 			*tp++ = (u_char) (val >> 8) & 0xff;
    321  1.1  christos 			*tp++ = (u_char) val & 0xff;
    322  1.1  christos 			saw_xdigit = 0;
    323  1.1  christos 			digits = 0;
    324  1.1  christos 			val = 0;
    325  1.1  christos 			continue;
    326  1.1  christos 		}
    327  1.1  christos 		if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
    328  1.1  christos 		     getv4(curtok, tp, &bits) > 0) {
    329  1.1  christos 			tp += NS_INADDRSZ;
    330  1.1  christos 			saw_xdigit = 0;
    331  1.1  christos 			ipv4 = 1;
    332  1.1  christos 			break;	/* '\0' was seen by inet_pton4(). */
    333  1.1  christos 		}
    334  1.1  christos 		if (ch == '/' && getbits(src, &bits) > 0)
    335  1.1  christos 			break;
    336  1.1  christos 		goto enoent;
    337  1.1  christos 	}
    338  1.1  christos 	if (saw_xdigit) {
    339  1.1  christos 		if (tp + NS_INT16SZ > endp)
    340  1.1  christos 			goto enoent;
    341  1.1  christos 		*tp++ = (u_char) (val >> 8) & 0xff;
    342  1.1  christos 		*tp++ = (u_char) val & 0xff;
    343  1.1  christos 	}
    344  1.1  christos 	if (bits == -1)
    345  1.1  christos 		bits = 128;
    346  1.1  christos 
    347  1.1  christos 	words = (bits + 15) / 16;
    348  1.1  christos 	if (words < 2)
    349  1.1  christos 		words = 2;
    350  1.1  christos 	if (ipv4)
    351  1.1  christos 		words = 8;
    352  1.1  christos 	endp =  tmp + 2 * words;
    353  1.1  christos 
    354  1.1  christos 	if (colonp != NULL) {
    355  1.1  christos 		/*
    356  1.1  christos 		 * Since some memmove()'s erroneously fail to handle
    357  1.1  christos 		 * overlapping regions, we'll do the shift by hand.
    358  1.1  christos 		 */
    359  1.3  christos 		const ptrdiff_t n = tp - colonp;
    360  1.1  christos 		int i;
    361  1.1  christos 
    362  1.1  christos 		if (tp == endp)
    363  1.1  christos 			goto enoent;
    364  1.1  christos 		for (i = 1; i <= n; i++) {
    365  1.1  christos 			endp[- i] = colonp[n - i];
    366  1.1  christos 			colonp[n - i] = 0;
    367  1.1  christos 		}
    368  1.1  christos 		tp = endp;
    369  1.1  christos 	}
    370  1.1  christos 	if (tp != endp)
    371  1.1  christos 		goto enoent;
    372  1.1  christos 
    373  1.1  christos 	bytes = (bits + 7) / 8;
    374  1.1  christos 	if (bytes > size)
    375  1.1  christos 		goto emsgsize;
    376  1.1  christos 	memcpy(dst, tmp, bytes);
    377  1.1  christos 	return (bits);
    378  1.1  christos 
    379  1.1  christos  enoent:
    380  1.1  christos 	errno = ENOENT;
    381  1.1  christos 	return (-1);
    382  1.1  christos 
    383  1.1  christos  emsgsize:
    384  1.1  christos 	errno = EMSGSIZE;
    385  1.1  christos 	return (-1);
    386  1.1  christos }
    387  1.1  christos 
    388  1.1  christos /*
    389  1.1  christos  * int
    390  1.1  christos  * inet_net_pton(af, src, dst, size)
    391  1.1  christos  *	convert network number from presentation to network format.
    392  1.1  christos  *	accepts hex octets, hex strings, decimal octets, and /CIDR.
    393  1.1  christos  *	"size" is in bytes and describes "dst".
    394  1.1  christos  * return:
    395  1.6    andvar  *	number of bits, either inputed classfully or specified with /CIDR,
    396  1.1  christos  *	or -1 if some failure occurred (check errno).  ENOENT means it was
    397  1.1  christos  *	not a valid network specification.
    398  1.1  christos  * author:
    399  1.1  christos  *	Paul Vixie (ISC), June 1996
    400  1.1  christos  */
    401  1.1  christos int
    402  1.4      matt inet_net_pton(int af, const char *src, void *dst, size_t size)
    403  1.4      matt {
    404  1.1  christos 	switch (af) {
    405  1.1  christos 	case AF_INET:
    406  1.1  christos 		return (inet_net_pton_ipv4(src, dst, size));
    407  1.1  christos 	case AF_INET6:
    408  1.1  christos 		return (inet_net_pton_ipv6(src, dst, size));
    409  1.1  christos 	default:
    410  1.1  christos 		errno = EAFNOSUPPORT;
    411  1.1  christos 		return (-1);
    412  1.1  christos 	}
    413  1.1  christos }
    414