xref: /netbsd-src/external/bsd/unbound/dist/compat/inet_aton.c (revision 3b6c3722d8f990f9a667d638078aee8ccdc3c0f3)
1*3b6c3722Schristos /* From openssh4.3p2 compat/inet_aton.c */
2*3b6c3722Schristos /*
3*3b6c3722Schristos  * Copyright (c) 1983, 1990, 1993
4*3b6c3722Schristos  *    The Regents of the University of California.  All rights reserved.
5*3b6c3722Schristos  *
6*3b6c3722Schristos  * Redistribution and use in source and binary forms, with or without
7*3b6c3722Schristos  * modification, are permitted provided that the following conditions
8*3b6c3722Schristos  * are met:
9*3b6c3722Schristos  * 1. Redistributions of source code must retain the above copyright
10*3b6c3722Schristos  *    notice, this list of conditions and the following disclaimer.
11*3b6c3722Schristos  * 2. Redistributions in binary form must reproduce the above copyright
12*3b6c3722Schristos  *    notice, this list of conditions and the following disclaimer in the
13*3b6c3722Schristos  *    documentation and/or other materials provided with the distribution.
14*3b6c3722Schristos  * 3. Neither the name of the University nor the names of its contributors
15*3b6c3722Schristos  *    may be used to endorse or promote products derived from this software
16*3b6c3722Schristos  *    without specific prior written permission.
17*3b6c3722Schristos  *
18*3b6c3722Schristos  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19*3b6c3722Schristos  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20*3b6c3722Schristos  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21*3b6c3722Schristos  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22*3b6c3722Schristos  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23*3b6c3722Schristos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24*3b6c3722Schristos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25*3b6c3722Schristos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26*3b6c3722Schristos  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27*3b6c3722Schristos  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28*3b6c3722Schristos  * SUCH DAMAGE.
29*3b6c3722Schristos  * -
30*3b6c3722Schristos  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
31*3b6c3722Schristos  *
32*3b6c3722Schristos  * Permission to use, copy, modify, and distribute this software for any
33*3b6c3722Schristos  * purpose with or without fee is hereby granted, provided that the above
34*3b6c3722Schristos  * copyright notice and this permission notice appear in all copies, and that
35*3b6c3722Schristos  * the name of Digital Equipment Corporation not be used in advertising or
36*3b6c3722Schristos  * publicity pertaining to distribution of the document or software without
37*3b6c3722Schristos  * specific, written prior permission.
38*3b6c3722Schristos  *
39*3b6c3722Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
40*3b6c3722Schristos  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
41*3b6c3722Schristos  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
42*3b6c3722Schristos  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
43*3b6c3722Schristos  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
44*3b6c3722Schristos  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
45*3b6c3722Schristos  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
46*3b6c3722Schristos  * SOFTWARE.
47*3b6c3722Schristos  * -
48*3b6c3722Schristos  * --Copyright--
49*3b6c3722Schristos  */
50*3b6c3722Schristos 
51*3b6c3722Schristos /* OPENBSD ORIGINAL: lib/libc/net/inet_addr.c */
52*3b6c3722Schristos 
53*3b6c3722Schristos #include <config.h>
54*3b6c3722Schristos 
55*3b6c3722Schristos #if !defined(HAVE_INET_ATON)
56*3b6c3722Schristos 
57*3b6c3722Schristos #include <sys/types.h>
58*3b6c3722Schristos #include <sys/param.h>
59*3b6c3722Schristos #ifdef HAVE_NETINET_IN_H
60*3b6c3722Schristos #include <netinet/in.h>
61*3b6c3722Schristos #endif
62*3b6c3722Schristos #ifdef HAVE_ARPA_INET_H
63*3b6c3722Schristos #include <arpa/inet.h>
64*3b6c3722Schristos #endif
65*3b6c3722Schristos #include <ctype.h>
66*3b6c3722Schristos 
67*3b6c3722Schristos #if 0
68*3b6c3722Schristos /*
69*3b6c3722Schristos  * Ascii internet address interpretation routine.
70*3b6c3722Schristos  * The value returned is in network order.
71*3b6c3722Schristos  */
72*3b6c3722Schristos in_addr_t
73*3b6c3722Schristos inet_addr(const char *cp)
74*3b6c3722Schristos {
75*3b6c3722Schristos 	struct in_addr val;
76*3b6c3722Schristos 
77*3b6c3722Schristos 	if (inet_aton(cp, &val))
78*3b6c3722Schristos 		return (val.s_addr);
79*3b6c3722Schristos 	return (INADDR_NONE);
80*3b6c3722Schristos }
81*3b6c3722Schristos #endif
82*3b6c3722Schristos 
83*3b6c3722Schristos /*
84*3b6c3722Schristos  * Check whether "cp" is a valid ascii representation
85*3b6c3722Schristos  * of an Internet address and convert to a binary address.
86*3b6c3722Schristos  * Returns 1 if the address is valid, 0 if not.
87*3b6c3722Schristos  * This replaces inet_addr, the return value from which
88*3b6c3722Schristos  * cannot distinguish between failure and a local broadcast address.
89*3b6c3722Schristos  */
90*3b6c3722Schristos int
inet_aton(const char * cp,struct in_addr * addr)91*3b6c3722Schristos inet_aton(const char *cp, struct in_addr *addr)
92*3b6c3722Schristos {
93*3b6c3722Schristos 	uint32_t val;
94*3b6c3722Schristos 	int base, n;
95*3b6c3722Schristos 	char c;
96*3b6c3722Schristos 	unsigned int parts[4];
97*3b6c3722Schristos 	unsigned int *pp = parts;
98*3b6c3722Schristos 
99*3b6c3722Schristos 	c = *cp;
100*3b6c3722Schristos 	for (;;) {
101*3b6c3722Schristos 		/*
102*3b6c3722Schristos 		 * Collect number up to ``.''.
103*3b6c3722Schristos 		 * Values are specified as for C:
104*3b6c3722Schristos 		 * 0x=hex, 0=octal, isdigit=decimal.
105*3b6c3722Schristos 		 */
106*3b6c3722Schristos 		if (!isdigit((unsigned char)c))
107*3b6c3722Schristos 			return (0);
108*3b6c3722Schristos 		val = 0; base = 10;
109*3b6c3722Schristos 		if (c == '0') {
110*3b6c3722Schristos 			c = *++cp;
111*3b6c3722Schristos 			if (c == 'x' || c == 'X')
112*3b6c3722Schristos 				base = 16, c = *++cp;
113*3b6c3722Schristos 			else
114*3b6c3722Schristos 				base = 8;
115*3b6c3722Schristos 		}
116*3b6c3722Schristos 		for (;;) {
117*3b6c3722Schristos 			if (isascii((unsigned char)c) && isdigit((unsigned char)c)) {
118*3b6c3722Schristos 				val = (val * base) + (c - '0');
119*3b6c3722Schristos 				c = *++cp;
120*3b6c3722Schristos 			} else if (base == 16 && isascii((unsigned char)c) && isxdigit((unsigned char)c)) {
121*3b6c3722Schristos 				val = (val << 4) |
122*3b6c3722Schristos 					(c + 10 - (islower((unsigned char)c) ? 'a' : 'A'));
123*3b6c3722Schristos 				c = *++cp;
124*3b6c3722Schristos 			} else
125*3b6c3722Schristos 				break;
126*3b6c3722Schristos 		}
127*3b6c3722Schristos 		if (c == '.') {
128*3b6c3722Schristos 			/*
129*3b6c3722Schristos 			 * Internet format:
130*3b6c3722Schristos 			 *	a.b.c.d
131*3b6c3722Schristos 			 *	a.b.c	(with c treated as 16 bits)
132*3b6c3722Schristos 			 *	a.b	(with b treated as 24 bits)
133*3b6c3722Schristos 			 */
134*3b6c3722Schristos 			if (pp >= parts + 3)
135*3b6c3722Schristos 				return (0);
136*3b6c3722Schristos 			*pp++ = val;
137*3b6c3722Schristos 			c = *++cp;
138*3b6c3722Schristos 		} else
139*3b6c3722Schristos 			break;
140*3b6c3722Schristos 	}
141*3b6c3722Schristos 	/*
142*3b6c3722Schristos 	 * Check for trailing characters.
143*3b6c3722Schristos 	 */
144*3b6c3722Schristos 	if (c != '\0' && (!isascii((unsigned char)c) || !isspace((unsigned char)c)))
145*3b6c3722Schristos 		return (0);
146*3b6c3722Schristos 	/*
147*3b6c3722Schristos 	 * Concoct the address according to
148*3b6c3722Schristos 	 * the number of parts specified.
149*3b6c3722Schristos 	 */
150*3b6c3722Schristos 	n = pp - parts + 1;
151*3b6c3722Schristos 	switch (n) {
152*3b6c3722Schristos 
153*3b6c3722Schristos 	case 0:
154*3b6c3722Schristos 		return (0);		/* initial nondigit */
155*3b6c3722Schristos 
156*3b6c3722Schristos 	case 1:				/* a -- 32 bits */
157*3b6c3722Schristos 		break;
158*3b6c3722Schristos 
159*3b6c3722Schristos 	case 2:				/* a.b -- 8.24 bits */
160*3b6c3722Schristos 		if ((val > 0xffffff) || (parts[0] > 0xff))
161*3b6c3722Schristos 			return (0);
162*3b6c3722Schristos 		val |= parts[0] << 24;
163*3b6c3722Schristos 		break;
164*3b6c3722Schristos 
165*3b6c3722Schristos 	case 3:				/* a.b.c -- 8.8.16 bits */
166*3b6c3722Schristos 		if ((val > 0xffff) || (parts[0] > 0xff) || (parts[1] > 0xff))
167*3b6c3722Schristos 			return (0);
168*3b6c3722Schristos 		val |= (parts[0] << 24) | (parts[1] << 16);
169*3b6c3722Schristos 		break;
170*3b6c3722Schristos 
171*3b6c3722Schristos 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
172*3b6c3722Schristos 		if ((val > 0xff) || (parts[0] > 0xff) || (parts[1] > 0xff) || (parts[2] > 0xff))
173*3b6c3722Schristos 			return (0);
174*3b6c3722Schristos 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
175*3b6c3722Schristos 		break;
176*3b6c3722Schristos 	}
177*3b6c3722Schristos 	if (addr)
178*3b6c3722Schristos 		addr->s_addr = htonl(val);
179*3b6c3722Schristos 	return (1);
180*3b6c3722Schristos }
181*3b6c3722Schristos 
182*3b6c3722Schristos #endif /* !defined(HAVE_INET_ATON) */
183