Line | Count | Source |
1 | | /*************************************************************************** |
2 | | * _ _ ____ _ |
3 | | * Project ___| | | | _ \| | |
4 | | * / __| | | | |_) | | |
5 | | * | (__| |_| | _ <| |___ |
6 | | * \___|\___/|_| \_\_____| |
7 | | * |
8 | | * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. |
9 | | * |
10 | | * This software is licensed as described in the file COPYING, which |
11 | | * you should have received as part of this distribution. The terms |
12 | | * are also available at https://curl.se/docs/copyright.html. |
13 | | * |
14 | | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
15 | | * copies of the Software, and permit persons to whom the Software is |
16 | | * furnished to do so, under the terms of the COPYING file. |
17 | | * |
18 | | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
19 | | * KIND, either express or implied. |
20 | | * |
21 | | * SPDX-License-Identifier: curl |
22 | | * |
23 | | ***************************************************************************/ |
24 | | /* |
25 | | * IDN conversions |
26 | | */ |
27 | | #include "curl_setup.h" |
28 | | |
29 | | #ifdef HAVE_NETINET_IN_H |
30 | | #include <netinet/in.h> |
31 | | #endif |
32 | | #ifdef HAVE_NETDB_H |
33 | | #include <netdb.h> |
34 | | #endif |
35 | | #ifdef HAVE_ARPA_INET_H |
36 | | #include <arpa/inet.h> |
37 | | #endif |
38 | | #ifdef HAVE_NET_IF_H |
39 | | #include <net/if.h> |
40 | | #endif |
41 | | #ifdef HAVE_IPHLPAPI_H |
42 | | #include <Iphlpapi.h> |
43 | | #endif |
44 | | #ifdef HAVE_SYS_IOCTL_H |
45 | | #include <sys/ioctl.h> |
46 | | #endif |
47 | | #ifdef HAVE_SYS_PARAM_H |
48 | | #include <sys/param.h> |
49 | | #endif |
50 | | |
51 | | #ifdef __VMS |
52 | | #include <in.h> |
53 | | #include <inet.h> |
54 | | #endif |
55 | | |
56 | | #ifdef HAVE_SYS_UN_H |
57 | | #include <sys/un.h> |
58 | | #endif |
59 | | |
60 | | #if defined(HAVE_IF_NAMETOINDEX) && defined(USE_WINSOCK) |
61 | | #if defined(__MINGW32__) && (__MINGW64_VERSION_MAJOR <= 5) |
62 | | #include <wincrypt.h> /* workaround for old mingw-w64 missing to include it */ |
63 | | #endif |
64 | | #include <iphlpapi.h> |
65 | | #endif |
66 | | |
67 | | #include "curl_addrinfo.h" |
68 | | #include "curl_trc.h" |
69 | | #include "protocol.h" |
70 | | #include "http_proxy.h" |
71 | | #include "idn.h" |
72 | | #include "curlx/strdup.h" |
73 | | #include "curlx/strparse.h" |
74 | | #include "peer.h" |
75 | | #include "urldata.h" |
76 | | #include "url.h" |
77 | | #include "urlapi-int.h" |
78 | | #include "vtls/vtls.h" |
79 | | |
80 | | struct peer_parse { |
81 | | const struct Curl_scheme *scheme; |
82 | | struct Curl_str host_user; |
83 | | struct Curl_str host; |
84 | | struct Curl_str zoneid; |
85 | | char *tmp_host_user; |
86 | | char *tmp_host; |
87 | | char *tmp_zoneid; |
88 | | uint32_t scopeid; |
89 | | uint16_t port; |
90 | | bool ipv6; |
91 | | bool unix_socket; |
92 | | bool abstract_uds; |
93 | | }; |
94 | | |
95 | | static void peer_parse_clear(struct peer_parse *pp) |
96 | 212k | { |
97 | 212k | curlx_free(pp->tmp_host_user); |
98 | 212k | curlx_free(pp->tmp_host); |
99 | 212k | curlx_free(pp->tmp_zoneid); |
100 | 212k | memset(pp, 0, sizeof(*pp)); |
101 | 212k | } |
102 | | |
103 | | static CURLcode peer_create(struct peer_parse *pp, |
104 | | struct Curl_peer **ppeer) |
105 | 212k | { |
106 | 212k | struct Curl_peer *peer = NULL; |
107 | 212k | CURLcode result = CURLE_OK; |
108 | 212k | size_t zone_alen = 0, host_alen = 0; |
109 | | |
110 | 212k | if(!pp || !pp->scheme) |
111 | 0 | return CURLE_FAILED_INIT; |
112 | 212k | if(!pp->host.len && !(pp->scheme->flags & PROTOPT_NONETWORK)) |
113 | 0 | return CURLE_FAILED_INIT; |
114 | | |
115 | 212k | if((pp->host.str != pp->host_user.str) || |
116 | 212k | (pp->host.len != pp->host_user.len)) { |
117 | 0 | host_alen = pp->host.len + 1; |
118 | 0 | } |
119 | 212k | zone_alen = pp->zoneid.len ? (pp->zoneid.len + 1) : 0; |
120 | | |
121 | | /* null-terminator already part of struct */ |
122 | 212k | peer = curlx_calloc(1, sizeof(*peer) + |
123 | 212k | pp->host_user.len + host_alen + zone_alen); |
124 | 212k | if(!peer) { |
125 | 67 | result = CURLE_OUT_OF_MEMORY; |
126 | 67 | goto out; |
127 | 67 | } |
128 | | |
129 | 212k | Curl_refcount_init(&peer->refcount, 1); |
130 | 212k | peer->scheme = pp->scheme; |
131 | 212k | peer->hostname = peer->user_hostname; |
132 | 212k | peer->port = pp->port; |
133 | 212k | peer->scopeid = pp->scopeid; |
134 | 212k | peer->ipv6 = pp->ipv6; |
135 | 212k | peer->unix_socket = pp->unix_socket; |
136 | 212k | peer->abstract_uds = pp->abstract_uds; |
137 | | |
138 | 212k | if(pp->host_user.len) |
139 | 212k | memcpy(peer->user_hostname, pp->host_user.str, pp->host_user.len); |
140 | | |
141 | 212k | if(host_alen) { |
142 | 0 | peer->hostname = peer->user_hostname + pp->host_user.len + 1; |
143 | 0 | memcpy(peer->hostname, pp->host.str, pp->host.len); |
144 | 0 | } |
145 | | |
146 | 212k | if(zone_alen) { |
147 | 0 | peer->zoneid = peer->user_hostname + pp->host_user.len + 1 + host_alen; |
148 | 0 | memcpy(peer->zoneid, pp->zoneid.str, pp->zoneid.len); |
149 | 0 | #ifdef USE_IPV6 |
150 | | /* Determine scope_id if not already provided */ |
151 | 0 | if(!peer->scopeid) { |
152 | 0 | const char *p = peer->zoneid; |
153 | 0 | curl_off_t scope; |
154 | 0 | if(!curlx_str_number(&p, &scope, UINT_MAX) && !*p) { |
155 | | /* A plain number, use it directly as a scope id. */ |
156 | 0 | peer->scopeid = (uint32_t)scope; |
157 | 0 | } |
158 | 0 | #ifdef HAVE_IF_NAMETOINDEX |
159 | 0 | else { |
160 | | /* Zone identifier is not fully numeric */ |
161 | 0 | unsigned int idx = 0; |
162 | 0 | idx = if_nametoindex(peer->zoneid); |
163 | 0 | if(idx) { |
164 | 0 | peer->scopeid = (uint32_t)idx; |
165 | 0 | } |
166 | 0 | else { |
167 | | /* Do we want to return an error here? */ |
168 | 0 | } |
169 | 0 | } |
170 | 0 | #endif /* HAVE_IF_NAMETOINDEX */ |
171 | 0 | } |
172 | 0 | #endif /* USE_IPV6 */ |
173 | 0 | } |
174 | | |
175 | 212k | out: |
176 | 212k | if(!result) |
177 | 212k | *ppeer = peer; |
178 | 67 | else |
179 | 67 | Curl_peer_unlink(&peer); |
180 | 212k | return result; |
181 | 212k | } |
182 | | |
183 | | static CURLcode peer_parse_host(struct Curl_easy *data, |
184 | | struct peer_parse *pp, |
185 | | bool scan_for_ipv6) |
186 | 212k | { |
187 | 212k | if(!pp || !pp->host_user.str || !pp->host_user.len) |
188 | 0 | return CURLE_FAILED_INIT; |
189 | | |
190 | 212k | if(pp->host_user.str[0] == '[') { |
191 | 0 | const char *s = pp->host_user.str + 1; |
192 | 0 | struct Curl_str tmp; |
193 | 0 | if(curlx_str_until(&s, &tmp, pp->host_user.len - 1, ']')) |
194 | 0 | return CURLE_URL_MALFORMAT; |
195 | | |
196 | 0 | if(!Curl_looks_like_ipv6(tmp.str, tmp.len, TRUE, |
197 | 0 | &pp->host, &pp->zoneid)) { |
198 | 0 | failf(data, "Invalid IPv6 address format in '%.*s'", |
199 | 0 | (int)pp->host_user.len, pp->host_user.str); |
200 | 0 | return CURLE_URL_MALFORMAT; |
201 | 0 | } |
202 | 0 | pp->ipv6 = TRUE; |
203 | 0 | } |
204 | 212k | else { |
205 | 212k | #ifdef USE_IDN |
206 | 212k | if(!Curl_is_ASCII_str(&pp->host_user)) { |
207 | 14 | CURLcode result; |
208 | 14 | if(!pp->tmp_host_user) { |
209 | | /* need a null-terminated string for IDN */ |
210 | 14 | pp->tmp_host_user = curlx_memdup0(pp->host_user.str, |
211 | 14 | pp->host_user.len); |
212 | 14 | if(!pp->tmp_host_user) |
213 | 0 | return CURLE_OUT_OF_MEMORY; |
214 | 14 | } |
215 | 14 | result = Curl_idn_decode(pp->tmp_host_user, &pp->tmp_host); |
216 | 14 | if(!result && |
217 | 0 | Curl_hostname_check(pp->tmp_host, strlen(pp->tmp_host))) { |
218 | 0 | failf(data, "Illegal hostname: %s", pp->tmp_host); |
219 | 0 | result = CURLE_URL_MALFORMAT; |
220 | 0 | } |
221 | 14 | if(result) |
222 | 14 | return result; |
223 | 0 | pp->host.str = pp->tmp_host; |
224 | 0 | pp->host.len = strlen(pp->host.str); |
225 | 0 | } |
226 | 212k | else |
227 | 212k | #endif |
228 | 212k | if(scan_for_ipv6 && |
229 | 0 | Curl_looks_like_ipv6(pp->host_user.str, pp->host_user.len, TRUE, |
230 | 0 | &pp->host, &pp->zoneid)) { |
231 | 0 | if(pp->host_user.len < MAX_IPADR_LEN) { |
232 | 0 | char tmp[MAX_IPADR_LEN]; |
233 | 0 | memcpy(tmp, pp->host_user.str, pp->host_user.len); |
234 | 0 | tmp[pp->host_user.len] = 0; |
235 | 0 | pp->ipv6 = !Curl_is_ipv4addr(tmp); |
236 | 0 | } |
237 | 0 | else |
238 | 0 | pp->ipv6 = TRUE; |
239 | 0 | } |
240 | 212k | else |
241 | 212k | pp->host = pp->host_user; |
242 | 212k | } |
243 | 212k | return CURLE_OK; |
244 | 212k | } |
245 | | |
246 | | CURLcode Curl_peer_create(struct Curl_easy *data, |
247 | | const struct Curl_scheme *scheme, |
248 | | const char *hostname, |
249 | | uint16_t port, |
250 | | struct Curl_peer **ppeer) |
251 | 0 | { |
252 | 0 | struct peer_parse pp; |
253 | 0 | CURLcode result; |
254 | |
|
255 | 0 | Curl_peer_unlink(ppeer); |
256 | 0 | memset(&pp, 0, sizeof(pp)); |
257 | 0 | pp.scheme = scheme; |
258 | 0 | pp.host_user.str = hostname; |
259 | 0 | pp.host_user.len = strlen(hostname); |
260 | 0 | pp.port = port; |
261 | |
|
262 | 0 | result = peer_parse_host(data, &pp, TRUE); |
263 | 0 | if(!result) |
264 | 0 | result = peer_create(&pp, ppeer); |
265 | |
|
266 | 0 | peer_parse_clear(&pp); |
267 | 0 | return result; |
268 | 0 | } |
269 | | |
270 | | #ifdef USE_UNIX_SOCKETS |
271 | | CURLcode Curl_peer_uds_create(const struct Curl_scheme *scheme, |
272 | | const char *path, |
273 | | bool abstract_unix_socket, |
274 | | struct Curl_peer **ppeer) |
275 | 0 | { |
276 | 0 | struct peer_parse pp; |
277 | 0 | size_t pathlen = path ? strlen(path) : 0; |
278 | 0 | CURLcode result = CURLE_OK; |
279 | |
|
280 | 0 | Curl_peer_unlink(ppeer); |
281 | 0 | memset(&pp, 0, sizeof(pp)); |
282 | 0 | if(!scheme) |
283 | 0 | return CURLE_FAILED_INIT; |
284 | 0 | if(!pathlen) |
285 | 0 | return CURLE_FAILED_INIT; |
286 | | |
287 | 0 | pp.scheme = scheme; |
288 | 0 | pp.host_user.str = pp.host.str = path; |
289 | 0 | pp.host_user.len = pp.host.len = pathlen; |
290 | 0 | pp.unix_socket = TRUE; |
291 | 0 | pp.abstract_uds = abstract_unix_socket; |
292 | |
|
293 | 0 | result = peer_create(&pp, ppeer); |
294 | 0 | peer_parse_clear(&pp); |
295 | 0 | return result; |
296 | 0 | } |
297 | | #endif /* USE_UNIX_SOCKETS */ |
298 | | |
299 | | void Curl_peer_link(struct Curl_peer **pdest, struct Curl_peer *src) |
300 | 836k | { |
301 | 836k | if(*pdest != src) { |
302 | 654k | Curl_peer_unlink(pdest); |
303 | 654k | *pdest = src; |
304 | 654k | if(src) { |
305 | 654k | uint32_t newcount = Curl_refcount_incr(&src->refcount); |
306 | 654k | (void)newcount; |
307 | 654k | DEBUGASSERT(newcount < UINT32_MAX); |
308 | 654k | } |
309 | 654k | } |
310 | 836k | } |
311 | | |
312 | | void Curl_peer_unlink(struct Curl_peer **ppeer) |
313 | 3.38M | { |
314 | 3.38M | if(*ppeer) { |
315 | 866k | struct Curl_peer *peer = *ppeer; |
316 | 866k | uint32_t newcount; |
317 | | |
318 | 866k | *ppeer = NULL; |
319 | | |
320 | 866k | newcount = Curl_refcount_decr(&peer->refcount); |
321 | 866k | DEBUGASSERT(newcount < UINT32_MAX); /* wraparound check */ |
322 | | |
323 | 866k | if(!newcount) |
324 | 212k | curlx_free(peer); |
325 | 866k | } |
326 | 3.38M | } |
327 | | |
328 | | bool Curl_peer_equal(struct Curl_peer *p1, struct Curl_peer *p2) |
329 | 559k | { |
330 | 559k | return (p1 == p2) || |
331 | 223k | (p1 && p2 && |
332 | 193k | (p1->scheme == p2->scheme) && |
333 | 193k | Curl_peer_same_destination(p1, p2)); |
334 | 559k | } |
335 | | |
336 | | static bool peer_same_hostname(struct Curl_peer *p1, struct Curl_peer *p2) |
337 | 347k | { |
338 | | /* UNIX domain socket paths must be compared case-sensitive, |
339 | | * as many filesystem are like that. */ |
340 | 347k | return (p1->unix_socket == p2->unix_socket) && |
341 | 347k | (p1->abstract_uds == p2->abstract_uds) && |
342 | 347k | (p1->ipv6 == p2->ipv6) && |
343 | | /* unix_socket paths are case-sensitive, hostnames are not. |
344 | | * Do strcmp() first on both, since we do a lot of matches |
345 | | * on the same names and platform strcmp is way faster. */ |
346 | 347k | (!strcmp(p1->hostname, p2->hostname) || |
347 | 134k | (!p1->unix_socket && |
348 | 134k | curl_strequal(p1->hostname, p2->hostname))); |
349 | 347k | } |
350 | | |
351 | | static bool peer_same_zoneid(const char *z1, const char *z2) |
352 | 212k | { |
353 | 212k | return (z1 == z2) || |
354 | 0 | (z1 && z2 && !strcmp(z1, z2)); |
355 | 212k | } |
356 | | |
357 | | bool Curl_peer_same_destination(struct Curl_peer *p1, struct Curl_peer *p2) |
358 | 658k | { |
359 | 658k | return (p1 == p2) || |
360 | 347k | (p1 && p2 && |
361 | 347k | (p1->port == p2->port) && |
362 | 347k | peer_same_hostname(p1, p2) && |
363 | 212k | (p1->scopeid == p2->scopeid) && |
364 | 212k | (p1->scopeid || peer_same_zoneid(p1->zoneid, p2->zoneid))); |
365 | 658k | } |
366 | | |
367 | | CURLcode Curl_peer_from_url(CURLU *uh, struct Curl_easy *data, |
368 | | uint16_t port_override, |
369 | | uint32_t scopeid_override, |
370 | | struct Curl_peer **ppeer) |
371 | 107k | { |
372 | 107k | struct peer_parse pp; |
373 | 107k | char *zoneid = NULL, *scheme = NULL, *hostname = NULL; |
374 | 107k | CURLUcode uc; |
375 | 107k | CURLcode result; |
376 | | |
377 | 107k | Curl_peer_unlink(ppeer); |
378 | 107k | memset(&pp, 0, sizeof(pp)); |
379 | | |
380 | 107k | uc = curl_url_get(uh, CURLUPART_SCHEME, &scheme, 0); |
381 | 107k | if(uc) |
382 | 43 | return Curl_uc_to_curlcode(uc); |
383 | 107k | pp.scheme = Curl_get_scheme(scheme); |
384 | 107k | if(!pp.scheme) { |
385 | 0 | failf(data, "Protocol \"%s\" not supported%s", scheme, |
386 | 0 | data->state.this_is_a_follow ? " (in redirect)" : ""); |
387 | 0 | result = CURLE_UNSUPPORTED_PROTOCOL; |
388 | 0 | goto out; |
389 | 0 | } |
390 | | |
391 | 107k | uc = curl_url_get(uh, CURLUPART_HOST, &hostname, 0); |
392 | 107k | if(uc) { |
393 | 23 | if((uc == CURLUE_NO_HOST) && (pp.scheme->flags & PROTOPT_NONETWORK)) |
394 | 0 | ; /* acceptable */ |
395 | 23 | else { |
396 | 23 | result = CURLE_OUT_OF_MEMORY; |
397 | 23 | goto out; |
398 | 23 | } |
399 | 23 | } |
400 | 107k | else if(strlen(hostname) > MAX_URL_LEN) { |
401 | 0 | failf(data, "Too long hostname (maximum is %d)", MAX_URL_LEN); |
402 | 0 | result = CURLE_URL_MALFORMAT; |
403 | 0 | goto out; |
404 | 0 | } |
405 | | |
406 | 107k | pp.host_user.str = hostname ? hostname : ""; |
407 | 107k | pp.host_user.len = strlen(pp.host_user.str); |
408 | 107k | if(pp.host_user.len) { |
409 | 107k | result = peer_parse_host(data, &pp, FALSE); |
410 | 107k | if(result) |
411 | 14 | goto out; |
412 | 107k | } |
413 | 0 | else |
414 | 0 | pp.host = pp.host_user; |
415 | | |
416 | 107k | if(port_override) { |
417 | | /* if set, we use this instead of the port possibly given in the URL */ |
418 | 0 | char portbuf[16]; |
419 | 0 | curl_msnprintf(portbuf, sizeof(portbuf), "%d", port_override); |
420 | 0 | uc = curl_url_set(uh, CURLUPART_PORT, portbuf, 0); |
421 | 0 | if(uc) { |
422 | 0 | result = CURLE_OUT_OF_MEMORY; |
423 | 0 | goto out; |
424 | 0 | } |
425 | 0 | else |
426 | 0 | pp.port = port_override; |
427 | 0 | } |
428 | 107k | else { |
429 | 107k | uc = Curl_url_get_port(uh, &pp.port); |
430 | 107k | if(uc) { |
431 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
432 | 0 | result = CURLE_OUT_OF_MEMORY; |
433 | 0 | goto out; |
434 | 0 | } |
435 | 0 | else if(!(pp.scheme->flags & PROTOPT_NONETWORK)) { |
436 | 0 | result = CURLE_URL_MALFORMAT; |
437 | 0 | goto out; |
438 | 0 | } |
439 | | /* no port ok when not a network scheme */ |
440 | 0 | } |
441 | 107k | } |
442 | | |
443 | 107k | if(scopeid_override) |
444 | | /* Override any scope id from an URL zone. */ |
445 | 0 | pp.scopeid = scopeid_override; |
446 | 107k | else { |
447 | 107k | if(curl_url_get(uh, CURLUPART_ZONEID, &zoneid, 0) == |
448 | 107k | CURLUE_OUT_OF_MEMORY) { |
449 | 0 | result = CURLE_OUT_OF_MEMORY; |
450 | 0 | goto out; |
451 | 0 | } |
452 | 107k | if(zoneid) { |
453 | 0 | pp.zoneid.str = zoneid; |
454 | 0 | pp.zoneid.len = strlen(zoneid); |
455 | 0 | } |
456 | 107k | } |
457 | | |
458 | 107k | result = peer_create(&pp, ppeer); |
459 | 107k | if(result) |
460 | 32 | failf(data, "Error %d creating peer for %s:%u", |
461 | 32 | (int)result, pp.host_user.str, pp.port); |
462 | | |
463 | 107k | out: |
464 | 107k | peer_parse_clear(&pp); |
465 | 107k | curlx_free(scheme); |
466 | 107k | curlx_free(hostname); |
467 | 107k | curlx_free(zoneid); |
468 | 107k | return result; |
469 | 107k | } |
470 | | |
471 | | /* Parse a "host:port" string to connect to into a peer. |
472 | | * IPv6 addresses might appear in brackets or without them. */ |
473 | | CURLcode Curl_peer_from_connect_to(struct Curl_easy *data, |
474 | | const struct Curl_peer *dest, |
475 | | const char *connect_to, |
476 | | struct Curl_peer **ppeer) |
477 | 104k | { |
478 | 104k | struct peer_parse pp; |
479 | 104k | const char *portstr = NULL; |
480 | 104k | bool port_switch = FALSE; |
481 | 104k | CURLcode result; |
482 | | |
483 | 104k | Curl_peer_unlink(ppeer); |
484 | 104k | memset(&pp, 0, sizeof(pp)); |
485 | 104k | if(!connect_to || !*connect_to) |
486 | 0 | return CURLE_FAILED_INIT; |
487 | | |
488 | 104k | pp.scheme = dest->scheme; |
489 | | |
490 | | /* detect and extract RFC6874-style IPv6-addresses */ |
491 | 104k | if(connect_to[0] == '[') { |
492 | 0 | const char *s = strchr(connect_to + 1, ']'); |
493 | 0 | if(!s) { |
494 | 0 | failf(data, "Invalid IPv6 address format in '%s'", connect_to); |
495 | 0 | result = CURLE_SETOPT_OPTION_SYNTAX; |
496 | 0 | goto out; |
497 | 0 | } |
498 | 0 | portstr = strchr(s, ':'); |
499 | 0 | pp.host_user.str = connect_to; |
500 | 0 | pp.host_user.len = s - pp.host_user.str + 1; |
501 | 0 | pp.ipv6 = TRUE; |
502 | 0 | } |
503 | 104k | else { |
504 | 104k | portstr = strchr(connect_to, ':'); |
505 | 104k | pp.host_user.str = connect_to; |
506 | 104k | pp.host_user.len = portstr ? |
507 | 104k | (size_t)(portstr - connect_to) : strlen(connect_to); |
508 | 104k | } |
509 | | |
510 | 104k | if(!pp.host_user.len) { /* no hostname found, only port switch */ |
511 | 0 | port_switch = TRUE; |
512 | 0 | pp.host_user.str = dest->user_hostname; |
513 | 0 | pp.host_user.len = strlen(dest->user_hostname); |
514 | 0 | } |
515 | | |
516 | 104k | result = peer_parse_host(data, &pp, FALSE); |
517 | 104k | if(result) |
518 | 0 | goto out; |
519 | | /* On a port switch, ipv6 must stay the same */ |
520 | 104k | DEBUGASSERT(!port_switch || ((bool)pp.ipv6 == (bool)dest->ipv6)); |
521 | | |
522 | 104k | if(portstr && portstr[1]) { |
523 | 0 | const char *p = portstr + 1; |
524 | 0 | curl_off_t portparse; |
525 | 0 | if(curlx_str_number(&p, &portparse, 0xffff)) { |
526 | 0 | failf(data, "No valid port number in '%s'", connect_to); |
527 | 0 | result = CURLE_SETOPT_OPTION_SYNTAX; |
528 | 0 | goto out; |
529 | 0 | } |
530 | 0 | pp.port = (uint16_t)portparse; /* we know it will fit */ |
531 | 0 | } |
532 | 104k | else |
533 | 104k | pp.port = dest->port; |
534 | | |
535 | | #ifndef USE_IPV6 |
536 | | if(pp.ipv6) { |
537 | | failf(data, "Use of IPv6 in *_CONNECT_TO without IPv6 support built-in"); |
538 | | result = CURLE_NOT_BUILT_IN; |
539 | | goto out; |
540 | | } |
541 | | #endif |
542 | 104k | if(pp.ipv6 && port_switch) { |
543 | 0 | if(dest->zoneid) { |
544 | 0 | pp.zoneid.str = dest->zoneid; |
545 | 0 | pp.zoneid.len = strlen(dest->zoneid); |
546 | 0 | } |
547 | 0 | pp.scopeid = dest->scopeid; |
548 | 0 | } |
549 | | |
550 | 104k | result = peer_create(&pp, ppeer); |
551 | 104k | CURL_TRC_M(data, "connect-to peer_create2 -> %d", (int)result); |
552 | | |
553 | 104k | out: |
554 | 104k | CURL_TRC_M(data, "parse connect_to peer: %s -> %d", connect_to, (int)result); |
555 | 104k | peer_parse_clear(&pp); |
556 | 104k | return result; |
557 | 104k | } |
558 | | |
559 | | #ifndef CURL_DISABLE_PROXY |
560 | | |
561 | | #ifdef USE_UNIX_SOCKETS |
562 | 0 | #define UNIX_SOCKET_PREFIX "localhost" |
563 | | #endif |
564 | | |
565 | | CURLcode Curl_scheme_to_proxytype(struct Curl_easy *data, |
566 | | const char *scheme, |
567 | | uint8_t *proxytype, const char *url) |
568 | 0 | { |
569 | 0 | if(!scheme) |
570 | 0 | return CURLE_OK; |
571 | | |
572 | 0 | if(curl_strequal("https", scheme)) { |
573 | 0 | if(*proxytype != CURLPROXY_HTTPS2 && *proxytype != CURLPROXY_HTTPS3) |
574 | 0 | *proxytype = CURLPROXY_HTTPS; |
575 | 0 | } |
576 | 0 | else if(curl_strequal("socks5h", scheme)) |
577 | 0 | *proxytype = CURLPROXY_SOCKS5_HOSTNAME; |
578 | 0 | else if(curl_strequal("socks5", scheme)) |
579 | 0 | *proxytype = CURLPROXY_SOCKS5; |
580 | 0 | else if(curl_strequal("socks4a", scheme)) |
581 | 0 | *proxytype = CURLPROXY_SOCKS4A; |
582 | 0 | else if(curl_strequal("socks4", scheme) || curl_strequal("socks", scheme)) |
583 | 0 | *proxytype = CURLPROXY_SOCKS4; |
584 | 0 | else if(curl_strequal("http", scheme)) { |
585 | 0 | if(*proxytype != CURLPROXY_HTTP_1_0) |
586 | 0 | *proxytype = CURLPROXY_HTTP; |
587 | 0 | } |
588 | 0 | else { |
589 | | /* Any other xxx:// reject! */ |
590 | 0 | failf(data, "Unsupported proxy scheme for \'%s\'", url); |
591 | 0 | return CURLE_COULDNT_CONNECT; |
592 | 0 | } |
593 | 0 | return CURLE_OK; |
594 | 0 | } |
595 | | |
596 | | CURLcode Curl_peer_from_proxy_url(CURLU *uh, |
597 | | struct Curl_easy *data, |
598 | | const char *url, |
599 | | uint8_t proxytype, |
600 | | struct Curl_peer **ppeer, |
601 | | uint8_t *pproxytype) |
602 | 0 | { |
603 | 0 | struct peer_parse pp; |
604 | 0 | char *scheme = NULL; |
605 | 0 | char *portptr = NULL; |
606 | 0 | #ifdef USE_UNIX_SOCKETS |
607 | 0 | bool is_socks = FALSE; |
608 | 0 | #endif |
609 | 0 | CURLUcode uc; |
610 | 0 | CURLcode result = CURLE_OK; |
611 | |
|
612 | 0 | Curl_peer_unlink(ppeer); |
613 | 0 | memset(&pp, 0, sizeof(pp)); |
614 | 0 | pp.port = CURL_DEFAULT_PROXY_PORT; |
615 | 0 | uc = curl_url_get(uh, CURLUPART_SCHEME, &scheme, |
616 | 0 | CURLU_NON_SUPPORT_SCHEME | CURLU_NO_GUESS_SCHEME); |
617 | 0 | if(uc) { |
618 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
619 | 0 | result = CURLE_OUT_OF_MEMORY; |
620 | 0 | goto out; |
621 | 0 | } |
622 | | /* URL came without scheme, the passed `proxytype` determines it */ |
623 | 0 | switch(proxytype) { |
624 | 0 | case CURLPROXY_HTTP: |
625 | 0 | case CURLPROXY_HTTP_1_0: |
626 | 0 | pp.scheme = &Curl_scheme_http; |
627 | 0 | break; |
628 | 0 | case CURLPROXY_HTTPS: |
629 | 0 | case CURLPROXY_HTTPS2: |
630 | 0 | case CURLPROXY_HTTPS3: |
631 | 0 | pp.scheme = &Curl_scheme_https; |
632 | 0 | break; |
633 | 0 | case CURLPROXY_SOCKS4: |
634 | 0 | pp.scheme = &Curl_scheme_socks4; |
635 | 0 | break; |
636 | 0 | case CURLPROXY_SOCKS4A: |
637 | 0 | pp.scheme = &Curl_scheme_socks4a; |
638 | 0 | break; |
639 | 0 | case CURLPROXY_SOCKS5: |
640 | 0 | pp.scheme = &Curl_scheme_socks5; |
641 | 0 | break; |
642 | 0 | case CURLPROXY_SOCKS5_HOSTNAME: |
643 | 0 | pp.scheme = &Curl_scheme_socks5h; |
644 | 0 | break; |
645 | 0 | default: |
646 | 0 | failf(data, "Unsupported proxy type %u for \'%s\'", proxytype, url); |
647 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
648 | 0 | goto out; |
649 | 0 | } |
650 | 0 | } |
651 | 0 | else { |
652 | 0 | pp.scheme = Curl_get_scheme(scheme); |
653 | 0 | result = Curl_scheme_to_proxytype(data, scheme, &proxytype, url); |
654 | 0 | if(result) |
655 | 0 | goto out; |
656 | 0 | } |
657 | 0 | DEBUGASSERT(pp.scheme); |
658 | |
|
659 | 0 | if(CURL_PROXY_IS_HTTPS(proxytype) && |
660 | 0 | !Curl_ssl_supports(data, SSLSUPP_HTTPS_PROXY)) { |
661 | 0 | failf(data, "Unsupported proxy \'%s\', libcurl is built without the " |
662 | 0 | "HTTPS-proxy support.", url); |
663 | 0 | result = CURLE_NOT_BUILT_IN; |
664 | 0 | goto out; |
665 | 0 | } |
666 | | |
667 | 0 | switch(pp.scheme->family) { |
668 | 0 | case CURLPROTO_SOCKS: |
669 | 0 | #ifdef USE_UNIX_SOCKETS |
670 | 0 | is_socks = TRUE; |
671 | 0 | #endif |
672 | 0 | break; |
673 | 0 | case CURLPROTO_HTTP: |
674 | 0 | break; |
675 | 0 | default: |
676 | 0 | failf(data, "Unsupported proxy protocol for \'%s\'", url); |
677 | 0 | result = CURLE_COULDNT_CONNECT; |
678 | 0 | goto out; |
679 | 0 | } |
680 | | |
681 | 0 | uc = curl_url_get(uh, CURLUPART_PORT, &portptr, 0); |
682 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
683 | 0 | result = CURLE_OUT_OF_MEMORY; |
684 | 0 | goto out; |
685 | 0 | } |
686 | 0 | if(portptr) { |
687 | 0 | curl_off_t num; |
688 | 0 | const char *p = portptr; |
689 | 0 | if(!curlx_str_number(&p, &num, UINT16_MAX)) |
690 | 0 | pp.port = (uint16_t)num; |
691 | | /* Should we not error out when the port number is invalid? */ |
692 | 0 | curlx_free(portptr); |
693 | 0 | } |
694 | 0 | else { |
695 | | /* No port in URL, take the set one or the proxy default */ |
696 | 0 | if(data->set.proxyport) |
697 | 0 | pp.port = data->set.proxyport; |
698 | 0 | else if(CURL_PROXY_IS_HTTPS(proxytype)) |
699 | 0 | pp.port = CURL_DEFAULT_HTTPS_PROXY_PORT; |
700 | 0 | else |
701 | 0 | pp.port = CURL_DEFAULT_PROXY_PORT; |
702 | 0 | } |
703 | | |
704 | | /* now, clone the proxy hostname */ |
705 | 0 | uc = curl_url_get(uh, CURLUPART_HOST, &pp.tmp_host_user, CURLU_URLDECODE); |
706 | 0 | if(uc) { |
707 | 0 | result = CURLE_OUT_OF_MEMORY; |
708 | 0 | goto out; |
709 | 0 | } |
710 | 0 | pp.host_user.str = pp.tmp_host_user; |
711 | 0 | pp.host_user.len = strlen(pp.tmp_host_user); |
712 | |
|
713 | 0 | #ifdef USE_UNIX_SOCKETS |
714 | 0 | if(is_socks && curl_strequal(UNIX_SOCKET_PREFIX, pp.tmp_host_user)) { |
715 | 0 | uc = curl_url_get(uh, CURLUPART_PATH, &pp.tmp_host, CURLU_URLDECODE); |
716 | 0 | if(uc) { |
717 | 0 | result = CURLE_OUT_OF_MEMORY; |
718 | 0 | goto out; |
719 | 0 | } |
720 | | /* path will be "/", if no path was found */ |
721 | 0 | if(strcmp("/", pp.tmp_host)) { |
722 | 0 | pp.host.str = pp.tmp_host; |
723 | 0 | pp.host.len = strlen(pp.tmp_host); |
724 | 0 | pp.unix_socket = TRUE; |
725 | 0 | } |
726 | 0 | else { |
727 | 0 | pp.host = pp.host_user; |
728 | 0 | } |
729 | 0 | } |
730 | 0 | #endif /* USE_UNIX_SOCKETS */ |
731 | | |
732 | 0 | if(!pp.host.len) { |
733 | 0 | result = peer_parse_host(data, &pp, FALSE); |
734 | 0 | if(result) |
735 | 0 | goto out; |
736 | 0 | } |
737 | | |
738 | 0 | uc = curl_url_get(uh, CURLUPART_ZONEID, &pp.tmp_zoneid, 0); |
739 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
740 | 0 | result = CURLE_OUT_OF_MEMORY; |
741 | 0 | goto out; |
742 | 0 | } |
743 | 0 | if(pp.tmp_zoneid) { |
744 | 0 | pp.zoneid.str = pp.tmp_zoneid; |
745 | 0 | pp.zoneid.len = strlen(pp.tmp_zoneid); |
746 | 0 | } |
747 | |
|
748 | 0 | *pproxytype = proxytype; |
749 | 0 | result = peer_create(&pp, ppeer); |
750 | |
|
751 | 0 | out: |
752 | 0 | peer_parse_clear(&pp); |
753 | 0 | curlx_free(scheme); |
754 | 0 | #ifdef DEBUGBUILD |
755 | 0 | if(!result) |
756 | 0 | DEBUGASSERT(*ppeer); |
757 | 0 | #endif |
758 | 0 | return result; |
759 | 0 | } |
760 | | |
761 | | #endif /* !CURL_DISABLE_PROXY */ |