Coverage Report

Created: 2026-07-30 07:03

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