1156952Sume/*
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";
69170244Sumestatic const char rcsid[] = "$Id: inet_addr.c,v 1.4.18.1 2005/04/27 05:00:52 sra Exp $";
70156952Sume#endif /* LIBC_SCCS and not lint */
71156956Sume#include <sys/cdefs.h>
72156956Sume__FBSDID("$FreeBSD$");
73156952Sume
74156952Sume#include "port_before.h"
75156952Sume
76156952Sume#include <sys/types.h>
77156952Sume#include <sys/param.h>
78156952Sume
79156952Sume#include <netinet/in.h>
80156952Sume#include <arpa/inet.h>
81156952Sume
82156952Sume#include <ctype.h>
83156952Sume
84156952Sume#include "port_after.h"
85156952Sume
86170244Sume/*%
87156952Sume * Ascii internet address interpretation routine.
88156952Sume * The value returned is in network order.
89156952Sume */
90156956Sumein_addr_t		/* XXX should be struct in_addr :( */
91156952Sumeinet_addr(const char *cp) {
92156952Sume	struct in_addr val;
93156952Sume
94156952Sume	if (inet_aton(cp, &val))
95156952Sume		return (val.s_addr);
96156952Sume	return (INADDR_NONE);
97156952Sume}
98156952Sume
99170244Sume/*%
100156952Sume * Check whether "cp" is a valid ascii representation
101156952Sume * of an Internet address and convert to a binary address.
102156952Sume * Returns 1 if the address is valid, 0 if not.
103156952Sume * This replaces inet_addr, the return value from which
104156952Sume * cannot distinguish between failure and a local broadcast address.
105156952Sume */
106156952Sumeint
107156952Sumeinet_aton(const char *cp, struct in_addr *addr) {
108156952Sume	u_long val;
109156952Sume	int base, n;
110156952Sume	char c;
111156952Sume	u_int8_t parts[4];
112156952Sume	u_int8_t *pp = parts;
113156952Sume	int digit;
114156952Sume
115156952Sume	c = *cp;
116156952Sume	for (;;) {
117156952Sume		/*
118156952Sume		 * Collect number up to ``.''.
119156952Sume		 * Values are specified as for C:
120156952Sume		 * 0x=hex, 0=octal, isdigit=decimal.
121156952Sume		 */
122156952Sume		if (!isdigit((unsigned char)c))
123156952Sume			return (0);
124156952Sume		val = 0; base = 10; digit = 0;
125156952Sume		if (c == '0') {
126156952Sume			c = *++cp;
127156952Sume			if (c == 'x' || c == 'X')
128156952Sume				base = 16, c = *++cp;
129156952Sume			else {
130156952Sume				base = 8;
131156952Sume				digit = 1 ;
132156952Sume			}
133156952Sume		}
134156952Sume		for (;;) {
135156952Sume			if (isascii(c) && isdigit((unsigned char)c)) {
136156952Sume				if (base == 8 && (c == '8' || c == '9'))
137156952Sume					return (0);
138156952Sume				val = (val * base) + (c - '0');
139156952Sume				c = *++cp;
140156952Sume				digit = 1;
141156952Sume			} else if (base == 16 && isascii(c) &&
142156952Sume				   isxdigit((unsigned char)c)) {
143156952Sume				val = (val << 4) |
144156952Sume					(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
145156952Sume				c = *++cp;
146156952Sume				digit = 1;
147156952Sume			} else
148156952Sume				break;
149156952Sume		}
150156952Sume		if (c == '.') {
151156952Sume			/*
152156952Sume			 * Internet format:
153156952Sume			 *	a.b.c.d
154156952Sume			 *	a.b.c	(with c treated as 16 bits)
155156952Sume			 *	a.b	(with b treated as 24 bits)
156156952Sume			 */
157156952Sume			if (pp >= parts + 3 || val > 0xffU)
158156952Sume				return (0);
159156952Sume			*pp++ = val;
160156952Sume			c = *++cp;
161156952Sume		} else
162156952Sume			break;
163156952Sume	}
164156952Sume	/*
165156952Sume	 * Check for trailing characters.
166156952Sume	 */
167156952Sume	if (c != '\0' && (!isascii(c) || !isspace((unsigned char)c)))
168156952Sume		return (0);
169156952Sume	/*
170156952Sume	 * Did we get a valid digit?
171156952Sume	 */
172156952Sume	if (!digit)
173156952Sume		return (0);
174156952Sume	/*
175156952Sume	 * Concoct the address according to
176156952Sume	 * the number of parts specified.
177156952Sume	 */
178156952Sume	n = pp - parts + 1;
179156952Sume	switch (n) {
180170244Sume	case 1:				/*%< a -- 32 bits */
181156952Sume		break;
182156952Sume
183170244Sume	case 2:				/*%< a.b -- 8.24 bits */
184156952Sume		if (val > 0xffffffU)
185156952Sume			return (0);
186156952Sume		val |= parts[0] << 24;
187156952Sume		break;
188156952Sume
189170244Sume	case 3:				/*%< a.b.c -- 8.8.16 bits */
190156952Sume		if (val > 0xffffU)
191156952Sume			return (0);
192156952Sume		val |= (parts[0] << 24) | (parts[1] << 16);
193156952Sume		break;
194156952Sume
195170244Sume	case 4:				/*%< a.b.c.d -- 8.8.8.8 bits */
196156952Sume		if (val > 0xffU)
197156952Sume			return (0);
198156952Sume		val |= (parts[0] << 24) | (parts[1] << 16) | (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