Coverage Report

Created: 2026-10-15 06:17

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