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