Coverage Report

Created: 2026-07-30 07:26

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
352k
{
97
352k
  curlx_free(pp->tmp_host_user);
98
352k
  curlx_free(pp->tmp_host);
99
352k
  curlx_free(pp->tmp_zoneid);
100
352k
  memset(pp, 0, sizeof(*pp));
101
352k
}
102
103
static CURLcode peer_create(struct peer_parse *pp,
104
                            struct Curl_peer **ppeer)
105
349k
{
106
349k
  struct Curl_peer *peer = NULL;
107
349k
  CURLcode result = CURLE_OK;
108
349k
  size_t zone_alen = 0, host_alen = 0;
109
110
349k
  if(!pp || !pp->scheme)
111
0
    return CURLE_FAILED_INIT;
112
349k
  if(!pp->host.len && !(pp->scheme->flags & PROTOPT_NONETWORK))
113
14
    return CURLE_FAILED_INIT;
114
115
349k
  if((pp->host.str != pp->host_user.str) ||
116
345k
     (pp->host.len != pp->host_user.len)) {
117
4.88k
    host_alen = pp->host.len + 1;
118
4.88k
  }
119
349k
  zone_alen = pp->zoneid.len ? (pp->zoneid.len + 1) : 0;
120
121
  /* null-terminator already part of struct */
122
349k
  peer = curlx_calloc(1, sizeof(*peer) +
123
349k
                         pp->host_user.len + host_alen + zone_alen);
124
349k
  if(!peer) {
125
0
    result = CURLE_OUT_OF_MEMORY;
126
0
    goto out;
127
0
  }
128
129
349k
  peer->refcount = 1;
130
349k
  peer->scheme = pp->scheme;
131
349k
  peer->hostname = peer->user_hostname;
132
349k
  peer->port = pp->port;
133
349k
  peer->scopeid = pp->scopeid;
134
349k
  peer->ipv6 = pp->ipv6;
135
349k
  peer->unix_socket = pp->unix_socket;
136
349k
  peer->abstract_uds = pp->abstract_uds;
137
138
349k
  if(pp->host_user.len)
139
347k
    memcpy(peer->user_hostname, pp->host_user.str, pp->host_user.len);
140
141
349k
  if(host_alen) {
142
4.88k
    peer->hostname = peer->user_hostname + pp->host_user.len + 1;
143
4.88k
    memcpy(peer->hostname, pp->host.str, pp->host.len);
144
4.88k
  }
145
146
349k
  if(zone_alen) {
147
1.57k
    peer->zoneid = peer->user_hostname + pp->host_user.len + 1 + host_alen;
148
1.57k
    memcpy(peer->zoneid, pp->zoneid.str, pp->zoneid.len);
149
1.57k
#ifdef USE_IPV6
150
    /* Determine scope_id if not already provided */
151
1.57k
    if(!peer->scopeid) {
152
1.57k
      const char *p = peer->zoneid;
153
1.57k
      curl_off_t scope;
154
1.57k
      if(!curlx_str_number(&p, &scope, UINT_MAX)) {
155
        /* A plain number, use it directly as a scope id. */
156
348
        peer->scopeid = (uint32_t)scope;
157
348
      }
158
1.23k
#ifdef HAVE_IF_NAMETOINDEX
159
1.23k
      else {
160
        /* Zone identifier is not numeric */
161
1.23k
        unsigned int idx = 0;
162
1.23k
        idx = if_nametoindex(peer->zoneid);
163
1.23k
        if(idx) {
164
20
          peer->scopeid = (uint32_t)idx;
165
20
        }
166
1.21k
        else {
167
          /* Do we want to return an error here? */
168
1.21k
        }
169
1.23k
      }
170
1.57k
#endif /* HAVE_IF_NAMETOINDEX */
171
1.57k
    }
172
1.57k
#endif /* USE_IPV6 */
173
1.57k
  }
174
175
349k
out:
176
349k
  if(!result)
177
349k
    *ppeer = peer;
178
0
  else
179
0
    Curl_peer_unlink(&peer);
180
349k
  return result;
181
349k
}
182
183
static CURLcode peer_parse_host(struct Curl_easy *data,
184
                                struct peer_parse *pp,
185
                                bool scan_for_ipv6)
186
340k
{
187
340k
  if(!pp || !pp->host_user.str || !pp->host_user.len)
188
0
    return CURLE_FAILED_INIT;
189
190
340k
  if(pp->host_user.str[0] == '[') {
191
4.83k
    const char *s = pp->host_user.str + 1;
192
4.83k
    struct Curl_str tmp;
193
4.83k
    if(curlx_str_until(&s, &tmp, pp->host_user.len - 1, ']'))
194
1
      return CURLE_URL_MALFORMAT;
195
196
4.83k
    if(!Curl_looks_like_ipv6(tmp.str, tmp.len, TRUE,
197
4.83k
                             &pp->host, &pp->zoneid)) {
198
4
      failf(data, "Invalid IPv6 address format in '%.*s'",
199
4
            (int)pp->host_user.len, pp->host_user.str);
200
4
      return CURLE_URL_MALFORMAT;
201
4
    }
202
4.82k
    pp->ipv6 = TRUE;
203
4.82k
  }
204
336k
  else {
205
336k
#ifdef USE_IDN
206
336k
    if(!Curl_is_ASCII_str(&pp->host_user)) {
207
1.79k
      CURLcode result;
208
1.79k
      if(!pp->tmp_host_user) {
209
        /* need a null-terminated string for IDN */
210
1.71k
        pp->tmp_host_user = curlx_memdup0(pp->host_user.str,
211
1.71k
                                          pp->host_user.len);
212
1.71k
        if(!pp->tmp_host_user)
213
0
          return CURLE_OUT_OF_MEMORY;
214
1.71k
      }
215
1.79k
      result = Curl_idn_decode(pp->tmp_host_user, &pp->tmp_host);
216
1.79k
      if(result)
217
1.79k
        return result;
218
0
      pp->host.str = pp->tmp_host;
219
0
      pp->host.len = strlen(pp->host.str);
220
0
    }
221
334k
    else
222
334k
#endif
223
334k
    if(scan_for_ipv6 &&
224
2.15k
       Curl_looks_like_ipv6(pp->host_user.str, pp->host_user.len, TRUE,
225
2.15k
                            &pp->host, &pp->zoneid)) {
226
2.04k
      if(pp->host_user.len < MAX_IPADR_LEN) {
227
2.04k
        char tmp[MAX_IPADR_LEN];
228
2.04k
        memcpy(tmp, pp->host_user.str, pp->host_user.len);
229
2.04k
        tmp[pp->host_user.len] = 0;
230
2.04k
        pp->ipv6 = !Curl_is_ipv4addr(tmp);
231
2.04k
      }
232
1
      else
233
1
        pp->ipv6 = TRUE;
234
2.04k
    }
235
332k
    else
236
332k
      pp->host = pp->host_user;
237
336k
  }
238
339k
  return CURLE_OK;
239
340k
}
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
2.16k
{
247
2.16k
  struct peer_parse pp;
248
2.16k
  CURLcode result;
249
250
2.16k
  Curl_peer_unlink(ppeer);
251
2.16k
  memset(&pp, 0, sizeof(pp));
252
2.16k
  pp.scheme = scheme;
253
2.16k
  pp.host_user.str = hostname;
254
2.16k
  pp.host_user.len = strlen(hostname);
255
2.16k
  pp.port = port;
256
257
2.16k
  result = peer_parse_host(data, &pp, TRUE);
258
2.16k
  if(!result)
259
2.15k
    result = peer_create(&pp, ppeer);
260
261
2.16k
  peer_parse_clear(&pp);
262
2.16k
  return result;
263
2.16k
}
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
8.31k
{
271
8.31k
  struct peer_parse pp;
272
8.31k
  size_t pathlen = path ? strlen(path) : 0;
273
8.31k
  CURLcode result = CURLE_OK;
274
275
8.31k
  Curl_peer_unlink(ppeer);
276
8.31k
  memset(&pp, 0, sizeof(pp));
277
8.31k
  if(!scheme)
278
0
    return CURLE_FAILED_INIT;
279
8.31k
  if(!pathlen)
280
14
    return CURLE_FAILED_INIT;
281
282
8.30k
  pp.scheme = scheme;
283
8.30k
  pp.host_user.str = pp.host.str = path;
284
8.30k
  pp.host_user.len = pp.host.len = pathlen;
285
8.30k
  pp.unix_socket = TRUE;
286
8.30k
  pp.abstract_uds = abstract_unix_socket;
287
288
8.30k
  result = peer_create(&pp, ppeer);
289
8.30k
  peer_parse_clear(&pp);
290
8.30k
  return result;
291
8.31k
}
292
#endif /* USE_UNIX_SOCKETS */
293
294
void Curl_peer_link(struct Curl_peer **pdest, struct Curl_peer *src)
295
1.60M
{
296
1.60M
  if(*pdest != src) {
297
1.36M
    Curl_peer_unlink(pdest);
298
1.36M
    *pdest = src;
299
1.36M
    if(src) {
300
1.36M
      DEBUGASSERT(src->refcount < UINT32_MAX);
301
1.36M
      src->refcount++;
302
1.36M
    }
303
1.36M
  }
304
1.60M
}
305
306
void Curl_peer_unlink(struct Curl_peer **ppeer)
307
7.04M
{
308
7.04M
  if(*ppeer) {
309
1.71M
    struct Curl_peer *peer = *ppeer;
310
311
1.71M
    DEBUGASSERT(peer->refcount);
312
1.71M
    *ppeer = NULL;
313
1.71M
    if(peer->refcount)
314
1.71M
      peer->refcount--;
315
1.71M
    if(!peer->refcount) {
316
349k
      curlx_free(peer);
317
349k
    }
318
1.71M
  }
319
7.04M
}
320
321
bool Curl_peer_equal(struct Curl_peer *p1, struct Curl_peer *p2)
322
690k
{
323
690k
  return (p1 == p2) ||
324
236k
         (p1 && p2 &&
325
225k
          (p1->scheme == p2->scheme) &&
326
219k
          Curl_peer_same_destination(p1, p2));
327
690k
}
328
329
static bool peer_same_hostname(struct Curl_peer *p1, struct Curl_peer *p2)
330
255k
{
331
  /* UNIX domain socket paths must be compared case-sensitive,
332
   * as many filesystem are like that. */
333
255k
  return (p1->unix_socket == p2->unix_socket) &&
334
255k
         (p1->abstract_uds == p2->abstract_uds) &&
335
255k
         (p1->ipv6 == p2->ipv6) &&
336
252k
         (p1->unix_socket ?
337
7.09k
          !strcmp(p1->hostname, p2->hostname) :
338
252k
          curl_strequal(p1->hostname, p2->hostname));
339
255k
}
340
341
bool Curl_peer_same_destination(struct Curl_peer *p1, struct Curl_peer *p2)
342
692k
{
343
692k
  return (p1 == p2) ||
344
269k
         (p1 && p2 &&
345
269k
          (p1->port == p2->port) &&
346
255k
          peer_same_hostname(p1, p2) &&
347
116k
          (p1->scopeid == p2->scopeid) &&
348
116k
          (p1->scopeid || curl_strequal(p1->zoneid, p2->zoneid)));
349
692k
}
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
165k
{
356
165k
  struct peer_parse pp;
357
165k
  char *zoneid = NULL, *scheme = NULL, *hostname = NULL;
358
165k
  CURLUcode uc;
359
165k
  CURLcode result;
360
361
165k
  Curl_peer_unlink(ppeer);
362
165k
  memset(&pp, 0, sizeof(pp));
363
364
165k
  uc = curl_url_get(uh, CURLUPART_SCHEME, &scheme, 0);
365
165k
  if(uc)
366
0
    return Curl_uc_to_curlcode(uc);
367
165k
  pp.scheme = Curl_get_scheme(scheme);
368
165k
  if(!pp.scheme) {
369
491
    failf(data, "Protocol \"%s\" not supported%s", scheme,
370
491
          data->state.this_is_a_follow ? " (in redirect)" : "");
371
491
    result = CURLE_UNSUPPORTED_PROTOCOL;
372
491
    goto out;
373
491
  }
374
375
164k
  uc = curl_url_get(uh, CURLUPART_HOST, &hostname, 0);
376
164k
  if(uc) {
377
2.00k
    if((uc == CURLUE_NO_HOST) && (pp.scheme->flags & PROTOPT_NONETWORK))
378
2.00k
      ; /* acceptable */
379
0
    else {
380
0
      result = CURLE_OUT_OF_MEMORY;
381
0
      goto out;
382
0
    }
383
2.00k
  }
384
162k
  else if(strlen(hostname) > MAX_URL_LEN) {
385
16
    failf(data, "Too long hostname (maximum is %d)", MAX_URL_LEN);
386
16
    result = CURLE_URL_MALFORMAT;
387
16
    goto out;
388
16
  }
389
390
164k
  pp.host_user.str = hostname ? hostname : "";
391
164k
  pp.host_user.len = strlen(pp.host_user.str);
392
164k
  if(pp.host_user.len) {
393
162k
    result = peer_parse_host(data, &pp, FALSE);
394
162k
    if(result)
395
1.70k
      goto out;
396
162k
  }
397
2.00k
  else
398
2.00k
    pp.host = pp.host_user;
399
400
163k
  if(port_override) {
401
    /* if set, we use this instead of the port possibly given in the URL */
402
1.94k
    char portbuf[16];
403
1.94k
    curl_msnprintf(portbuf, sizeof(portbuf), "%d", port_override);
404
1.94k
    uc = curl_url_set(uh, CURLUPART_PORT, portbuf, 0);
405
1.94k
    if(uc) {
406
0
      result = CURLE_OUT_OF_MEMORY;
407
0
      goto out;
408
0
    }
409
1.94k
    else
410
1.94k
      pp.port = port_override;
411
1.94k
  }
412
161k
  else {
413
161k
    uc = Curl_url_get_port(uh, &pp.port);
414
161k
    if(uc) {
415
1.97k
      if(uc == CURLUE_OUT_OF_MEMORY) {
416
0
        result = CURLE_OUT_OF_MEMORY;
417
0
        goto out;
418
0
      }
419
1.97k
      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
1.97k
    }
425
161k
  }
426
427
163k
  if(scopeid_override)
428
    /* Override any scope id from an URL zone. */
429
3.82k
    pp.scopeid = scopeid_override;
430
159k
  else {
431
159k
    if(curl_url_get(uh, CURLUPART_ZONEID, &zoneid, 0) ==
432
159k
       CURLUE_OUT_OF_MEMORY) {
433
0
      result = CURLE_OUT_OF_MEMORY;
434
0
      goto out;
435
0
    }
436
159k
    if(zoneid) {
437
1.22k
      pp.zoneid.str = zoneid;
438
1.22k
      pp.zoneid.len = strlen(zoneid);
439
1.22k
    }
440
159k
  }
441
442
163k
  result = peer_create(&pp, ppeer);
443
163k
  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
165k
out:
448
165k
  peer_parse_clear(&pp);
449
165k
  curlx_free(scheme);
450
165k
  curlx_free(hostname);
451
165k
  curlx_free(zoneid);
452
165k
  return result;
453
163k
}
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
136k
{
462
136k
  struct peer_parse pp;
463
136k
  const char *portstr = NULL;
464
136k
  CURLcode result;
465
466
136k
  Curl_peer_unlink(ppeer);
467
136k
  memset(&pp, 0, sizeof(pp));
468
136k
  if(!connect_to || !*connect_to)
469
0
    return CURLE_FAILED_INIT;
470
471
136k
  pp.scheme = dest->scheme;
472
473
  /* detect and extract RFC6874-style IPv6-addresses */
474
136k
  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
136k
  else {
487
136k
    portstr = strchr(connect_to, ':');
488
136k
    pp.host_user.str = connect_to;
489
136k
    pp.host_user.len = portstr ?
490
136k
      (size_t)(portstr - connect_to) : strlen(connect_to);
491
136k
  }
492
493
136k
  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
136k
  result = peer_parse_host(data, &pp, FALSE);
499
136k
  if(result)
500
0
    goto out;
501
502
136k
  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
136k
  else
513
136k
    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
136k
  result = peer_create(&pp, ppeer);
524
136k
  CURL_TRC_M(data, "connect-to peer_create2 -> %d", (int)result);
525
526
136k
out:
527
136k
  CURL_TRC_M(data, "parse connect_to peer: %s -> %d", connect_to, (int)result);
528
136k
  peer_parse_clear(&pp);
529
136k
  return result;
530
136k
}
531
532
#ifndef CURL_DISABLE_PROXY
533
534
#ifdef USE_UNIX_SOCKETS
535
6.45k
#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
2.08k
{
542
2.08k
  if(!scheme)
543
0
    return CURLE_OK;
544
545
2.08k
  if(curl_strequal("https", scheme)) {
546
2.03k
    if(*proxytype != CURLPROXY_HTTPS2 && *proxytype != CURLPROXY_HTTPS3)
547
2.02k
      *proxytype = CURLPROXY_HTTPS;
548
2.03k
  }
549
55
  else if(curl_strequal("socks5h", scheme))
550
0
    *proxytype = CURLPROXY_SOCKS5_HOSTNAME;
551
55
  else if(curl_strequal("socks5", scheme))
552
0
    *proxytype = CURLPROXY_SOCKS5;
553
55
  else if(curl_strequal("socks4a", scheme))
554
0
    *proxytype = CURLPROXY_SOCKS4A;
555
55
  else if(curl_strequal("socks4", scheme) || curl_strequal("socks", scheme))
556
0
    *proxytype = CURLPROXY_SOCKS4;
557
55
  else if(curl_strequal("http", scheme)) {
558
32
    if(*proxytype != CURLPROXY_HTTP_1_0)
559
32
      *proxytype = CURLPROXY_HTTP;
560
32
  }
561
23
  else {
562
    /* Any other xxx:// reject! */
563
23
    failf(data, "Unsupported proxy scheme for \'%s\'", url);
564
23
    return CURLE_COULDNT_CONNECT;
565
23
  }
566
2.06k
  return CURLE_OK;
567
2.08k
}
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
39.7k
{
576
39.7k
  struct peer_parse pp;
577
39.7k
  char *scheme = NULL;
578
39.7k
  char *portptr = NULL;
579
39.7k
#ifdef USE_UNIX_SOCKETS
580
39.7k
  bool is_socks = FALSE;
581
39.7k
#endif
582
39.7k
  CURLUcode uc;
583
39.7k
  CURLcode result = CURLE_OK;
584
585
39.7k
  Curl_peer_unlink(ppeer);
586
39.7k
  memset(&pp, 0, sizeof(pp));
587
39.7k
  pp.port = CURL_DEFAULT_PROXY_PORT;
588
39.7k
  uc = curl_url_get(uh, CURLUPART_SCHEME, &scheme,
589
39.7k
                    CURLU_NON_SUPPORT_SCHEME | CURLU_NO_GUESS_SCHEME);
590
39.7k
  if(uc) {
591
38.7k
    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
38.7k
    switch(proxytype) {
597
21.5k
    case CURLPROXY_HTTP:
598
21.5k
    case CURLPROXY_HTTP_1_0:
599
21.5k
      pp.scheme = &Curl_scheme_http;
600
21.5k
      break;
601
4.73k
    case CURLPROXY_HTTPS:
602
10.6k
    case CURLPROXY_HTTPS2:
603
10.6k
    case CURLPROXY_HTTPS3:
604
10.6k
      pp.scheme = &Curl_scheme_https;
605
10.6k
      break;
606
5.88k
    case CURLPROXY_SOCKS4:
607
5.88k
      pp.scheme = &Curl_scheme_socks4;
608
5.88k
      break;
609
78
    case CURLPROXY_SOCKS4A:
610
78
      pp.scheme = &Curl_scheme_socks4a;
611
78
      break;
612
308
    case CURLPROXY_SOCKS5:
613
308
      pp.scheme = &Curl_scheme_socks5;
614
308
      break;
615
191
    case CURLPROXY_SOCKS5_HOSTNAME:
616
191
      pp.scheme = &Curl_scheme_socks5h;
617
191
      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
38.7k
    }
623
38.7k
  }
624
1.03k
  else {
625
1.03k
    pp.scheme = Curl_get_scheme(scheme);
626
1.03k
    result = Curl_scheme_to_proxytype(data, scheme, &proxytype, url);
627
1.03k
    if(result)
628
0
      goto out;
629
1.03k
  }
630
39.7k
  DEBUGASSERT(pp.scheme);
631
632
39.7k
  if(CURL_PROXY_IS_HTTPS(proxytype) &&
633
11.7k
     !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
39.7k
  switch(pp.scheme->family) {
641
6.45k
  case CURLPROTO_SOCKS:
642
6.45k
#ifdef USE_UNIX_SOCKETS
643
6.45k
    is_socks = TRUE;
644
6.45k
#endif
645
6.45k
    break;
646
33.2k
  case CURLPROTO_HTTP:
647
33.2k
    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
39.7k
  }
653
654
39.7k
  uc = curl_url_get(uh, CURLUPART_PORT, &portptr, CURLU_NO_DEFAULT_PORT);
655
39.7k
  if(uc == CURLUE_OUT_OF_MEMORY) {
656
0
    result = CURLE_OUT_OF_MEMORY;
657
0
    goto out;
658
0
  }
659
39.7k
  if(portptr) {
660
1.72k
    curl_off_t num;
661
1.72k
    const char *p = portptr;
662
1.72k
    if(!curlx_str_number(&p, &num, UINT16_MAX))
663
1.72k
      pp.port = (uint16_t)num;
664
    /* Should we not error out when the port number is invalid? */
665
1.72k
    curlx_free(portptr);
666
1.72k
  }
667
38.0k
  else {
668
    /* No port in URL, take the set one or the scheme's default */
669
38.0k
    if(data->set.proxyport)
670
142
      pp.port = data->set.proxyport;
671
37.8k
    else
672
37.8k
      pp.port = pp.scheme->defport;
673
38.0k
  }
674
675
  /* now, clone the proxy hostname */
676
39.7k
  uc = curl_url_get(uh, CURLUPART_HOST, &pp.tmp_host_user, CURLU_URLDECODE);
677
39.7k
  if(uc) {
678
0
    result = CURLE_OUT_OF_MEMORY;
679
0
    goto out;
680
0
  }
681
39.7k
  pp.host_user.str = pp.tmp_host_user;
682
39.7k
  pp.host_user.len = strlen(pp.tmp_host_user);
683
684
39.7k
#ifdef USE_UNIX_SOCKETS
685
39.7k
  if(is_socks && curl_strequal(UNIX_SOCKET_PREFIX, pp.tmp_host_user)) {
686
511
    uc = curl_url_get(uh, CURLUPART_PATH, &pp.tmp_host, CURLU_URLDECODE);
687
511
    if(uc) {
688
7
      result = CURLE_OUT_OF_MEMORY;
689
7
      goto out;
690
7
    }
691
    /* path will be "/", if no path was found */
692
504
    if(strcmp("/", pp.tmp_host)) {
693
25
      pp.host.str = pp.tmp_host;
694
25
      pp.host.len = strlen(pp.tmp_host);
695
25
      pp.unix_socket = TRUE;
696
25
    }
697
479
    else {
698
479
      pp.host = pp.host_user;
699
479
    }
700
504
  }
701
39.7k
#endif /* USE_UNIX_SOCKETS */
702
703
39.7k
  if(!pp.host.len) {
704
39.2k
    result = peer_parse_host(data, &pp, FALSE);
705
39.2k
    if(result)
706
88
      goto out;
707
39.2k
  }
708
709
39.6k
  uc = curl_url_get(uh, CURLUPART_ZONEID, &pp.tmp_zoneid, 0);
710
39.6k
  if(uc == CURLUE_OUT_OF_MEMORY) {
711
0
    result = CURLE_OUT_OF_MEMORY;
712
0
    goto out;
713
0
  }
714
39.6k
  if(pp.tmp_zoneid) {
715
327
    pp.zoneid.str = pp.tmp_zoneid;
716
327
    pp.zoneid.len = strlen(pp.tmp_zoneid);
717
327
  }
718
719
39.6k
  *pproxytype = proxytype;
720
39.6k
  result = peer_create(&pp, ppeer);
721
722
39.7k
out:
723
39.7k
  peer_parse_clear(&pp);
724
39.7k
  curlx_free(scheme);
725
39.7k
#ifdef DEBUGBUILD
726
39.7k
  if(!result)
727
39.7k
    DEBUGASSERT(*ppeer);
728
39.7k
#endif
729
39.7k
  return result;
730
39.7k
}
731
732
#endif /* !CURL_DISABLE_PROXY */