tun2socks.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. /**
  2. * @file tun2socks.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stddef.h>
  25. #include <string.h>
  26. #include <misc/version.h>
  27. #include <misc/loggers_string.h>
  28. #include <misc/loglevel.h>
  29. #include <misc/minmax.h>
  30. #include <misc/offset.h>
  31. #include <misc/dead.h>
  32. #include <structure/LinkedList2.h>
  33. #include <system/BLog.h>
  34. #include <system/BReactor.h>
  35. #include <system/BSocket.h>
  36. #include <system/BSignal.h>
  37. #include <system/BAddr.h>
  38. #include <flow/PacketBuffer.h>
  39. #include <flow/BufferWriter.h>
  40. #include <flow/SinglePacketBuffer.h>
  41. #include <socksclient/BSocksClient.h>
  42. #include <tuntap/BTap.h>
  43. #include <lwip/init.h>
  44. #include <lwip/tcp_impl.h>
  45. #include <lwip/netif.h>
  46. #include <lwip/tcp.h>
  47. #ifndef BADVPN_USE_WINAPI
  48. #include <system/BLog_syslog.h>
  49. #endif
  50. #include <tun2socks/tun2socks.h>
  51. #include <generated/blog_channel_tun2socks.h>
  52. #define LOGGER_STDOUT 1
  53. #define LOGGER_SYSLOG 2
  54. #define SYNC_DECL \
  55. BPending sync_mark; \
  56. #define SYNC_FROMHERE \
  57. BPending_Init(&sync_mark, BReactor_PendingGroup(&ss), NULL, NULL); \
  58. BPending_Set(&sync_mark);
  59. #define SYNC_BREAK \
  60. BPending_Free(&sync_mark);
  61. #define SYNC_COMMIT \
  62. BReactor_Synchronize(&ss, &sync_mark); \
  63. BPending_Free(&sync_mark);
  64. // command-line options
  65. struct {
  66. int help;
  67. int version;
  68. int logger;
  69. #ifndef BADVPN_USE_WINAPI
  70. char *logger_syslog_facility;
  71. char *logger_syslog_ident;
  72. #endif
  73. int loglevel;
  74. int loglevels[BLOG_NUM_CHANNELS];
  75. char *tundev;
  76. char *netif_ipaddr;
  77. char *netif_netmask;
  78. char *socks_server_addr;
  79. } options;
  80. // TCP client
  81. struct tcp_client {
  82. dead_t dead;
  83. dead_t dead_client;
  84. LinkedList2Node list_node;
  85. BAddr local_addr;
  86. BAddr remote_addr;
  87. struct tcp_pcb *pcb;
  88. int client_closed;
  89. uint8_t buf[TCP_WND];
  90. int buf_used;
  91. BSocksClient socks_client;
  92. int socks_up;
  93. int socks_closed;
  94. StreamPassInterface *socks_send_if;
  95. int socks_send_prev_buf_used;
  96. BPending socks_send_finished_job;
  97. StreamRecvInterface *socks_recv_if;
  98. uint8_t socks_recv_buf[CLIENT_SOCKS_RECV_BUF_SIZE];
  99. int socks_recv_buf_used;
  100. int socks_recv_buf_sent;
  101. int socks_recv_waiting;
  102. int socks_recv_tcp_pending;
  103. };
  104. // IP address of netif
  105. BIPAddr netif_ipaddr;
  106. // netmask of netif
  107. BIPAddr netif_netmask;
  108. // SOCKS server address
  109. BAddr socks_server_addr;
  110. // reactor
  111. BReactor ss;
  112. // set to 1 by terminate
  113. int quitting;
  114. // TUN device
  115. BTap device;
  116. // device writing
  117. BufferWriter device_write_writer;
  118. PacketBuffer device_write_buffer;
  119. // device reading
  120. SinglePacketBuffer device_read_buffer;
  121. PacketPassInterface device_read_interface;
  122. // TCP timer
  123. BTimer tcp_timer;
  124. // job for initializing lwip
  125. BPending lwip_init_job;
  126. // lwip netif
  127. int have_netif;
  128. struct netif netif;
  129. // lwip TCP listener
  130. struct tcp_pcb *listener;
  131. // TCP clients
  132. LinkedList2 tcp_clients;
  133. // number of clients
  134. int num_clients;
  135. static void terminate (void);
  136. static void print_help (const char *name);
  137. static void print_version (void);
  138. static int parse_arguments (int argc, char *argv[]);
  139. static int process_arguments (void);
  140. static void signal_handler (void *unused);
  141. static void lwip_init_job_hadler (void *unused);
  142. static void tcp_timer_handler (void *unused);
  143. static void device_error_handler (void *unused);
  144. static void device_read_handler_send (void *unused, uint8_t *data, int data_len);
  145. static err_t netif_init_func (struct netif *netif);
  146. static err_t netif_output_func (struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr);
  147. static void client_log (struct tcp_client *client, int level, const char *fmt, ...);
  148. static err_t listener_accept_func (void *arg, struct tcp_pcb *newpcb, err_t err);
  149. static void client_handle_freed_client (struct tcp_client *client, int was_abrt);
  150. static int client_free_client (struct tcp_client *client);
  151. static void client_abort_client (struct tcp_client *client);
  152. static void client_free_socks (struct tcp_client *client);
  153. static void client_murder (struct tcp_client *client);
  154. static void client_dealloc (struct tcp_client *client);
  155. static void client_err_func (void *arg, err_t err);
  156. static err_t client_recv_func (void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err);
  157. static void client_socks_handler (struct tcp_client *client, int event);
  158. static void client_send_to_socks (struct tcp_client *client);
  159. static void client_socks_send_handler_done (struct tcp_client *client, int data_len);
  160. static void client_socks_send_finished_job_handler (struct tcp_client *client);
  161. static void client_socks_recv_initiate (struct tcp_client *client);
  162. static void client_socks_recv_handler_done (struct tcp_client *client, int data_len);
  163. static int client_socks_recv_send_out (struct tcp_client *client);
  164. static err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len);
  165. int main (int argc, char **argv)
  166. {
  167. if (argc <= 0) {
  168. return 1;
  169. }
  170. // parse command-line arguments
  171. if (!parse_arguments(argc, argv)) {
  172. fprintf(stderr, "Failed to parse arguments\n");
  173. print_help(argv[0]);
  174. goto fail0;
  175. }
  176. // handle --help and --version
  177. if (options.help) {
  178. print_version();
  179. print_help(argv[0]);
  180. return 0;
  181. }
  182. if (options.version) {
  183. print_version();
  184. return 0;
  185. }
  186. // initialize logger
  187. switch (options.logger) {
  188. case LOGGER_STDOUT:
  189. BLog_InitStdout();
  190. break;
  191. #ifndef BADVPN_USE_WINAPI
  192. case LOGGER_SYSLOG:
  193. if (!BLog_InitSyslog(options.logger_syslog_ident, options.logger_syslog_facility)) {
  194. fprintf(stderr, "Failed to initialize syslog logger\n");
  195. goto fail0;
  196. }
  197. break;
  198. #endif
  199. default:
  200. ASSERT(0);
  201. }
  202. // configure logger channels
  203. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  204. if (options.loglevels[i] >= 0) {
  205. BLog_SetChannelLoglevel(i, options.loglevels[i]);
  206. }
  207. else if (options.loglevel >= 0) {
  208. BLog_SetChannelLoglevel(i, options.loglevel);
  209. }
  210. }
  211. BLog(BLOG_NOTICE, "initializing "GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION);
  212. // initialize sockets
  213. if (BSocket_GlobalInit() < 0) {
  214. BLog(BLOG_ERROR, "BSocket_GlobalInit failed");
  215. goto fail1;
  216. }
  217. // process arguments
  218. if (!process_arguments()) {
  219. BLog(BLOG_ERROR, "Failed to process arguments");
  220. goto fail1;
  221. }
  222. // init time
  223. BTime_Init();
  224. // init reactor
  225. if (!BReactor_Init(&ss)) {
  226. BLog(BLOG_ERROR, "BReactor_Init failed");
  227. goto fail1;
  228. }
  229. // set not quitting
  230. quitting = 0;
  231. // setup signal handler
  232. if (!BSignal_Init(&ss, signal_handler, NULL)) {
  233. BLog(BLOG_ERROR, "BSignal_Init failed");
  234. goto fail2;
  235. }
  236. // init TUN device
  237. if (!BTap_Init(&device, &ss, options.tundev, device_error_handler, NULL, 1)) {
  238. BLog(BLOG_ERROR, "BTap_Init failed");
  239. goto fail3;
  240. }
  241. // NOTE: the order of the following is important:
  242. // first device writing must evaluate,
  243. // then lwip (so it can send packets to the device),
  244. // then device reading (so it can pass received packets to lwip).
  245. // init device reading
  246. PacketPassInterface_Init(&device_read_interface, BTap_GetMTU(&device), device_read_handler_send, NULL, BReactor_PendingGroup(&ss));
  247. if (!SinglePacketBuffer_Init(&device_read_buffer, BTap_GetOutput(&device), &device_read_interface, BReactor_PendingGroup(&ss))) {
  248. BLog(BLOG_ERROR, "SinglePacketBuffer_Init failed");
  249. goto fail4;
  250. }
  251. // init lwip init job
  252. BPending_Init(&lwip_init_job, BReactor_PendingGroup(&ss), lwip_init_job_hadler, NULL);
  253. BPending_Set(&lwip_init_job);
  254. // init device writing
  255. BufferWriter_Init(&device_write_writer, BTap_GetMTU(&device), BReactor_PendingGroup(&ss));
  256. if (!PacketBuffer_Init(&device_write_buffer, BufferWriter_GetOutput(&device_write_writer), BTap_GetInput(&device), DEVICE_WRITE_BUFFER_SIZE, BReactor_PendingGroup(&ss))) {
  257. BLog(BLOG_ERROR, "PacketBuffer_Init failed");
  258. goto fail5;
  259. }
  260. // init TCP timer
  261. // it won't trigger before lwip is initialized, becuase the lwip init is a job
  262. BTimer_Init(&tcp_timer, TCP_TMR_INTERVAL, tcp_timer_handler, NULL);
  263. BReactor_SetTimer(&ss, &tcp_timer);
  264. // set no netif
  265. have_netif = 0;
  266. // set no listener
  267. listener = NULL;
  268. // init clients list
  269. LinkedList2_Init(&tcp_clients);
  270. // init number of clients
  271. num_clients = 0;
  272. goto event_loop;
  273. fail5:
  274. BufferWriter_Free(&device_write_writer);
  275. BPending_Free(&lwip_init_job);
  276. SinglePacketBuffer_Free(&device_read_buffer);
  277. fail4:
  278. PacketPassInterface_Free(&device_read_interface);
  279. BTap_Free(&device);
  280. fail3:
  281. BSignal_Finish();
  282. fail2:
  283. BReactor_Free(&ss);
  284. fail1:
  285. BLog(BLOG_ERROR, "initialization failed");
  286. BLog_Free();
  287. fail0:
  288. // finish objects
  289. DebugObjectGlobal_Finish();
  290. return 1;
  291. event_loop:
  292. // enter event loop
  293. BLog(BLOG_NOTICE, "entering event loop");
  294. int ret = BReactor_Exec(&ss);
  295. // free clients
  296. LinkedList2Node *node;
  297. while (node = LinkedList2_GetFirst(&tcp_clients)) {
  298. struct tcp_client *client = UPPER_OBJECT(node, struct tcp_client, list_node);
  299. client_log(client, BLOG_INFO, "killing");
  300. client_murder(client);
  301. }
  302. // free listener
  303. if (listener) {
  304. tcp_close(listener);
  305. }
  306. // free netif
  307. if (have_netif) {
  308. netif_remove(&netif);
  309. }
  310. // free reactor
  311. BReactor_Free(&ss);
  312. // free logger
  313. BLog(BLOG_NOTICE, "exiting");
  314. BLog_Free();
  315. // finish objects
  316. DebugObjectGlobal_Finish();
  317. return ret;
  318. }
  319. void terminate (void)
  320. {
  321. ASSERT(!quitting)
  322. BLog(BLOG_NOTICE, "tearing down");
  323. // free TCP timer
  324. BReactor_RemoveTimer(&ss, &tcp_timer);
  325. // free device writing
  326. PacketBuffer_Free(&device_write_buffer);
  327. BufferWriter_Free(&device_write_writer);
  328. // free lwip init job
  329. BPending_Free(&lwip_init_job);
  330. // free device reading
  331. SinglePacketBuffer_Free(&device_read_buffer);
  332. PacketPassInterface_Free(&device_read_interface);
  333. // free device
  334. BTap_Free(&device);
  335. // remove signal handler
  336. BSignal_Finish();
  337. // set quitting
  338. quitting = 1;
  339. // exit reactor
  340. BReactor_Quit(&ss, 1);
  341. }
  342. void print_help (const char *name)
  343. {
  344. printf(
  345. "Usage:\n"
  346. " %s\n"
  347. " [--help]\n"
  348. " [--version]\n"
  349. " [--logger <"LOGGERS_STRING">]\n"
  350. #ifndef BADVPN_USE_WINAPI
  351. " (logger=syslog?\n"
  352. " [--syslog-facility <string>]\n"
  353. " [--syslog-ident <string>]\n"
  354. " )\n"
  355. #endif
  356. " [--loglevel <0-5/none/error/warning/notice/info/debug>]\n"
  357. " [--channel-loglevel <channel-name> <0-5/none/error/warning/notice/info/debug>] ...\n"
  358. " [--tundev <name>]\n"
  359. " --netif-ipaddr <ipaddr>\n"
  360. " --netif-netmask <ipnetmask>\n"
  361. " --socks-server-addr <addr>\n"
  362. "Address format is a.b.c.d:port (IPv4) or [addr]:port (IPv6).\n",
  363. name
  364. );
  365. }
  366. void print_version (void)
  367. {
  368. printf(GLOBAL_PRODUCT_NAME" "PROGRAM_NAME" "GLOBAL_VERSION"\n"GLOBAL_COPYRIGHT_NOTICE"\n");
  369. }
  370. int parse_arguments (int argc, char *argv[])
  371. {
  372. if (argc <= 0) {
  373. return 0;
  374. }
  375. options.help = 0;
  376. options.version = 0;
  377. options.logger = LOGGER_STDOUT;
  378. #ifndef BADVPN_USE_WINAPI
  379. options.logger_syslog_facility = "daemon";
  380. options.logger_syslog_ident = argv[0];
  381. #endif
  382. options.loglevel = -1;
  383. for (int i = 0; i < BLOG_NUM_CHANNELS; i++) {
  384. options.loglevels[i] = -1;
  385. }
  386. options.tundev = NULL;
  387. options.netif_ipaddr = NULL;
  388. options.netif_netmask = NULL;
  389. options.socks_server_addr = NULL;
  390. int i;
  391. for (i = 1; i < argc; i++) {
  392. char *arg = argv[i];
  393. if (!strcmp(arg, "--help")) {
  394. options.help = 1;
  395. }
  396. else if (!strcmp(arg, "--version")) {
  397. options.version = 1;
  398. }
  399. else if (!strcmp(arg, "--logger")) {
  400. if (1 >= argc - i) {
  401. fprintf(stderr, "%s: requires an argument\n", arg);
  402. return 0;
  403. }
  404. char *arg2 = argv[i + 1];
  405. if (!strcmp(arg2, "stdout")) {
  406. options.logger = LOGGER_STDOUT;
  407. }
  408. #ifndef BADVPN_USE_WINAPI
  409. else if (!strcmp(arg2, "syslog")) {
  410. options.logger = LOGGER_SYSLOG;
  411. }
  412. #endif
  413. else {
  414. fprintf(stderr, "%s: wrong argument\n", arg);
  415. return 0;
  416. }
  417. i++;
  418. }
  419. #ifndef BADVPN_USE_WINAPI
  420. else if (!strcmp(arg, "--syslog-facility")) {
  421. if (1 >= argc - i) {
  422. fprintf(stderr, "%s: requires an argument\n", arg);
  423. return 0;
  424. }
  425. options.logger_syslog_facility = argv[i + 1];
  426. i++;
  427. }
  428. else if (!strcmp(arg, "--syslog-ident")) {
  429. if (1 >= argc - i) {
  430. fprintf(stderr, "%s: requires an argument\n", arg);
  431. return 0;
  432. }
  433. options.logger_syslog_ident = argv[i + 1];
  434. i++;
  435. }
  436. #endif
  437. else if (!strcmp(arg, "--loglevel")) {
  438. if (1 >= argc - i) {
  439. fprintf(stderr, "%s: requires an argument\n", arg);
  440. return 0;
  441. }
  442. if ((options.loglevel = parse_loglevel(argv[i + 1])) < 0) {
  443. fprintf(stderr, "%s: wrong argument\n", arg);
  444. return 0;
  445. }
  446. i++;
  447. }
  448. else if (!strcmp(arg, "--channel-loglevel")) {
  449. if (2 >= argc - i) {
  450. fprintf(stderr, "%s: requires two arguments\n", arg);
  451. return 0;
  452. }
  453. int channel = BLogGlobal_GetChannelByName(argv[i + 1]);
  454. if (channel < 0) {
  455. fprintf(stderr, "%s: wrong channel argument\n", arg);
  456. return 0;
  457. }
  458. int loglevel = parse_loglevel(argv[i + 2]);
  459. if (loglevel < 0) {
  460. fprintf(stderr, "%s: wrong loglevel argument\n", arg);
  461. return 0;
  462. }
  463. options.loglevels[channel] = loglevel;
  464. i += 2;
  465. }
  466. else if (!strcmp(arg, "--tundev")) {
  467. if (1 >= argc - i) {
  468. fprintf(stderr, "%s: requires an argument\n", arg);
  469. return 0;
  470. }
  471. options.tundev = argv[i + 1];
  472. i++;
  473. }
  474. else if (!strcmp(arg, "--netif-ipaddr")) {
  475. if (1 >= argc - i) {
  476. fprintf(stderr, "%s: requires an argument\n", arg);
  477. return 0;
  478. }
  479. options.netif_ipaddr = argv[i + 1];
  480. i++;
  481. }
  482. else if (!strcmp(arg, "--netif-netmask")) {
  483. if (1 >= argc - i) {
  484. fprintf(stderr, "%s: requires an argument\n", arg);
  485. return 0;
  486. }
  487. options.netif_netmask = argv[i + 1];
  488. i++;
  489. }
  490. else if (!strcmp(arg, "--socks-server-addr")) {
  491. if (1 >= argc - i) {
  492. fprintf(stderr, "%s: requires an argument\n", arg);
  493. return 0;
  494. }
  495. options.socks_server_addr = argv[i + 1];
  496. i++;
  497. }
  498. else {
  499. fprintf(stderr, "unknown option: %s\n", arg);
  500. return 0;
  501. }
  502. }
  503. if (options.help || options.version) {
  504. return 1;
  505. }
  506. if (!options.netif_ipaddr) {
  507. fprintf(stderr, "--netif-ipaddr is required\n");
  508. return 0;
  509. }
  510. if (!options.netif_netmask) {
  511. fprintf(stderr, "--netif-netmask is required\n");
  512. return 0;
  513. }
  514. if (!options.socks_server_addr) {
  515. fprintf(stderr, "--socks-server-addr is required\n");
  516. return 0;
  517. }
  518. return 1;
  519. }
  520. int process_arguments (void)
  521. {
  522. // resolve netif ipaddr
  523. if (!BIPAddr_Resolve(&netif_ipaddr, options.netif_ipaddr, 0)) {
  524. BLog(BLOG_ERROR, "netif ipaddr: BIPAddr_Resolve failed");
  525. return 0;
  526. }
  527. if (netif_ipaddr.type != BADDR_TYPE_IPV4) {
  528. BLog(BLOG_ERROR, "netif ipaddr: must be an IPv4 address");
  529. return 0;
  530. }
  531. // resolve netif netmask
  532. if (!BIPAddr_Resolve(&netif_netmask, options.netif_netmask, 0)) {
  533. BLog(BLOG_ERROR, "netif netmask: BIPAddr_Resolve failed");
  534. return 0;
  535. }
  536. if (netif_netmask.type != BADDR_TYPE_IPV4) {
  537. BLog(BLOG_ERROR, "netif netmask: must be an IPv4 address");
  538. return 0;
  539. }
  540. // resolve SOCKS server address
  541. if (!BAddr_Parse2(&socks_server_addr, options.socks_server_addr, NULL, 0, 0)) {
  542. BLog(BLOG_ERROR, "socks server addr: BAddr_Parse2 failed");
  543. return 0;
  544. }
  545. return 1;
  546. }
  547. void signal_handler (void *unused)
  548. {
  549. ASSERT(!quitting)
  550. BLog(BLOG_NOTICE, "termination requested");
  551. terminate();
  552. return;
  553. }
  554. void lwip_init_job_hadler (void *unused)
  555. {
  556. ASSERT(!quitting)
  557. ASSERT(netif_ipaddr.type == BADDR_TYPE_IPV4)
  558. ASSERT(netif_netmask.type == BADDR_TYPE_IPV4)
  559. ASSERT(!have_netif)
  560. BLog(BLOG_DEBUG, "lwip init");
  561. // NOTE: the device may fail during this, but there's no harm in not checking
  562. // for that at every step
  563. int res;
  564. // init lwip
  565. lwip_init();
  566. // make addresses for netif
  567. ip_addr_t addr;
  568. addr.addr = netif_ipaddr.ipv4;
  569. ip_addr_t netmask;
  570. netmask.addr = netif_netmask.ipv4;
  571. ip_addr_t gw;
  572. ip_addr_set_any(&gw);
  573. // init netif
  574. if (!netif_add(&netif, &addr, &netmask, &gw, NULL, netif_init_func, ip_input)) {
  575. BLog(BLOG_ERROR, "netif_add failed");
  576. goto fail;
  577. }
  578. have_netif = 1;
  579. // set netif up
  580. netif_set_up(&netif);
  581. // set netif pretend TCP
  582. netif_set_pretend_tcp(&netif, 1);
  583. // init listener
  584. struct tcp_pcb *l = tcp_new();
  585. if (!l) {
  586. BLog(BLOG_ERROR, "tcp_new failed");
  587. goto fail;
  588. }
  589. // bind listener
  590. if (tcp_bind_to_netif(l, "ho0") != ERR_OK) {
  591. BLog(BLOG_ERROR, "tcp_bind_to_netif failed");
  592. tcp_close(l);
  593. goto fail;
  594. }
  595. // listen listener
  596. struct tcp_pcb *l2 = tcp_listen(l);
  597. if (!l2) {
  598. BLog(BLOG_ERROR, "tcp_listen failed");
  599. tcp_close(l);
  600. goto fail;
  601. }
  602. listener = l2;
  603. // setup accept handler
  604. tcp_accept(listener, listener_accept_func);
  605. return;
  606. fail:
  607. if (!quitting) {
  608. terminate();
  609. }
  610. }
  611. void tcp_timer_handler (void *unused)
  612. {
  613. ASSERT(!quitting)
  614. BLog(BLOG_DEBUG, "TCP timer");
  615. // schedule next timer
  616. // TODO: calculate timeout so we don't drift
  617. BReactor_SetTimer(&ss, &tcp_timer);
  618. tcp_tmr();
  619. return;
  620. }
  621. void device_error_handler (void *unused)
  622. {
  623. ASSERT(!quitting)
  624. BLog(BLOG_ERROR, "device error");
  625. terminate();
  626. return;
  627. }
  628. void device_read_handler_send (void *unused, uint8_t *data, int data_len)
  629. {
  630. ASSERT(!quitting)
  631. BLog(BLOG_DEBUG, "device: received packet");
  632. // accept packet
  633. PacketPassInterface_Done(&device_read_interface);
  634. // obtain pbuf
  635. struct pbuf *p = pbuf_alloc(PBUF_RAW, data_len, PBUF_POOL);
  636. if (!p) {
  637. BLog(BLOG_WARNING, "device read: pbuf_alloc failed");
  638. return;
  639. }
  640. // write packet to pbuf
  641. ASSERT_FORCE(pbuf_take(p, data, data_len) == ERR_OK)
  642. // pass pbuf to input
  643. if (netif.input(p, &netif) != ERR_OK) {
  644. BLog(BLOG_WARNING, "device read: input failed");
  645. pbuf_free(p);
  646. }
  647. }
  648. err_t netif_init_func (struct netif *netif)
  649. {
  650. BLog(BLOG_DEBUG, "netif func init");
  651. netif->name[0] = 'h';
  652. netif->name[1] = 'o';
  653. netif->output = netif_output_func;
  654. return ERR_OK;
  655. }
  656. err_t netif_output_func (struct netif *netif, struct pbuf *p, ip_addr_t *ipaddr)
  657. {
  658. SYNC_DECL
  659. BLog(BLOG_DEBUG, "device write: send packet");
  660. if (quitting) {
  661. return ERR_OK;
  662. }
  663. uint8_t *out;
  664. if (!BufferWriter_StartPacket(&device_write_writer, &out)) {
  665. DEBUG("netif func output: BufferWriter_StartPacket failed");
  666. return ERR_OK;
  667. }
  668. int len = 0;
  669. while (p) {
  670. int remain = BTap_GetMTU(&device) - len;
  671. if (p->len > remain) {
  672. BLog(BLOG_WARNING, "netif func output: no space left");
  673. break;
  674. }
  675. memcpy(out + len, p->payload, p->len);
  676. len += p->len;
  677. p = p->next;
  678. }
  679. SYNC_FROMHERE
  680. BufferWriter_EndPacket(&device_write_writer, len);
  681. SYNC_COMMIT
  682. return ERR_OK;
  683. }
  684. void client_log (struct tcp_client *client, int level, const char *fmt, ...)
  685. {
  686. va_list vl;
  687. va_start(vl, fmt);
  688. char local_addr_s[BADDR_MAX_PRINT_LEN];
  689. BAddr_Print(&client->local_addr, local_addr_s);
  690. char remote_addr_s[BADDR_MAX_PRINT_LEN];
  691. BAddr_Print(&client->remote_addr, remote_addr_s);
  692. BLog_Append("%05d (%s %s): ", num_clients, local_addr_s, remote_addr_s);
  693. BLog_LogToChannelVarArg(BLOG_CURRENT_CHANNEL, level, fmt, vl);
  694. va_end(vl);
  695. }
  696. err_t listener_accept_func (void *arg, struct tcp_pcb *newpcb, err_t err)
  697. {
  698. ASSERT(listener)
  699. ASSERT(err == ERR_OK)
  700. // signal accepted
  701. tcp_accepted(listener);
  702. // allocate client structure
  703. struct tcp_client *client = malloc(sizeof(*client));
  704. if (!client) {
  705. BLog(BLOG_ERROR, "listener accept: malloc failed");
  706. return ERR_MEM;
  707. }
  708. SYNC_DECL
  709. SYNC_FROMHERE
  710. // init SOCKS
  711. BAddr addr;
  712. BAddr_InitIPv4(&addr, newpcb->local_ip.addr, hton16(newpcb->local_port));
  713. #ifdef OVERRIDE_DEST_ADDR
  714. ASSERT_FORCE(BAddr_Parse2(&addr, OVERRIDE_DEST_ADDR, NULL, 0, 1))
  715. #endif
  716. if (!BSocksClient_Init(&client->socks_client, socks_server_addr, addr, (BSocksClient_handler)client_socks_handler, client, &ss)) {
  717. BLog(BLOG_ERROR, "listener accept: BSocksClient_Init failed");
  718. SYNC_BREAK
  719. free(client);
  720. return ERR_MEM;
  721. }
  722. // init dead vars
  723. DEAD_INIT(client->dead);
  724. DEAD_INIT(client->dead_client);
  725. // add to linked list
  726. LinkedList2_Append(&tcp_clients, &client->list_node);
  727. // increment counter
  728. ASSERT(num_clients >= 0)
  729. num_clients++;
  730. // set pcb
  731. client->pcb = newpcb;
  732. // set client not closed
  733. client->client_closed = 0;
  734. // read addresses
  735. BAddr_InitIPv4(&client->local_addr, client->pcb->local_ip.addr, hton16(client->pcb->local_port));
  736. BAddr_InitIPv4(&client->remote_addr, client->pcb->remote_ip.addr, hton16(client->pcb->remote_port));
  737. // setup handler argument
  738. tcp_arg(client->pcb, client);
  739. // setup handlers
  740. tcp_err(client->pcb, client_err_func);
  741. tcp_recv(client->pcb, client_recv_func);
  742. // setup buffer
  743. client->buf_used = 0;
  744. // set SOCKS not up, not closed
  745. client->socks_up = 0;
  746. client->socks_closed = 0;
  747. client_log(client, BLOG_INFO, "accepted");
  748. DEAD_ENTER(client->dead_client)
  749. SYNC_COMMIT
  750. if (DEAD_LEAVE(client->dead_client) == -1) {
  751. return ERR_ABRT;
  752. }
  753. return ERR_OK;
  754. }
  755. void client_handle_freed_client (struct tcp_client *client, int was_abrt)
  756. {
  757. ASSERT(!client->client_closed)
  758. ASSERT(was_abrt == 0 || was_abrt == 1)
  759. // pcb was taken care of by the caller
  760. // kill client dead var
  761. DEAD_KILL_WITH(client->dead_client, (was_abrt ? -1 : 1));
  762. // set client closed
  763. client->client_closed = 1;
  764. // if we have data to be sent to SOCKS and can send it, keep sending
  765. if (client->buf_used > 0 && !client->socks_closed) {
  766. client_log(client, BLOG_INFO, "waiting untill buffered data is sent to SOCKS");
  767. } else {
  768. if (!client->socks_closed) {
  769. client_free_socks(client);
  770. } else {
  771. client_dealloc(client);
  772. }
  773. }
  774. }
  775. int client_free_client (struct tcp_client *client)
  776. {
  777. ASSERT(!client->client_closed)
  778. int was_abrt = 0;
  779. // remove callbacks
  780. tcp_err(client->pcb, NULL);
  781. tcp_recv(client->pcb, NULL);
  782. tcp_sent(client->pcb, NULL);
  783. // free pcb
  784. err_t err = tcp_close(client->pcb);
  785. if (err != ERR_OK) {
  786. client_log(client, BLOG_ERROR, "tcp_close failed (%d)", err);
  787. tcp_abort(client->pcb);
  788. was_abrt = 1;
  789. }
  790. client_handle_freed_client(client, was_abrt);
  791. return was_abrt;
  792. }
  793. void client_abort_client (struct tcp_client *client)
  794. {
  795. ASSERT(!client->client_closed)
  796. // remove callbacks
  797. tcp_err(client->pcb, NULL);
  798. tcp_recv(client->pcb, NULL);
  799. tcp_sent(client->pcb, NULL);
  800. // free pcb
  801. tcp_abort(client->pcb);
  802. client_handle_freed_client(client, 1);
  803. }
  804. void client_free_socks (struct tcp_client *client)
  805. {
  806. ASSERT(!client->socks_closed)
  807. // stop sending to SOCKS
  808. if (client->socks_up) {
  809. // remove send finished job
  810. BPending_Free(&client->socks_send_finished_job);
  811. // stop receiving from client
  812. if (!client->client_closed) {
  813. tcp_recv(client->pcb, NULL);
  814. }
  815. }
  816. // free SOCKS
  817. BSocksClient_Free(&client->socks_client);
  818. // set SOCKS closed
  819. client->socks_closed = 1;
  820. // if we have data to be sent to the client and we can send it, keep sending
  821. if (client->socks_up && (client->socks_recv_buf_used >= 0 || client->socks_recv_tcp_pending > 0) && !client->client_closed) {
  822. client_log(client, BLOG_INFO, "waiting until buffered data is sent to client");
  823. } else {
  824. if (!client->client_closed) {
  825. client_free_client(client);
  826. } else {
  827. client_dealloc(client);
  828. }
  829. }
  830. }
  831. void client_murder (struct tcp_client *client)
  832. {
  833. // free client
  834. if (!client->client_closed) {
  835. // remove callbacks
  836. tcp_err(client->pcb, NULL);
  837. tcp_recv(client->pcb, NULL);
  838. tcp_sent(client->pcb, NULL);
  839. // abort
  840. tcp_abort(client->pcb);
  841. // kill client dead var
  842. DEAD_KILL_WITH(client->dead_client, -1);
  843. // set client closed
  844. client->client_closed = 1;
  845. }
  846. // free SOCKS
  847. if (!client->socks_closed) {
  848. if (client->socks_up) {
  849. // remove send finished job
  850. BPending_Free(&client->socks_send_finished_job);
  851. }
  852. // free SOCKS
  853. BSocksClient_Free(&client->socks_client);
  854. // set SOCKS closed
  855. client->socks_closed = 1;
  856. }
  857. // dealloc entry
  858. client_dealloc(client);
  859. }
  860. void client_dealloc (struct tcp_client *client)
  861. {
  862. ASSERT(client->client_closed)
  863. ASSERT(client->socks_closed)
  864. // decrement counter
  865. ASSERT(num_clients > 0)
  866. num_clients--;
  867. // remove client entry
  868. LinkedList2_Remove(&tcp_clients, &client->list_node);
  869. // kill dead var
  870. DEAD_KILL(client->dead);
  871. // free memory
  872. free(client);
  873. }
  874. void client_err_func (void *arg, err_t err)
  875. {
  876. struct tcp_client *client = arg;
  877. ASSERT(!client->client_closed)
  878. client_log(client, BLOG_INFO, "client error (%d)", (int)err);
  879. // the pcb was taken care of by the caller
  880. client_handle_freed_client(client, 0);
  881. }
  882. err_t client_recv_func (void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  883. {
  884. struct tcp_client *client = arg;
  885. ASSERT(!client->client_closed)
  886. ASSERT(err == ERR_OK) // checked in lwIP source. Otherwise, I've no idea what should
  887. // be done with the pbuf in case of an error.
  888. if (!p) {
  889. client_log(client, BLOG_INFO, "client closed");
  890. int ret = client_free_client(client);
  891. return (err ? ERR_ABRT : ERR_OK);
  892. }
  893. ASSERT(p->tot_len > 0)
  894. // check if we have enough buffer
  895. if (p->tot_len > sizeof(client->buf) - client->buf_used) {
  896. client_log(client, BLOG_ERROR, "no buffer for data !?!");
  897. return ERR_MEM;
  898. }
  899. // copy data to buffer
  900. ASSERT_EXECUTE(pbuf_copy_partial(p, client->buf + client->buf_used, p->tot_len, 0) == p->tot_len)
  901. client->buf_used += p->tot_len;
  902. // if there was nothing in the buffer before, and SOCKS is up, start send data
  903. if (client->buf_used == p->tot_len && client->socks_up) {
  904. ASSERT(!client->socks_closed) // this callback is removed when SOCKS is closed
  905. SYNC_DECL
  906. SYNC_FROMHERE
  907. client_send_to_socks(client);
  908. DEAD_ENTER(client->dead_client)
  909. SYNC_COMMIT
  910. if (DEAD_LEAVE(client->dead_client) == -1) {
  911. return ERR_ABRT;
  912. }
  913. }
  914. // free pbuff
  915. pbuf_free(p);
  916. return ERR_OK;
  917. }
  918. void client_socks_handler (struct tcp_client *client, int event)
  919. {
  920. ASSERT(!client->socks_closed)
  921. switch (event) {
  922. case BSOCKSCLIENT_EVENT_ERROR: {
  923. client_log(client, BLOG_INFO, "SOCKS error");
  924. client_free_socks(client);
  925. } break;
  926. case BSOCKSCLIENT_EVENT_UP: {
  927. ASSERT(!client->socks_up)
  928. client_log(client, BLOG_INFO, "SOCKS up");
  929. // init sending
  930. client->socks_send_if = BSocksClient_GetSendInterface(&client->socks_client);
  931. StreamPassInterface_Sender_Init(client->socks_send_if, (StreamPassInterface_handler_done)client_socks_send_handler_done, client);
  932. client->socks_send_prev_buf_used = -1;
  933. BPending_Init(&client->socks_send_finished_job, BReactor_PendingGroup(&ss), (BPending_handler)client_socks_send_finished_job_handler, client);
  934. // init receiving
  935. client->socks_recv_if = BSocksClient_GetRecvInterface(&client->socks_client);
  936. StreamRecvInterface_Receiver_Init(client->socks_recv_if, (StreamRecvInterface_handler_done)client_socks_recv_handler_done, client);
  937. client->socks_recv_buf_used = -1;
  938. client->socks_recv_tcp_pending = 0;
  939. tcp_sent(client->pcb, client_sent_func);
  940. // set up
  941. client->socks_up = 1;
  942. // start sending data if there is any
  943. if (client->buf_used > 0) {
  944. client_send_to_socks(client);
  945. }
  946. // start receiving data if client is still up
  947. if (!client->client_closed) {
  948. client_socks_recv_initiate(client);
  949. }
  950. } break;
  951. case BSOCKSCLIENT_EVENT_ERROR_CLOSED: {
  952. ASSERT(client->socks_up)
  953. client_log(client, BLOG_INFO, "SOCKS closed");
  954. client_free_socks(client);
  955. } break;
  956. default:
  957. ASSERT(0);
  958. }
  959. }
  960. void client_send_to_socks (struct tcp_client *client)
  961. {
  962. ASSERT(!client->socks_closed)
  963. ASSERT(client->socks_up)
  964. ASSERT(client->buf_used > 0)
  965. ASSERT(client->socks_send_prev_buf_used == -1)
  966. // remember amount of data in buffer
  967. client->socks_send_prev_buf_used = client->buf_used;
  968. // schedule finished job
  969. BPending_Set(&client->socks_send_finished_job);
  970. // schedule sending
  971. StreamPassInterface_Sender_Send(client->socks_send_if, client->buf, client->buf_used);
  972. }
  973. void client_socks_send_handler_done (struct tcp_client *client, int data_len)
  974. {
  975. ASSERT(!client->socks_closed)
  976. ASSERT(client->socks_up)
  977. ASSERT(client->buf_used > 0)
  978. ASSERT(client->socks_send_prev_buf_used > 0)
  979. ASSERT(data_len > 0)
  980. ASSERT(data_len <= client->buf_used)
  981. // remove sent data from buffer
  982. memmove(client->buf, client->buf + data_len, client->buf_used - data_len);
  983. client->buf_used -= data_len;
  984. // send any further data
  985. if (client->buf_used > 0) {
  986. StreamPassInterface_Sender_Send(client->socks_send_if, client->buf, client->buf_used);
  987. }
  988. }
  989. void client_socks_send_finished_job_handler (struct tcp_client *client)
  990. {
  991. ASSERT(!client->socks_closed)
  992. ASSERT(client->socks_up)
  993. ASSERT(client->socks_send_prev_buf_used > 0)
  994. ASSERT(client->buf_used <= client->socks_send_prev_buf_used)
  995. // calculate how much data was sent
  996. int sent = client->socks_send_prev_buf_used - client->buf_used;
  997. // unset remembered amount of data in buffer
  998. client->socks_send_prev_buf_used = -1;
  999. if (client->client_closed) {
  1000. // client was closed we've sent everything we had buffered; we're done with it
  1001. client_log(client, BLOG_INFO, "removing after client went down");
  1002. client_free_socks(client);
  1003. } else {
  1004. // confirm sent data
  1005. if (sent > 0) {
  1006. tcp_recved(client->pcb, sent);
  1007. }
  1008. }
  1009. }
  1010. void client_socks_recv_initiate (struct tcp_client *client)
  1011. {
  1012. ASSERT(!client->client_closed)
  1013. ASSERT(!client->socks_closed)
  1014. ASSERT(client->socks_up)
  1015. ASSERT(client->socks_recv_buf_used == -1)
  1016. StreamRecvInterface_Receiver_Recv(client->socks_recv_if, client->socks_recv_buf, sizeof(client->socks_recv_buf));
  1017. }
  1018. void client_socks_recv_handler_done (struct tcp_client *client, int data_len)
  1019. {
  1020. ASSERT(data_len > 0)
  1021. ASSERT(data_len <= sizeof(client->socks_recv_buf))
  1022. ASSERT(!client->socks_closed)
  1023. ASSERT(client->socks_up)
  1024. ASSERT(client->socks_recv_buf_used == -1)
  1025. // if client was closed, stop receiving
  1026. if (client->client_closed) {
  1027. return;
  1028. }
  1029. // set amount of data in buffer
  1030. client->socks_recv_buf_used = data_len;
  1031. client->socks_recv_buf_sent = 0;
  1032. client->socks_recv_waiting = 0;
  1033. // send to client
  1034. if (client_socks_recv_send_out(client) < 0) {
  1035. return;
  1036. }
  1037. // continue receiving if needed
  1038. if (client->socks_recv_buf_used == -1) {
  1039. client_socks_recv_initiate(client);
  1040. }
  1041. }
  1042. int client_socks_recv_send_out (struct tcp_client *client)
  1043. {
  1044. ASSERT(!client->client_closed)
  1045. ASSERT(client->socks_up)
  1046. ASSERT(client->socks_recv_buf_used > 0)
  1047. ASSERT(client->socks_recv_buf_sent < client->socks_recv_buf_used)
  1048. ASSERT(!client->socks_recv_waiting)
  1049. // return value -1 means tcp_abort() was done,
  1050. // 0 means it wasn't and the client (pcb) is still up
  1051. do {
  1052. int to_write = BMIN(client->socks_recv_buf_used - client->socks_recv_buf_sent, tcp_sndbuf(client->pcb));
  1053. if (to_write == 0) {
  1054. break;
  1055. }
  1056. err_t err = tcp_write(client->pcb, client->socks_recv_buf + client->socks_recv_buf_sent, to_write, TCP_WRITE_FLAG_COPY);
  1057. if (err != ERR_OK) {
  1058. if (err == ERR_MEM) {
  1059. break;
  1060. }
  1061. client_log(client, BLOG_INFO, "tcp_write failed (%d)", (int)err);
  1062. client_abort_client(client);
  1063. return -1;
  1064. }
  1065. client->socks_recv_buf_sent += to_write;
  1066. client->socks_recv_tcp_pending += to_write;
  1067. } while (client->socks_recv_buf_sent < client->socks_recv_buf_used);
  1068. // start sending now
  1069. err_t err = tcp_output(client->pcb);
  1070. if (err != ERR_OK) {
  1071. client_log(client, BLOG_INFO, "tcp_output failed (%d)", (int)err);
  1072. client_abort_client(client);
  1073. return -1;
  1074. }
  1075. // more data to queue?
  1076. if (client->socks_recv_buf_sent < client->socks_recv_buf_used) {
  1077. if (client->socks_recv_tcp_pending == 0) {
  1078. client_log(client, BLOG_ERROR, "can't queue data, but all data was confirmed !?!");
  1079. client_abort_client(client);
  1080. return -1;
  1081. }
  1082. // set waiting, continue in client_sent_func
  1083. client->socks_recv_waiting = 1;
  1084. return 0;
  1085. }
  1086. // everything was queued
  1087. client->socks_recv_buf_used = -1;
  1088. return 0;
  1089. }
  1090. err_t client_sent_func (void *arg, struct tcp_pcb *tpcb, u16_t len)
  1091. {
  1092. struct tcp_client *client = arg;
  1093. ASSERT(!client->client_closed)
  1094. ASSERT(client->socks_up)
  1095. ASSERT(len > 0)
  1096. ASSERT(len <= client->socks_recv_tcp_pending)
  1097. // decrement pending
  1098. client->socks_recv_tcp_pending -= len;
  1099. // continue queuing
  1100. if (client->socks_recv_buf_used > 0) {
  1101. ASSERT(client->socks_recv_waiting)
  1102. ASSERT(client->socks_recv_buf_sent < client->socks_recv_buf_used)
  1103. // set not waiting
  1104. client->socks_recv_waiting = 0;
  1105. // possibly send more data
  1106. if (client_socks_recv_send_out(client) < 0) {
  1107. return ERR_ABRT;
  1108. }
  1109. // we just queued some data, so it can't have been confirmed yet
  1110. ASSERT(client->socks_recv_tcp_pending > 0)
  1111. // continue receiving if needed
  1112. if (client->socks_recv_buf_used == -1 && !client->socks_closed) {
  1113. SYNC_DECL
  1114. SYNC_FROMHERE
  1115. client_socks_recv_initiate(client);
  1116. DEAD_ENTER(client->dead)
  1117. SYNC_COMMIT
  1118. if (DEAD_LEAVE(client->dead)) {
  1119. return ERR_ABRT;
  1120. }
  1121. }
  1122. return ERR_OK;
  1123. }
  1124. // have we sent everything after SOCKS was closed?
  1125. if (client->socks_closed && client->socks_recv_tcp_pending == 0) {
  1126. client_log(client, BLOG_INFO, "removing after SOCKS went down");
  1127. int ret = client_free_client(client);
  1128. return (ret ? ERR_ABRT : ERR_OK);
  1129. }
  1130. return ERR_OK;
  1131. }