/src/curl/lib/curlx/inet_pton.c
Line | Count | Source |
1 | | /* This is from the BIND 4.9.4 release, modified to compile by itself */ |
2 | | |
3 | | /* Copyright (c) Internet Software Consortium. |
4 | | * |
5 | | * Permission to use, copy, modify, and distribute this software for any |
6 | | * purpose with or without fee is hereby granted, provided that the above |
7 | | * copyright notice and this permission notice appear in all copies. |
8 | | * |
9 | | * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS |
10 | | * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES |
11 | | * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE |
12 | | * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL |
13 | | * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
14 | | * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS |
15 | | * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS |
16 | | * SOFTWARE. |
17 | | * |
18 | | * SPDX-License-Identifier: ISC |
19 | | */ |
20 | | #include "curl_setup.h" |
21 | | |
22 | | #ifdef HAVE_SYS_PARAM_H |
23 | | #include <sys/param.h> |
24 | | #endif |
25 | | #ifdef HAVE_NETINET_IN_H |
26 | | #include <netinet/in.h> |
27 | | #endif |
28 | | #ifdef HAVE_ARPA_INET_H |
29 | | #include <arpa/inet.h> |
30 | | #endif |
31 | | |
32 | | #include "curlx/inet_pton.h" |
33 | | #include "curlx/strparse.h" |
34 | | |
35 | 653 | #define IN6ADDRSZ 16 |
36 | 74 | #define INADDRSZ 4 |
37 | 466 | #define INT16SZ 2 |
38 | | |
39 | | /* |
40 | | * If USE_IPV6 is disabled, we still want to parse IPv6 addresses, so make |
41 | | * sure we have _some_ value for AF_INET6 without polluting our fake value |
42 | | * everywhere. |
43 | | */ |
44 | | #if !defined(USE_IPV6) && !defined(AF_INET6) |
45 | | #define AF_INET6 (AF_INET + 1) |
46 | | #endif |
47 | | |
48 | | /* |
49 | | * WARNING: Do not even consider trying to compile this on a system where |
50 | | * sizeof(int) < 4. sizeof(int) > 4 is fine; all the world's not a VAX. |
51 | | */ |
52 | | |
53 | | /* int inet_pton4(src, dst) |
54 | | * like inet_aton() but without all the hexadecimal and shorthand. |
55 | | * return: |
56 | | * 1 if `src' is a valid dotted quad, else 0. |
57 | | * notice: |
58 | | * does not touch `dst' unless it is returning 1. |
59 | | * author: |
60 | | * Paul Vixie, 1996. |
61 | | */ |
62 | | static int inet_pton4(const char *src, unsigned char *dst) |
63 | 69 | { |
64 | 69 | int saw_digit, octets, ch; |
65 | 69 | unsigned char tmp[INADDRSZ], *tp; |
66 | | |
67 | 69 | saw_digit = 0; |
68 | 69 | octets = 0; |
69 | 69 | tp = tmp; |
70 | 69 | *tp = 0; |
71 | 246 | while((ch = (unsigned char)*src++) != '\0') { |
72 | 219 | if(ISDIGIT(ch)) { |
73 | 138 | unsigned int val = (*tp * 10) + (ch - '0'); |
74 | | |
75 | 138 | if(saw_digit && *tp == 0) |
76 | 1 | return 0; |
77 | 137 | if(val > 255) |
78 | 10 | return 0; |
79 | 127 | *tp = (unsigned char)val; |
80 | 127 | if(!saw_digit) { |
81 | 71 | if(++octets > 4) |
82 | 0 | return 0; |
83 | 71 | saw_digit = 1; |
84 | 71 | } |
85 | 127 | } |
86 | 81 | else if(ch == '.' && saw_digit) { |
87 | 52 | if(octets == 4) |
88 | 2 | return 0; |
89 | 50 | *++tp = 0; |
90 | 50 | saw_digit = 0; |
91 | 50 | } |
92 | 29 | else |
93 | 29 | return 0; |
94 | 219 | } |
95 | 27 | if(octets < 4) |
96 | 25 | return 0; |
97 | 2 | memcpy(dst, tmp, INADDRSZ); |
98 | 2 | return 1; |
99 | 27 | } |
100 | | |
101 | | /* int inet_pton6(src, dst) |
102 | | * convert presentation level address to network order binary form. |
103 | | * return: |
104 | | * 1 if `src' is a valid [RFC1884 2.2] address, else 0. |
105 | | * notice: |
106 | | * (1) does not touch `dst' unless it is returning 1. |
107 | | * (2) :: in a full address is silently ignored. |
108 | | * credit: |
109 | | * inspired by Mark Andrews. |
110 | | * author: |
111 | | * Paul Vixie, 1996. |
112 | | */ |
113 | | static int inet_pton6(const char *src, unsigned char *dst) |
114 | 271 | { |
115 | 271 | unsigned char tmp[IN6ADDRSZ], *tp, *endp, *colonp; |
116 | 271 | const char *curtok; |
117 | 271 | int ch, saw_xdigit; |
118 | 271 | size_t val; |
119 | | |
120 | 271 | memset((tp = tmp), 0, IN6ADDRSZ); |
121 | 271 | endp = tp + IN6ADDRSZ; |
122 | 271 | colonp = NULL; |
123 | | /* Leading :: requires some special handling. */ |
124 | 271 | if(*src == ':' && *++src != ':') |
125 | 5 | return 0; |
126 | 266 | curtok = src; |
127 | 266 | saw_xdigit = 0; |
128 | 266 | val = 0; |
129 | 1.81k | while((ch = (unsigned char)*src++) != '\0') { |
130 | 1.67k | if(ISXDIGIT(ch)) { |
131 | 1.09k | val <<= 4; |
132 | 1.09k | val |= curlx_hexval(ch); |
133 | 1.09k | if(++saw_xdigit > 4) |
134 | 14 | return 0; |
135 | 1.08k | continue; |
136 | 1.09k | } |
137 | 575 | if(ch == ':') { |
138 | 477 | curtok = src; |
139 | 477 | if(!saw_xdigit) { |
140 | 122 | if(colonp) |
141 | 9 | return 0; |
142 | 113 | colonp = tp; |
143 | 113 | continue; |
144 | 122 | } |
145 | 355 | if(!*src) |
146 | 6 | return 0; |
147 | 349 | if(tp + INT16SZ > endp) |
148 | 1 | return 0; |
149 | 348 | *tp++ = (unsigned char)((val >> 8) & 0xff); |
150 | 348 | *tp++ = (unsigned char)(val & 0xff); |
151 | 348 | saw_xdigit = 0; |
152 | 348 | val = 0; |
153 | 348 | continue; |
154 | 349 | } |
155 | 98 | if(ch == '.' && ((tp + INADDRSZ) <= endp) && |
156 | 69 | inet_pton4(curtok, tp) > 0) { |
157 | 2 | tp += INADDRSZ; |
158 | 2 | saw_xdigit = 0; |
159 | 2 | break; /* '\0' was seen by inet_pton4(). */ |
160 | 2 | } |
161 | 96 | return 0; |
162 | 98 | } |
163 | 140 | if(saw_xdigit) { |
164 | 117 | if(tp + INT16SZ > endp) |
165 | 1 | return 0; |
166 | 116 | *tp++ = (unsigned char)((val >> 8) & 0xff); |
167 | 116 | *tp++ = (unsigned char)(val & 0xff); |
168 | 116 | } |
169 | 139 | if(colonp) { |
170 | | /* |
171 | | * Since some memmove()'s erroneously fail to handle |
172 | | * overlapping regions, we do the shift by hand. |
173 | | */ |
174 | 103 | const ssize_t n = tp - colonp; |
175 | 103 | ssize_t i; |
176 | | |
177 | 103 | if(tp == endp) |
178 | 1 | return 0; |
179 | 462 | for(i = 1; i <= n; i++) { |
180 | 360 | *(endp - i) = *(colonp + n - i); |
181 | 360 | *(colonp + n - i) = 0; |
182 | 360 | } |
183 | 102 | tp = endp; |
184 | 102 | } |
185 | 138 | if(tp != endp) |
186 | 27 | return 0; |
187 | 111 | memcpy(dst, tmp, IN6ADDRSZ); |
188 | 111 | return 1; |
189 | 138 | } |
190 | | |
191 | | /* |
192 | | * Convert from presentation format (which usually means ASCII printable) |
193 | | * to network format (which is usually some kind of binary format). |
194 | | * |
195 | | * Return: |
196 | | * 1 if the address was valid for the specified address family |
197 | | * 0 if the address was not valid (`dst' is untouched in this case) |
198 | | * -1 if some other error occurred (`dst' is untouched in this case, too) |
199 | | * |
200 | | * author: |
201 | | * Paul Vixie, 1996. |
202 | | */ |
203 | | int curlx_inet_pton(int af, const char *src, void *dst) |
204 | 271 | { |
205 | 271 | switch(af) { |
206 | 0 | case AF_INET: |
207 | 0 | return inet_pton4(src, (unsigned char *)dst); |
208 | 271 | case AF_INET6: |
209 | 271 | return inet_pton6(src, (unsigned char *)dst); |
210 | 0 | default: |
211 | 0 | return -1; |
212 | 271 | } |
213 | | /* NOTREACHED */ |
214 | 271 | } |