master
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#include "curl_setup.h"
26#ifndef CURL_DISABLE_PROXY
28#ifdef HAVE_NETINET_IN_H
29#include <netinet/in.h>
30#endif
31#ifdef HAVE_ARPA_INET_H
32#include <arpa/inet.h>
33#endif
35#include "urldata.h"
36#include "bufq.h"
37#include "curl_addrinfo.h"
38#include "curl_trc.h"
39#include "select.h"
40#include "cfilters.h"
41#include "connect.h"
42#include "socks.h"
43#include "vdns/cf-dns.h"
44#include "curlx/inet_pton.h"
46/* for the (SOCKS) connect state machine */
47enum socks_state_t {
48 SOCKS_ST_INIT,
49 /* SOCKS Version 4 states */
50 SOCKS4_ST_START,
51 SOCKS4_ST_RESOLVING,
52 SOCKS4_ST_SEND,
53 SOCKS4_ST_RECV,
54 /* SOCKS Version 5 states */
55 SOCKS5_ST_START,
56 SOCKS5_ST_REQ0_SEND,
57 SOCKS5_ST_RESP0_RECV, /* set up read */
58 SOCKS5_ST_GSSAPI_INIT,
59 SOCKS5_ST_AUTH_INIT, /* setup outgoing auth buffer */
60 SOCKS5_ST_AUTH_SEND, /* send auth */
61 SOCKS5_ST_AUTH_RECV, /* read auth response */
62 SOCKS5_ST_REQ1_INIT, /* init SOCKS "request" */
63 SOCKS5_ST_RESOLVING,
64 SOCKS5_ST_REQ1_SEND,
65 SOCKS5_ST_RESP1_RECV,
66 /* Terminal states, all SOCKS versions */
67 SOCKS_ST_SUCCESS,
68 SOCKS_ST_FAILED
69};
71#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
72static const char * const cf_socks_statename[] = {
73 "SOCKS_INIT",
74 "SOCKS4_START",
75 "SOCKS4_RESOLVING",
76 "SOCKS4_SEND",
77 "SOCKS4_RECV",
78 "SOCKS5_START",
79 "SOCKS5_REQ0_SEND",
80 "SOCKS5_RESP0_RECV",
81 "SOCKS5_GSSAPI_INIT",
82 "SOCKS5_AUTH_INIT",
83 "SOCKS5_AUTH_SEND",
84 "SOCKS5_AUTH_RECV",
85 "SOCKS5_REQ1_INIT",
86 "SOCKS5_RESOLVING",
87 "SOCKS5_REQ1_SEND",
88 "SOCKS5_RESP1_RECV",
89 "SOCKS_SUCCESS",
90 "SOCKS_FAILED"
91};
92#endif
94#define SOCKS_CHUNK_SIZE 1024
95#define SOCKS_CHUNKS 1
97struct socks_ctx {
98 enum socks_state_t state;
99 struct bufq iobuf;
100 struct Curl_peer *dest;
101 struct Curl_creds *creds;
102 CURLproxycode presult;
103 uint32_t resolv_id;
104 uint8_t ip_version;
105 uint8_t proxy_type;
106 unsigned char version;
107 BIT(resolve_local);
108 BIT(socks4a);
109};
111#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
112/*
113 * Helper read-from-socket functions. Does the same as Curl_read() but it
114 * blocks until all bytes amount of buffersize will be read. No more, no less.
115 *
116 * This is STUPID BLOCKING behavior. Only used by the SOCKS GSSAPI functions.
117 */
118CURLcode Curl_blockread_all(struct Curl_cfilter *cf,
119 struct Curl_easy *data,
120 char *buf, /* store read data here */
121 size_t blen, /* space in buf */
122 size_t *pnread) /* amount bytes read */
123{
124 size_t nread = 0;
125 CURLcode result;
127 *pnread = 0;
128 for(;;) {
129 timediff_t timeout_ms = Curl_timeleft_ms(data);
130 curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
132 if(timeout_ms < 0) {
133 /* we already got the timeout */
134 return CURLE_OPERATION_TIMEDOUT;
135 }
136 if(!timeout_ms)
137 timeout_ms = TIMEDIFF_T_MAX;
138 if(SOCKET_READABLE(sock, timeout_ms) <= 0)
139 return CURLE_OPERATION_TIMEDOUT;
140 result = Curl_conn_cf_recv(cf->next, data, buf, blen, &nread);
141 if(result == CURLE_AGAIN)
142 continue;
143 else if(result)
144 return result;
146 if(blen == nread) {
147 *pnread += nread;
148 return CURLE_OK;
149 }
150 if(!nread) /* EOF */
151 return CURLE_RECV_ERROR;
153 buf += nread;
154 blen -= nread;
155 *pnread += nread;
156 }
157}
158#endif
160#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
161#define sxstate(x, c, d, y) socksstate(x, c, d, y, __LINE__)
162#else
163#define sxstate(x, c, d, y) socksstate(x, c, d, y)
164#endif
166/* always use this function to change state, to make debugging easier */
167static void socksstate(struct socks_ctx *sx,
168 struct Curl_cfilter *cf,
169 struct Curl_easy *data,
170 enum socks_state_t state
171#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
172 , int lineno
173#endif
174)
175{
176 enum socks_state_t oldstate = sx->state;
178 if(oldstate == state)
179 /* do not bother when the new state is the same as the old state */
180 return;
182 sx->state = state;
184#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
185 CURL_TRC_CF(data, cf, "[%s] -> [%s] (line %d)",
186 cf_socks_statename[oldstate],
187 cf_socks_statename[sx->state], lineno);
188#else
189 (void)cf;
190 (void)data;
191#endif
192}
194static CURLproxycode socks_failed(struct socks_ctx *sx,
195 struct Curl_cfilter *cf,
196 struct Curl_easy *data,
197 CURLproxycode presult)
198{
199 sxstate(sx, cf, data, SOCKS_ST_FAILED);
200 sx->presult = presult;
201 return presult;
202}
204static CURLproxycode socks_flush(struct socks_ctx *sx,
205 struct Curl_cfilter *cf,
206 struct Curl_easy *data,
207 bool *done)
208{
209 CURLcode result;
210 size_t nwritten;
212 *done = FALSE;
213 while(!Curl_bufq_is_empty(&sx->iobuf)) {
214 result = Curl_cf_send_bufq(cf->next, data, &sx->iobuf, NULL, 0,
215 &nwritten);
216 if(result == CURLE_AGAIN)
217 return CURLPX_OK;
218 else if(result) {
219 failf(data, "Failed to send SOCKS request: %s",
220 curl_easy_strerror(result));
221 return socks_failed(sx, cf, data, CURLPX_SEND_CONNECT);
222 }
223 }
224 *done = TRUE;
225 return CURLPX_OK;
226}
228static CURLproxycode socks_recv(struct socks_ctx *sx,
229 struct Curl_cfilter *cf,
230 struct Curl_easy *data,
231 size_t min_bytes,
232 bool *done)
233{
234 CURLcode result;
235 size_t nread;
237 *done = FALSE;
238 while(Curl_bufq_len(&sx->iobuf) < min_bytes) {
239 result = Curl_cf_recv_bufq(cf->next, data, &sx->iobuf,
240 min_bytes - Curl_bufq_len(&sx->iobuf),
241 &nread);
242 if(result == CURLE_AGAIN)
243 return CURLPX_OK;
244 else if(result) {
245 failf(data, "Failed to receive SOCKS response: %s",
246 curl_easy_strerror(result));
247 return CURLPX_RECV_CONNECT;
248 }
249 else if(!nread) { /* EOF */
250 if(Curl_bufq_len(&sx->iobuf) < min_bytes) {
251 failf(data, "Failed to receive SOCKS response, "
252 "proxy closed connection");
253 return CURLPX_RECV_CONNECT;
254 }
255 break;
256 }
257 }
258 *done = TRUE;
259 return CURLPX_OK;
260}
262static CURLproxycode socks4_req_add_hd(struct socks_ctx *sx,
263 struct Curl_easy *data)
264{
265 unsigned char buf[4];
266 size_t nwritten;
267 CURLcode result;
269 (void)data;
270 buf[0] = 4; /* version (SOCKS4) */
271 buf[1] = 1; /* connect */
272 buf[2] = (unsigned char)((sx->dest->port >> 8) & 0xffU); /* MSB */
273 buf[3] = (unsigned char)(sx->dest->port & 0xffU); /* LSB */
275 result = Curl_bufq_write(&sx->iobuf, buf, 4, &nwritten);
276 if(result || (nwritten != 4))
277 return CURLPX_SEND_REQUEST;
278 return CURLPX_OK;
279}
281static CURLproxycode socks4_req_add_user(struct socks_ctx *sx,
282 struct Curl_easy *data)
283{
284 CURLcode result;
285 size_t nwritten;
287 if(sx->creds) {
288 size_t plen = strlen(sx->creds->user);
289 if(plen > 255) {
290 /* there is no real size limit to this field in the protocol, but
291 SOCKS5 limits the proxy user field to 255 bytes and it seems likely
292 that a longer field is either a mistake or malicious input */
293 failf(data, "Too long SOCKS proxy username");
294 return CURLPX_LONG_USER;
295 }
296 /* add proxy name WITH null-terminator */
297 result = Curl_bufq_cwrite(&sx->iobuf, sx->creds->user, plen + 1,
298 &nwritten);
299 if(result || (nwritten != (plen + 1)))
300 return CURLPX_SEND_REQUEST;
301 }
302 else {
303 /* empty username */
304 unsigned char b = 0;
305 result = Curl_bufq_write(&sx->iobuf, &b, 1, &nwritten);
306 if(result || (nwritten != 1))
307 return CURLPX_SEND_REQUEST;
308 }
309 return CURLPX_OK;
310}
312static CURLproxycode socks4_resolving(struct socks_ctx *sx,
313 struct Curl_cfilter *cf,
314 struct Curl_easy *data,
315 bool *done)
316{
317 const struct Curl_addrinfo *ai = NULL;
318 CURLcode result;
319 size_t nwritten;
321 *done = FALSE;
322 result = Curl_conn_dns_addr_result(cf->conn, cf->sockindex, sx->dest);
323 if(result) {
324 if(result != CURLE_AGAIN) {
325 failf(data, "error %d resolving SOCKS destination %s:%u",
326 (int)result, sx->dest->hostname, sx->dest->port);
327 return CURLPX_RESOLVE_HOST;
328 }
329 return CURLPX_OK;
330 }
332 ai = Curl_conn_dns_get_ai(data, sx->dest, cf->sockindex, AF_INET, 0);
333 if(ai) {
334 struct sockaddr_in *saddr_in;
335 char ipbuf[64];
337 Curl_printable_address(ai, ipbuf, sizeof(ipbuf));
338 CURL_TRC_CF(data, cf, "SOCKS4 connect to IPv4 %s (locally resolved)",
339 ipbuf);
341 saddr_in = (struct sockaddr_in *)(void *)ai->ai_addr;
342 result = Curl_bufq_write(&sx->iobuf,
343 (unsigned char *)&saddr_in->sin_addr.s_addr, 4,
344 &nwritten);
346 if(result || (nwritten != 4))
347 return CURLPX_SEND_REQUEST;
348 }
349 else {
350 /* No IPv4 address resolved */
351 failf(data, "SOCKS4 connection to %s not supported", sx->dest->hostname);
352 return CURLPX_RESOLVE_HOST;
353 }
355 *done = TRUE;
356 return CURLPX_OK;
357}
359static CURLproxycode socks4_check_resp(struct socks_ctx *sx,
360 struct Curl_cfilter *cf,
361 struct Curl_easy *data)
362{
363 const unsigned char *resp;
364 size_t rlen;
366 if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < 8) {
367 failf(data, "SOCKS4 reply is incomplete.");
368 return CURLPX_RECV_CONNECT;
369 }
371 DEBUGASSERT(rlen == 8);
372 /*
373 * Response format
374 *
375 * +----+----+----+----+----+----+----+----+
376 * | VN | CD | DSTPORT | DSTIP |
377 * +----+----+----+----+----+----+----+----+
378 * # of bytes: 1 1 2 4
379 *
380 * VN is the version of the reply code and should be 0. CD is the result
381 * code with one of the following values:
382 *
383 * 90: request granted
384 * 91: request rejected or failed
385 * 92: request rejected because SOCKS server cannot connect to
386 * identd on the client
387 * 93: request rejected because the client program and identd
388 * report different user-ids
389 */
391 /* wrong version ? */
392 if(resp[0]) {
393 failf(data, "SOCKS4 reply has wrong version, version should be 0.");
394 return CURLPX_BAD_VERSION;
395 }
397 /* Result */
398 switch(resp[1]) {
399 case 90:
400 CURL_TRC_CF(data, cf, "SOCKS4%s request granted.", sx->socks4a ? "a" : "");
401 Curl_bufq_skip(&sx->iobuf, 8);
402 return CURLPX_OK;
403 case 91:
404 failf(data,
405 "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)"
406 ", request rejected or failed.",
407 resp[4], resp[5], resp[6], resp[7],
408 (unsigned int)((resp[2] << 8) | resp[3]), resp[1]);
409 return CURLPX_REQUEST_FAILED;
410 case 92:
411 failf(data,
412 "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)"
413 ", request rejected because SOCKS server cannot connect to "
414 "identd on the client.",
415 resp[4], resp[5], resp[6], resp[7],
416 (unsigned int)((resp[2] << 8) | resp[3]), resp[1]);
417 return CURLPX_IDENTD;
418 case 93:
419 failf(data,
420 "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)"
421 ", request rejected because the client program and identd "
422 "report different user-ids.",
423 resp[4], resp[5], resp[6], resp[7],
424 (unsigned int)((resp[2] << 8) | resp[3]), resp[1]);
425 return CURLPX_IDENTD_DIFFER;
426 default:
427 failf(data,
428 "[SOCKS] cannot complete SOCKS4 connection to %u.%u.%u.%u:%u. (%u)"
429 ", Unknown.",
430 resp[4], resp[5], resp[6], resp[7],
431 (unsigned int)((resp[2] << 8) | resp[3]), resp[1]);
432 return CURLPX_UNKNOWN_FAIL;
433 }
434}
436/*
437 * This function logs in to a SOCKS4 proxy and sends the specifics to the final
438 * destination server.
439 *
440 * Reference :
441 * https://www.openssh.com/txt/socks4.protocol
442 *
443 * Note :
444 * Set protocol4a=true for "SOCKS 4A (Simple Extension to SOCKS 4 Protocol)"
445 * Nonsupport "Identification Protocol (RFC1413)"
446 */
447static CURLproxycode socks4_connect(struct Curl_cfilter *cf,
448 struct socks_ctx *sx,
449 struct Curl_easy *data)
450{
451 size_t nwritten;
452 CURLproxycode presult;
453 CURLcode result;
454 bool done;
456process_state:
457 switch(sx->state) {
458 case SOCKS_ST_INIT:
459 sx->version = 4;
460 sxstate(sx, cf, data, SOCKS4_ST_START);
461 FALLTHROUGH();
463 case SOCKS4_ST_START:
464 Curl_bufq_reset(&sx->iobuf);
465 sx->socks4a = (sx->proxy_type == CURLPROXY_SOCKS4A);
466 sx->presult = CURLPX_OK;
468 /* SOCKS4 can only do IPv4, insist! */
469 sx->ip_version = CURL_IPRESOLVE_V4;
470 CURL_TRC_CF(data, cf, "SOCKS4%s connecting to %s:%u",
471 sx->socks4a ? "a" : "",
472 sx->dest->hostname, sx->dest->port);
474 /*
475 * Compose socks4 request
476 *
477 * Request format
478 *
479 * +----+----+----+----+----+----+----+----+----+----+....+----+
480 * | VN | CD | DSTPORT | DSTIP | USERID |NULL|
481 * +----+----+----+----+----+----+----+----+----+----+....+----+
482 * # of bytes: 1 1 2 4 variable 1
483 */
484 presult = socks4_req_add_hd(sx, data);
485 if(presult)
486 return socks_failed(sx, cf, data, presult);
488 /* DNS resolve only for SOCKS4, not SOCKS4a */
489 if(!sx->resolve_local) {
490 /* socks4a, not resolving locally, sends the hostname.
491 * add an invalid address + user + hostname */
492 unsigned char buf[4] = { 0, 0, 0, 1 };
493 size_t hlen = strlen(sx->dest->hostname) + 1; /* including NUL */
495 if(hlen > 255) {
496 failf(data, "SOCKS4: too long hostname");
497 return socks_failed(sx, cf, data, CURLPX_LONG_HOSTNAME);
498 }
499 result = Curl_bufq_write(&sx->iobuf, buf, 4, &nwritten);
500 if(result || (nwritten != 4))
501 return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST);
502 presult = socks4_req_add_user(sx, data);
503 if(presult)
504 return socks_failed(sx, cf, data, presult);
505 result = Curl_bufq_cwrite(&sx->iobuf, sx->dest->hostname, hlen,
506 &nwritten);
507 if(result || (nwritten != hlen))
508 return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST);
509 /* request complete */
510 sxstate(sx, cf, data, SOCKS4_ST_SEND);
511 goto process_state;
512 }
513 sxstate(sx, cf, data, SOCKS4_ST_RESOLVING);
514 FALLTHROUGH();
516 case SOCKS4_ST_RESOLVING:
517 presult = socks4_resolving(sx, cf, data, &done);
518 if(presult)
519 return socks_failed(sx, cf, data, presult);
520 if(!done)
521 return CURLPX_OK;
522 /* append user */
523 presult = socks4_req_add_user(sx, data);
524 if(presult)
525 return socks_failed(sx, cf, data, presult);
526 sxstate(sx, cf, data, SOCKS4_ST_SEND);
527 FALLTHROUGH();
529 case SOCKS4_ST_SEND:
530 presult = socks_flush(sx, cf, data, &done);
531 if(presult)
532 return socks_failed(sx, cf, data, presult);
533 else if(!done)
534 return CURLPX_OK;
535 sxstate(sx, cf, data, SOCKS4_ST_RECV);
536 FALLTHROUGH();
538 case SOCKS4_ST_RECV:
539 /* Receive 8-byte response */
540 presult = socks_recv(sx, cf, data, 8, &done);
541 if(presult)
542 return socks_failed(sx, cf, data, presult);
543 else if(!done)
544 return CURLPX_OK;
545 presult = socks4_check_resp(sx, cf, data);
546 if(presult)
547 return socks_failed(sx, cf, data, presult);
548 sxstate(sx, cf, data, SOCKS_ST_SUCCESS);
549 FALLTHROUGH();
551 case SOCKS_ST_SUCCESS:
552 return CURLPX_OK;
554 case SOCKS_ST_FAILED:
555 DEBUGASSERT(sx->presult);
556 return sx->presult;
558 default:
559 DEBUGASSERT(0);
560 return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST);
561 }
562}
564static CURLproxycode socks5_req0_init(struct Curl_cfilter *cf,
565 struct socks_ctx *sx,
566 struct Curl_easy *data)
567{
568 const unsigned char auth = data->set.socks5auth;
569 unsigned char req[5]; /* version + len + 3 possible auth methods */
570 unsigned char nauths;
571 size_t req_len, nwritten;
572 CURLcode result;
574 (void)cf;
575 /* RFC1928 chapter 5 specifies max 255 chars for domain name in packet */
576 if(!sx->resolve_local && strlen(sx->dest->hostname) > 255) {
577 failf(data, "SOCKS5: the destination hostname is too long to be "
578 "resolved remotely by the proxy.");
579 return CURLPX_LONG_HOSTNAME;
580 }
582 if(auth & ~(CURLAUTH_BASIC | CURLAUTH_GSSAPI))
583 infof(data, "warning: unsupported value passed to "
584 "CURLOPT_SOCKS5_AUTH: %u", auth);
585 if(!(auth & CURLAUTH_BASIC))
586 /* disable username/password auth */
587 Curl_creds_unlink(&sx->creds);
589 if(cf->sockindex == FIRSTSOCKET)
590 /* the reuse of this connection depends on how this negotiation ends */
591 cf->conn->bits.socks5_authenticated = FALSE;
593 req[0] = 5; /* version */
594 nauths = 0;
595 if(!data->set.socks5_auth_only) {
596 ++nauths;
597 req[1 + nauths] = 0; /* no authentication */
598 }
599#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
600 if(auth & CURLAUTH_GSSAPI) {
601 ++nauths;
602 req[1 + nauths] = 1; /* GSS-API */
603 }
604#endif
605 if(sx->creds) {
606 ++nauths;
607 req[1 + nauths] = 2; /* username/password */
608 }
609 if(!nauths) {
610 failf(data, "SOCKS5: no acceptable authentication method is available.");
611 return CURLPX_NO_AUTH;
612 }
613 req[1] = nauths;
614 req_len = 2 + nauths;
616 result = Curl_bufq_write(&sx->iobuf, req, req_len, &nwritten);
617 if(result || (nwritten != req_len))
618 return CURLPX_SEND_REQUEST;
619 return CURLPX_OK;
620}
622static CURLproxycode socks5_check_resp0(struct socks_ctx *sx,
623 struct Curl_cfilter *cf,
624 struct Curl_easy *data)
625{
626 const unsigned char *resp;
627 unsigned char auth_mode;
628 size_t rlen;
630 if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < 2) {
631 failf(data, "SOCKS5 initial reply is incomplete.");
632 return CURLPX_RECV_CONNECT;
633 }
635 if(resp[0] != 5) {
636 failf(data, "Received invalid version in initial SOCKS5 response.");
637 return CURLPX_BAD_VERSION;
638 }
640 auth_mode = resp[1];
641 Curl_bufq_skip(&sx->iobuf, 2);
643 switch(auth_mode) {
644 case 0:
645 if(data->set.socks5_auth_only) {
646 failf(data, "SOCKS5 proxy selected no authentication.");
647 return CURLPX_NO_AUTH;
648 }
649 /* DONE! No authentication needed. Send request. */
650 sxstate(sx, cf, data, SOCKS5_ST_REQ1_INIT);
651 return CURLPX_OK;
652 case 1:
653 if(data->set.socks5auth & CURLAUTH_GSSAPI) {
654 if(cf->sockindex == FIRSTSOCKET)
655 cf->conn->bits.socks5_authenticated = TRUE;
656 sxstate(sx, cf, data, SOCKS5_ST_GSSAPI_INIT);
657 return CURLPX_OK;
658 }
659 failf(data, "SOCKS5 GSSAPI per-message authentication is not enabled.");
660 return CURLPX_GSSAPI_PERMSG;
661 case 2:
662 /* regular name + password authentication */
663 if(data->set.socks5auth & CURLAUTH_BASIC) {
664 if(cf->sockindex == FIRSTSOCKET)
665 cf->conn->bits.socks5_authenticated = TRUE;
666 sxstate(sx, cf, data, SOCKS5_ST_AUTH_INIT);
667 return CURLPX_OK;
668 }
669 failf(data, "BASIC authentication proposed but not enabled.");
670 return CURLPX_NO_AUTH;
671 case 255:
672 failf(data, "No authentication method was acceptable.");
673 return CURLPX_NO_AUTH;
674 default:
675 failf(data, "Unknown SOCKS5 mode attempted to be used by server.");
676 return CURLPX_UNKNOWN_MODE;
677 }
678}
680static CURLproxycode socks5_auth_init(struct Curl_cfilter *cf,
681 struct socks_ctx *sx,
682 struct Curl_easy *data)
683{
684 /* Needs username and password */
685 size_t ulen = 0, plen = 0, nwritten;
686 unsigned char buf[2];
687 CURLcode result;
689 if(sx->creds) {
690 ulen = strlen(sx->creds->user);
691 plen = strlen(sx->creds->passwd);
692 /* the lengths must fit in a single byte */
693 if(ulen > 255) {
694 failf(data, "Excessive username length for proxy auth");
695 return CURLPX_LONG_USER;
696 }
697 if(plen > 255) {
698 failf(data, "Excessive password length for proxy auth");
699 return CURLPX_LONG_PASSWD;
700 }
701 }
703 /* username/password request looks like
704 * +----+------+----------+------+----------+
705 * |VER | ULEN | UNAME | PLEN | PASSWD |
706 * +----+------+----------+------+----------+
707 * | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
708 * +----+------+----------+------+----------+
709 */
710 buf[0] = 1; /* username/pw subnegotiation version */
711 buf[1] = (unsigned char)ulen;
712 result = Curl_bufq_write(&sx->iobuf, buf, 2, &nwritten);
713 if(result || (nwritten != 2))
714 return CURLPX_SEND_REQUEST;
715 if(ulen) {
716 result = Curl_bufq_cwrite(&sx->iobuf, sx->creds->user, ulen, &nwritten);
717 if(result || (nwritten != ulen))
718 return CURLPX_SEND_REQUEST;
719 }
720 buf[0] = (unsigned char)plen;
721 result = Curl_bufq_write(&sx->iobuf, buf, 1, &nwritten);
722 if(result || (nwritten != 1))
723 return CURLPX_SEND_REQUEST;
724 if(plen) {
725 result = Curl_bufq_cwrite(&sx->iobuf, sx->creds->passwd, plen, &nwritten);
726 if(result || (nwritten != plen))
727 return CURLPX_SEND_REQUEST;
728 }
729 sxstate(sx, cf, data, SOCKS5_ST_AUTH_SEND);
730 return CURLPX_OK;
731}
733static CURLproxycode socks5_check_auth_resp(struct socks_ctx *sx,
734 struct Curl_cfilter *cf,
735 struct Curl_easy *data)
736{
737 const unsigned char *resp;
738 unsigned char auth_status;
739 size_t rlen;
741 (void)cf;
742 if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < 2) {
743 failf(data, "SOCKS5 sub-negotiation response incomplete.");
744 return CURLPX_RECV_CONNECT;
745 }
747 /* ignore the first (VER) byte */
748 auth_status = resp[1];
749 if(auth_status) {
750 failf(data, "User was rejected by the SOCKS5 server (%d %d).",
751 resp[0], resp[1]);
752 return CURLPX_USER_REJECTED;
753 }
754 Curl_bufq_skip(&sx->iobuf, 2);
755 return CURLPX_OK;
756}
758static CURLproxycode socks5_req1_init(struct socks_ctx *sx,
759 struct Curl_cfilter *cf,
760 struct Curl_easy *data)
761{
762 unsigned char req[5];
763 unsigned char ipbuf[16];
764 const unsigned char *destination;
765 unsigned char desttype, destlen, hdlen;
766 size_t nwritten;
767 CURLcode result;
769 req[0] = 5; /* version (SOCKS5) */
770 req[1] = 1; /* connect */
771 req[2] = 0; /* must be zero */
772 if(sx->resolve_local) {
773 /* rest of request is added after resolving */
774 result = Curl_bufq_write(&sx->iobuf, req, 3, &nwritten);
775 if(result || (nwritten != 3))
776 return CURLPX_SEND_REQUEST;
777 return CURLPX_OK;
778 }
780 /* remote resolving, send what type+addr/string to resolve */
781#ifdef USE_IPV6
782 if(strchr(sx->dest->hostname, ':')) {
783 desttype = 4;
784 destination = ipbuf;
785 destlen = 16;
786 if(curlx_inet_pton(AF_INET6, sx->dest->hostname, ipbuf) != 1)
787 return CURLPX_BAD_ADDRESS_TYPE;
788 }
789 else
790#endif
791 if(curlx_inet_pton(AF_INET, sx->dest->hostname, ipbuf) == 1) {
792 desttype = 1;
793 destination = ipbuf;
794 destlen = 4;
795 }
796 else {
797 const size_t hostname_len = strlen(sx->dest->hostname);
798 /* socks5_req0_init() already rejects hostnames longer than 255 bytes, so
799 this cast to unsigned char is safe. Assert to guard against future
800 refactoring that might remove or reorder that earlier check. */
801 DEBUGASSERT(hostname_len <= 255);
802 desttype = 3;
803 destination = (const unsigned char *)sx->dest->hostname;
804 destlen = (unsigned char)hostname_len; /* 1-byte length */
805 }
807 req[3] = desttype;
808 req[4] = destlen;
809 hdlen = (desttype == 3) ? 5 : 4; /* no length byte for ip addresses */
810 result = Curl_bufq_write(&sx->iobuf, req, hdlen, &nwritten);
811 if(result || (nwritten != hdlen))
812 return CURLPX_SEND_REQUEST;
813 result = Curl_bufq_write(&sx->iobuf, destination, destlen, &nwritten);
814 if(result || (nwritten != destlen))
815 return CURLPX_SEND_REQUEST;
816 /* PORT MSB+LSB */
817 req[0] = (unsigned char)((sx->dest->port >> 8) & 0xff);
818 req[1] = (unsigned char)(sx->dest->port & 0xff);
819 result = Curl_bufq_write(&sx->iobuf, req, 2, &nwritten);
820 if(result || (nwritten != 2))
821 return CURLPX_SEND_REQUEST;
822 CURL_TRC_CF(data, cf, "SOCKS5 connect to %s:%u (remotely resolved)",
823 sx->dest->hostname, sx->dest->port);
824 return CURLPX_OK;
825}
827static CURLproxycode socks5_resolving(struct socks_ctx *sx,
828 struct Curl_cfilter *cf,
829 struct Curl_easy *data,
830 bool *done)
831{
832 const struct Curl_addrinfo *ai = NULL;
833 char dest[MAX_IPADR_LEN]; /* printable address */
834 const unsigned char *destination = NULL;
835 unsigned char desttype = 1, destlen = 4;
836 unsigned char req[2];
837 CURLcode result;
838 CURLproxycode presult = CURLPX_OK;
839 size_t nwritten;
841 *done = FALSE;
842 result = Curl_conn_dns_addr_result(cf->conn, cf->sockindex, sx->dest);
843 if(result) {
844 if(result != CURLE_AGAIN) {
845 failf(data, "error %d resolving SOCKS destination %s:%u",
846 (int)result, sx->dest->hostname, sx->dest->port);
847 return CURLPX_RESOLVE_HOST;
848 }
849 return CURLPX_OK;
850 }
852#ifdef USE_IPV6
853 if(data->set.ipver != CURL_IPRESOLVE_V4)
854 ai = Curl_conn_dns_get_ai(data, sx->dest, cf->sockindex, AF_INET6, 0);
855#endif
856 if(!ai)
857 ai = Curl_conn_dns_get_ai(data, sx->dest, cf->sockindex, AF_INET, 0);
859 if(!ai) {
860 failf(data, "Failed to resolve \"%s\" for SOCKS5 connect.",
861 sx->dest->hostname);
862 presult = CURLPX_RESOLVE_HOST;
863 goto out;
864 }
866 Curl_printable_address(ai, dest, sizeof(dest));
868 if(ai->ai_family == AF_INET) {
869 struct sockaddr_in *saddr_in;
870 desttype = 1; /* ATYP: IPv4 = 1 */
871 destlen = 4;
872 saddr_in = (struct sockaddr_in *)(void *)ai->ai_addr;
873 destination = (const unsigned char *)&saddr_in->sin_addr.s_addr;
874 CURL_TRC_CF(data, cf, "SOCKS5 connect to %s:%u (locally resolved)",
875 dest, sx->dest->port);
876 }
877#ifdef USE_IPV6
878 else if(ai->ai_family == AF_INET6) {
879 struct sockaddr_in6 *saddr_in6;
880 desttype = 4; /* ATYP: IPv6 = 4 */
881 destlen = 16;
882 saddr_in6 = (struct sockaddr_in6 *)(void *)ai->ai_addr;
883 destination = (const unsigned char *)&saddr_in6->sin6_addr.s6_addr;
884 CURL_TRC_CF(data, cf, "SOCKS5 connect to [%s]:%u (locally resolved)",
885 dest, sx->dest->port);
886 }
887#endif
889 if(!destination) {
890 failf(data, "SOCKS5 connection to %s not supported", dest);
891 presult = CURLPX_RESOLVE_HOST;
892 goto out;
893 }
895 req[0] = desttype;
896 result = Curl_bufq_write(&sx->iobuf, req, 1, &nwritten);
897 if(result || (nwritten != 1)) {
898 presult = CURLPX_SEND_REQUEST;
899 goto out;
900 }
901 result = Curl_bufq_write(&sx->iobuf, destination, destlen, &nwritten);
902 if(result || (nwritten != destlen)) {
903 presult = CURLPX_SEND_REQUEST;
904 goto out;
905 }
906 /* PORT MSB+LSB */
907 req[0] = (unsigned char)((sx->dest->port >> 8) & 0xffU);
908 req[1] = (unsigned char)(sx->dest->port & 0xffU);
909 result = Curl_bufq_write(&sx->iobuf, req, 2, &nwritten);
910 if(result || (nwritten != 2)) {
911 presult = CURLPX_SEND_REQUEST;
912 goto out;
913 }
915out:
916 *done = (presult == CURLPX_OK);
917 return presult;
918}
920static CURLproxycode socks5_recv_resp1(struct socks_ctx *sx,
921 struct Curl_cfilter *cf,
922 struct Curl_easy *data,
923 bool *done)
924{
925 const unsigned char *resp;
926 size_t rlen, resp_len = 8; /* minimum response length */
927 CURLproxycode presult;
929 presult = socks_recv(sx, cf, data, resp_len, done);
930 if(presult)
931 return presult;
932 else if(!*done)
933 return CURLPX_OK;
935 if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < resp_len) {
936 failf(data, "SOCKS5 response is incomplete.");
937 return CURLPX_RECV_CONNECT;
938 }
940 /* Response packet includes BND.ADDR is variable length parameter by RFC
941 1928, so the response packet MUST be read until the end to avoid errors
942 at subsequent protocol level.
944 +----+-----+-------+------+----------+----------+
945 |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT |
946 +----+-----+-------+------+----------+----------+
947 | 1 | 1 | 0x00 | 1 | Variable | 2 |
948 +----+-----+-------+------+----------+----------+
950 ATYP:
951 o IPv4 address: 0x01, BND.ADDR = 4-byte
952 o domain name: 0x03, BND.ADDR = [ 1-byte length, string ]
953 o IPv6 address: 0x04, BND.ADDR = 16-byte
954 */
955 if(resp[0] != 5) { /* version */
956 failf(data, "SOCKS5 reply has wrong version, version should be 5.");
957 return CURLPX_BAD_VERSION;
958 }
959 else if(resp[1]) { /* Anything besides 0 is an error */
960 CURLproxycode rc = CURLPX_REPLY_UNASSIGNED;
961 int code = resp[1];
962 failf(data, "cannot complete SOCKS5 connection to %s. (%d)",
963 sx->dest->hostname, code);
964 if(code < 9) {
965 /* RFC 1928 section 6 lists: */
966 static const CURLproxycode lookup[] = {
967 CURLPX_OK,
968 CURLPX_REPLY_GENERAL_SERVER_FAILURE,
969 CURLPX_REPLY_NOT_ALLOWED,
970 CURLPX_REPLY_NETWORK_UNREACHABLE,
971 CURLPX_REPLY_HOST_UNREACHABLE,
972 CURLPX_REPLY_CONNECTION_REFUSED,
973 CURLPX_REPLY_TTL_EXPIRED,
974 CURLPX_REPLY_COMMAND_NOT_SUPPORTED,
975 CURLPX_REPLY_ADDRESS_TYPE_NOT_SUPPORTED,
976 };
977 rc = lookup[code];
978 }
979 return rc;
980 }
982 /* Calculate real packet size */
983 switch(resp[3]) {
984 case 1: /* IPv4 */
985 resp_len = 4 + 4 + 2;
986 break;
987 case 3: /* domain name */
988 resp_len = 4 + 1 + resp[4] + 2; /* header, var length, var bytes, port */
989 break;
990 case 4: /* IPv6 */
991 resp_len = 4 + 16 + 2;
992 break;
993 default:
994 failf(data, "SOCKS5 reply has wrong address type.");
995 return CURLPX_BAD_ADDRESS_TYPE;
996 }
998 /* receive the rest of the response */
999 presult = socks_recv(sx, cf, data, resp_len, done);
1000 if(presult)
1001 return presult;
1002 else if(!*done)
1003 return CURLPX_OK;
1005 if(!Curl_bufq_peek(&sx->iobuf, &resp, &rlen) || rlen < resp_len) {
1006 failf(data, "SOCKS5 response is incomplete.");
1007 return CURLPX_RECV_CONNECT;
1008 }
1009 /* got it all */
1010 *done = TRUE;
1011 return CURLPX_OK;
1012}
1014/*
1015 * This function logs in to a SOCKS5 proxy and sends the specifics to the final
1016 * destination server.
1017 */
1018static CURLproxycode socks5_connect(struct Curl_cfilter *cf,
1019 struct socks_ctx *sx,
1020 struct Curl_easy *data)
1021{
1022 CURLproxycode presult;
1023 bool done;
1025process_state:
1026 switch(sx->state) {
1027 case SOCKS_ST_INIT:
1028 sx->version = 5;
1029 sxstate(sx, cf, data, SOCKS5_ST_START);
1030 FALLTHROUGH();
1032 case SOCKS5_ST_START:
1033 CURL_TRC_CF(data, cf, "SOCKS5: connecting to %s:%u",
1034 sx->dest->hostname, sx->dest->port);
1035 presult = socks5_req0_init(cf, sx, data);
1036 if(presult)
1037 return socks_failed(sx, cf, data, presult);
1038 sxstate(sx, cf, data, SOCKS5_ST_REQ0_SEND);
1039 FALLTHROUGH();
1041 case SOCKS5_ST_REQ0_SEND:
1042 presult = socks_flush(sx, cf, data, &done);
1043 if(presult)
1044 return socks_failed(sx, cf, data, presult);
1045 else if(!done)
1046 return CURLPX_OK;
1047 /* done sending! */
1048 sxstate(sx, cf, data, SOCKS5_ST_RESP0_RECV);
1049 FALLTHROUGH();
1051 case SOCKS5_ST_RESP0_RECV:
1052 presult = socks_recv(sx, cf, data, 2, &done);
1053 if(presult)
1054 return socks_failed(sx, cf, data, presult);
1055 else if(!done)
1056 return CURLPX_OK;
1057 presult = socks5_check_resp0(sx, cf, data);
1058 if(presult)
1059 return socks_failed(sx, cf, data, presult);
1060 /* socks5_check_resp0() sets next socks state */
1061 goto process_state;
1063 case SOCKS5_ST_GSSAPI_INIT: {
1064#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1065 /* GSSAPI stuff done non-blocking */
1066 CURLcode result = Curl_SOCKS5_gssapi_negotiate(cf, data, sx->creds);
1067 if(result) {
1068 failf(data, "Unable to negotiate SOCKS5 GSS-API context.");
1069 return CURLPX_GSSAPI;
1070 }
1071 sxstate(sx, cf, data, SOCKS5_ST_REQ1_INIT);
1072 goto process_state;
1073#else
1074 failf(data, "SOCKS5 GSSAPI per-message authentication is not supported.");
1075 return socks_failed(sx, cf, data, CURLPX_GSSAPI_PERMSG);
1076#endif
1077 }
1079 case SOCKS5_ST_AUTH_INIT:
1080 presult = socks5_auth_init(cf, sx, data);
1081 if(presult)
1082 return socks_failed(sx, cf, data, presult);
1083 sxstate(sx, cf, data, SOCKS5_ST_AUTH_SEND);
1084 FALLTHROUGH();
1086 case SOCKS5_ST_AUTH_SEND:
1087 presult = socks_flush(sx, cf, data, &done);
1088 if(presult)
1089 return socks_failed(sx, cf, data, presult);
1090 else if(!done)
1091 return CURLPX_OK;
1092 sxstate(sx, cf, data, SOCKS5_ST_AUTH_RECV);
1093 FALLTHROUGH();
1095 case SOCKS5_ST_AUTH_RECV:
1096 presult = socks_recv(sx, cf, data, 2, &done);
1097 if(presult)
1098 return socks_failed(sx, cf, data, presult);
1099 else if(!done)
1100 return CURLPX_OK;
1101 presult = socks5_check_auth_resp(sx, cf, data);
1102 if(presult)
1103 return socks_failed(sx, cf, data, presult);
1104 /* Everything is good so far, user was authenticated! */
1105 sxstate(sx, cf, data, SOCKS5_ST_REQ1_INIT);
1106 FALLTHROUGH();
1108 case SOCKS5_ST_REQ1_INIT:
1109 presult = socks5_req1_init(sx, cf, data);
1110 if(presult)
1111 return socks_failed(sx, cf, data, presult);
1112 if(!sx->resolve_local) {
1113 /* we do not resolve, request is complete */
1114 sxstate(sx, cf, data, SOCKS5_ST_REQ1_SEND);
1115 goto process_state;
1116 }
1117 sxstate(sx, cf, data, SOCKS5_ST_RESOLVING);
1118 FALLTHROUGH();
1120 case SOCKS5_ST_RESOLVING:
1121 presult = socks5_resolving(sx, cf, data, &done);
1122 if(presult)
1123 return socks_failed(sx, cf, data, presult);
1124 if(!done)
1125 return CURLPX_OK;
1126 sxstate(sx, cf, data, SOCKS5_ST_REQ1_SEND);
1127 FALLTHROUGH();
1129 case SOCKS5_ST_REQ1_SEND:
1130 presult = socks_flush(sx, cf, data, &done);
1131 if(presult)
1132 return socks_failed(sx, cf, data, presult);
1133 else if(!done)
1134 return CURLPX_OK;
1135#if defined(HAVE_GSSAPI) || defined(USE_WINDOWS_SSPI)
1136 if(cf->conn->socks5_gssapi_enctype) {
1137 failf(data, "SOCKS5 GSS-API protection not yet implemented.");
1138 return CURLPX_GSSAPI_PROTECTION;
1139 }
1140#endif
1141 sxstate(sx, cf, data, SOCKS5_ST_RESP1_RECV);
1142 FALLTHROUGH();
1144 case SOCKS5_ST_RESP1_RECV:
1145 presult = socks5_recv_resp1(sx, cf, data, &done);
1146 if(presult)
1147 return socks_failed(sx, cf, data, presult);
1148 if(!done)
1149 return CURLPX_OK;
1150 CURL_TRC_CF(data, cf, "SOCKS5 request granted.");
1151 sxstate(sx, cf, data, SOCKS_ST_SUCCESS);
1152 FALLTHROUGH();
1154 case SOCKS_ST_SUCCESS:
1155 return CURLPX_OK;
1157 case SOCKS_ST_FAILED:
1158 DEBUGASSERT(sx->presult);
1159 return sx->presult;
1161 default:
1162 DEBUGASSERT(0);
1163 return socks_failed(sx, cf, data, CURLPX_SEND_REQUEST);
1164 }
1165}
1167static void socks_proxy_ctx_free(struct socks_ctx *ctx)
1168{
1169 if(ctx) {
1170 Curl_peer_unlink(&ctx->dest);
1171 Curl_creds_unlink(&ctx->creds);
1172 Curl_bufq_free(&ctx->iobuf);
1173 curlx_free(ctx);
1174 }
1175}
1177/* After a TCP connection to the proxy has been verified, this function does
1178 the next magic steps. If 'done' is not set TRUE, it is not done yet and
1179 must be called again.
1181 Note: this function's sub-functions call failf() */
1182static CURLcode socks_proxy_cf_connect(struct Curl_cfilter *cf,
1183 struct Curl_easy *data,
1184 bool *done)
1185{
1186 struct socks_ctx *ctx = cf->ctx;
1187 CURLproxycode pxresult = CURLPX_OK;
1188 CURLcode result;
1190 if(cf->connected) {
1191 *done = TRUE;
1192 return CURLE_OK;
1193 }
1195 result = cf->next->cft->do_connect(cf->next, data, done);
1196 if(result || !*done)
1197 return result;
1199 switch(ctx->proxy_type) {
1200 case CURLPROXY_SOCKS5:
1201 case CURLPROXY_SOCKS5_HOSTNAME:
1202 pxresult = socks5_connect(cf, ctx, data);
1203 break;
1205 case CURLPROXY_SOCKS4:
1206 case CURLPROXY_SOCKS4A:
1207 pxresult = socks4_connect(cf, ctx, data);
1208 break;
1210 default:
1211 DEBUGASSERT(0); /* should not come here, checked it at creation time */
1212 result = CURLE_COULDNT_CONNECT;
1213 goto out;
1214 }
1216 if(pxresult) {
1217 result = CURLE_PROXY;
1218 data->info.pxcode = (uint8_t)pxresult;
1219 goto out;
1220 }
1221 else if(ctx->state != SOCKS_ST_SUCCESS)
1222 goto out;
1224#ifdef CURLVERBOSE
1225 if(Curl_trc_is_verbose(data)) {
1226 struct ip_quadruple ipquad;
1227 bool is_ipv6;
1228 if(!Curl_conn_cf_get_ip_info(cf->next, data, &is_ipv6, &ipquad))
1229 infof(data, "Opened %sSOCKS connection from %s port %d to %s port %d "
1230 "(via %s port %u)",
1231 (cf->sockindex == SECONDARYSOCKET) ? "2nd " : "",
1232 ipquad.local_ip, ipquad.local_port,
1233 ctx->dest->hostname, ctx->dest->port,
1234 ipquad.remote_ip, ipquad.remote_port);
1235 else
1236 infof(data, "Opened %sSOCKS connection",
1237 (cf->sockindex == SECONDARYSOCKET) ? "2nd " : "");
1238 }
1239#endif
1240 cf->connected = TRUE;
1242out:
1243 *done = (bool)cf->connected;
1244 if(*done || result)
1245 Curl_creds_unlink(&ctx->creds);
1246 return result;
1247}
1249static CURLcode socks_cf_adjust_pollset(struct Curl_cfilter *cf,
1250 struct Curl_easy *data,
1251 struct easy_pollset *ps)
1252{
1253 struct socks_ctx *sx = cf->ctx;
1254 CURLcode result = CURLE_OK;
1256 if(!cf->connected && sx) {
1257 /* If we are not connected, the filter below is and has nothing
1258 * to wait on, we determine what to wait for. */
1259 curl_socket_t sock = Curl_conn_cf_get_socket(cf, data);
1260 switch(sx->state) {
1261 case SOCKS4_ST_SEND:
1262 case SOCKS5_ST_REQ0_SEND:
1263 case SOCKS5_ST_AUTH_SEND:
1264 case SOCKS5_ST_REQ1_SEND:
1265 CURL_TRC_CF(data, cf, "adjust pollset out (%d)", (int)sx->state);
1266 result = Curl_pollset_set_out_only(data, ps, sock);
1267 break;
1268 default:
1269 CURL_TRC_CF(data, cf, "adjust pollset in (%d)", (int)sx->state);
1270 result = Curl_pollset_set_in_only(data, ps, sock);
1271 break;
1272 }
1273 }
1274 return result;
1275}
1277static void socks_proxy_cf_destroy(struct Curl_cfilter *cf,
1278 struct Curl_easy *data)
1279{
1280 (void)data;
1281 socks_proxy_ctx_free(cf->ctx);
1282 cf->ctx = NULL;
1283}
1285static CURLcode socks_cf_query(struct Curl_cfilter *cf,
1286 struct Curl_easy *data,
1287 int query, int *pres1, void *pres2)
1288{
1289 struct socks_ctx *sx = cf->ctx;
1291 switch(query) {
1292 case CF_QUERY_HOST_PORT:
1293 if(sx) {
1294 *pres1 = sx->dest->port;
1295 *((const char **)pres2) = sx->dest->hostname;
1296 return CURLE_OK;
1297 }
1298 break;
1299 case CF_QUERY_ALPN_NEGOTIATED: {
1300 const char **palpn = pres2;
1301 DEBUGASSERT(palpn);
1302 *palpn = NULL;
1303 return CURLE_OK;
1304 }
1305 default:
1306 break;
1307 }
1308 return cf->next ?
1309 cf->next->cft->query(cf->next, data, query, pres1, pres2) :
1310 CURLE_UNKNOWN_OPTION;
1311}
1313struct Curl_cftype Curl_cft_socks_proxy = {
1314 "SOCKS",
1315 CF_TYPE_IP_CONNECT | CF_TYPE_PROXY,
1316 0,
1317 socks_proxy_cf_destroy,
1318 socks_proxy_cf_connect,
1319 Curl_cf_def_shutdown,
1320 socks_cf_adjust_pollset,
1321 Curl_cf_def_data_pending,
1322 Curl_cf_def_send,
1323 Curl_cf_def_recv,
1324 Curl_cf_def_cntrl,
1325 Curl_cf_def_conn_is_alive,
1326 Curl_cf_def_conn_keep_alive,
1327 socks_cf_query,
1328};
1330CURLcode Curl_cf_socks_proxy_insert_after(struct Curl_cfilter *cf_at,
1331 struct Curl_easy *data,
1332 struct Curl_peer *dest,
1333 uint8_t ip_version,
1334 uint8_t proxy_type,
1335 struct Curl_creds *creds)
1336{
1337 struct Curl_cfilter *cf;
1338 struct socks_ctx *ctx;
1339 bool resolve_local = FALSE;
1340 uint8_t dns_queries = Curl_resolv_dns_queries(data, ip_version);
1341 CURLcode result;
1343 if(!dest)
1344 return CURLE_FAILED_INIT;
1346 switch(proxy_type) {
1347 case CURLPROXY_SOCKS5:
1348 resolve_local = TRUE;
1349 break;
1350 case CURLPROXY_SOCKS5_HOSTNAME:
1351 break;
1352 case CURLPROXY_SOCKS4:
1353 resolve_local = TRUE;
1354 dns_queries = (uint8_t)(dns_queries & ~CURL_DNSQ_AAAA);
1355 break;
1356 case CURLPROXY_SOCKS4A:
1357 break;
1358 default:
1359 failf(data, "unknown proxytype %d option given", proxy_type);
1360 return CURLE_COULDNT_CONNECT;
1361 }
1363 /* NUL byte already part of struct size */
1364 ctx = curlx_calloc(1, sizeof(*ctx));
1365 if(!ctx) {
1366 return CURLE_OUT_OF_MEMORY;
1367 }
1369 Curl_peer_link(&ctx->dest, dest);
1370 ctx->ip_version = ip_version;
1371 ctx->proxy_type = proxy_type;
1372 ctx->resolve_local = resolve_local;
1373 Curl_creds_link(&ctx->creds, creds);
1374 Curl_bufq_init2(&ctx->iobuf, SOCKS_CHUNK_SIZE, SOCKS_CHUNKS,
1375 BUFQ_OPT_SOFT_LIMIT);
1377 result = Curl_cf_create(&cf, &Curl_cft_socks_proxy, ctx);
1378 if(!result) {
1379 Curl_conn_cf_insert_after(cf_at, cf);
1380 if(ctx->resolve_local) {
1381 result = Curl_conn_dns_add_addr_resolve(data, cf_at->conn,
1382 cf_at->sockindex,
1383 ctx->dest, dns_queries,
1384 TRNSPRT_TCP);
1385 }
1386 }
1387 else
1388 socks_proxy_ctx_free(ctx);
1389 return result;
1390}
1392#endif /* CURL_DISABLE_PROXY */
Diagnostics4 git calls · 29 ms
CallStatusSizeTime029 ms
discover200152 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=152 git.server.agent=gitty/1.0 git.server.object_format=sha1 git.server.caps=agent ls-refs fetch object-format git.server.fetch=shallow filter sideband-all git.protocol.version=2 git.pkt.frames=8 git.pkt.data=6 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=152 git.pkt.payload.max=34
ls-refs200149 B · hit3 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=149 git.response.cache=HIT git.response.duration_ms=3 git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=149 git.pkt.payload.max=78 git.ls_refs.count=2 git.ls_refs.head=refs/heads/master
fetchblob:none · deepen 10200287 KiB · hit3 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=23 git.response.bytes=293470 git.response.cache=HIT git.response.duration_ms=3 git.fetch.filter=blob:none git.fetch.filter.requested=tree:0 git.fetch.filter.fallback=blob:none git.fetch.filter.learned=false git.fetch.wants=31c911efe5652725e930f4d2e60c9060e5f6646d git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=10 git.pkt.data=8 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=293470 git.pkt.payload.max=65516 git.sideband.band1.bytes=293354 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=87 git.pack.bytes=293354 git.pack.ofs_delta=0 git.pack.ref_delta=1 git.pack.chain.max=1 git.pack.trailer=6a646bcda6740bab42e321b51998b52ea15146c0 git.pack.full=86Open pack map
fetch20011 KiB · hit23 ms
git.upload-pack fetch http.response.status_code=200 git.response.bytes=10894 git.response.cache=HIT git.response.duration_ms=23 git.fetch.wants=ca7d3e27daa8a5998b13d1d09b8ba1d6f2e3baeb git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=3 git.pkt.data=2 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10894 git.pkt.payload.max=10873 git.sideband.band1.bytes=10872 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://5ad2236b39de4c8bd8f6270ea63f3ea9.artifacts.cloudflare.net/git/public-v0/curl.git
- Ref
- refs/heads/master
- Commit
- 31c911efe5652725e930f4d2e60c9060e5f6646d
- cf-ray
- a5e21e8b5c22a87d
- Request
- 80887876…
- Protocol
- v2 · sha1
- Agent
- gitty/1.0
- Filter
- advertised
- Fetch
- shallow filter sideband-all
- Capabilities
- agent ls-refs fetch object-format