xref: /openbsd/lib/libc/net/inet_addr.c (revision b2d13c95)
1*b2d13c95Sguenther /*	$OpenBSD: inet_addr.c,v 1.12 2015/09/13 21:36:08 guenther Exp $	*/
27949d54bSdownsj 
3df930be7Sderaadt /*
47949d54bSdownsj  * ++Copyright++ 1983, 1990, 1993
57949d54bSdownsj  * -
6df930be7Sderaadt  * Copyright (c) 1983, 1990, 1993
7df930be7Sderaadt  *    The Regents of the University of California.  All rights reserved.
8df930be7Sderaadt  *
9df930be7Sderaadt  * Redistribution and use in source and binary forms, with or without
10df930be7Sderaadt  * modification, are permitted provided that the following conditions
11df930be7Sderaadt  * are met:
12df930be7Sderaadt  * 1. Redistributions of source code must retain the above copyright
13df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer.
14df930be7Sderaadt  * 2. Redistributions in binary form must reproduce the above copyright
15df930be7Sderaadt  *    notice, this list of conditions and the following disclaimer in the
16df930be7Sderaadt  *    documentation and/or other materials provided with the distribution.
176580fee3Smillert  * 3. Neither the name of the University nor the names of its contributors
18df930be7Sderaadt  *    may be used to endorse or promote products derived from this software
19df930be7Sderaadt  *    without specific prior written permission.
20df930be7Sderaadt  *
21df930be7Sderaadt  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22df930be7Sderaadt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23df930be7Sderaadt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24df930be7Sderaadt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25df930be7Sderaadt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26df930be7Sderaadt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27df930be7Sderaadt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28df930be7Sderaadt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29df930be7Sderaadt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30df930be7Sderaadt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31df930be7Sderaadt  * SUCH DAMAGE.
327949d54bSdownsj  * -
337949d54bSdownsj  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
347949d54bSdownsj  *
357949d54bSdownsj  * Permission to use, copy, modify, and distribute this software for any
367949d54bSdownsj  * purpose with or without fee is hereby granted, provided that the above
377949d54bSdownsj  * copyright notice and this permission notice appear in all copies, and that
387949d54bSdownsj  * the name of Digital Equipment Corporation not be used in advertising or
397949d54bSdownsj  * publicity pertaining to distribution of the document or software without
407949d54bSdownsj  * specific, written prior permission.
417949d54bSdownsj  *
427949d54bSdownsj  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
437949d54bSdownsj  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
447949d54bSdownsj  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
457949d54bSdownsj  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
467949d54bSdownsj  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
477949d54bSdownsj  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
487949d54bSdownsj  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
497949d54bSdownsj  * SOFTWARE.
507949d54bSdownsj  * -
517949d54bSdownsj  * --Copyright--
52df930be7Sderaadt  */
53df930be7Sderaadt 
547949d54bSdownsj #include <sys/types.h>
55df930be7Sderaadt #include <netinet/in.h>
56df930be7Sderaadt #include <arpa/inet.h>
57df930be7Sderaadt #include <ctype.h>
58df930be7Sderaadt 
59df930be7Sderaadt /*
60df930be7Sderaadt  * Ascii internet address interpretation routine.
61df930be7Sderaadt  * The value returned is in network order.
62df930be7Sderaadt  */
6332210aa3Smillert in_addr_t
inet_addr(const char * cp)64db5b349cSotto inet_addr(const char *cp)
65df930be7Sderaadt {
66df930be7Sderaadt 	struct in_addr val;
67df930be7Sderaadt 
68df930be7Sderaadt 	if (inet_aton(cp, &val))
69df930be7Sderaadt 		return (val.s_addr);
70df930be7Sderaadt 	return (INADDR_NONE);
71df930be7Sderaadt }
72df930be7Sderaadt 
73df930be7Sderaadt /*
74df930be7Sderaadt  * Check whether "cp" is a valid ascii representation
75df930be7Sderaadt  * of an Internet address and convert to a binary address.
76df930be7Sderaadt  * Returns 1 if the address is valid, 0 if not.
77df930be7Sderaadt  * This replaces inet_addr, the return value from which
78df930be7Sderaadt  * cannot distinguish between failure and a local broadcast address.
79df930be7Sderaadt  */
80df930be7Sderaadt int
inet_aton(const char * cp,struct in_addr * addr)81db5b349cSotto inet_aton(const char *cp, struct in_addr *addr)
82df930be7Sderaadt {
83db5b349cSotto 	in_addr_t val;
84db5b349cSotto 	int base, n;
85db5b349cSotto 	char c;
86df930be7Sderaadt 	u_int parts[4];
87db5b349cSotto 	u_int *pp = parts;
88df930be7Sderaadt 
897949d54bSdownsj 	c = *cp;
90df930be7Sderaadt 	for (;;) {
91df930be7Sderaadt 		/*
92df930be7Sderaadt 		 * Collect number up to ``.''.
93df930be7Sderaadt 		 * Values are specified as for C:
947949d54bSdownsj 		 * 0x=hex, 0=octal, isdigit=decimal.
95df930be7Sderaadt 		 */
96dfe5467eSderaadt 		if (!isdigit((unsigned char)c))
977949d54bSdownsj 			return (0);
98df930be7Sderaadt 		val = 0; base = 10;
997949d54bSdownsj 		if (c == '0') {
1007949d54bSdownsj 			c = *++cp;
1017949d54bSdownsj 			if (c == 'x' || c == 'X')
1027949d54bSdownsj 				base = 16, c = *++cp;
103df930be7Sderaadt 			else
104df930be7Sderaadt 				base = 8;
105df930be7Sderaadt 		}
1067949d54bSdownsj 		for (;;) {
107dfe5467eSderaadt 			if (isascii((unsigned char)c) &&
108dfe5467eSderaadt 			    isdigit((unsigned char)c)) {
109df930be7Sderaadt 				val = (val * base) + (c - '0');
1107949d54bSdownsj 				c = *++cp;
111dfe5467eSderaadt 			} else if (base == 16 &&
112dfe5467eSderaadt 			    isascii((unsigned char)c) &&
113dfe5467eSderaadt 			    isxdigit((unsigned char)c)) {
1147949d54bSdownsj 				val = (val << 4) |
115dfe5467eSderaadt 				    (c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
1167949d54bSdownsj 				c = *++cp;
1177949d54bSdownsj 			} else
118df930be7Sderaadt 				break;
119df930be7Sderaadt 		}
1207949d54bSdownsj 		if (c == '.') {
121df930be7Sderaadt 			/*
122df930be7Sderaadt 			 * Internet format:
123df930be7Sderaadt 			 *	a.b.c.d
1247949d54bSdownsj 			 *	a.b.c	(with c treated as 16 bits)
125df930be7Sderaadt 			 *	a.b	(with b treated as 24 bits)
126df930be7Sderaadt 			 */
1277949d54bSdownsj 			if (pp >= parts + 3)
128df930be7Sderaadt 				return (0);
1297949d54bSdownsj 			*pp++ = val;
1307949d54bSdownsj 			c = *++cp;
131df930be7Sderaadt 		} else
132df930be7Sderaadt 			break;
133df930be7Sderaadt 	}
134df930be7Sderaadt 	/*
135df930be7Sderaadt 	 * Check for trailing characters.
136df930be7Sderaadt 	 */
137dfe5467eSderaadt 	if (c != '\0' &&
138dfe5467eSderaadt 	    (!isascii((unsigned char)c) || !isspace((unsigned char)c)))
139df930be7Sderaadt 		return (0);
140df930be7Sderaadt 	/*
141df930be7Sderaadt 	 * Concoct the address according to
142df930be7Sderaadt 	 * the number of parts specified.
143df930be7Sderaadt 	 */
144df930be7Sderaadt 	n = pp - parts + 1;
145df930be7Sderaadt 	switch (n) {
146df930be7Sderaadt 
1471ddd5cc1Sdm 	case 0:
1481ddd5cc1Sdm 		return (0);		/* initial nondigit */
1491ddd5cc1Sdm 
150df930be7Sderaadt 	case 1:				/* a -- 32 bits */
151df930be7Sderaadt 		break;
152df930be7Sderaadt 
153df930be7Sderaadt 	case 2:				/* a.b -- 8.24 bits */
154c28e4a3aSyanick 		if ((val > 0xffffff) || (parts[0] > 0xff))
155df930be7Sderaadt 			return (0);
156df930be7Sderaadt 		val |= parts[0] << 24;
157df930be7Sderaadt 		break;
158df930be7Sderaadt 
159df930be7Sderaadt 	case 3:				/* a.b.c -- 8.8.16 bits */
160c28e4a3aSyanick 		if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
161df930be7Sderaadt 			return (0);
162df930be7Sderaadt 		val |= (parts[0] << 24) | (parts[1] << 16);
163df930be7Sderaadt 		break;
164df930be7Sderaadt 
165df930be7Sderaadt 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
166c28e4a3aSyanick 		if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
167df930be7Sderaadt 			return (0);
168df930be7Sderaadt 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
169df930be7Sderaadt 		break;
170df930be7Sderaadt 	}
171df930be7Sderaadt 	if (addr)
172df930be7Sderaadt 		addr->s_addr = htonl(val);
173df930be7Sderaadt 	return (1);
174df930be7Sderaadt }
175*b2d13c95Sguenther DEF_WEAK(inet_aton);
176