]>
Commit | Line | Data |
---|---|---|
5e30b8ef FW |
1 | /* Legacy IPv4 text-to-address functions. |
2 | Copyright (C) 2019 Free Software Foundation, Inc. | |
3 | This file is part of the GNU C Library. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
6 | modify it under the terms of the GNU Lesser General Public | |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
9 | ||
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | Lesser General Public License for more details. | |
14 | ||
15 | You should have received a copy of the GNU Lesser General Public | |
16 | License along with the GNU C Library; if not, see | |
17 | <http://www.gnu.org/licenses/>. */ | |
18 | ||
28f540f4 RM |
19 | /* |
20 | * Copyright (c) 1983, 1990, 1993 | |
3d61b63c | 21 | * The Regents of the University of California. All rights reserved. |
c5598d47 | 22 | * |
28f540f4 RM |
23 | * Redistribution and use in source and binary forms, with or without |
24 | * modification, are permitted provided that the following conditions | |
25 | * are met: | |
26 | * 1. Redistributions of source code must retain the above copyright | |
27 | * notice, this list of conditions and the following disclaimer. | |
28 | * 2. Redistributions in binary form must reproduce the above copyright | |
29 | * notice, this list of conditions and the following disclaimer in the | |
30 | * documentation and/or other materials provided with the distribution. | |
28f540f4 RM |
31 | * 4. Neither the name of the University nor the names of its contributors |
32 | * may be used to endorse or promote products derived from this software | |
33 | * without specific prior written permission. | |
c5598d47 | 34 | * |
28f540f4 RM |
35 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
36 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
38 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
39 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
40 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
41 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
42 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
43 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
44 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
45 | * SUCH DAMAGE. | |
e685e07d UD |
46 | */ |
47 | ||
48 | /* | |
3d61b63c | 49 | * Portions Copyright (c) 1993 by Digital Equipment Corporation. |
c5598d47 | 50 | * |
3d61b63c RM |
51 | * Permission to use, copy, modify, and distribute this software for any |
52 | * purpose with or without fee is hereby granted, provided that the above | |
53 | * copyright notice and this permission notice appear in all copies, and that | |
54 | * the name of Digital Equipment Corporation not be used in advertising or | |
55 | * publicity pertaining to distribution of the document or software without | |
56 | * specific, written prior permission. | |
c5598d47 | 57 | * |
3d61b63c RM |
58 | * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL |
59 | * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES | |
60 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT | |
61 | * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
62 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
63 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
64 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
65 | * SOFTWARE. | |
e685e07d UD |
66 | */ |
67 | ||
68 | /* | |
69 | * Portions Copyright (c) 1996-1999 by Internet Software Consortium. | |
70 | * | |
71 | * Permission to use, copy, modify, and distribute this software for any | |
72 | * purpose with or without fee is hereby granted, provided that the above | |
73 | * copyright notice and this permission notice appear in all copies. | |
74 | * | |
75 | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS | |
76 | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES | |
77 | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE | |
78 | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL | |
79 | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR | |
80 | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS | |
81 | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS | |
82 | * SOFTWARE. | |
28f540f4 RM |
83 | */ |
84 | ||
df21c858 | 85 | #include <sys/types.h> |
28f540f4 | 86 | #include <sys/param.h> |
e685e07d | 87 | |
28f540f4 RM |
88 | #include <netinet/in.h> |
89 | #include <arpa/inet.h> | |
e685e07d | 90 | |
28f540f4 | 91 | #include <ctype.h> |
e685e07d | 92 | |
ed3c7876 FW |
93 | #include <endian.h> |
94 | #include <stdint.h> | |
95 | #include <stdlib.h> | |
96 | #include <limits.h> | |
97 | #include <errno.h> | |
3d61b63c | 98 | |
5e30b8ef FW |
99 | /* Check whether "cp" is a valid ASCII representation of an IPv4 |
100 | Internet address and convert it to a binary address. Returns 1 if | |
101 | the address is valid, 0 if not. This replaces inet_addr, the | |
102 | return value from which cannot distinguish between failure and a | |
108bc404 FW |
103 | local broadcast address. Write a pointer to the first |
104 | non-converted character to *endp. */ | |
105 | static int | |
106 | inet_aton_end (const char *cp, struct in_addr *addr, const char **endp) | |
c5598d47 | 107 | { |
5e30b8ef FW |
108 | static const in_addr_t max[4] = { 0xffffffff, 0xffffff, 0xffff, 0xff }; |
109 | in_addr_t val; | |
110 | char c; | |
111 | union iaddr | |
112 | { | |
113 | uint8_t bytes[4]; | |
114 | uint32_t word; | |
115 | } res; | |
116 | uint8_t *pp = res.bytes; | |
117 | int digit; | |
118 | ||
119 | int saved_errno = errno; | |
120 | __set_errno (0); | |
121 | ||
122 | res.word = 0; | |
123 | ||
124 | c = *cp; | |
125 | for (;;) | |
126 | { | |
127 | /* Collect number up to ``.''. Values are specified as for C: | |
128 | 0x=hex, 0=octal, isdigit=decimal. */ | |
129 | if (!isdigit (c)) | |
130 | goto ret_0; | |
131 | { | |
132 | char *endp; | |
133 | unsigned long ul = strtoul (cp, &endp, 0); | |
134 | if (ul == ULONG_MAX && errno == ERANGE) | |
8a40ed68 | 135 | goto ret_0; |
5e30b8ef FW |
136 | if (ul > 0xfffffffful) |
137 | goto ret_0; | |
138 | val = ul; | |
139 | digit = cp != endp; | |
140 | cp = endp; | |
141 | } | |
142 | c = *cp; | |
143 | if (c == '.') | |
144 | { | |
145 | /* Internet format: | |
146 | a.b.c.d | |
147 | a.b.c (with c treated as 16 bits) | |
148 | a.b (with b treated as 24 bits). */ | |
149 | if (pp > res.bytes + 2 || val > 0xff) | |
150 | goto ret_0; | |
151 | *pp++ = val; | |
152 | c = *++cp; | |
153 | } | |
154 | else | |
155 | break; | |
156 | } | |
157 | /* Check for trailing characters. */ | |
158 | if (c != '\0' && (!isascii (c) || !isspace (c))) | |
159 | goto ret_0; | |
160 | /* Did we get a valid digit? */ | |
161 | if (!digit) | |
162 | goto ret_0; | |
163 | ||
164 | /* Check whether the last part is in its limits depending on the | |
165 | number of parts in total. */ | |
166 | if (val > max[pp - res.bytes]) | |
167 | goto ret_0; | |
168 | ||
169 | if (addr != NULL) | |
170 | addr->s_addr = res.word | htonl (val); | |
108bc404 | 171 | *endp = cp; |
5e30b8ef FW |
172 | |
173 | __set_errno (saved_errno); | |
174 | return 1; | |
175 | ||
176 | ret_0: | |
177 | __set_errno (saved_errno); | |
178 | return 0; | |
28f540f4 | 179 | } |
108bc404 FW |
180 | |
181 | int | |
182 | __inet_aton_exact (const char *cp, struct in_addr *addr) | |
183 | { | |
184 | struct in_addr val; | |
185 | const char *endp; | |
186 | /* Check that inet_aton_end parsed the entire string. */ | |
187 | if (inet_aton_end (cp, &val, &endp) != 0 && *endp == 0) | |
188 | { | |
189 | *addr = val; | |
190 | return 1; | |
191 | } | |
192 | else | |
193 | return 0; | |
194 | } | |
195 | libc_hidden_def (__inet_aton_exact) | |
196 | ||
197 | /* inet_aton ignores trailing garbage. */ | |
198 | int | |
199 | __inet_aton_ignore_trailing (const char *cp, struct in_addr *addr) | |
200 | { | |
201 | const char *endp; | |
202 | return inet_aton_end (cp, addr, &endp); | |
203 | } | |
204 | weak_alias (__inet_aton_ignore_trailing, inet_aton) | |
205 | ||
206 | /* ASCII IPv4 Internet address interpretation routine. The value | |
207 | returned is in network order. */ | |
208 | in_addr_t | |
209 | __inet_addr (const char *cp) | |
210 | { | |
211 | struct in_addr val; | |
212 | const char *endp; | |
213 | if (inet_aton_end (cp, &val, &endp)) | |
214 | return val.s_addr; | |
215 | return INADDR_NONE; | |
216 | } | |
217 | weak_alias (__inet_addr, inet_addr) |