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