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