Coverage Report

Created: 2026-09-01 06:58

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
10.8k
{
97
10.8k
  curlx_free(pp->tmp_host_user);
98
10.8k
  curlx_free(pp->tmp_host);
99
10.8k
  curlx_free(pp->tmp_zoneid);
100
10.8k
  memset(pp, 0, sizeof(*pp));
101
10.8k
}
102
103
static CURLcode peer_create(struct peer_parse *pp,
104
                            struct Curl_peer **ppeer)
105
10.5k
{
106
10.5k
  struct Curl_peer *peer = NULL;
107
10.5k
  CURLcode result = CURLE_OK;
108
10.5k
  size_t zone_alen = 0, host_alen = 0;
109
110
10.5k
  if(!pp || !pp->scheme)
111
0
    return CURLE_FAILED_INIT;
112
10.5k
  if(!pp->host.len && !(pp->scheme->flags & PROTOPT_NONETWORK))
113
0
    return CURLE_FAILED_INIT;
114
115
10.5k
  if((pp->host.str != pp->host_user.str) ||
116
10.2k
     (pp->host.len != pp->host_user.len)) {
117
292
    host_alen = pp->host.len + 1;
118
292
  }
119
10.5k
  zone_alen = pp->zoneid.len ? (pp->zoneid.len + 1) : 0;
120
121
  /* null-terminator already part of struct */
122
10.5k
  peer = curlx_calloc(1, sizeof(*peer) +
123
10.5k
                         pp->host_user.len + host_alen + zone_alen);
124
10.5k
  if(!peer) {
125
0
    result = CURLE_OUT_OF_MEMORY;
126
0
    goto out;
127
0
  }
128
129
10.5k
  peer->refcount = 1;
130
10.5k
  peer->scheme = pp->scheme;
131
10.5k
  peer->hostname = peer->user_hostname;
132
10.5k
  peer->port = pp->port;
133
10.5k
  peer->scopeid = pp->scopeid;
134
10.5k
  peer->ipv6 = pp->ipv6;
135
10.5k
  peer->unix_socket = pp->unix_socket;
136
10.5k
  peer->abstract_uds = pp->abstract_uds;
137
138
10.5k
  if(pp->host_user.len)
139
10.4k
    memcpy(peer->user_hostname, pp->host_user.str, pp->host_user.len);
140
141
10.5k
  if(host_alen) {
142
292
    peer->hostname = peer->user_hostname + pp->host_user.len + 1;
143
292
    memcpy(peer->hostname, pp->host.str, pp->host.len);
144
292
  }
145
146
10.5k
  if(zone_alen) {
147
91
    peer->zoneid = peer->user_hostname + pp->host_user.len + 1 + host_alen;
148
91
    memcpy(peer->zoneid, pp->zoneid.str, pp->zoneid.len);
149
91
#ifdef USE_IPV6
150
    /* Determine scope_id if not already provided */
151
91
    if(!peer->scopeid) {
152
91
      const char *p = peer->zoneid;
153
91
      curl_off_t scope;
154
91
      if(!curlx_str_number(&p, &scope, UINT_MAX)) {
155
        /* A plain number, use it directly as a scope id. */
156
34
        peer->scopeid = (uint32_t)scope;
157
34
      }
158
57
#ifdef HAVE_IF_NAMETOINDEX
159
57
      else {
160
        /* Zone identifier is not numeric */
161
57
        unsigned int idx = 0;
162
57
        idx = if_nametoindex(peer->zoneid);
163
57
        if(idx) {
164
2
          peer->scopeid = (uint32_t)idx;
165
2
        }
166
55
        else {
167
          /* Do we want to return an error here? */
168
55
        }
169
57
      }
170
91
#endif /* HAVE_IF_NAMETOINDEX */
171
91
    }
172
91
#endif /* USE_IPV6 */
173
91
  }
174
175
10.5k
out:
176
10.5k
  if(!result)
177
10.5k
    *ppeer = peer;
178
0
  else
179
0
    Curl_peer_unlink(&peer);
180
10.5k
  return result;
181
10.5k
}
182
183
static CURLcode peer_parse_host(struct Curl_easy *data,
184
                                struct peer_parse *pp,
185
                                bool scan_for_ipv6)
186
10.5k
{
187
10.5k
  if(!pp || !pp->host_user.str || !pp->host_user.len)
188
0
    return CURLE_FAILED_INIT;
189
190
10.5k
  if(pp->host_user.str[0] == '[') {
191
292
    const char *s = pp->host_user.str + 1;
192
292
    struct Curl_str tmp;
193
292
    if(curlx_str_until(&s, &tmp, pp->host_user.len - 1, ']'))
194
0
      return CURLE_URL_MALFORMAT;
195
196
292
    if(!Curl_looks_like_ipv6(tmp.str, tmp.len, TRUE,
197
292
                             &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
292
    pp->ipv6 = TRUE;
203
292
  }
204
10.2k
  else {
205
10.2k
#ifdef USE_IDN
206
10.2k
    if(!Curl_is_ASCII_str(&pp->host_user)) {
207
161
      CURLcode result;
208
161
      if(!pp->tmp_host_user) {
209
        /* need a null-terminated string for IDN */
210
161
        pp->tmp_host_user = curlx_memdup0(pp->host_user.str,
211
161
                                          pp->host_user.len);
212
161
        if(!pp->tmp_host_user)
213
0
          return CURLE_OUT_OF_MEMORY;
214
161
      }
215
161
      result = Curl_idn_decode(pp->tmp_host_user, &pp->tmp_host);
216
161
      if(result)
217
161
        return result;
218
0
      pp->host.str = pp->tmp_host;
219
0
      pp->host.len = strlen(pp->host.str);
220
0
    }
221
10.1k
    else
222
10.1k
#endif
223
10.1k
    if(scan_for_ipv6 &&
224
96
       Curl_looks_like_ipv6(pp->host_user.str, pp->host_user.len, TRUE,
225
96
                            &pp->host, &pp->zoneid)) {
226
96
      if(pp->host_user.len < MAX_IPADR_LEN) {
227
96
        char tmp[MAX_IPADR_LEN];
228
96
        memcpy(tmp, pp->host_user.str, pp->host_user.len);
229
96
        tmp[pp->host_user.len] = 0;
230
96
        pp->ipv6 = !Curl_is_ipv4addr(tmp);
231
96
      }
232
0
      else
233
0
        pp->ipv6 = TRUE;
234
96
    }
235
10.0k
    else
236
10.0k
      pp->host = pp->host_user;
237
10.2k
  }
238
10.4k
  return CURLE_OK;
239
10.5k
}
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
96
{
247
96
  struct peer_parse pp;
248
96
  CURLcode result;
249
250
96
  Curl_peer_unlink(ppeer);
251
96
  memset(&pp, 0, sizeof(pp));
252
96
  pp.scheme = scheme;
253
96
  pp.host_user.str = hostname;
254
96
  pp.host_user.len = strlen(hostname);
255
96
  pp.port = port;
256
257
96
  result = peer_parse_host(data, &pp, TRUE);
258
96
  if(!result)
259
96
    result = peer_create(&pp, ppeer);
260
261
96
  peer_parse_clear(&pp);
262
96
  return result;
263
96
}
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
76
{
271
76
  struct peer_parse pp;
272
76
  size_t pathlen = path ? strlen(path) : 0;
273
76
  CURLcode result = CURLE_OK;
274
275
76
  Curl_peer_unlink(ppeer);
276
76
  memset(&pp, 0, sizeof(pp));
277
76
  if(!scheme)
278
0
    return CURLE_FAILED_INIT;
279
76
  if(!pathlen)
280
1
    return CURLE_FAILED_INIT;
281
282
75
  pp.scheme = scheme;
283
75
  pp.host_user.str = pp.host.str = path;
284
75
  pp.host_user.len = pp.host.len = pathlen;
285
75
  pp.unix_socket = TRUE;
286
75
  pp.abstract_uds = abstract_unix_socket;
287
288
75
  result = peer_create(&pp, ppeer);
289
75
  peer_parse_clear(&pp);
290
75
  return result;
291
76
}
292
#endif /* USE_UNIX_SOCKETS */
293
294
void Curl_peer_link(struct Curl_peer **pdest, struct Curl_peer *src)
295
48.4k
{
296
48.4k
  if(*pdest != src) {
297
44.7k
    Curl_peer_unlink(pdest);
298
44.7k
    *pdest = src;
299
44.7k
    if(src) {
300
44.7k
      DEBUGASSERT(src->refcount < UINT32_MAX);
301
44.7k
      src->refcount++;
302
44.7k
    }
303
44.7k
  }
304
48.4k
}
305
306
void Curl_peer_unlink(struct Curl_peer **ppeer)
307
254k
{
308
254k
  if(*ppeer) {
309
55.2k
    struct Curl_peer *peer = *ppeer;
310
311
55.2k
    DEBUGASSERT(peer->refcount);
312
55.2k
    *ppeer = NULL;
313
55.2k
    if(peer->refcount)
314
55.2k
      peer->refcount--;
315
55.2k
    if(!peer->refcount) {
316
10.5k
      curlx_free(peer);
317
10.5k
    }
318
55.2k
  }
319
254k
}
320
321
bool Curl_peer_equal(struct Curl_peer *p1, struct Curl_peer *p2)
322
16.7k
{
323
16.7k
  return (p1 == p2) ||
324
3.91k
         (p1 && p2 &&
325
3.84k
          (p1->scheme == p2->scheme) &&
326
3.80k
          Curl_peer_same_destination(p1, p2));
327
16.7k
}
328
329
static bool peer_same_hostname(struct Curl_peer *p1, struct Curl_peer *p2)
330
3.75k
{
331
  /* UNIX domain socket paths must be compared case-sensitive,
332
   * as many filesystem are like that. */
333
3.75k
  return (p1->unix_socket == p2->unix_socket) &&
334
3.75k
         (p1->abstract_uds == p2->abstract_uds) &&
335
3.75k
         (p1->ipv6 == p2->ipv6) &&
336
3.52k
         (p1->unix_socket ?
337
0
          !strcmp(p1->hostname, p2->hostname) :
338
3.52k
          curl_strequal(p1->hostname, p2->hostname));
339
3.75k
}
340
341
bool Curl_peer_same_destination(struct Curl_peer *p1, struct Curl_peer *p2)
342
15.3k
{
343
15.3k
  return (p1 == p2) ||
344
4.00k
         (p1 && p2 &&
345
4.00k
          (p1->port == p2->port) &&
346
3.75k
          peer_same_hostname(p1, p2) &&
347
64
          (p1->scopeid == p2->scopeid) &&
348
27
          (p1->scopeid || curl_strequal(p1->zoneid, p2->zoneid)));
349
15.3k
}
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
5.07k
{
356
5.07k
  struct peer_parse pp;
357
5.07k
  char *zoneid = NULL, *scheme = NULL, *hostname = NULL;
358
5.07k
  CURLUcode uc;
359
5.07k
  CURLcode result;
360
361
5.07k
  Curl_peer_unlink(ppeer);
362
5.07k
  memset(&pp, 0, sizeof(pp));
363
364
5.07k
  uc = curl_url_get(uh, CURLUPART_SCHEME, &scheme, 0);
365
5.07k
  if(uc)
366
0
    return Curl_uc_to_curlcode(uc);
367
5.07k
  pp.scheme = Curl_get_scheme(scheme);
368
5.07k
  if(!pp.scheme) {
369
169
    failf(data, "Protocol \"%s\" not supported%s", scheme,
370
169
          data->state.this_is_a_follow ? " (in redirect)" : "");
371
169
    result = CURLE_UNSUPPORTED_PROTOCOL;
372
169
    goto out;
373
169
  }
374
375
4.90k
  uc = curl_url_get(uh, CURLUPART_HOST, &hostname, 0);
376
4.90k
  if(uc) {
377
28
    if((uc == CURLUE_NO_HOST) && (pp.scheme->flags & PROTOPT_NONETWORK))
378
28
      ; /* acceptable */
379
0
    else {
380
0
      result = CURLE_OUT_OF_MEMORY;
381
0
      goto out;
382
0
    }
383
28
  }
384
4.87k
  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
4.90k
  pp.host_user.str = hostname ? hostname : "";
391
4.90k
  pp.host_user.len = strlen(pp.host_user.str);
392
4.90k
  if(pp.host_user.len) {
393
4.87k
    result = peer_parse_host(data, &pp, FALSE);
394
4.87k
    if(result)
395
161
      goto out;
396
4.87k
  }
397
28
  else
398
28
    pp.host = pp.host_user;
399
400
4.74k
  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
4.69k
  else {
413
4.69k
    uc = Curl_url_get_port(uh, &pp.port);
414
4.69k
    if(uc) {
415
25
      if(uc == CURLUE_OUT_OF_MEMORY) {
416
0
        result = CURLE_OUT_OF_MEMORY;
417
0
        goto out;
418
0
      }
419
25
      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
25
    }
425
4.69k
  }
426
427
4.74k
  if(scopeid_override)
428
    /* Override any scope id from an URL zone. */
429
140
    pp.scopeid = scopeid_override;
430
4.60k
  else {
431
4.60k
    if(curl_url_get(uh, CURLUPART_ZONEID, &zoneid, 0) ==
432
4.60k
       CURLUE_OUT_OF_MEMORY) {
433
0
      result = CURLE_OUT_OF_MEMORY;
434
0
      goto out;
435
0
    }
436
4.60k
    if(zoneid) {
437
91
      pp.zoneid.str = zoneid;
438
91
      pp.zoneid.len = strlen(zoneid);
439
91
    }
440
4.60k
  }
441
442
4.74k
  result = peer_create(&pp, ppeer);
443
4.74k
  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
5.07k
out:
448
5.07k
  peer_parse_clear(&pp);
449
5.07k
  curlx_free(scheme);
450
5.07k
  curlx_free(hostname);
451
5.07k
  curlx_free(zoneid);
452
5.07k
  return result;
453
4.74k
}
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
3.65k
{
462
3.65k
  struct peer_parse pp;
463
3.65k
  const char *portstr = NULL;
464
3.65k
  CURLcode result;
465
466
3.65k
  Curl_peer_unlink(ppeer);
467
3.65k
  memset(&pp, 0, sizeof(pp));
468
3.65k
  if(!connect_to || !*connect_to)
469
0
    return CURLE_FAILED_INIT;
470
471
3.65k
  pp.scheme = dest->scheme;
472
473
  /* detect and extract RFC6874-style IPv6-addresses */
474
3.65k
  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
3.65k
  else {
487
3.65k
    portstr = strchr(connect_to, ':');
488
3.65k
    pp.host_user.str = connect_to;
489
3.65k
    pp.host_user.len = portstr ?
490
3.65k
      (size_t)(portstr - connect_to) : strlen(connect_to);
491
3.65k
  }
492
493
3.65k
  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
3.65k
  result = peer_parse_host(data, &pp, FALSE);
499
3.65k
  if(result)
500
0
    goto out;
501
502
3.65k
  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
3.65k
  else
513
3.65k
    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
3.65k
  result = peer_create(&pp, ppeer);
524
3.65k
  CURL_TRC_M(data, "connect-to peer_create2 -> %d", (int)result);
525
526
3.65k
out:
527
3.65k
  CURL_TRC_M(data, "parse connect_to peer: %s -> %d", connect_to, (int)result);
528
3.65k
  peer_parse_clear(&pp);
529
3.65k
  return result;
530
3.65k
}
531
532
#ifndef CURL_DISABLE_PROXY
533
534
#ifdef USE_UNIX_SOCKETS
535
95
#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.90k
{
542
3.90k
  if(!scheme)
543
0
    return CURLE_OK;
544
545
3.90k
  if(curl_strequal("https", scheme)) {
546
0
    if(*proxytype != CURLPROXY_HTTPS2 && *proxytype != CURLPROXY_HTTPS3)
547
0
      *proxytype = CURLPROXY_HTTPS;
548
0
  }
549
3.90k
  else if(curl_strequal("socks5h", scheme))
550
0
    *proxytype = CURLPROXY_SOCKS5_HOSTNAME;
551
3.90k
  else if(curl_strequal("socks5", scheme))
552
190
    *proxytype = CURLPROXY_SOCKS5;
553
3.71k
  else if(curl_strequal("socks4a", scheme))
554
0
    *proxytype = CURLPROXY_SOCKS4A;
555
3.71k
  else if(curl_strequal("socks4", scheme) || curl_strequal("socks", scheme))
556
0
    *proxytype = CURLPROXY_SOCKS4;
557
3.71k
  else if(curl_strequal("http", scheme)) {
558
3.71k
    if(*proxytype != CURLPROXY_HTTP_1_0)
559
3.71k
      *proxytype = CURLPROXY_HTTP;
560
3.71k
  }
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
3.90k
  return CURLE_OK;
567
3.90k
}
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
1.95k
{
576
1.95k
  struct peer_parse pp;
577
1.95k
  char *scheme = NULL;
578
1.95k
  char *portptr = NULL;
579
1.95k
#ifdef USE_UNIX_SOCKETS
580
1.95k
  bool is_socks = FALSE;
581
1.95k
#endif
582
1.95k
  CURLUcode uc;
583
1.95k
  CURLcode result = CURLE_OK;
584
585
1.95k
  Curl_peer_unlink(ppeer);
586
1.95k
  memset(&pp, 0, sizeof(pp));
587
1.95k
  pp.port = CURL_DEFAULT_PROXY_PORT;
588
1.95k
  uc = curl_url_get(uh, CURLUPART_SCHEME, &scheme,
589
1.95k
                    CURLU_NON_SUPPORT_SCHEME | CURLU_NO_GUESS_SCHEME);
590
1.95k
  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
1.95k
  else {
625
1.95k
    pp.scheme = Curl_get_scheme(scheme);
626
1.95k
    result = Curl_scheme_to_proxytype(data, scheme, &proxytype, url);
627
1.95k
    if(result)
628
0
      goto out;
629
1.95k
  }
630
1.95k
  DEBUGASSERT(pp.scheme);
631
632
1.95k
  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
1.95k
  switch(pp.scheme->family) {
641
95
  case CURLPROTO_SOCKS:
642
95
#ifdef USE_UNIX_SOCKETS
643
95
    is_socks = TRUE;
644
95
#endif
645
95
    break;
646
1.85k
  case CURLPROTO_HTTP:
647
1.85k
    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
1.95k
  }
653
654
1.95k
  uc = curl_url_get(uh, CURLUPART_PORT, &portptr, CURLU_NO_DEFAULT_PORT);
655
1.95k
  if(uc == CURLUE_OUT_OF_MEMORY) {
656
0
    result = CURLE_OUT_OF_MEMORY;
657
0
    goto out;
658
0
  }
659
1.95k
  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
1.95k
  else {
668
    /* No port in URL, take the set one or the scheme's default */
669
1.95k
    if(data->set.proxyport)
670
22
      pp.port = data->set.proxyport;
671
1.93k
    else
672
1.93k
      pp.port = pp.scheme->defport;
673
1.95k
  }
674
675
  /* now, clone the proxy hostname */
676
1.95k
  uc = curl_url_get(uh, CURLUPART_HOST, &pp.tmp_host_user, CURLU_URLDECODE);
677
1.95k
  if(uc) {
678
0
    result = CURLE_OUT_OF_MEMORY;
679
0
    goto out;
680
0
  }
681
1.95k
  pp.host_user.str = pp.tmp_host_user;
682
1.95k
  pp.host_user.len = strlen(pp.tmp_host_user);
683
684
1.95k
#ifdef USE_UNIX_SOCKETS
685
1.95k
  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
1.95k
#endif /* USE_UNIX_SOCKETS */
702
703
1.95k
  if(!pp.host.len) {
704
1.95k
    result = peer_parse_host(data, &pp, FALSE);
705
1.95k
    if(result)
706
0
      goto out;
707
1.95k
  }
708
709
1.95k
  uc = curl_url_get(uh, CURLUPART_ZONEID, &pp.tmp_zoneid, 0);
710
1.95k
  if(uc == CURLUE_OUT_OF_MEMORY) {
711
0
    result = CURLE_OUT_OF_MEMORY;
712
0
    goto out;
713
0
  }
714
1.95k
  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
1.95k
  *pproxytype = proxytype;
720
1.95k
  result = peer_create(&pp, ppeer);
721
722
1.95k
out:
723
1.95k
  peer_parse_clear(&pp);
724
1.95k
  curlx_free(scheme);
725
1.95k
#ifdef DEBUGBUILD
726
1.95k
  if(!result)
727
1.95k
    DEBUGASSERT(*ppeer);
728
1.95k
#endif
729
1.95k
  return result;
730
1.95k
}
731
732
#endif /* !CURL_DISABLE_PROXY */