Coverage Report

Created: 2026-12-01 06:25

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