xref: /freebsd-src/sbin/ipf/libipf/inet_addr.c (revision 51e16cb8fc536913f490ac6bc9c17e92ebd0411b)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * ++Copyright++ 1983, 1990, 1993
441edb306SCy Schubert  * -
541edb306SCy Schubert  * Copyright (c) 1983, 1990, 1993
641edb306SCy Schubert  *    The Regents of the University of California.  All rights reserved.
741edb306SCy Schubert  *
841edb306SCy Schubert  * Redistribution and use in source and binary forms, with or without
941edb306SCy Schubert  * modification, are permitted provided that the following conditions
1041edb306SCy Schubert  * are met:
1141edb306SCy Schubert  * 1. Redistributions of source code must retain the above copyright
1241edb306SCy Schubert  *    notice, this list of conditions and the following disclaimer.
1341edb306SCy Schubert  * 2. Redistributions in binary form must reproduce the above copyright
1441edb306SCy Schubert  *    notice, this list of conditions and the following disclaimer in the
1541edb306SCy Schubert  *    documentation and/or other materials provided with the distribution.
1641edb306SCy Schubert  * 3. All advertising materials mentioning features or use of this software
1741edb306SCy Schubert  *    must display the following acknowledgement:
1841edb306SCy Schubert  * 	This product includes software developed by the University of
1941edb306SCy Schubert  * 	California, Berkeley and its contributors.
2041edb306SCy Schubert  * 4. Neither the name of the University nor the names of its contributors
2141edb306SCy Schubert  *    may be used to endorse or promote products derived from this software
2241edb306SCy Schubert  *    without specific prior written permission.
2341edb306SCy Schubert  *
2441edb306SCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2541edb306SCy Schubert  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2641edb306SCy Schubert  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2741edb306SCy Schubert  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2841edb306SCy Schubert  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2941edb306SCy Schubert  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3041edb306SCy Schubert  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3141edb306SCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3241edb306SCy Schubert  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3341edb306SCy Schubert  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3441edb306SCy Schubert  * SUCH DAMAGE.
3541edb306SCy Schubert  * -
3641edb306SCy Schubert  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
3741edb306SCy Schubert  *
3841edb306SCy Schubert  * Permission to use, copy, modify, and distribute this software for any
3941edb306SCy Schubert  * purpose with or without fee is hereby granted, provided that the above
4041edb306SCy Schubert  * copyright notice and this permission notice appear in all copies, and that
4141edb306SCy Schubert  * the name of Digital Equipment Corporation not be used in advertising or
4241edb306SCy Schubert  * publicity pertaining to distribution of the document or software without
4341edb306SCy Schubert  * specific, written prior permission.
4441edb306SCy Schubert  *
4541edb306SCy Schubert  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
4641edb306SCy Schubert  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
4741edb306SCy Schubert  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
4841edb306SCy Schubert  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
4941edb306SCy Schubert  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
5041edb306SCy Schubert  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5141edb306SCy Schubert  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
5241edb306SCy Schubert  * SOFTWARE.
5341edb306SCy Schubert  * -
5441edb306SCy Schubert  * --Copyright--
5541edb306SCy Schubert  */
5641edb306SCy Schubert 
5741edb306SCy Schubert 
5841edb306SCy Schubert #include <sys/param.h>
5941edb306SCy Schubert #include <netinet/in.h>
6041edb306SCy Schubert #include <arpa/inet.h>
6141edb306SCy Schubert #include <ctype.h>
6241edb306SCy Schubert 
6341edb306SCy Schubert #ifndef	__P
6441edb306SCy Schubert #  define	__P(x)	x
6541edb306SCy Schubert #endif
6641edb306SCy Schubert int inet_aton(const char *, struct in_addr *);
6741edb306SCy Schubert 
6841edb306SCy Schubert /*
6941edb306SCy Schubert  * Because the ctype(3) posix definition, if used "safely" in code everywhere,
7041edb306SCy Schubert  * would mean all normal code that walks through strings needed casts.  Yuck.
7141edb306SCy Schubert  */
7241edb306SCy Schubert #define	ISALNUM(x)	isalnum((u_char)(x))
7341edb306SCy Schubert #define	ISALPHA(x)	isalpha((u_char)(x))
7441edb306SCy Schubert #define	ISASCII(x)	isascii((u_char)(x))
7541edb306SCy Schubert #define	ISDIGIT(x)	isdigit((u_char)(x))
7641edb306SCy Schubert #define	ISPRINT(x)	isprint((u_char)(x))
7741edb306SCy Schubert #define	ISSPACE(x)	isspace((u_char)(x))
7841edb306SCy Schubert #define	ISUPPER(x)	isupper((u_char)(x))
7941edb306SCy Schubert #define	ISXDIGIT(x)	isxdigit((u_char)(x))
8041edb306SCy Schubert #define	ISLOWER(x)	islower((u_char)(x))
8141edb306SCy Schubert 
8241edb306SCy Schubert /*
8341edb306SCy Schubert  * Check whether "cp" is a valid ascii representation
8441edb306SCy Schubert  * of an Internet address and convert to a binary address.
8541edb306SCy Schubert  * Returns 1 if the address is valid, 0 if not.
8641edb306SCy Schubert * This replaces inet_addr, the return value from which
8741edb306SCy Schubert  * cannot distinguish between failure and a local broadcast address.
8841edb306SCy Schubert  */
8941edb306SCy Schubert int
inet_aton(register const char * cp,struct in_addr * addr)90*efeb8bffSCy Schubert inet_aton(register const char *cp, struct in_addr *addr)
9141edb306SCy Schubert {
9241edb306SCy Schubert 	register u_long val;
9341edb306SCy Schubert 	register int base, n;
9441edb306SCy Schubert 	register char c;
9541edb306SCy Schubert 	u_int parts[4];
9641edb306SCy Schubert 	register u_int *pp = parts;
9741edb306SCy Schubert 
9841edb306SCy Schubert 	c = *cp;
9941edb306SCy Schubert 	for (;;) {
10041edb306SCy Schubert 		/*
10141edb306SCy Schubert 		 * Collect number up to ``.''.
10241edb306SCy Schubert 		 * Values are specified as for C:
10341edb306SCy Schubert 		 * 0x=hex, 0=octal, isdigit=decimal.
10441edb306SCy Schubert 		 */
10541edb306SCy Schubert 		if (!ISDIGIT(c))
10641edb306SCy Schubert 			return (0);
10741edb306SCy Schubert 		val = 0; base = 10;
10841edb306SCy Schubert 		if (c == '0') {
10941edb306SCy Schubert 			c = *++cp;
11041edb306SCy Schubert 			if (c == 'x' || c == 'X')
11141edb306SCy Schubert 				base = 16, c = *++cp;
11241edb306SCy Schubert 			else
11341edb306SCy Schubert 				base = 8;
11441edb306SCy Schubert 		}
11541edb306SCy Schubert 		for (;;) {
11641edb306SCy Schubert 			if (ISASCII(c) && ISDIGIT(c)) {
11741edb306SCy Schubert 				val = (val * base) + (c - '0');
11841edb306SCy Schubert 				c = *++cp;
11941edb306SCy Schubert 			} else if (base == 16 && ISASCII(c) && ISXDIGIT(c)) {
12041edb306SCy Schubert 				val = (val << 4) |
12141edb306SCy Schubert 					(c + 10 - (ISLOWER(c) ? 'a' : 'A'));
12241edb306SCy Schubert 				c = *++cp;
12341edb306SCy Schubert 			} else
12441edb306SCy Schubert 				break;
12541edb306SCy Schubert 		}
12641edb306SCy Schubert 		if (c == '.') {
12741edb306SCy Schubert 			/*
12841edb306SCy Schubert 			 * Internet format:
12941edb306SCy Schubert 			 *	a.b.c.d
13041edb306SCy Schubert 			 *	a.b.c	(with c treated as 16 bits)
13141edb306SCy Schubert 			 *	a.b	(with b treated as 24 bits)
13241edb306SCy Schubert 			 */
13341edb306SCy Schubert 			if (pp >= parts + 3)
13441edb306SCy Schubert 				return (0);
13541edb306SCy Schubert 			*pp++ = val;
13641edb306SCy Schubert 			c = *++cp;
13741edb306SCy Schubert 		} else
13841edb306SCy Schubert 			break;
13941edb306SCy Schubert 	}
14041edb306SCy Schubert 	/*
14141edb306SCy Schubert 	 * Check for trailing characters.
14241edb306SCy Schubert 	 */
14341edb306SCy Schubert 	if (c != '\0' && (!ISASCII(c) || !ISSPACE(c)))
14441edb306SCy Schubert 		return (0);
14541edb306SCy Schubert 	/*
14641edb306SCy Schubert 	 * Concoct the address according to
14741edb306SCy Schubert 	 * the number of parts specified.
14841edb306SCy Schubert 	 */
14941edb306SCy Schubert 	n = pp - parts + 1;
15041edb306SCy Schubert 	switch (n) {
15141edb306SCy Schubert 
15241edb306SCy Schubert 	case 0:
15341edb306SCy Schubert 		return (0);		/* initial nondigit */
15441edb306SCy Schubert 
15541edb306SCy Schubert 	case 1:				/* a -- 32 bits */
15641edb306SCy Schubert 		break;
15741edb306SCy Schubert 
15841edb306SCy Schubert 	case 2:				/* a.b -- 8.24 bits */
15941edb306SCy Schubert 		if (val > 0xffffff)
16041edb306SCy Schubert 			return (0);
16141edb306SCy Schubert 		val |= parts[0] << 24;
16241edb306SCy Schubert 		break;
16341edb306SCy Schubert 
16441edb306SCy Schubert 	case 3:				/* a.b.c -- 8.8.16 bits */
16541edb306SCy Schubert 		if (val > 0xffff)
16641edb306SCy Schubert 			return (0);
16741edb306SCy Schubert 		val |= (parts[0] << 24) | (parts[1] << 16);
16841edb306SCy Schubert 		break;
16941edb306SCy Schubert 
17041edb306SCy Schubert 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
17141edb306SCy Schubert 		if (val > 0xff)
17241edb306SCy Schubert 			return (0);
17341edb306SCy Schubert 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
17441edb306SCy Schubert 		break;
17541edb306SCy Schubert 	}
17641edb306SCy Schubert 	if (addr)
17741edb306SCy Schubert 		addr->s_addr = htonl(val);
17841edb306SCy Schubert 	return (1);
17941edb306SCy Schubert }
18041edb306SCy Schubert 
18141edb306SCy Schubert /* these are compatibility routines, not needed on recent BSD releases */
18241edb306SCy Schubert 
18341edb306SCy Schubert /*
18441edb306SCy Schubert  * Ascii internet address interpretation routine.
18541edb306SCy Schubert  * The value returned is in network order.
18641edb306SCy Schubert  */
18741edb306SCy Schubert #if 0
188*efeb8bffSCy Schubert inet_addr(const char *cp)
18941edb306SCy Schubert {
19041edb306SCy Schubert 	struct in_addr val;
19141edb306SCy Schubert 
19241edb306SCy Schubert 	if (inet_aton(cp, &val))
19341edb306SCy Schubert 		return (val.s_addr);
19441edb306SCy Schubert 	return (0xffffffff);
19541edb306SCy Schubert }
19641edb306SCy Schubert #endif
197