2 * Copyright (c) 2001 Charles Mott <cm@linktel.net>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/ctype.h>
32 #include <sys/limits.h>
33 #include <sys/systm.h>
35 #include <netinet/in.h>
38 inet_aton(const char *cp, struct in_addr *addr)
50 * Run through the string, grabbing numbers until
51 * the end of the string, or some error
57 l = strtoul(c, &endptr, 0);
59 if (l == ULONG_MAX || (l == 0 && endptr == c))
65 * If the whole string is invalid, endptr will equal
66 * c.. this way we can make sure someone hasn't
67 * gone '.12' or something which would get past
75 /* Check the next character past the previous number's end */
79 /* Make sure we only do 3 dots .. */
80 if (n == 3) /* Whoops. Quit. */
91 if (isspace((unsigned char)*c)) {
96 /* Invalid character, then fail. */
103 /* Concoct the address according to the number of parts specified. */
105 case 0: /* a -- 32 bits */
108 * Nothing is necessary here. Overflow checking was
109 * already done in strtoul().
112 case 1: /* a.b -- 8.24 bits */
113 if (val > 0xffffff || parts[0] > 0xff)
115 val |= parts[0] << 24;
118 case 2: /* a.b.c -- 8.8.16 bits */
119 if (val > 0xffff || parts[0] > 0xff || parts[1] > 0xff)
121 val |= (parts[0] << 24) | (parts[1] << 16);
124 case 3: /* a.b.c.d -- 8.8.8.8 bits */
125 if (val > 0xff || parts[0] > 0xff || parts[1] > 0xff ||
128 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
133 addr->s_addr = htonl(val);