1 /*
2  * Portions Copyright (C) Internet Systems Consortium, Inc. ("ISC")
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
7  *
8  * See the COPYRIGHT file distributed with this work for additional
9  * information regarding copyright ownership.
10  */
11 
12 /*
13  * Copyright (c) 1983, 1990, 1993
14  *    The Regents of the University of California.  All rights reserved.
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions
18  * are met:
19  * 1. Redistributions of source code must retain the above copyright
20  *    notice, this list of conditions and the following disclaimer.
21  * 2. Redistributions in binary form must reproduce the above copyright
22  *    notice, this list of conditions and the following disclaimer in the
23  *    documentation and/or other materials provided with the distribution.
24  * 3. Neither the name of the University nor the names of its contributors
25  *    may be used to endorse or promote products derived from this software
26  *    without specific prior written permission.
27  *
28  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38  * SUCH DAMAGE.
39  */
40 
41 /*
42  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
43  *
44  * Permission to use, copy, modify, and distribute this software for any
45  * purpose with or without fee is hereby granted, provided that the above
46  * copyright notice and this permission notice appear in all copies, and that
47  * the name of Digital Equipment Corporation not be used in advertising or
48  * publicity pertaining to distribution of the document or software without
49  * specific, written prior permission.
50  *
51  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
52  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
53  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
54  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
55  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
56  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
57  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
58  * SOFTWARE.
59  */
60 
61 /*! \file lwinetaton.c
62  */
63 #if defined(LIBC_SCCS) && !defined(lint)
64 static char sccsid[] = "@(#)inet_addr.c	8.1 (Berkeley) 6/17/93";
65 #endif /* LIBC_SCCS and not lint */
66 
67 #include <config.h>
68 
69 #include <ctype.h>
70 #include <inttypes.h>
71 #include <stddef.h>
72 
73 #include <lwres/net.h>
74 
75 #include "assert_p.h"
76 
77 /*!
78  * Check whether "cp" is a valid ascii representation
79  * of an Internet address and convert to a binary address.
80  * Returns 1 if the address is valid, 0 if not.
81  * This replaces inet_addr, the return value from which
82  * cannot distinguish between failure and a local broadcast address.
83  */
84 int
lwres_net_aton(const char * cp,struct in_addr * addr)85 lwres_net_aton(const char *cp, struct in_addr *addr) {
86 	uint32_t val;
87 	int base;
88 	ptrdiff_t n;
89 	unsigned char c;
90 	uint8_t parts[4];
91 	uint8_t *pp = parts;
92 	int digit;
93 
94 	REQUIRE(cp != NULL);
95 
96 	c = *cp;
97 	for (;;) {
98 		/*
99 		 * Collect number up to ``.''.
100 		 * Values are specified as for C:
101 		 * 0x=hex, 0=octal, isdigit=decimal.
102 		 */
103 		if (!isdigit(c & 0xff))
104 			return (0);
105 		val = 0;
106 		base = 10;
107 		digit = 0;
108 		if (c == '0') {
109 			c = *++cp;
110 			if (c == 'x' || c == 'X') {
111 				base = 16;
112 				c = *++cp;
113 			} else {
114 				base = 8;
115 				digit = 1;
116 			}
117 		}
118 		for (;;) {
119 			/*
120 			 * isascii() is valid for all integer values, and
121 			 * when it is true, c is known to be in scope
122 			 * for isdigit().  No cast necessary.  Similar
123 			 * comment applies for later ctype uses.
124 			 */
125 			if (isascii(c) && isdigit(c)) {
126 				if (base == 8 && (c == '8' || c == '9'))
127 					return (0);
128 				val = (val * base) + (c - '0');
129 				c = *++cp;
130 				digit = 1;
131 			} else if (base == 16 && isascii(c) && isxdigit(c)) {
132 				val = (val << 4) |
133 					(c + 10 - (islower(c) ? 'a' : 'A'));
134 				c = *++cp;
135 				digit = 1;
136 			} else
137 				break;
138 		}
139 		if (c == '.') {
140 			/*
141 			 * Internet format:
142 			 *	a.b.c.d
143 			 *	a.b.c	(with c treated as 16 bits)
144 			 *	a.b	(with b treated as 24 bits)
145 			 */
146 			if (pp >= parts + 3 || val > 0xffU)
147 				return (0);
148 			*pp++ = (uint8_t)val;
149 			c = *++cp;
150 		} else
151 			break;
152 	}
153 	/*
154 	 * Check for trailing characters.
155 	 */
156 	if (c != '\0' && (!isascii(c) || !isspace(c)))
157 		return (0);
158 	/*
159 	 * Did we get a valid digit?
160 	 */
161 	if (!digit)
162 		return (0);
163 	/*
164 	 * Concoct the address according to
165 	 * the number of parts specified.
166 	 */
167 	n = pp - parts + 1;
168 	switch (n) {
169 	case 1:				/* a -- 32 bits */
170 		break;
171 
172 	case 2:				/* a.b -- 8.24 bits */
173 		if (val > 0xffffffU)
174 			return (0);
175 #define ulshift(x, n) ((unsigned int)(x) << (n))
176 		val |= ulshift(parts[0], 24);
177 		break;
178 
179 	case 3:				/* a.b.c -- 8.8.16 bits */
180 		if (val > 0xffffU)
181 			return (0);
182 		val |= ulshift(parts[0], 24) | ulshift(parts[1], 16);
183 		break;
184 
185 	case 4:				/* a.b.c.d -- 8.8.8.8 bits */
186 		if (val > 0xffU)
187 			return (0);
188 		val |= ulshift(parts[0], 24) | ulshift(parts[1], 16) |
189 			ulshift(parts[2], 8);
190 		break;
191 	}
192 	if (addr != NULL)
193 		addr->s_addr = htonl(val);
194 
195 	return (1);
196 }
197