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