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