Artifactspublic-v0

curl

master
ply4 git calls183 ms295 KiB2 cachedprotocol v2
956 lines27 KiBRaw
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 "first.h"
26/* Function
27 *
28 * Accepts a TCP connection on a custom port (IPv4 or IPv6). Connects to a
29 * given addr + port backend (that is NOT extracted form the client's
30 * request). The backend server default to connect to can be set with
31 * --backend and --backendport.
32 *
33 * Read commands from FILE (set with --config). The commands control how to
34 * act and is reset to defaults each client TCP connect.
35 *
36 * Config file keywords:
37 *
38 * "version [number: 5]" - requires the communication to use this version.
39 * "nmethods_min [number: 1]" - the minimum numberf NMETHODS the client must
40 * state
41 * "nmethods_max [number: 3]" - the minimum numberf NMETHODS the client must
42 * state
43 * "user [string]" - the username that must match (if method is 2)
44 * "password [string]" - the password that must match (if method is 2)
45 * "backend [IPv4]" - numerical IPv4 address of backend to connect to
46 * "backendport [number:0]" - TCP port of backend to connect to. 0 means use
47 * the client's specified port number.
48 * "method [number: 0]" - connect method to respond with:
49 * 0 - no auth
50 * 1 - GSSAPI (not supported)
51 * 2 - user + password
52 * "method2 [number]" - connect method to respond with on the second
53 * connection of the test, defaults to the "method" one
54 * "response [number]" - the decimal number to respond to a connect
55 * SOCKS5: 0 is OK, SOCKS4: 90 is ok
56 *
57 */
59/* based on sockfilt.c */
61static const char *backendaddr = "127.0.0.1";
62static uint16_t backendport = 0; /* default is use client's */
63static unsigned int connections; /* clients served in the current test */
64static int last_testnum = -1; /* the test the connections belong to */
66struct socksd_configurable {
67 unsigned char version; /* initial version byte in the request must match
68 this */
69 unsigned char nmethods_min; /* minimum number of nmethods to expect */
70 unsigned char nmethods_max; /* maximum number of nmethods to expect */
71 unsigned char responseversion;
72 unsigned char responsemethod;
73 int responsemethod2; /* negative means unset */
74 int testnum;
75 unsigned char reqcmd;
76 unsigned char connectrep;
77 uint16_t port; /* backend port */
78 char addr[32]; /* backend IPv4 numerical */
79 char user[256];
80 char password[256];
81};
83#define CONFIG_VERSION 5
84#define CONFIG_NMETHODS_MIN 1 /* unauth, gssapi, auth */
85#define CONFIG_NMETHODS_MAX 3
86#define CONFIG_RESPONSEVERSION CONFIG_VERSION
87#define CONFIG_RESPONSEMETHOD 0 /* no auth */
88#define CONFIG_RESPONSEMETHOD2 (-1) /* unset, use CONFIG_RESPONSEMETHOD */
89#define CONFIG_REQCMD 1 /* CONNECT */
90#define CONFIG_PORT backendport
91#define CONFIG_ADDR backendaddr
92#define CONFIG_CONNECTREP 0
94static struct socksd_configurable s_config;
96static const char *reqlogfile = "log/socksd-request.log";
98static void socksd_resetdefaults(void)
100 logmsg("Reset to defaults");
101 s_config.version = CONFIG_VERSION;
102 s_config.nmethods_min = CONFIG_NMETHODS_MIN;
103 s_config.nmethods_max = CONFIG_NMETHODS_MAX;
104 s_config.responseversion = CONFIG_RESPONSEVERSION;
105 s_config.responsemethod = CONFIG_RESPONSEMETHOD;
106 s_config.responsemethod2 = CONFIG_RESPONSEMETHOD2;
107 s_config.testnum = 0;
108 s_config.reqcmd = CONFIG_REQCMD;
109 s_config.connectrep = CONFIG_CONNECTREP;
110 s_config.port = CONFIG_PORT;
111 curlx_strcopy(s_config.addr, sizeof(s_config.addr),
112 CONFIG_ADDR, strlen(CONFIG_ADDR));
113 curlx_strcopy(s_config.user, sizeof(s_config.user),
114 "user", CURL_CSTRLEN("user"));
115 curlx_strcopy(s_config.password, sizeof(s_config.password),
116 "password", CURL_CSTRLEN("password"));
119static void socksd_getconfig(void)
121 FILE *fp = curlx_fopen(configfile, FOPEN_READTEXT);
122 socksd_resetdefaults();
123 if(fp) {
124 char buffer[512];
125 logmsg("parse config file");
126 while(fgets(buffer, sizeof(buffer), fp)) {
127 char key[32];
128 char value[260];
129 const char *pval;
130 curl_off_t num;
131 if(sscanf(buffer, "%31s %259s", key, value) == 2) {
132 if(!strcmp(key, "version")) {
133 pval = value;
134 if(!curlx_str_number(&pval, &num, 0xff)) {
135 s_config.version = (unsigned char)num;
136 logmsg("version [%d] set", s_config.version);
137 }
138 }
139 else if(!strcmp(key, "nmethods_min")) {
140 pval = value;
141 if(!curlx_str_number(&pval, &num, 0xff)) {
142 s_config.nmethods_min = (unsigned char)num;
143 logmsg("nmethods_min [%d] set", s_config.nmethods_min);
144 }
145 }
146 else if(!strcmp(key, "nmethods_max")) {
147 pval = value;
148 if(!curlx_str_number(&pval, &num, 0xff)) {
149 s_config.nmethods_max = (unsigned char)num;
150 logmsg("nmethods_max [%d] set", s_config.nmethods_max);
151 }
152 }
153 else if(!strcmp(key, "backend")) {
154 curlx_strcopy(s_config.addr, sizeof(s_config.addr),
155 value, strlen(value));
156 logmsg("backend [%s] set", s_config.addr);
157 }
158 else if(!strcmp(key, "backendport")) {
159 pval = value;
160 if(!curlx_str_number(&pval, &num, 0xffff)) {
161 s_config.port = (uint16_t)num;
162 logmsg("backendport [%d] set", s_config.port);
163 }
164 }
165 else if(!strcmp(key, "user")) {
166 curlx_strcopy(s_config.user, sizeof(s_config.user),
167 value, strlen(value));
168 logmsg("user [%s] set", s_config.user);
169 }
170 else if(!strcmp(key, "password")) {
171 curlx_strcopy(s_config.password, sizeof(s_config.password),
172 value, strlen(value));
173 logmsg("password [%s] set", s_config.password);
174 }
175 /* Methods:
176 o 0x00 NO AUTHENTICATION REQUIRED
177 o 0x01 GSSAPI
178 o 0x02 USERNAME/PASSWORD
179 */
180 else if(!strcmp(key, "method")) {
181 pval = value;
182 if(!curlx_str_number(&pval, &num, 0xff)) {
183 s_config.responsemethod = (unsigned char)num;
184 logmsg("method [%d] set", s_config.responsemethod);
185 }
186 }
187 else if(!strcmp(key, "method2")) {
188 pval = value;
189 if(!curlx_str_number(&pval, &num, 0xff)) {
190 s_config.responsemethod2 = (int)num;
191 logmsg("method2 [%d] set", s_config.responsemethod2);
192 }
193 }
194 else if(!strcmp(key, "Testnum")) {
195 pval = value;
196 if(!curlx_str_number(&pval, &num, INT_MAX)) {
197 s_config.testnum = (int)num;
198 logmsg("testnum = %d", s_config.testnum);
199 }
200 }
201 else if(!strcmp(key, "response")) {
202 pval = value;
203 if(!curlx_str_number(&pval, &num, 0xff)) {
204 s_config.connectrep = (unsigned char)num;
205 logmsg("response [%d] set", s_config.connectrep);
206 }
207 }
208 }
209 }
210 curlx_fclose(fp);
211 }
214/* RFC 1928, SOCKS5 byte index */
215#define SOCKS5_VERSION 0
216#define SOCKS5_NMETHODS 1 /* number of methods that is listed */
218/* in the request: */
219#define SOCKS5_REQCMD 1
220#define SOCKS5_RESERVED 2
221#define SOCKS5_ATYP 3
222#define SOCKS5_DSTADDR 4
224/* connect response */
225#define SOCKS5_REP 1
226#define SOCKS5_BNDADDR 4
228/* auth request */
229#define SOCKS5_ULEN 1
230#define SOCKS5_UNAME 2
232#define SOCKS4_CD 1
233#define SOCKS4_DSTPORT 2
235/* connect to a given IPv4 address, not the one asked for */
236static curl_socket_t socksconnect(uint16_t connectport,
237 const char *connectaddr)
239 srvr_sockaddr_union_t me;
240 curl_socket_t sock = socket(AF_INET, SOCK_STREAM, 0);
241 if(sock == CURL_SOCKET_BAD)
242 return CURL_SOCKET_BAD;
243 memset(&me.sa4, 0, sizeof(me.sa4));
244 me.sa4.sin_family = AF_INET;
245 me.sa4.sin_port = htons(connectport);
246 me.sa4.sin_addr.s_addr = INADDR_ANY;
247 curlx_inet_pton(AF_INET, connectaddr, &me.sa4.sin_addr);
249 if(connect(sock, &me.sa, sizeof(me.sa4))) {
250 char errbuf[STRERROR_LEN];
251 int sockerr = SOCKERRNO;
252 logmsg("Failed connecting to %s:%hu (%d) %s", connectaddr, connectport,
253 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
254 return CURL_SOCKET_BAD;
255 }
256 logmsg("Connected fine to %s:%d", connectaddr, connectport);
257 return sock;
260static curl_socket_t socks4(curl_socket_t fd,
261 const unsigned char *buffer,
262 ssize_t rc)
264 unsigned char response[256 + 16];
265 curl_socket_t connfd;
266 unsigned char cd;
267 uint16_t s4port;
269 if(buffer[SOCKS4_CD] != 1) {
270 logmsg("SOCKS4 CD is not 1: %d", buffer[SOCKS4_CD]);
271 return CURL_SOCKET_BAD;
272 }
273 if(rc < 9) {
274 logmsg("SOCKS4 connect message too short: %ld", (long)rc);
275 return CURL_SOCKET_BAD;
276 }
277 if(!s_config.port)
278 s4port = (uint16_t)((buffer[SOCKS4_DSTPORT] << 8) |
279 buffer[SOCKS4_DSTPORT + 1]);
280 else
281 s4port = s_config.port;
283 connfd = socksconnect(s4port, s_config.addr);
284 if(connfd == CURL_SOCKET_BAD) {
285 /* failed */
286 cd = 91;
287 }
288 else {
289 /* success */
290 cd = 90;
291 }
292 response[0] = 0; /* reply version 0 */
293 response[1] = cd; /* result */
294 /* copy port and address from connect request */
295 memcpy(&response[2], &buffer[SOCKS4_DSTPORT], 6);
296 rc = swrite(fd, response, 8);
297 if(rc != 8) {
298 logmsg("Sending SOCKS4 response failed!");
299 return CURL_SOCKET_BAD;
300 }
301 logmsg("Sent %ld bytes", (long)rc);
302 loghex(response, rc);
304 if(cd == 90)
305 /* now do the transfer */
306 return connfd;
308 if(connfd != CURL_SOCKET_BAD)
309 sclose(connfd);
311 return CURL_SOCKET_BAD;
314static curl_socket_t sockit(curl_socket_t fd)
316 unsigned char buffer[(2 * 256) + 16];
317 unsigned char response[(2 * 256) + 16];
318 ssize_t rc;
319 unsigned char len;
320 unsigned char type;
321 unsigned char rep = 0;
322 const unsigned char *address;
323 uint16_t socksport;
324 curl_socket_t connfd = CURL_SOCKET_BAD;
325 uint16_t s5port;
327 socksd_getconfig();
329 if(last_testnum != s_config.testnum) {
330 last_testnum = s_config.testnum;
331 connections = 0;
332 }
334 if((++connections == 2) && (s_config.responsemethod2 >= 0))
335 s_config.responsemethod = (unsigned char)s_config.responsemethod2;
337 rc = sread(fd, buffer, sizeof(buffer));
338 if(rc <= 0) {
339 logmsg("SOCKS identifier message missing, recv returned %ld", (long)rc);
340 return CURL_SOCKET_BAD;
341 }
343 logmsg("READ %ld bytes", (long)rc);
344 loghex(buffer, rc);
346 if(buffer[SOCKS5_VERSION] == 4)
347 return socks4(fd, buffer, rc);
349 if(rc < 3) {
350 logmsg("SOCKS5 identifier message too short: %ld", (long)rc);
351 return CURL_SOCKET_BAD;
352 }
354 if(buffer[SOCKS5_VERSION] != s_config.version) {
355 logmsg("VERSION byte not %d", s_config.version);
356 return CURL_SOCKET_BAD;
357 }
358 if((buffer[SOCKS5_NMETHODS] < s_config.nmethods_min) ||
359 (buffer[SOCKS5_NMETHODS] > s_config.nmethods_max)) {
360 logmsg("NMETHODS byte not within %d - %d ",
361 s_config.nmethods_min, s_config.nmethods_max);
362 return CURL_SOCKET_BAD;
363 }
364 /* after NMETHODS follows that many bytes listing the methods the client
365 says it supports */
366 if(rc != (buffer[SOCKS5_NMETHODS] + 2)) {
367 logmsg("Expected %d bytes, got %ld",
368 buffer[SOCKS5_NMETHODS] + 2, (long)rc);
369 return CURL_SOCKET_BAD;
370 }
371 logmsg("Incoming request deemed fine!");
373 /* respond with two bytes: VERSION + METHOD */
374 response[0] = s_config.responseversion;
375 response[1] = s_config.responsemethod;
376 rc = swrite(fd, response, 2);
377 if(rc != 2) {
378 logmsg("Sending response failed!");
379 return CURL_SOCKET_BAD;
380 }
381 logmsg("Sent %ld bytes", (long)rc);
382 loghex(response, rc);
384 /* expect the request or auth */
385 rc = sread(fd, buffer, sizeof(buffer));
386 if(rc <= 0) {
387 logmsg("SOCKS5 request or auth message missing, recv returned %ld",
388 (long)rc);
389 return CURL_SOCKET_BAD;
390 }
392 logmsg("READ %ld bytes", (long)rc);
393 loghex(buffer, rc);
395 if(s_config.responsemethod == 2) {
396 /* RFC 1929 authentication
397 +----+------+----------+------+----------+
398 |VER | ULEN | UNAME | PLEN | PASSWD |
399 +----+------+----------+------+----------+
400 | 1 | 1 | 1 to 255 | 1 | 1 to 255 |
401 +----+------+----------+------+----------+
402 */
403 unsigned char ulen;
404 unsigned char plen;
405 bool login = TRUE;
406 if(rc < 5) {
407 logmsg("Too short auth input: %ld", (long)rc);
408 return CURL_SOCKET_BAD;
409 }
410 if(buffer[SOCKS5_VERSION] != 1) {
411 logmsg("Auth VERSION byte not 1, got %d", buffer[SOCKS5_VERSION]);
412 return CURL_SOCKET_BAD;
413 }
414 ulen = buffer[SOCKS5_ULEN];
415 if(rc < 4 + ulen) {
416 logmsg("Too short packet for username: %ld", (long)rc);
417 return CURL_SOCKET_BAD;
418 }
419 plen = buffer[SOCKS5_ULEN + ulen + 1];
420 if(rc < 3 + ulen + plen) {
421 logmsg("Too short packet for ulen %d plen %d: %ld",
422 ulen, plen, (long)rc);
423 return CURL_SOCKET_BAD;
424 }
425 if((ulen != strlen(s_config.user)) ||
426 (plen != strlen(s_config.password)) ||
427 memcmp(&buffer[SOCKS5_UNAME], s_config.user, ulen) ||
428 memcmp(&buffer[SOCKS5_UNAME + ulen + 1], s_config.password, plen)) {
429 /* no match! */
430 logmsg("mismatched credentials!");
431 login = FALSE;
432 }
433 response[0] = 1;
434 response[1] = login ? 0 : 1;
435 rc = swrite(fd, response, 2);
436 if(rc != 2) {
437 logmsg("Sending auth response failed!");
438 return CURL_SOCKET_BAD;
439 }
440 logmsg("Sent %ld bytes", (long)rc);
441 loghex(response, rc);
442 if(!login)
443 return CURL_SOCKET_BAD;
445 /* expect the request */
446 rc = sread(fd, buffer, sizeof(buffer));
447 if(rc <= 0) {
448 logmsg("SOCKS5 request message missing, recv returned %ld", (long)rc);
449 return CURL_SOCKET_BAD;
450 }
452 logmsg("READ %ld bytes", (long)rc);
453 loghex(buffer, rc);
454 }
455 if(rc < 6) {
456 logmsg("Too short for request: %ld", (long)rc);
457 return CURL_SOCKET_BAD;
458 }
460 if(buffer[SOCKS5_VERSION] != s_config.version) {
461 logmsg("Request VERSION byte not %d", s_config.version);
462 return CURL_SOCKET_BAD;
463 }
464 /* 1 == CONNECT */
465 if(buffer[SOCKS5_REQCMD] != s_config.reqcmd) {
466 logmsg("Request COMMAND byte not %d", s_config.reqcmd);
467 return CURL_SOCKET_BAD;
468 }
469 /* reserved, should be zero */
470 if(buffer[SOCKS5_RESERVED]) {
471 logmsg("Request COMMAND byte not %d", s_config.reqcmd);
472 return CURL_SOCKET_BAD;
473 }
474 /* ATYP:
475 o IPv4 address: 0x01
476 o domain name: 0x03
477 o IPv6 address: 0x04
478 */
479 type = buffer[SOCKS5_ATYP];
480 address = &buffer[SOCKS5_DSTADDR];
481 switch(type) {
482 case 1:
483 /* 4 bytes IPv4 address */
484 len = 4;
485 break;
486 case 3:
487 /* The first octet of the address field contains the number of octets of
488 name that follow */
489 len = buffer[SOCKS5_DSTADDR];
490 len++;
491 break;
492 case 4:
493 /* 16 bytes IPv6 address */
494 len = 16;
495 break;
496 default:
497 logmsg("Unknown ATYP %d", type);
498 return CURL_SOCKET_BAD;
499 }
500 if(rc < (4 + len + 2)) {
501 logmsg("Request too short: %ld, expected %d", (long)rc, 4 + len + 2);
502 return CURL_SOCKET_BAD;
503 }
504 logmsg("Received ATYP %d", type);
506 {
507 FILE *dump;
508 dump = curlx_fopen(reqlogfile, "ab");
509 if(dump) {
510 int i;
511 fprintf(dump, "atyp %u =>", type);
512 switch(type) {
513 case 1:
514 /* 4 bytes IPv4 address */
515 fprintf(dump, " %u.%u.%u.%u\n",
516 address[0], address[1], address[2], address[3]);
517 break;
518 case 3:
519 /* The first octet of the address field contains the number of octets
520 of name that follow */
521 fprintf(dump, " %.*s\n", len - 1, &address[1]);
522 break;
523 case 4:
524 /* 16 bytes IPv6 address */
525 for(i = 0; i < 16; i++) {
526 fprintf(dump, " %02x", address[i]);
527 }
528 fprintf(dump, "\n");
529 break;
530 }
531 curlx_fclose(dump);
532 }
533 }
535 if(!s_config.port) {
536 const unsigned char *portp = &buffer[SOCKS5_DSTADDR + len];
537 s5port = (uint16_t)((portp[0] << 8) | portp[1]);
538 }
539 else
540 s5port = s_config.port;
542 if(!s_config.connectrep)
543 connfd = socksconnect(s5port, s_config.addr);
545 if(connfd == CURL_SOCKET_BAD) {
546 /* failed */
547 rep = 1;
548 }
549 else {
550 rep = s_config.connectrep;
551 }
553 response[SOCKS5_VERSION] = s_config.responseversion;
555 /* o REP Reply field:
556 o 0x00 succeeded
557 o 0x01 general SOCKS server failure
558 o 0x02 connection not allowed by ruleset
559 o 0x03 Network unreachable
560 o 0x04 Host unreachable
561 o 0x05 Connection refused
562 o 0x06 TTL expired
563 o 0x07 Command not supported
564 o 0x08 Address type not supported
565 o 0x09 to 0xFF unassigned
566 */
567 response[SOCKS5_REP] = rep;
568 response[SOCKS5_RESERVED] = 0; /* must be zero */
569 response[SOCKS5_ATYP] = type; /* address type */
571 /* mirror back the original addr + port */
573 /* address or hostname */
574 memcpy(&response[SOCKS5_BNDADDR], address, len);
576 /* port number */
577 memcpy(&response[SOCKS5_BNDADDR + len],
578 &buffer[SOCKS5_DSTADDR + len], sizeof(socksport));
580 rc = swrite(fd, response, len + 6);
581 if(rc != (len + 6)) {
582 logmsg("Sending connect response failed!");
583 return CURL_SOCKET_BAD;
584 }
585 logmsg("Sent %ld bytes", (long)rc);
586 loghex(response, rc);
588 if(!rep)
589 return connfd;
591 if(connfd != CURL_SOCKET_BAD)
592 sclose(connfd);
594 return CURL_SOCKET_BAD;
597struct perclient {
598 size_t fromremote;
599 size_t fromclient;
600 curl_socket_t remotefd;
601 curl_socket_t clientfd;
602 bool used;
603};
605/* return non-zero when transfer is done */
606static int tunnel(struct perclient *cp, fd_set *fds)
608 ssize_t nread;
609 ssize_t nwrite;
610 char buffer[512];
611 if(FD_ISSET(cp->clientfd, fds)) {
612 /* read from client, send to remote */
613 nread = sread(cp->clientfd, buffer, sizeof(buffer));
614 if(nread > 0) {
615 nwrite = swrite(cp->remotefd, buffer, nread);
616 if(nwrite != nread)
617 return 1;
618 cp->fromclient += nwrite;
619 }
620 else
621 return 1;
622 }
623 if(FD_ISSET(cp->remotefd, fds)) {
624 /* read from remote, send to client */
625 nread = sread(cp->remotefd, buffer, sizeof(buffer));
626 if(nread > 0) {
627 nwrite = swrite(cp->clientfd, buffer, nread);
628 if(nwrite != nread)
629 return 1;
630 cp->fromremote += nwrite;
631 }
632 else
633 return 1;
634 }
635 return 0;
638/* sockfdp is a pointer to an established stream or CURL_SOCKET_BAD
640 if sockfd is CURL_SOCKET_BAD, listendfd is a listening socket we must
641 accept() */
642static bool socksd_incoming(curl_socket_t listenfd)
644 fd_set fds_read;
645 fd_set fds_write;
646 fd_set fds_err;
647 int clients = 0; /* connected clients */
648 struct perclient c[2];
650 memset(c, 0, sizeof(c));
651 if(got_exit_signal) {
652 logmsg("signalled to die, exiting...");
653 return FALSE;
654 }
656#ifdef HAVE_GETPPID
657 /* As a last resort, quit if socks5 process becomes orphan. */
658 if(getppid() <= 1) {
659 logmsg("process becomes orphan, exiting");
660 return FALSE;
661 }
662#endif
664 do {
665 int i;
666 ssize_t rc;
667 int sockerr = 0;
668 char errbuf[STRERROR_LEN];
669 curl_socket_t sockfd = listenfd;
670 int maxfd = (int)sockfd;
672 FD_ZERO(&fds_read);
673 FD_ZERO(&fds_write);
674 FD_ZERO(&fds_err);
676 /* there is always a socket to wait for */
677 FD_SET(sockfd, &fds_read);
679 for(i = 0; i < 2; i++) {
680 if(c[i].used) {
681 curl_socket_t fd = c[i].clientfd;
682 FD_SET(fd, &fds_read);
683 if((int)fd > maxfd)
684 maxfd = (int)fd;
685 fd = c[i].remotefd;
686 FD_SET(fd, &fds_read);
687 if((int)fd > maxfd)
688 maxfd = (int)fd;
689 }
690 }
692 do {
693 /* select() blocking behavior call on blocking descriptors please */
694 rc = select(maxfd + 1, &fds_read, &fds_write, &fds_err, NULL);
695 if(got_exit_signal) {
696 logmsg("signalled to die, exiting...");
697 return FALSE;
698 }
699 } while((rc == -1) && ((sockerr = SOCKERRNO) == SOCKEINTR));
701 if(rc < 0) {
702 logmsg("select() failed with error (%d) %s",
703 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
704 return FALSE;
705 }
707 if((clients < 2) && FD_ISSET(sockfd, &fds_read)) {
708 curl_socket_t newfd = accept(sockfd, NULL, NULL);
709 if(newfd == CURL_SOCKET_BAD) {
710 sockerr = SOCKERRNO;
711 logmsg("accept() failed with error (%d) %s",
712 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
713 }
714 else {
715 curl_socket_t remotefd;
716 logmsg("====> Client connect, "
717 "Read config from %s", configfile);
718 remotefd = sockit(newfd); /* SOCKS until done */
719 if(remotefd == CURL_SOCKET_BAD) {
720 logmsg("====> Client disconnect");
721 sclose(newfd);
722 }
723 else {
724 struct perclient *cp = &c[0];
725 logmsg("====> Tunnel transfer");
727 if(c[0].used)
728 cp = &c[1];
729 cp->fromremote = 0;
730 cp->fromclient = 0;
731 cp->clientfd = newfd;
732 cp->remotefd = remotefd;
733 cp->used = TRUE;
734 clients++;
735 }
736 }
737 }
738 for(i = 0; i < 2; i++) {
739 struct perclient *cp = &c[i];
740 if(cp->used && tunnel(cp, &fds_read)) {
741 logmsg("SOCKS transfer completed. Bytes: < %lu > %lu",
742 (unsigned long)cp->fromremote, (unsigned long)cp->fromclient);
743 sclose(cp->clientfd);
744 sclose(cp->remotefd);
745 cp->used = FALSE;
746 clients--;
747 }
748 }
749 } while(clients);
751 return TRUE;
754static int test_socksd(int argc, const char *argv[])
756 curl_socket_t sock = CURL_SOCKET_BAD;
757 curl_socket_t msgsock = CURL_SOCKET_BAD;
758 int wrotepidfile = 0;
759 int wroteportfile = 0;
760 bool juggle_again;
761 char errbuf[STRERROR_LEN];
762 int arg = 1;
763#ifdef USE_UNIX_SOCKETS
764 bool unlink_socket = FALSE;
765#endif
767 pidname = ".socksd.pid";
768 serverlogfile = "log/socksd.log";
769 configfile = "socksd.config";
770 server_port = 8905;
772 while(argc > arg) {
773 const char *opt;
774 curl_off_t num;
775 if(!strcmp("--version", argv[arg])) {
776 printf("socksd IPv4%s\n",
777#ifdef USE_IPV6
778 "/IPv6"
779#else
780 ""
781#endif
782 );
783 return 0;
784 }
785 else if(!strcmp("--pidfile", argv[arg])) {
786 arg++;
787 if(argc > arg)
788 pidname = argv[arg++];
789 }
790 else if(!strcmp("--portfile", argv[arg])) {
791 arg++;
792 if(argc > arg)
793 portname = argv[arg++];
794 }
795 else if(!strcmp("--config", argv[arg])) {
796 arg++;
797 if(argc > arg)
798 configfile = argv[arg++];
799 }
800 else if(!strcmp("--backend", argv[arg])) {
801 arg++;
802 if(argc > arg)
803 backendaddr = argv[arg++];
804 }
805 else if(!strcmp("--backendport", argv[arg])) {
806 arg++;
807 if(argc > arg) {
808 opt = argv[arg];
809 if(!curlx_str_number(&opt, &num, 0xffff))
810 backendport = (uint16_t)num;
811 arg++;
812 }
813 }
814 else if(!strcmp("--logfile", argv[arg])) {
815 arg++;
816 if(argc > arg)
817 serverlogfile = argv[arg++];
818 }
819 else if(!strcmp("--reqfile", argv[arg])) {
820 arg++;
821 if(argc > arg)
822 reqlogfile = argv[arg++];
823 }
824 else if(!strcmp("--ipv6", argv[arg])) {
825#ifdef USE_IPV6
826 socket_type = "IPv6";
827 socket_domain = AF_INET6;
828#endif
829 arg++;
830 }
831 else if(!strcmp("--ipv4", argv[arg])) {
832 socket_type = "IPv4";
833 socket_domain = AF_INET;
834 arg++;
835 }
836 else if(!strcmp("--unix-socket", argv[arg])) {
837 arg++;
838 if(argc > arg) {
839#ifdef USE_UNIX_SOCKETS
840 struct sockaddr_un sau;
841 server_unix_socket = argv[arg];
842 if(strlen(server_unix_socket) >= sizeof(sau.sun_path)) {
843 fprintf(stderr,
844 "socksd: socket path must be shorter than %u chars: %s\n",
845 (unsigned int)sizeof(sau.sun_path), server_unix_socket);
846 return 0;
847 }
848 socket_type = "unix";
849 socket_domain = AF_UNIX;
850#endif
851 arg++;
852 }
853 }
854 else if(!strcmp("--port", argv[arg])) {
855 arg++;
856 if(argc > arg) {
857 opt = argv[arg];
858 if(!curlx_str_number(&opt, &num, 0xffff))
859 server_port = (uint16_t)num;
860 arg++;
861 }
862 }
863 else {
864 puts("Usage: socksd [option]\n"
865 " --backend [ipv4 addr]\n"
866 " --backendport [TCP port]\n"
867 " --config [file]\n"
868 " --version\n"
869 " --logfile [file]\n"
870 " --pidfile [file]\n"
871 " --portfile [file]\n"
872 " --reqfile [file]\n"
873 " --ipv4\n"
874 " --ipv6\n"
875 " --unix-socket [file]\n"
876 " --port [port]\n");
877 return 0;
878 }
879 }
881 CURL_BINMODE(stdin);
882 CURL_BINMODE(stdout);
883 CURL_BINMODE(stderr);
885 install_signal_handlers(FALSE);
887 sock = socket(socket_domain, SOCK_STREAM, 0);
889 if(sock == CURL_SOCKET_BAD) {
890 int sockerr = SOCKERRNO;
891 logmsg("Error creating socket (%d) %s",
892 sockerr, curlx_strerror(sockerr, errbuf, sizeof(errbuf)));
893 goto socks5_cleanup;
894 }
896 {
897 /* passive daemon style */
898 sock = sockdaemon(sock, &server_port, server_unix_socket, FALSE);
899 if(sock == CURL_SOCKET_BAD) {
900 goto socks5_cleanup;
901 }
902#ifdef USE_UNIX_SOCKETS
903 unlink_socket = TRUE;
904#endif
905 msgsock = CURL_SOCKET_BAD; /* no stream socket yet */
906 }
908 logmsg("Running %s version", socket_type);
910#ifdef USE_UNIX_SOCKETS
911 if(socket_domain == AF_UNIX)
912 logmsg("Listening on Unix socket %s", server_unix_socket);
913 else
914#endif
915 logmsg("Listening on port %hu", server_port);
917 wrotepidfile = write_pidfile(pidname);
918 if(!wrotepidfile) {
919 goto socks5_cleanup;
920 }
922 if(portname) {
923 wroteportfile = write_portfile(portname, server_port);
924 if(!wroteportfile) {
925 goto socks5_cleanup;
926 }
927 }
929 do {
930 juggle_again = socksd_incoming(sock);
931 } while(juggle_again);
933socks5_cleanup:
935 if((msgsock != sock) && (msgsock != CURL_SOCKET_BAD))
936 sclose(msgsock);
938 if(sock != CURL_SOCKET_BAD)
939 sclose(sock);
941#ifdef USE_UNIX_SOCKETS
942 if(unlink_socket && socket_domain == AF_UNIX && server_unix_socket &&
943 unlink(server_unix_socket))
944 logmsg("unlink(%s): %d (%s)", server_unix_socket,
945 errno, curlx_strerror(errno, errbuf, sizeof(errbuf)));
946#endif
948 if(wrotepidfile)
949 unlink(pidname);
950 if(wroteportfile)
951 unlink(portname);
953 restore_signal_handlers(FALSE);
955 return 0;
Diagnostics4 git calls · 183 ms
CallStatusSizeTime0183 ms
discover200152 B4 ms
git.upload-pack discover
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=4
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 · hit6 ms
git.upload-pack ls-refs
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=ls-refs
git.upstream.dur_ms=2
git.response.bytes=149
git.response.cache=HIT
git.response.duration_ms=2
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 · hit2 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=2
git.response.bytes=293470
git.response.cache=HIT
git.response.duration_ms=2
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=86
Open pack map
fetch2007.7 KiB175 ms
git.upload-pack fetch
http.response.status_code=200
git.upstream.cf_ray=-
git.upstream.phase=fetch
git.upstream.dur_ms=175
git.response.bytes=7884
git.response.cache=MISS
git.response.duration_ms=175
git.fetch.wants=979afc6953f5f49b83536d16d62dfa95b81f9e2f
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=7884
git.pkt.payload.max=7863
git.sideband.band1.bytes=7862
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
a5e2a9776dbfe6b0
Request
28cb7674…
Protocol
v2 · sha1
Agent
gitty/1.0
Filter
advertised
Fetch
shallow filter sideband-all
Capabilities
agent ls-refs fetch object-format