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