1331722Seadler/*
2156952Sume * Copyright (c) 1983, 1990, 1993
3156952Sume *    The Regents of the University of California.  All rights reserved.
4156952Sume *
5156952Sume * Redistribution and use in source and binary forms, with or without
6156952Sume * modification, are permitted provided that the following conditions
7156952Sume * are met:
8156952Sume * 1. Redistributions of source code must retain the above copyright
9156952Sume *    notice, this list of conditions and the following disclaimer.
10156952Sume * 2. Redistributions in binary form must reproduce the above copyright
11156952Sume *    notice, this list of conditions and the following disclaimer in the
12156952Sume *    documentation and/or other materials provided with the distribution.
13156952Sume * 4. Neither the name of the University nor the names of its contributors
14156952Sume *    may be used to endorse or promote products derived from this software
15156952Sume *    without specific prior written permission.
16156952Sume *
17156952Sume * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18156952Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19156952Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20156952Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21156952Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22156952Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23156952Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24156952Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25156952Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26156952Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27156952Sume * SUCH DAMAGE.
28156952Sume */
29156952Sume
30156952Sume/*
31156952Sume * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32156952Sume *
33156952Sume * Permission to use, copy, modify, and distribute this software for any
34156952Sume * purpose with or without fee is hereby granted, provided that the above
35156952Sume * copyright notice and this permission notice appear in all copies, and that
36156952Sume * the name of Digital Equipment Corporation not be used in advertising or
37156952Sume * publicity pertaining to distribution of the document or software without
38156952Sume * specific, written prior permission.
39156952Sume *
40156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41156952Sume * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42156952Sume * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43156952Sume * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44156952Sume * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45156952Sume * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46156952Sume * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47156952Sume * SOFTWARE.
48156952Sume */
49156952Sume
50156952Sume/*
51156952Sume * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
52156952Sume * Portions Copyright (c) 1996-1999 by Internet Software Consortium.
53156952Sume *
54156952Sume * Permission to use, copy, modify, and distribute this software for any
55156952Sume * purpose with or without fee is hereby granted, provided that the above
56156952Sume * copyright notice and this permission notice appear in all copies.
57156952Sume *
58156952Sume * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
59156952Sume * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
60156952Sume * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
61156952Sume * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
62156952Sume * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
63156952Sume * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
64156952Sume * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
65156952Sume */
66156952Sume
67156952Sume#if defined(LIBC_SCCS) && !defined(lint)
68156952Sumestatic const char sccsid[] = "@(#)inet_addr.c	8.1 (Berkeley) 6/17/93";
69269867Sumestatic const char rcsid[] = "$Id: inet_addr.c,v 1.5 2005/04/27 04:56:19 sra Exp $";
70156952Sume#endif /* LIBC_SCCS and not lint */
71156956Sume#include <sys/cdefs.h>
72156956Sume__FBSDID("$FreeBSD: stable/11/lib/libc/inet/inet_addr.c 338124 2018-08-21 01:17:28Z pfg $");
73156952Sume
74156952Sume#include "port_before.h"
75156952Sume
76156952Sume#include <sys/param.h>
77156952Sume
78156952Sume#include <netinet/in.h>
79156952Sume#include <arpa/inet.h>
80156952Sume
81156952Sume#include <ctype.h>
82156952Sume
83156952Sume#include "port_after.h"
84156952Sume
85170244Sume/*%
86156952Sume * Ascii internet address interpretation routine.
87156952Sume * The value returned is in network order.
88156952Sume */
89156956Sumein_addr_t		/* XXX should be struct in_addr :( */
90156952Sumeinet_addr(const char *cp) {
91156952Sume	struct in_addr val;
92156952Sume
93156952Sume	if (inet_aton(cp, &val))
94156952Sume		return (val.s_addr);
95156952Sume	return (INADDR_NONE);
96156952Sume}
97156952Sume
98170244Sume/*%
99156952Sume * Check whether "cp" is a valid ascii representation
100156952Sume * of an Internet address and convert to a binary address.
101156952Sume * Returns 1 if the address is valid, 0 if not.
102156952Sume * This replaces inet_addr, the return value from which
103156952Sume * cannot distinguish between failure and a local broadcast address.
104156952Sume */
105156952Sumeint
106156952Sumeinet_aton(const char *cp, struct in_addr *addr) {
107156952Sume	u_long val;
108156952Sume	int base, n;
109156952Sume	char c;
110156952Sume	u_int8_t parts[4];
111156952Sume	u_int8_t *pp = parts;
112156952Sume	int digit;
113156952Sume
114156952Sume	c = *cp;
115156952Sume	for (;;) {
116156952Sume		/*
117156952Sume		 * Collect number up to ``.''.
118156952Sume		 * Values are specified as for C:
119156952Sume		 * 0x=hex, 0=octal, isdigit=decimal.
120156952Sume		 */
121156952Sume		if (!isdigit((unsigned char)c))
122156952Sume			return (0);
123156952Sume		val = 0; base = 10; digit = 0;
124156952Sume		if (c == '0') {
125156952Sume			c = *++cp;
126156952Sume			if (c == 'x' || c == 'X')
127156952Sume				base = 16, c = *++cp;
128156952Sume			else {
129156952Sume				base = 8;
130156952Sume				digit = 1 ;
131156952Sume			}
132156952Sume		}
133156952Sume		for (;;) {
134156952Sume			if (isascii(c) && isdigit((unsigned char)c)) {
135156952Sume				if (base == 8 && (c == '8' || c == '9'))
136156952Sume					return (0);
137156952Sume				val = (val * base) + (c - '0');
138156952Sume				c = *++cp;
139156952Sume				digit = 1;
140156952Sume			} else if (base == 16 && isascii(c) &&
141156952Sume				   isxdigit((unsigned char)c)) {
142156952Sume				val = (val << 4) |
143156952Sume					(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
144156952Sume				c = *++cp;
145156952Sume				digit = 1;
146156952Sume			} else
147156952Sume				break;
148156952Sume		}
149156952Sume		if (c == '.') {
150156952Sume			/*
151156952Sume			 * Internet format:
152156952Sume			 *	a.b.c.d
153156952Sume			 *	a.b.c	(with c treated as 16 bits)
154156952Sume			 *	a.b	(with b treated as 24 bits)
155156952Sume			 */
156156952Sume			if (pp >= parts + 3 || val > 0xffU)
157156952Sume				return (0);
158156952Sume			*pp++ = val;
159156952Sume			c = *++cp;
160156952Sume		} else
161156952Sume			break;
162156952Sume	}
163156952Sume	/*
164156952Sume	 * Check for trailing characters.
165156952Sume	 */
166156952Sume	if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))
167156952Sume		return (0);
168156952Sume	/*
169156952Sume	 * Did we get a valid digit?
170156952Sume	 */
171156952Sume	if (!digit)
172156952Sume		return (0);
173156952Sume	/*
174156952Sume	 * Concoct the address according to
175156952Sume	 * the number of parts specified.
176156952Sume	 */
177156952Sume	n = pp - parts + 1;
178156952Sume	switch (n) {
179170244Sume	case 1:				/*%< a -- 32 bits */
180156952Sume		break;
181156952Sume
182170244Sume	case 2:				/*%< a.b -- 8.24 bits */
183156952Sume		if (val > 0xffffffU)
184156952Sume			return (0);
185338124Spfg		val |= (uint32_t)parts[0] << 24;
186156952Sume		break;
187156952Sume
188170244Sume	case 3:				/*%< a.b.c -- 8.8.16 bits */
189156952Sume		if (val > 0xffffU)
190156952Sume			return (0);
191338124Spfg		val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16);
192156952Sume		break;
193156952Sume
194170244Sume	case 4:				/*%< a.b.c.d -- 8.8.8.8 bits */
195156952Sume		if (val > 0xffU)
196156952Sume			return (0);
197338124Spfg		val |= ((uint32_t)parts[0] << 24) | (parts[1] << 16) |
198338124Spfg		    (parts[2] << 8);
199156952Sume		break;
200156952Sume	}
201156952Sume	if (addr != NULL)
202156952Sume		addr->s_addr = htonl(val);
203156952Sume	return (1);
204156952Sume}
205156956Sume
206156956Sume/*
207156956Sume * Weak aliases for applications that use certain private entry points,
208156956Sume * and fail to include <arpa/inet.h>.
209156956Sume */
210156956Sume#undef inet_addr
211156956Sume__weak_reference(__inet_addr, inet_addr);
212156956Sume#undef inet_aton
213156956Sume__weak_reference(__inet_aton, inet_aton);
214170244Sume
215170244Sume/*! \file */
216