Coverage Report

Created: 2026-09-14 07:07

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