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