xref: /titanic_50/usr/src/cmd/ipf/lib/common/inet_addr.c (revision ab25eeb551a4be927a4b6ae2cf8aff7ed17decb4)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * ++Copyright++ 1983, 1990, 1993
37c478bd9Sstevel@tonic-gate  * -
47c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1990, 1993
57c478bd9Sstevel@tonic-gate  *    The Regents of the University of California.  All rights reserved.
67c478bd9Sstevel@tonic-gate  *
77c478bd9Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
87c478bd9Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
97c478bd9Sstevel@tonic-gate  * are met:
107c478bd9Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
117c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
127c478bd9Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
137c478bd9Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
147c478bd9Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
157c478bd9Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
167c478bd9Sstevel@tonic-gate  *    must display the following acknowledgement:
177c478bd9Sstevel@tonic-gate  * 	This product includes software developed by the University of
187c478bd9Sstevel@tonic-gate  * 	California, Berkeley and its contributors.
197c478bd9Sstevel@tonic-gate  * 4. Neither the name of the University nor the names of its contributors
207c478bd9Sstevel@tonic-gate  *    may be used to endorse or promote products derived from this software
217c478bd9Sstevel@tonic-gate  *    without specific prior written permission.
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
247c478bd9Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
257c478bd9Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
267c478bd9Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
277c478bd9Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
287c478bd9Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
297c478bd9Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
307c478bd9Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
317c478bd9Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
327c478bd9Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
337c478bd9Sstevel@tonic-gate  * SUCH DAMAGE.
347c478bd9Sstevel@tonic-gate  * -
357c478bd9Sstevel@tonic-gate  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * Permission to use, copy, modify, and distribute this software for any
387c478bd9Sstevel@tonic-gate  * purpose with or without fee is hereby granted, provided that the above
397c478bd9Sstevel@tonic-gate  * copyright notice and this permission notice appear in all copies, and that
407c478bd9Sstevel@tonic-gate  * the name of Digital Equipment Corporation not be used in advertising or
417c478bd9Sstevel@tonic-gate  * publicity pertaining to distribution of the document or software without
427c478bd9Sstevel@tonic-gate  * specific, written prior permission.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
457c478bd9Sstevel@tonic-gate  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
467c478bd9Sstevel@tonic-gate  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
477c478bd9Sstevel@tonic-gate  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
487c478bd9Sstevel@tonic-gate  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
497c478bd9Sstevel@tonic-gate  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
507c478bd9Sstevel@tonic-gate  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
517c478bd9Sstevel@tonic-gate  * SOFTWARE.
527c478bd9Sstevel@tonic-gate  * -
537c478bd9Sstevel@tonic-gate  * --Copyright--
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #if !defined(lint)
577c478bd9Sstevel@tonic-gate static const char sccsid[] = "@(#)inet_addr.c	8.1 (Berkeley) 6/17/93";
58*ab25eeb5Syz155240 static const char rcsid[] = "@(#)$Id: inet_addr.c,v 1.8.2.3 2004/12/09 19:41:20 darrenr Exp $";
597c478bd9Sstevel@tonic-gate #endif /* LIBC_SCCS and not lint */
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #include <sys/param.h>
627c478bd9Sstevel@tonic-gate #include <netinet/in.h>
637c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
647c478bd9Sstevel@tonic-gate #include <ctype.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #ifndef	__P
677c478bd9Sstevel@tonic-gate # ifdef	__STDC__
687c478bd9Sstevel@tonic-gate #  define	__P(x)	x
697c478bd9Sstevel@tonic-gate # else
707c478bd9Sstevel@tonic-gate #  define	__P(x)	()
717c478bd9Sstevel@tonic-gate # endif
727c478bd9Sstevel@tonic-gate #endif
73*ab25eeb5Syz155240 #ifndef linux
747c478bd9Sstevel@tonic-gate int inet_aton __P((const char *, struct in_addr *));
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /*
77*ab25eeb5Syz155240  * Because the ctype(3) posix definition, if used "safely" in code everywhere,
78*ab25eeb5Syz155240  * would mean all normal code that walks through strings needed casts.  Yuck.
79*ab25eeb5Syz155240  */
80*ab25eeb5Syz155240 #define	ISALNUM(x)	isalnum((u_char)(x))
81*ab25eeb5Syz155240 #define	ISALPHA(x)	isalpha((u_char)(x))
82*ab25eeb5Syz155240 #define	ISASCII(x)	isascii((u_char)(x))
83*ab25eeb5Syz155240 #define	ISDIGIT(x)	isdigit((u_char)(x))
84*ab25eeb5Syz155240 #define	ISPRINT(x)	isprint((u_char)(x))
85*ab25eeb5Syz155240 #define	ISSPACE(x)	isspace((u_char)(x))
86*ab25eeb5Syz155240 #define	ISUPPER(x)	isupper((u_char)(x))
87*ab25eeb5Syz155240 #define	ISXDIGIT(x)	isxdigit((u_char)(x))
88*ab25eeb5Syz155240 #define	ISLOWER(x)	islower((u_char)(x))
89*ab25eeb5Syz155240 
90*ab25eeb5Syz155240 /*
917c478bd9Sstevel@tonic-gate  * Check whether "cp" is a valid ascii representation
927c478bd9Sstevel@tonic-gate  * of an Internet address and convert to a binary address.
937c478bd9Sstevel@tonic-gate  * Returns 1 if the address is valid, 0 if not.
947c478bd9Sstevel@tonic-gate  * This replaces inet_addr, the return value from which
957c478bd9Sstevel@tonic-gate  * cannot distinguish between failure and a local broadcast address.
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate int
inet_aton(cp,addr)987c478bd9Sstevel@tonic-gate inet_aton(cp, addr)
997c478bd9Sstevel@tonic-gate 	register const char *cp;
1007c478bd9Sstevel@tonic-gate 	struct in_addr *addr;
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	register u_long val;
1037c478bd9Sstevel@tonic-gate 	register int base, n;
1047c478bd9Sstevel@tonic-gate 	register char c;
1057c478bd9Sstevel@tonic-gate 	u_int parts[4];
1067c478bd9Sstevel@tonic-gate 	register u_int *pp = parts;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	c = *cp;
1097c478bd9Sstevel@tonic-gate 	for (;;) {
1107c478bd9Sstevel@tonic-gate 		/*
1117c478bd9Sstevel@tonic-gate 		 * Collect number up to ``.''.
1127c478bd9Sstevel@tonic-gate 		 * Values are specified as for C:
1137c478bd9Sstevel@tonic-gate 		 * 0x=hex, 0=octal, isdigit=decimal.
1147c478bd9Sstevel@tonic-gate 		 */
115*ab25eeb5Syz155240 		if (!ISDIGIT(c))
1167c478bd9Sstevel@tonic-gate 			return (0);
1177c478bd9Sstevel@tonic-gate 		val = 0; base = 10;
1187c478bd9Sstevel@tonic-gate 		if (c == '0') {
1197c478bd9Sstevel@tonic-gate 			c = *++cp;
1207c478bd9Sstevel@tonic-gate 			if (c == 'x' || c == 'X')
1217c478bd9Sstevel@tonic-gate 				base = 16, c = *++cp;
1227c478bd9Sstevel@tonic-gate 			else
1237c478bd9Sstevel@tonic-gate 				base = 8;
1247c478bd9Sstevel@tonic-gate 		}
1257c478bd9Sstevel@tonic-gate 		for (;;) {
126*ab25eeb5Syz155240 			if (ISASCII(c) && ISDIGIT(c)) {
1277c478bd9Sstevel@tonic-gate 				val = (val * base) + (c - '0');
1287c478bd9Sstevel@tonic-gate 				c = *++cp;
129*ab25eeb5Syz155240 			} else if (base == 16 && ISASCII(c) && ISXDIGIT(c)) {
1307c478bd9Sstevel@tonic-gate 				val = (val << 4) |
131*ab25eeb5Syz155240 					(c + 10 - (ISLOWER(c) ? 'a' : 'A'));
1327c478bd9Sstevel@tonic-gate 				c = *++cp;
1337c478bd9Sstevel@tonic-gate 			} else
1347c478bd9Sstevel@tonic-gate 				break;
1357c478bd9Sstevel@tonic-gate 		}
1367c478bd9Sstevel@tonic-gate 		if (c == '.') {
1377c478bd9Sstevel@tonic-gate 			/*
1387c478bd9Sstevel@tonic-gate 			 * Internet format:
1397c478bd9Sstevel@tonic-gate 			 *	a.b.c.d
1407c478bd9Sstevel@tonic-gate 			 *	a.b.c	(with c treated as 16 bits)
1417c478bd9Sstevel@tonic-gate 			 *	a.b	(with b treated as 24 bits)
1427c478bd9Sstevel@tonic-gate 			 */
1437c478bd9Sstevel@tonic-gate 			if (pp >= parts + 3)
1447c478bd9Sstevel@tonic-gate 				return (0);
1457c478bd9Sstevel@tonic-gate 			*pp++ = val;
1467c478bd9Sstevel@tonic-gate 			c = *++cp;
1477c478bd9Sstevel@tonic-gate 		} else
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 	/*
1517c478bd9Sstevel@tonic-gate 	 * Check for trailing characters.
1527c478bd9Sstevel@tonic-gate 	 */
153*ab25eeb5Syz155240 	if (c != '\0' && (!ISASCII(c) || !ISSPACE(c)))
1547c478bd9Sstevel@tonic-gate 		return (0);
1557c478bd9Sstevel@tonic-gate 	/*
1567c478bd9Sstevel@tonic-gate 	 * Concoct the address according to
1577c478bd9Sstevel@tonic-gate 	 * the number of parts specified.
1587c478bd9Sstevel@tonic-gate 	 */
1597c478bd9Sstevel@tonic-gate 	n = pp - parts + 1;
1607c478bd9Sstevel@tonic-gate 	switch (n) {
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	case 0:
1637c478bd9Sstevel@tonic-gate 		return (0);		/* initial nondigit */
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	case 1:				/* a -- 32 bits */
1667c478bd9Sstevel@tonic-gate 		break;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	case 2:				/* a.b -- 8.24 bits */
1697c478bd9Sstevel@tonic-gate 		if (val > 0xffffff)
1707c478bd9Sstevel@tonic-gate 			return (0);
1717c478bd9Sstevel@tonic-gate 		val |= parts[0] << 24;
1727c478bd9Sstevel@tonic-gate 		break;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	case 3:				/* a.b.c -- 8.8.16 bits */
1757c478bd9Sstevel@tonic-gate 		if (val > 0xffff)
1767c478bd9Sstevel@tonic-gate 			return (0);
1777c478bd9Sstevel@tonic-gate 		val |= (parts[0] << 24) | (parts[1] << 16);
1787c478bd9Sstevel@tonic-gate 		break;
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
1817c478bd9Sstevel@tonic-gate 		if (val > 0xff)
1827c478bd9Sstevel@tonic-gate 			return (0);
1837c478bd9Sstevel@tonic-gate 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
1847c478bd9Sstevel@tonic-gate 		break;
1857c478bd9Sstevel@tonic-gate 	}
1867c478bd9Sstevel@tonic-gate 	if (addr)
1877c478bd9Sstevel@tonic-gate 		addr->s_addr = htonl(val);
1887c478bd9Sstevel@tonic-gate 	return (1);
1897c478bd9Sstevel@tonic-gate }
190*ab25eeb5Syz155240 #endif
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate /* these are compatibility routines, not needed on recent BSD releases */
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Ascii internet address interpretation routine.
1967c478bd9Sstevel@tonic-gate  * The value returned is in network order.
1977c478bd9Sstevel@tonic-gate  */
198*ab25eeb5Syz155240 #if 0
1997c478bd9Sstevel@tonic-gate inet_addr(cp)
200*ab25eeb5Syz155240 	const char *cp;
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	struct in_addr val;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	if (inet_aton(cp, &val))
2057c478bd9Sstevel@tonic-gate 		return (val.s_addr);
2067c478bd9Sstevel@tonic-gate 	return (0xffffffff);
2077c478bd9Sstevel@tonic-gate }
208*ab25eeb5Syz155240 #endif
209