xref: /netbsd-src/common/lib/libc/inet/inet_addr.c (revision 4fb9e183e51db5a5a4fbc4333285b60923a3b609)
1*4fb9e183Skamil /*	$NetBSD: inet_addr.c,v 1.4 2018/07/26 00:20:41 kamil Exp $	*/
237c9f0a6Schristos 
337c9f0a6Schristos /*
437c9f0a6Schristos  * Copyright (c) 1983, 1990, 1993
537c9f0a6Schristos  *    The Regents of the University of California.  All rights reserved.
637c9f0a6Schristos  *
737c9f0a6Schristos  * Redistribution and use in source and binary forms, with or without
837c9f0a6Schristos  * modification, are permitted provided that the following conditions
937c9f0a6Schristos  * are met:
1037c9f0a6Schristos  * 1. Redistributions of source code must retain the above copyright
1137c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer.
1237c9f0a6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1337c9f0a6Schristos  *    notice, this list of conditions and the following disclaimer in the
1437c9f0a6Schristos  *    documentation and/or other materials provided with the distribution.
1537c9f0a6Schristos  * 3. All advertising materials mentioning features or use of this software
1637c9f0a6Schristos  *    must display the following acknowledgement:
1737c9f0a6Schristos  * 	This product includes software developed by the University of
1837c9f0a6Schristos  * 	California, Berkeley and its contributors.
1937c9f0a6Schristos  * 4. Neither the name of the University nor the names of its contributors
2037c9f0a6Schristos  *    may be used to endorse or promote products derived from this software
2137c9f0a6Schristos  *    without specific prior written permission.
2237c9f0a6Schristos  *
2337c9f0a6Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2437c9f0a6Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2537c9f0a6Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2637c9f0a6Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2737c9f0a6Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2837c9f0a6Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2937c9f0a6Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3037c9f0a6Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3137c9f0a6Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3237c9f0a6Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3337c9f0a6Schristos  * SUCH DAMAGE.
3437c9f0a6Schristos  */
3537c9f0a6Schristos 
3637c9f0a6Schristos /*
3737c9f0a6Schristos  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
3837c9f0a6Schristos  *
3937c9f0a6Schristos  * Permission to use, copy, modify, and distribute this software for any
4037c9f0a6Schristos  * purpose with or without fee is hereby granted, provided that the above
4137c9f0a6Schristos  * copyright notice and this permission notice appear in all copies, and that
4237c9f0a6Schristos  * the name of Digital Equipment Corporation not be used in advertising or
4337c9f0a6Schristos  * publicity pertaining to distribution of the document or software without
4437c9f0a6Schristos  * specific, written prior permission.
4537c9f0a6Schristos  *
4637c9f0a6Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
4737c9f0a6Schristos  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
4837c9f0a6Schristos  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
4937c9f0a6Schristos  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
5037c9f0a6Schristos  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
5137c9f0a6Schristos  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5237c9f0a6Schristos  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
5337c9f0a6Schristos  * SOFTWARE.
5437c9f0a6Schristos  */
5537c9f0a6Schristos 
5637c9f0a6Schristos /*
5737c9f0a6Schristos  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5837c9f0a6Schristos  * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
5937c9f0a6Schristos  *
6037c9f0a6Schristos  * Permission to use, copy, modify, and distribute this software for any
6137c9f0a6Schristos  * purpose with or without fee is hereby granted, provided that the above
6237c9f0a6Schristos  * copyright notice and this permission notice appear in all copies.
6337c9f0a6Schristos  *
6437c9f0a6Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
6537c9f0a6Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
6637c9f0a6Schristos  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
6737c9f0a6Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
6837c9f0a6Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
6937c9f0a6Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
7037c9f0a6Schristos  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
7137c9f0a6Schristos  */
7237c9f0a6Schristos 
7337c9f0a6Schristos #if !defined(_KERNEL) && !defined(_STANDALONE)
7437c9f0a6Schristos #include <sys/cdefs.h>
7537c9f0a6Schristos #if defined(LIBC_SCCS) && !defined(lint)
7637c9f0a6Schristos #if 0
7737c9f0a6Schristos static const char sccsid[] = "@(#)inet_addr.c	8.1 (Berkeley) 6/17/93";
7837c9f0a6Schristos static const char rcsid[] = "Id: inet_addr.c,v 1.2.206.2 2004/03/17 00:29:45 marka Exp";
7937c9f0a6Schristos #else
80*4fb9e183Skamil __RCSID("$NetBSD: inet_addr.c,v 1.4 2018/07/26 00:20:41 kamil Exp $");
8137c9f0a6Schristos #endif
8237c9f0a6Schristos #endif /* LIBC_SCCS and not lint */
8337c9f0a6Schristos 
8437c9f0a6Schristos #include "port_before.h"
8537c9f0a6Schristos 
8637c9f0a6Schristos #include "namespace.h"
8737c9f0a6Schristos #include <sys/types.h>
8837c9f0a6Schristos #include <sys/param.h>
8937c9f0a6Schristos 
9037c9f0a6Schristos #include <netinet/in.h>
9137c9f0a6Schristos #include <arpa/inet.h>
9237c9f0a6Schristos 
9337c9f0a6Schristos #include <ctype.h>
9437c9f0a6Schristos 
9537c9f0a6Schristos #include "port_after.h"
9637c9f0a6Schristos 
9737c9f0a6Schristos #ifdef __weak_alias
__weak_alias(inet_aton,_inet_aton)9837c9f0a6Schristos __weak_alias(inet_aton,_inet_aton)
9937c9f0a6Schristos #endif
10037c9f0a6Schristos #else
10137c9f0a6Schristos #include <lib/libkern/libkern.h>
10237c9f0a6Schristos #include <netinet/in.h>
10337c9f0a6Schristos #endif
10437c9f0a6Schristos 
10537c9f0a6Schristos /*
10637c9f0a6Schristos  * Ascii internet address interpretation routine.
10737c9f0a6Schristos  * The value returned is in network order.
10837c9f0a6Schristos  */
109267197ecSapb uint32_t
11037c9f0a6Schristos inet_addr(const char *cp) {
11137c9f0a6Schristos 	struct in_addr val;
11237c9f0a6Schristos 
11337c9f0a6Schristos 	if (inet_aton(cp, &val))
11437c9f0a6Schristos 		return (val.s_addr);
11537c9f0a6Schristos 	return (INADDR_NONE);
11637c9f0a6Schristos }
11737c9f0a6Schristos 
11837c9f0a6Schristos /*
11937c9f0a6Schristos  * Check whether "cp" is a valid ascii representation
12037c9f0a6Schristos  * of an Internet address and convert to a binary address.
12137c9f0a6Schristos  * Returns 1 if the address is valid, 0 if not.
12237c9f0a6Schristos  * This replaces inet_addr, the return value from which
12337c9f0a6Schristos  * cannot distinguish between failure and a local broadcast address.
12437c9f0a6Schristos  */
12537c9f0a6Schristos int
inet_aton(const char * cp,struct in_addr * addr)12637c9f0a6Schristos inet_aton(const char *cp, struct in_addr *addr) {
127267197ecSapb 	uint32_t val;
128a5fd370aSchristos 	int base;
129a5fd370aSchristos 	size_t n;
13037c9f0a6Schristos 	char c;
131267197ecSapb 	uint8_t parts[4];
132267197ecSapb 	uint8_t *pp = parts;
13337c9f0a6Schristos 	int digit;
13437c9f0a6Schristos 
13537c9f0a6Schristos 	c = *cp;
13637c9f0a6Schristos 	for (;;) {
13737c9f0a6Schristos 		/*
13837c9f0a6Schristos 		 * Collect number up to ``.''.
13937c9f0a6Schristos 		 * Values are specified as for C:
14037c9f0a6Schristos 		 * 0x=hex, 0=octal, isdigit=decimal.
14137c9f0a6Schristos 		 */
14237c9f0a6Schristos 		if (!isdigit((unsigned char)c))
14337c9f0a6Schristos 			return (0);
14437c9f0a6Schristos 		val = 0; base = 10; digit = 0;
14537c9f0a6Schristos 		if (c == '0') {
14637c9f0a6Schristos 			c = *++cp;
14737c9f0a6Schristos 			if (c == 'x' || c == 'X')
14837c9f0a6Schristos 				base = 16, c = *++cp;
14937c9f0a6Schristos 			else {
15037c9f0a6Schristos 				base = 8;
15137c9f0a6Schristos 				digit = 1 ;
15237c9f0a6Schristos 			}
15337c9f0a6Schristos 		}
15437c9f0a6Schristos 		for (;;) {
15537c9f0a6Schristos 			if (isascii(c) && isdigit((unsigned char)c)) {
15637c9f0a6Schristos 				if (base == 8 && (c == '8' || c == '9'))
15737c9f0a6Schristos 					return (0);
15837c9f0a6Schristos 				val = (val * base) + (c - '0');
15937c9f0a6Schristos 				c = *++cp;
16037c9f0a6Schristos 				digit = 1;
16137c9f0a6Schristos 			} else if (base == 16 && isascii(c) &&
16237c9f0a6Schristos 				   isxdigit((unsigned char)c)) {
16337c9f0a6Schristos 				val = (val << 4) |
16437c9f0a6Schristos 					(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
16537c9f0a6Schristos 				c = *++cp;
16637c9f0a6Schristos 				digit = 1;
16737c9f0a6Schristos 			} else
16837c9f0a6Schristos 				break;
16937c9f0a6Schristos 		}
17037c9f0a6Schristos 		if (c == '.') {
17137c9f0a6Schristos 			/*
17237c9f0a6Schristos 			 * Internet format:
17337c9f0a6Schristos 			 *	a.b.c.d
17437c9f0a6Schristos 			 *	a.b.c	(with c treated as 16 bits)
17537c9f0a6Schristos 			 *	a.b	(with b treated as 24 bits)
17637c9f0a6Schristos 			 */
17737c9f0a6Schristos 			if (pp >= parts + 3 || val > 0xffU)
17837c9f0a6Schristos 				return (0);
17937c9f0a6Schristos 			*pp++ = val;
18037c9f0a6Schristos 			c = *++cp;
18137c9f0a6Schristos 		} else
18237c9f0a6Schristos 			break;
18337c9f0a6Schristos 	}
18437c9f0a6Schristos 	/*
18537c9f0a6Schristos 	 * Check for trailing characters.
18637c9f0a6Schristos 	 */
18737c9f0a6Schristos 	if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))
18837c9f0a6Schristos 		return (0);
18937c9f0a6Schristos 	/*
19037c9f0a6Schristos 	 * Did we get a valid digit?
19137c9f0a6Schristos 	 */
19237c9f0a6Schristos 	if (!digit)
19337c9f0a6Schristos 		return (0);
19437c9f0a6Schristos 	/*
19537c9f0a6Schristos 	 * Concoct the address according to
19637c9f0a6Schristos 	 * the number of parts specified.
19737c9f0a6Schristos 	 */
19837c9f0a6Schristos 	n = pp - parts + 1;
19937c9f0a6Schristos 	switch (n) {
20037c9f0a6Schristos 	case 1:				/* a -- 32 bits */
20137c9f0a6Schristos 		break;
20237c9f0a6Schristos 
20337c9f0a6Schristos 	case 2:				/* a.b -- 8.24 bits */
20437c9f0a6Schristos 		if (val > 0xffffffU)
20537c9f0a6Schristos 			return (0);
206*4fb9e183Skamil 		val |= (uint32_t)parts[0] << 24;
20737c9f0a6Schristos 		break;
20837c9f0a6Schristos 
20937c9f0a6Schristos 	case 3:				/* a.b.c -- 8.8.16 bits */
21037c9f0a6Schristos 		if (val > 0xffffU)
21137c9f0a6Schristos 			return (0);
212*4fb9e183Skamil 		val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16);
21337c9f0a6Schristos 		break;
21437c9f0a6Schristos 
21537c9f0a6Schristos 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
21637c9f0a6Schristos 		if (val > 0xffU)
21737c9f0a6Schristos 			return (0);
218*4fb9e183Skamil 		val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16) |
219*4fb9e183Skamil 		        (parts[2] << 8);
22037c9f0a6Schristos 		break;
22137c9f0a6Schristos 	}
22237c9f0a6Schristos 	if (addr != NULL)
22337c9f0a6Schristos 		addr->s_addr = htonl(val);
22437c9f0a6Schristos 	return (1);
22537c9f0a6Schristos }
226