xref: /xnu-8792.61.2/libkern/net/inet_aton.c (revision 42e220869062b56f8d7d0726fd4c88954f87902c)
1*42e22086SApple OSS Distributions /*-
2*42e22086SApple OSS Distributions  * Copyright (c) 2001 Charles Mott <[email protected]>
3*42e22086SApple OSS Distributions  * All rights reserved.
4*42e22086SApple OSS Distributions  *
5*42e22086SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
6*42e22086SApple OSS Distributions  * modification, are permitted provided that the following conditions
7*42e22086SApple OSS Distributions  * are met:
8*42e22086SApple OSS Distributions  * 1. Redistributions of source code must retain the above copyright
9*42e22086SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer.
10*42e22086SApple OSS Distributions  * 2. Redistributions in binary form must reproduce the above copyright
11*42e22086SApple OSS Distributions  *    notice, this list of conditions and the following disclaimer in the
12*42e22086SApple OSS Distributions  *    documentation and/or other materials provided with the distribution.
13*42e22086SApple OSS Distributions  *
14*42e22086SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*42e22086SApple OSS Distributions  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*42e22086SApple OSS Distributions  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*42e22086SApple OSS Distributions  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*42e22086SApple OSS Distributions  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*42e22086SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*42e22086SApple OSS Distributions  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*42e22086SApple OSS Distributions  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*42e22086SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*42e22086SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*42e22086SApple OSS Distributions  * SUCH DAMAGE.
25*42e22086SApple OSS Distributions  */
26*42e22086SApple OSS Distributions #include <sys/param.h>
27*42e22086SApple OSS Distributions #include <sys/systm.h>
28*42e22086SApple OSS Distributions 
29*42e22086SApple OSS Distributions #include <netinet/in.h>
30*42e22086SApple OSS Distributions 
31*42e22086SApple OSS Distributions /* XXX ctype.h is missing, see libkern/stdio/scanf.c */
32*42e22086SApple OSS Distributions #if 1
33*42e22086SApple OSS Distributions static inline int
isspace(char c)34*42e22086SApple OSS Distributions isspace(char c)
35*42e22086SApple OSS Distributions {
36*42e22086SApple OSS Distributions 	return c == ' ' || c == '\t' || c == '\n' || c == '\12';
37*42e22086SApple OSS Distributions }
38*42e22086SApple OSS Distributions #endif
39*42e22086SApple OSS Distributions 
40*42e22086SApple OSS Distributions int
inet_aton(const char * cp,struct in_addr * addr)41*42e22086SApple OSS Distributions inet_aton(const char *cp, struct in_addr *addr)
42*42e22086SApple OSS Distributions {
43*42e22086SApple OSS Distributions 	u_long parts[4];
44*42e22086SApple OSS Distributions 	in_addr_t val = 0;
45*42e22086SApple OSS Distributions 	const char *c;
46*42e22086SApple OSS Distributions 	char *endptr;
47*42e22086SApple OSS Distributions 	int gotend, n;
48*42e22086SApple OSS Distributions 
49*42e22086SApple OSS Distributions 	c = (const char *)cp;
50*42e22086SApple OSS Distributions 	n = 0;
51*42e22086SApple OSS Distributions 
52*42e22086SApple OSS Distributions 	/*
53*42e22086SApple OSS Distributions 	 * Run through the string, grabbing numbers until
54*42e22086SApple OSS Distributions 	 * the end of the string, or some error
55*42e22086SApple OSS Distributions 	 */
56*42e22086SApple OSS Distributions 	gotend = 0;
57*42e22086SApple OSS Distributions 	while (!gotend) {
58*42e22086SApple OSS Distributions 		unsigned long l;
59*42e22086SApple OSS Distributions 
60*42e22086SApple OSS Distributions 		l = strtoul(c, &endptr, 0);
61*42e22086SApple OSS Distributions 
62*42e22086SApple OSS Distributions 		if (l == ULONG_MAX || (l == 0 && endptr == c)) {
63*42e22086SApple OSS Distributions 			return 0;
64*42e22086SApple OSS Distributions 		}
65*42e22086SApple OSS Distributions 
66*42e22086SApple OSS Distributions 		val = (in_addr_t)l;
67*42e22086SApple OSS Distributions 
68*42e22086SApple OSS Distributions 		/*
69*42e22086SApple OSS Distributions 		 * If the whole string is invalid, endptr will equal
70*42e22086SApple OSS Distributions 		 * c.. this way we can make sure someone hasn't
71*42e22086SApple OSS Distributions 		 * gone '.12' or something which would get past
72*42e22086SApple OSS Distributions 		 * the next check.
73*42e22086SApple OSS Distributions 		 */
74*42e22086SApple OSS Distributions 		if (endptr == c) {
75*42e22086SApple OSS Distributions 			return 0;
76*42e22086SApple OSS Distributions 		}
77*42e22086SApple OSS Distributions 		parts[n] = val;
78*42e22086SApple OSS Distributions 		c = endptr;
79*42e22086SApple OSS Distributions 
80*42e22086SApple OSS Distributions 		/* Check the next character past the previous number's end */
81*42e22086SApple OSS Distributions 		switch (*c) {
82*42e22086SApple OSS Distributions 		case '.':
83*42e22086SApple OSS Distributions 
84*42e22086SApple OSS Distributions 			/* Make sure we only do 3 dots .. */
85*42e22086SApple OSS Distributions 			if (n == 3) {   /* Whoops. Quit. */
86*42e22086SApple OSS Distributions 				return 0;
87*42e22086SApple OSS Distributions 			}
88*42e22086SApple OSS Distributions 			n++;
89*42e22086SApple OSS Distributions 			c++;
90*42e22086SApple OSS Distributions 			break;
91*42e22086SApple OSS Distributions 
92*42e22086SApple OSS Distributions 		case '\0':
93*42e22086SApple OSS Distributions 			gotend = 1;
94*42e22086SApple OSS Distributions 			break;
95*42e22086SApple OSS Distributions 
96*42e22086SApple OSS Distributions 		default:
97*42e22086SApple OSS Distributions 			if (isspace((unsigned char)*c)) {
98*42e22086SApple OSS Distributions 				gotend = 1;
99*42e22086SApple OSS Distributions 				break;
100*42e22086SApple OSS Distributions 			} else {
101*42e22086SApple OSS Distributions 				/* Invalid character, then fail. */
102*42e22086SApple OSS Distributions 				return 0;
103*42e22086SApple OSS Distributions 			}
104*42e22086SApple OSS Distributions 		}
105*42e22086SApple OSS Distributions 	}
106*42e22086SApple OSS Distributions 
107*42e22086SApple OSS Distributions 	/* Concoct the address according to the number of parts specified. */
108*42e22086SApple OSS Distributions 	switch (n) {
109*42e22086SApple OSS Distributions 	case 0:                         /* a -- 32 bits */
110*42e22086SApple OSS Distributions 
111*42e22086SApple OSS Distributions 		/*
112*42e22086SApple OSS Distributions 		 * Nothing is necessary here.  Overflow checking was
113*42e22086SApple OSS Distributions 		 * already done in strtoul().
114*42e22086SApple OSS Distributions 		 */
115*42e22086SApple OSS Distributions 		break;
116*42e22086SApple OSS Distributions 	case 1:                         /* a.b -- 8.24 bits */
117*42e22086SApple OSS Distributions 		if (val > 0xffffff || parts[0] > 0xff) {
118*42e22086SApple OSS Distributions 			return 0;
119*42e22086SApple OSS Distributions 		}
120*42e22086SApple OSS Distributions 		val |= parts[0] << 24;
121*42e22086SApple OSS Distributions 		break;
122*42e22086SApple OSS Distributions 
123*42e22086SApple OSS Distributions 	case 2:                         /* a.b.c -- 8.8.16 bits */
124*42e22086SApple OSS Distributions 		if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff) {
125*42e22086SApple OSS Distributions 			return 0;
126*42e22086SApple OSS Distributions 		}
127*42e22086SApple OSS Distributions 		val |= (parts[0] << 24) | (parts[1] << 16);
128*42e22086SApple OSS Distributions 		break;
129*42e22086SApple OSS Distributions 
130*42e22086SApple OSS Distributions 	case 3:                         /* a.b.c.d -- 8.8.8.8 bits */
131*42e22086SApple OSS Distributions 		if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
132*42e22086SApple OSS Distributions 		    parts[2] > 0xff) {
133*42e22086SApple OSS Distributions 			return 0;
134*42e22086SApple OSS Distributions 		}
135*42e22086SApple OSS Distributions 		val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
136*42e22086SApple OSS Distributions 		break;
137*42e22086SApple OSS Distributions 	}
138*42e22086SApple OSS Distributions 
139*42e22086SApple OSS Distributions 	if (addr != NULL) {
140*42e22086SApple OSS Distributions 		addr->s_addr = htonl(val);
141*42e22086SApple OSS Distributions 	}
142*42e22086SApple OSS Distributions 	return 1;
143*42e22086SApple OSS Distributions }
144