Coverage Report

Created: 2026-08-15 06:32

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