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 "vtls/vtls.h" |
78 | | |
79 | | struct peer_parse { |
80 | | const struct Curl_scheme *scheme; |
81 | | struct Curl_str host_user; |
82 | | struct Curl_str host; |
83 | | struct Curl_str zoneid; |
84 | | char *tmp_host_user; |
85 | | char *tmp_host; |
86 | | char *tmp_zoneid; |
87 | | uint32_t scopeid; |
88 | | uint16_t port; |
89 | | bool ipv6; |
90 | | bool unix_socket; |
91 | | bool abstract_uds; |
92 | | }; |
93 | | |
94 | | static void peer_parse_clear(struct peer_parse *pp) |
95 | 0 | { |
96 | 0 | curlx_free(pp->tmp_host_user); |
97 | 0 | curlx_free(pp->tmp_host); |
98 | 0 | curlx_free(pp->tmp_zoneid); |
99 | 0 | memset(pp, 0, sizeof(*pp)); |
100 | 0 | } |
101 | | |
102 | | static CURLcode peer_create(struct peer_parse *pp, |
103 | | struct Curl_peer **ppeer) |
104 | 0 | { |
105 | 0 | struct Curl_peer *peer = NULL; |
106 | 0 | CURLcode result = CURLE_OK; |
107 | 0 | size_t zone_alen = 0, host_alen = 0; |
108 | |
|
109 | 0 | if(!pp || !pp->scheme) |
110 | 0 | return CURLE_FAILED_INIT; |
111 | 0 | if(!pp->host.len && !(pp->scheme->flags & PROTOPT_NONETWORK)) |
112 | 0 | return CURLE_FAILED_INIT; |
113 | | |
114 | 0 | if((pp->host.str != pp->host_user.str) || |
115 | 0 | (pp->host.len != pp->host_user.len)) { |
116 | 0 | host_alen = pp->host.len + 1; |
117 | 0 | } |
118 | 0 | zone_alen = pp->zoneid.len ? (pp->zoneid.len + 1) : 0; |
119 | | |
120 | | /* null-terminator already part of struct */ |
121 | 0 | peer = curlx_calloc(1, sizeof(*peer) + |
122 | 0 | pp->host_user.len + host_alen + zone_alen); |
123 | 0 | if(!peer) { |
124 | 0 | result = CURLE_OUT_OF_MEMORY; |
125 | 0 | goto out; |
126 | 0 | } |
127 | | |
128 | 0 | peer->refcount = 1; |
129 | 0 | peer->scheme = pp->scheme; |
130 | 0 | peer->hostname = peer->user_hostname; |
131 | 0 | peer->port = pp->port; |
132 | 0 | peer->scopeid = pp->scopeid; |
133 | 0 | peer->ipv6 = pp->ipv6; |
134 | 0 | peer->unix_socket = pp->unix_socket; |
135 | 0 | peer->abstract_uds = pp->abstract_uds; |
136 | |
|
137 | 0 | if(pp->host_user.len) |
138 | 0 | memcpy(peer->user_hostname, pp->host_user.str, pp->host_user.len); |
139 | |
|
140 | 0 | if(host_alen) { |
141 | 0 | peer->hostname = peer->user_hostname + pp->host_user.len + 1; |
142 | 0 | memcpy(peer->hostname, pp->host.str, pp->host.len); |
143 | 0 | } |
144 | |
|
145 | 0 | if(zone_alen) { |
146 | 0 | peer->zoneid = peer->user_hostname + pp->host_user.len + 1 + host_alen; |
147 | 0 | memcpy(peer->zoneid, pp->zoneid.str, pp->zoneid.len); |
148 | 0 | #ifdef USE_IPV6 |
149 | | /* Determine scope_id if not already provided */ |
150 | 0 | if(!peer->scopeid) { |
151 | 0 | const char *p = peer->zoneid; |
152 | 0 | curl_off_t scope; |
153 | 0 | if(!curlx_str_number(&p, &scope, UINT_MAX)) { |
154 | | /* A plain number, use it directly as a scope id. */ |
155 | 0 | peer->scopeid = (uint32_t)scope; |
156 | 0 | } |
157 | 0 | #ifdef HAVE_IF_NAMETOINDEX |
158 | 0 | else { |
159 | | /* Zone identifier is not numeric */ |
160 | 0 | unsigned int idx = 0; |
161 | 0 | idx = if_nametoindex(peer->zoneid); |
162 | 0 | if(idx) { |
163 | 0 | peer->scopeid = (uint32_t)idx; |
164 | 0 | } |
165 | 0 | else { |
166 | | /* Do we want to return an error here? */ |
167 | 0 | } |
168 | 0 | } |
169 | 0 | #endif /* HAVE_IF_NAMETOINDEX */ |
170 | 0 | } |
171 | 0 | #endif /* USE_IPV6 */ |
172 | 0 | } |
173 | |
|
174 | 0 | out: |
175 | 0 | if(!result) |
176 | 0 | *ppeer = peer; |
177 | 0 | else |
178 | 0 | Curl_peer_unlink(&peer); |
179 | 0 | return result; |
180 | 0 | } |
181 | | |
182 | | static CURLcode peer_parse_host(struct Curl_easy *data, |
183 | | struct peer_parse *pp, |
184 | | bool scan_for_ipv6) |
185 | 0 | { |
186 | 0 | if(!pp || !pp->host_user.str || !pp->host_user.len) |
187 | 0 | return CURLE_FAILED_INIT; |
188 | | |
189 | 0 | if(pp->host_user.str[0] == '[') { |
190 | 0 | const char *s = pp->host_user.str + 1; |
191 | 0 | struct Curl_str tmp; |
192 | 0 | if(curlx_str_until(&s, &tmp, pp->host_user.len - 1, ']')) |
193 | 0 | return CURLE_URL_MALFORMAT; |
194 | | |
195 | 0 | if(!Curl_looks_like_ipv6(tmp.str, tmp.len, TRUE, |
196 | 0 | &pp->host, &pp->zoneid)) { |
197 | 0 | failf(data, "Invalid IPv6 address format in '%.*s'", |
198 | 0 | (int)pp->host_user.len, pp->host_user.str); |
199 | 0 | return CURLE_URL_MALFORMAT; |
200 | 0 | } |
201 | 0 | pp->ipv6 = TRUE; |
202 | 0 | } |
203 | 0 | else { |
204 | 0 | #ifdef USE_IDN |
205 | 0 | if(!Curl_is_ASCII_str(&pp->host_user)) { |
206 | 0 | CURLcode result; |
207 | 0 | if(!pp->tmp_host_user) { |
208 | | /* need a null-terminated string for IDN */ |
209 | 0 | pp->tmp_host_user = curlx_memdup0(pp->host_user.str, |
210 | 0 | pp->host_user.len); |
211 | 0 | if(!pp->tmp_host_user) |
212 | 0 | return CURLE_OUT_OF_MEMORY; |
213 | 0 | } |
214 | 0 | result = Curl_idn_decode(pp->tmp_host_user, &pp->tmp_host); |
215 | 0 | if(result) |
216 | 0 | return result; |
217 | 0 | pp->host.str = pp->tmp_host; |
218 | 0 | pp->host.len = strlen(pp->host.str); |
219 | 0 | } |
220 | 0 | else |
221 | 0 | #endif |
222 | 0 | if(scan_for_ipv6 && |
223 | 0 | Curl_looks_like_ipv6(pp->host_user.str, pp->host_user.len, TRUE, |
224 | 0 | &pp->host, &pp->zoneid)) { |
225 | 0 | pp->ipv6 = TRUE; |
226 | 0 | } |
227 | 0 | else |
228 | 0 | pp->host = pp->host_user; |
229 | 0 | } |
230 | 0 | return CURLE_OK; |
231 | 0 | } |
232 | | |
233 | | CURLcode Curl_peer_create(struct Curl_easy *data, |
234 | | const struct Curl_scheme *scheme, |
235 | | const char *hostname, |
236 | | uint16_t port, |
237 | | struct Curl_peer **ppeer) |
238 | 0 | { |
239 | 0 | struct peer_parse pp; |
240 | 0 | CURLcode result; |
241 | |
|
242 | 0 | Curl_peer_unlink(ppeer); |
243 | 0 | memset(&pp, 0, sizeof(pp)); |
244 | 0 | pp.scheme = scheme; |
245 | 0 | pp.host_user.str = hostname; |
246 | 0 | pp.host_user.len = strlen(hostname); |
247 | 0 | pp.port = port; |
248 | |
|
249 | 0 | result = peer_parse_host(data, &pp, TRUE); |
250 | 0 | if(!result) |
251 | 0 | result = peer_create(&pp, ppeer); |
252 | |
|
253 | 0 | peer_parse_clear(&pp); |
254 | 0 | return result; |
255 | 0 | } |
256 | | |
257 | | #ifdef USE_UNIX_SOCKETS |
258 | | CURLcode Curl_peer_uds_create(const struct Curl_scheme *scheme, |
259 | | const char *path, |
260 | | bool abstract_unix_socket, |
261 | | struct Curl_peer **ppeer) |
262 | 0 | { |
263 | 0 | struct peer_parse pp; |
264 | 0 | size_t pathlen = path ? strlen(path) : 0; |
265 | 0 | CURLcode result = CURLE_OK; |
266 | |
|
267 | 0 | Curl_peer_unlink(ppeer); |
268 | 0 | memset(&pp, 0, sizeof(pp)); |
269 | 0 | if(!scheme) |
270 | 0 | return CURLE_FAILED_INIT; |
271 | 0 | if(!pathlen) |
272 | 0 | return CURLE_FAILED_INIT; |
273 | | |
274 | 0 | pp.scheme = scheme; |
275 | 0 | pp.host_user.str = pp.host.str = path; |
276 | 0 | pp.host_user.len = pp.host.len = pathlen; |
277 | 0 | pp.unix_socket = TRUE; |
278 | 0 | pp.abstract_uds = abstract_unix_socket; |
279 | |
|
280 | 0 | result = peer_create(&pp, ppeer); |
281 | 0 | peer_parse_clear(&pp); |
282 | 0 | return result; |
283 | 0 | } |
284 | | #endif /* USE_UNIX_SOCKETS */ |
285 | | |
286 | | void Curl_peer_link(struct Curl_peer **pdest, struct Curl_peer *src) |
287 | 0 | { |
288 | 0 | if(*pdest != src) { |
289 | 0 | Curl_peer_unlink(pdest); |
290 | 0 | *pdest = src; |
291 | 0 | if(src) { |
292 | 0 | DEBUGASSERT(src->refcount < UINT32_MAX); |
293 | 0 | src->refcount++; |
294 | 0 | } |
295 | 0 | } |
296 | 0 | } |
297 | | |
298 | | void Curl_peer_unlink(struct Curl_peer **ppeer) |
299 | 6.28k | { |
300 | 6.28k | if(*ppeer) { |
301 | 0 | struct Curl_peer *peer = *ppeer; |
302 | |
|
303 | 0 | DEBUGASSERT(peer->refcount); |
304 | 0 | *ppeer = NULL; |
305 | 0 | if(peer->refcount) |
306 | 0 | peer->refcount--; |
307 | 0 | if(!peer->refcount) { |
308 | 0 | curlx_free(peer); |
309 | 0 | } |
310 | 0 | } |
311 | 6.28k | } |
312 | | |
313 | | bool Curl_peer_equal(struct Curl_peer *p1, struct Curl_peer *p2) |
314 | 0 | { |
315 | 0 | return (p1 == p2) || |
316 | 0 | (p1 && p2 && |
317 | 0 | (p1->scheme == p2->scheme) && |
318 | 0 | Curl_peer_same_destination(p1, p2)); |
319 | 0 | } |
320 | | |
321 | | static bool peer_same_hostname(struct Curl_peer *p1, struct Curl_peer *p2) |
322 | 0 | { |
323 | | /* UNIX domain socket paths must be compared case-sensitive, |
324 | | * as many filesystem are like that. */ |
325 | 0 | return (p1->unix_socket == p2->unix_socket) && |
326 | 0 | (p1->abstract_uds == p2->abstract_uds) && |
327 | 0 | (p1->ipv6 == p2->ipv6) && |
328 | 0 | (p1->unix_socket ? |
329 | 0 | !strcmp(p1->hostname, p2->hostname) : |
330 | 0 | curl_strequal(p1->hostname, p2->hostname)); |
331 | 0 | } |
332 | | |
333 | | bool Curl_peer_same_destination(struct Curl_peer *p1, struct Curl_peer *p2) |
334 | 0 | { |
335 | 0 | return (p1 == p2) || |
336 | 0 | (p1 && p2 && |
337 | 0 | (p1->port == p2->port) && |
338 | 0 | peer_same_hostname(p1, p2) && |
339 | 0 | (p1->scopeid == p2->scopeid) && |
340 | 0 | (p1->scopeid || curl_strequal(p1->zoneid, p2->zoneid))); |
341 | 0 | } |
342 | | |
343 | | CURLcode Curl_peer_from_url(CURLU *uh, struct Curl_easy *data, |
344 | | uint16_t port_override, |
345 | | uint32_t scopeid_override, |
346 | | struct urlpieces *up, |
347 | | struct Curl_peer **ppeer) |
348 | 0 | { |
349 | 0 | struct peer_parse pp; |
350 | 0 | char *zoneid = NULL; |
351 | 0 | CURLUcode uc; |
352 | 0 | CURLcode result; |
353 | |
|
354 | 0 | Curl_peer_unlink(ppeer); |
355 | 0 | memset(&pp, 0, sizeof(pp)); |
356 | |
|
357 | 0 | curlx_safefree(up->scheme); |
358 | 0 | uc = curl_url_get(uh, CURLUPART_SCHEME, &up->scheme, 0); |
359 | 0 | if(uc) |
360 | 0 | return Curl_uc_to_curlcode(uc); |
361 | 0 | pp.scheme = Curl_get_scheme(up->scheme); |
362 | 0 | if(!pp.scheme) { |
363 | 0 | failf(data, "Protocol \"%s\" not supported%s", up->scheme, |
364 | 0 | data->state.this_is_a_follow ? " (in redirect)" : ""); |
365 | 0 | result = CURLE_UNSUPPORTED_PROTOCOL; |
366 | 0 | goto out; |
367 | 0 | } |
368 | | |
369 | 0 | curlx_safefree(up->hostname); |
370 | 0 | uc = curl_url_get(uh, CURLUPART_HOST, &up->hostname, 0); |
371 | 0 | if(uc) { |
372 | 0 | if((uc == CURLUE_NO_HOST) && (pp.scheme->flags & PROTOPT_NONETWORK)) |
373 | 0 | ; /* acceptable */ |
374 | 0 | else { |
375 | 0 | result = CURLE_OUT_OF_MEMORY; |
376 | 0 | goto out; |
377 | 0 | } |
378 | 0 | } |
379 | 0 | else if(strlen(up->hostname) > MAX_URL_LEN) { |
380 | 0 | failf(data, "Too long hostname (maximum is %d)", MAX_URL_LEN); |
381 | 0 | result = CURLE_URL_MALFORMAT; |
382 | 0 | goto out; |
383 | 0 | } |
384 | | |
385 | 0 | pp.host_user.str = up->hostname ? up->hostname : ""; |
386 | 0 | pp.host_user.len = strlen(pp.host_user.str); |
387 | 0 | if(pp.host_user.len) { |
388 | 0 | result = peer_parse_host(data, &pp, FALSE); |
389 | 0 | if(result) |
390 | 0 | goto out; |
391 | 0 | } |
392 | 0 | else |
393 | 0 | pp.host = pp.host_user; |
394 | | |
395 | 0 | curlx_safefree(up->port); |
396 | 0 | if(port_override) { |
397 | | /* if set, we use this instead of the port possibly given in the URL */ |
398 | 0 | char portbuf[16]; |
399 | 0 | curl_msnprintf(portbuf, sizeof(portbuf), "%d", port_override); |
400 | 0 | uc = curl_url_set(uh, CURLUPART_PORT, portbuf, 0); |
401 | 0 | if(uc) { |
402 | 0 | result = CURLE_OUT_OF_MEMORY; |
403 | 0 | goto out; |
404 | 0 | } |
405 | 0 | else |
406 | 0 | pp.port = port_override; |
407 | 0 | } |
408 | 0 | else { |
409 | 0 | uc = curl_url_get(uh, CURLUPART_PORT, &up->port, CURLU_DEFAULT_PORT); |
410 | 0 | if(uc) { |
411 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
412 | 0 | result = CURLE_OUT_OF_MEMORY; |
413 | 0 | goto out; |
414 | 0 | } |
415 | 0 | else if(!(pp.scheme->flags & PROTOPT_NONETWORK)) { |
416 | 0 | result = CURLE_URL_MALFORMAT; |
417 | 0 | goto out; |
418 | 0 | } |
419 | | /* no port ok when not a network scheme */ |
420 | 0 | } |
421 | 0 | else { |
422 | 0 | const char *p = up->port; |
423 | 0 | curl_off_t offt; |
424 | 0 | if(curlx_str_number(&p, &offt, 0xffff)) |
425 | 0 | return CURLE_URL_MALFORMAT; |
426 | 0 | pp.port = (uint16_t)offt; |
427 | 0 | } |
428 | 0 | } |
429 | | |
430 | 0 | if(scopeid_override) |
431 | | /* Override any scope id from an url zone. */ |
432 | 0 | pp.scopeid = scopeid_override; |
433 | 0 | else { |
434 | 0 | if(curl_url_get(uh, CURLUPART_ZONEID, &zoneid, 0) == |
435 | 0 | CURLUE_OUT_OF_MEMORY) { |
436 | 0 | result = CURLE_OUT_OF_MEMORY; |
437 | 0 | goto out; |
438 | 0 | } |
439 | 0 | if(zoneid) { |
440 | 0 | pp.zoneid.str = zoneid; |
441 | 0 | pp.zoneid.len = strlen(zoneid); |
442 | 0 | } |
443 | 0 | } |
444 | | |
445 | 0 | result = peer_create(&pp, ppeer); |
446 | 0 | if(result) |
447 | 0 | failf(data, "Error %d creating peer for %s:%u", |
448 | 0 | result, pp.host_user.str, pp.port); |
449 | |
|
450 | 0 | out: |
451 | 0 | peer_parse_clear(&pp); |
452 | 0 | curlx_free(zoneid); |
453 | 0 | return result; |
454 | 0 | } |
455 | | |
456 | | /* Parse a "host:port" string to connect to into a peer. |
457 | | * IPv6 addresses might appear in brackets or without them. */ |
458 | | CURLcode Curl_peer_from_connect_to(struct Curl_easy *data, |
459 | | const struct Curl_peer *dest, |
460 | | const char *connect_to, |
461 | | struct Curl_peer **ppeer) |
462 | 0 | { |
463 | 0 | struct peer_parse pp; |
464 | 0 | const char *portstr = NULL; |
465 | 0 | CURLcode result; |
466 | |
|
467 | 0 | Curl_peer_unlink(ppeer); |
468 | 0 | memset(&pp, 0, sizeof(pp)); |
469 | 0 | if(!connect_to || !*connect_to) |
470 | 0 | return CURLE_FAILED_INIT; |
471 | | |
472 | 0 | pp.scheme = dest->scheme; |
473 | | |
474 | | /* detect and extract RFC6874-style IPv6-addresses */ |
475 | 0 | if(connect_to[0] == '[') { |
476 | 0 | const char *s = strchr(connect_to + 1, ']'); |
477 | 0 | if(!s) { |
478 | 0 | failf(data, "Invalid IPv6 address format in '%s'", connect_to); |
479 | 0 | result = CURLE_SETOPT_OPTION_SYNTAX; |
480 | 0 | goto out; |
481 | 0 | } |
482 | 0 | portstr = strchr(s, ':'); |
483 | 0 | pp.host_user.str = connect_to; |
484 | 0 | pp.host_user.len = s - pp.host_user.str + 1; |
485 | 0 | pp.ipv6 = TRUE; |
486 | 0 | } |
487 | 0 | else { |
488 | 0 | portstr = strchr(connect_to, ':'); |
489 | 0 | pp.host_user.str = connect_to; |
490 | 0 | pp.host_user.len = portstr ? |
491 | 0 | (size_t)(portstr - connect_to) : strlen(connect_to); |
492 | 0 | } |
493 | | |
494 | 0 | if(!pp.host_user.len) { /* no hostname found, only port switch */ |
495 | 0 | pp.host_user.str = dest->user_hostname; |
496 | 0 | pp.host_user.len = strlen(dest->user_hostname); |
497 | 0 | } |
498 | |
|
499 | 0 | result = peer_parse_host(data, &pp, FALSE); |
500 | 0 | if(result) |
501 | 0 | goto out; |
502 | | |
503 | 0 | if(portstr && portstr[1]) { |
504 | 0 | const char *p = portstr + 1; |
505 | 0 | curl_off_t portparse; |
506 | 0 | if(curlx_str_number(&p, &portparse, 0xffff)) { |
507 | 0 | failf(data, "No valid port number in '%s'", connect_to); |
508 | 0 | result = CURLE_SETOPT_OPTION_SYNTAX; |
509 | 0 | goto out; |
510 | 0 | } |
511 | 0 | pp.port = (uint16_t)portparse; /* we know it will fit */ |
512 | 0 | } |
513 | 0 | else |
514 | 0 | pp.port = dest->port; |
515 | | |
516 | | #ifndef USE_IPV6 |
517 | | if(pp.ipv6) { |
518 | | failf(data, "Use of IPv6 in *_CONNECT_TO without IPv6 support built-in"); |
519 | | result = CURLE_NOT_BUILT_IN; |
520 | | goto out; |
521 | | } |
522 | | #endif |
523 | | |
524 | 0 | result = peer_create(&pp, ppeer); |
525 | 0 | CURL_TRC_M(data, "connect-to peer_create2 -> %d", result); |
526 | |
|
527 | 0 | out: |
528 | 0 | CURL_TRC_M(data, "parse connect_to peer: %s -> %d", connect_to, result); |
529 | 0 | peer_parse_clear(&pp); |
530 | 0 | return result; |
531 | 0 | } |
532 | | |
533 | | #ifndef CURL_DISABLE_PROXY |
534 | | |
535 | | #ifdef USE_UNIX_SOCKETS |
536 | 0 | #define UNIX_SOCKET_PREFIX "localhost" |
537 | | #endif |
538 | | |
539 | | CURLcode Curl_scheme_to_proxytype(struct Curl_easy *data, |
540 | | const char *scheme, |
541 | | uint8_t *proxytype, const char *url) |
542 | 0 | { |
543 | 0 | if(!scheme) |
544 | 0 | return CURLE_OK; |
545 | | |
546 | 0 | if(curl_strequal("https", scheme)) { |
547 | 0 | if(*proxytype != CURLPROXY_HTTPS2 && *proxytype != CURLPROXY_HTTPS3) |
548 | 0 | *proxytype = CURLPROXY_HTTPS; |
549 | 0 | } |
550 | 0 | else if(curl_strequal("socks5h", scheme)) |
551 | 0 | *proxytype = CURLPROXY_SOCKS5_HOSTNAME; |
552 | 0 | else if(curl_strequal("socks5", scheme)) |
553 | 0 | *proxytype = CURLPROXY_SOCKS5; |
554 | 0 | else if(curl_strequal("socks4a", scheme)) |
555 | 0 | *proxytype = CURLPROXY_SOCKS4A; |
556 | 0 | else if(curl_strequal("socks4", scheme) || curl_strequal("socks", scheme)) |
557 | 0 | *proxytype = CURLPROXY_SOCKS4; |
558 | 0 | else if(curl_strequal("http", scheme)) { |
559 | 0 | if(*proxytype != CURLPROXY_HTTP_1_0) |
560 | 0 | *proxytype = CURLPROXY_HTTP; |
561 | 0 | } |
562 | 0 | else { |
563 | | /* Any other xxx:// reject! */ |
564 | 0 | failf(data, "Unsupported proxy scheme for \'%s\'", url); |
565 | 0 | return CURLE_COULDNT_CONNECT; |
566 | 0 | } |
567 | 0 | return CURLE_OK; |
568 | 0 | } |
569 | | |
570 | | CURLcode Curl_peer_from_proxy_url(CURLU *uh, |
571 | | struct Curl_easy *data, |
572 | | const char *url, |
573 | | uint8_t proxytype, |
574 | | struct Curl_peer **ppeer, |
575 | | uint8_t *pproxytype) |
576 | 0 | { |
577 | 0 | struct peer_parse pp; |
578 | 0 | char *scheme = NULL; |
579 | 0 | char *portptr = NULL; |
580 | 0 | #ifdef USE_UNIX_SOCKETS |
581 | 0 | bool is_socks = FALSE; |
582 | 0 | #endif |
583 | 0 | CURLUcode uc; |
584 | 0 | CURLcode result = CURLE_OK; |
585 | |
|
586 | 0 | Curl_peer_unlink(ppeer); |
587 | 0 | memset(&pp, 0, sizeof(pp)); |
588 | 0 | pp.port = CURL_DEFAULT_PROXY_PORT; |
589 | 0 | uc = curl_url_get(uh, CURLUPART_SCHEME, &scheme, |
590 | 0 | CURLU_NON_SUPPORT_SCHEME | CURLU_NO_GUESS_SCHEME); |
591 | 0 | if(uc) { |
592 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
593 | 0 | result = CURLE_OUT_OF_MEMORY; |
594 | 0 | goto out; |
595 | 0 | } |
596 | | /* url came without scheme, the passed `proxytype` determines it */ |
597 | 0 | switch(proxytype) { |
598 | 0 | case CURLPROXY_HTTP: |
599 | 0 | case CURLPROXY_HTTP_1_0: |
600 | 0 | pp.scheme = &Curl_scheme_http; |
601 | 0 | break; |
602 | 0 | case CURLPROXY_HTTPS: |
603 | 0 | case CURLPROXY_HTTPS2: |
604 | 0 | case CURLPROXY_HTTPS3: |
605 | 0 | pp.scheme = &Curl_scheme_https; |
606 | 0 | break; |
607 | 0 | case CURLPROXY_SOCKS4: |
608 | 0 | pp.scheme = &Curl_scheme_socks4; |
609 | 0 | break; |
610 | 0 | case CURLPROXY_SOCKS4A: |
611 | 0 | pp.scheme = &Curl_scheme_socks4a; |
612 | 0 | break; |
613 | 0 | case CURLPROXY_SOCKS5: |
614 | 0 | pp.scheme = &Curl_scheme_socks5; |
615 | 0 | break; |
616 | 0 | case CURLPROXY_SOCKS5_HOSTNAME: |
617 | 0 | pp.scheme = &Curl_scheme_socks5h; |
618 | 0 | break; |
619 | 0 | default: |
620 | 0 | failf(data, "Unsupported proxy type %u for \'%s\'", proxytype, url); |
621 | 0 | result = CURLE_COULDNT_RESOLVE_PROXY; |
622 | 0 | goto out; |
623 | 0 | } |
624 | 0 | } |
625 | 0 | else { |
626 | 0 | pp.scheme = Curl_get_scheme(scheme); |
627 | 0 | result = Curl_scheme_to_proxytype(data, scheme, &proxytype, url); |
628 | 0 | if(result) |
629 | 0 | goto out; |
630 | 0 | } |
631 | 0 | DEBUGASSERT(pp.scheme); |
632 | |
|
633 | 0 | if(IS_HTTPS_PROXY(proxytype) && |
634 | 0 | !Curl_ssl_supports(data, SSLSUPP_HTTPS_PROXY)) { |
635 | 0 | failf(data, "Unsupported proxy \'%s\', libcurl is built without the " |
636 | 0 | "HTTPS-proxy support.", url); |
637 | 0 | result = CURLE_NOT_BUILT_IN; |
638 | 0 | goto out; |
639 | 0 | } |
640 | | |
641 | 0 | switch(pp.scheme->family) { |
642 | 0 | case CURLPROTO_SOCKS: |
643 | 0 | #ifdef USE_UNIX_SOCKETS |
644 | 0 | is_socks = TRUE; |
645 | 0 | #endif |
646 | 0 | break; |
647 | 0 | case CURLPROTO_HTTP: |
648 | 0 | break; |
649 | 0 | default: |
650 | 0 | failf(data, "Unsupported proxy protocol for \'%s\'", url); |
651 | 0 | result = CURLE_COULDNT_CONNECT; |
652 | 0 | goto out; |
653 | 0 | } |
654 | | |
655 | 0 | uc = curl_url_get(uh, CURLUPART_PORT, &portptr, CURLU_NO_DEFAULT_PORT); |
656 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
657 | 0 | result = CURLE_OUT_OF_MEMORY; |
658 | 0 | goto out; |
659 | 0 | } |
660 | 0 | if(portptr) { |
661 | 0 | curl_off_t num; |
662 | 0 | const char *p = portptr; |
663 | 0 | if(!curlx_str_number(&p, &num, UINT16_MAX)) |
664 | 0 | pp.port = (uint16_t)num; |
665 | | /* Should we not error out when the port number is invalid? */ |
666 | 0 | curlx_free(portptr); |
667 | 0 | } |
668 | 0 | else { |
669 | | /* No port in url, take the set one or the scheme's default */ |
670 | 0 | if(data->set.proxyport) |
671 | 0 | pp.port = data->set.proxyport; |
672 | 0 | else |
673 | 0 | pp.port = pp.scheme->defport; |
674 | 0 | } |
675 | | |
676 | | /* now, clone the proxy hostname */ |
677 | 0 | uc = curl_url_get(uh, CURLUPART_HOST, &pp.tmp_host_user, CURLU_URLDECODE); |
678 | 0 | if(uc) { |
679 | 0 | result = CURLE_OUT_OF_MEMORY; |
680 | 0 | goto out; |
681 | 0 | } |
682 | 0 | pp.host_user.str = pp.tmp_host_user; |
683 | 0 | pp.host_user.len = strlen(pp.tmp_host_user); |
684 | |
|
685 | 0 | #ifdef USE_UNIX_SOCKETS |
686 | 0 | if(is_socks && curl_strequal(UNIX_SOCKET_PREFIX, pp.tmp_host_user)) { |
687 | 0 | uc = curl_url_get(uh, CURLUPART_PATH, &pp.tmp_host, CURLU_URLDECODE); |
688 | 0 | if(uc) { |
689 | 0 | result = CURLE_OUT_OF_MEMORY; |
690 | 0 | goto out; |
691 | 0 | } |
692 | | /* path will be "/", if no path was found */ |
693 | 0 | if(strcmp("/", pp.tmp_host)) { |
694 | 0 | pp.host.str = pp.tmp_host; |
695 | 0 | pp.host.len = strlen(pp.tmp_host); |
696 | 0 | pp.unix_socket = TRUE; |
697 | 0 | } |
698 | 0 | else { |
699 | 0 | pp.host = pp.host_user; |
700 | 0 | } |
701 | 0 | } |
702 | 0 | #endif /* USE_UNIX_SOCKETS */ |
703 | | |
704 | 0 | if(!pp.host.len) { |
705 | 0 | result = peer_parse_host(data, &pp, FALSE); |
706 | 0 | if(result) |
707 | 0 | goto out; |
708 | 0 | } |
709 | | |
710 | 0 | uc = curl_url_get(uh, CURLUPART_ZONEID, &pp.tmp_zoneid, 0); |
711 | 0 | if(uc == CURLUE_OUT_OF_MEMORY) { |
712 | 0 | result = CURLE_OUT_OF_MEMORY; |
713 | 0 | goto out; |
714 | 0 | } |
715 | 0 | if(pp.tmp_zoneid) { |
716 | 0 | pp.zoneid.str = pp.tmp_zoneid; |
717 | 0 | pp.zoneid.len = strlen(pp.tmp_zoneid); |
718 | 0 | } |
719 | |
|
720 | 0 | *pproxytype = proxytype; |
721 | 0 | result = peer_create(&pp, ppeer); |
722 | |
|
723 | 0 | out: |
724 | 0 | peer_parse_clear(&pp); |
725 | 0 | curlx_free(scheme); |
726 | 0 | #ifdef DEBUGBUILD |
727 | 0 | if(!result) |
728 | 0 | DEBUGASSERT(*ppeer); |
729 | 0 | #endif |
730 | 0 | return result; |
731 | 0 | } |
732 | | |
733 | | #endif /* !CURL_DISABLE_PROXY */ |