proxy.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Network proxy abstraction in PuTTY
  3. *
  4. * A proxy layer, if necessary, wedges itself between the network
  5. * code and the higher level backend.
  6. */
  7. #include <assert.h>
  8. #include <ctype.h>
  9. #include <string.h>
  10. #include "putty.h"
  11. #include "network.h"
  12. #include "proxy.h"
  13. #define do_proxy_dns(conf) \
  14. (conf_get_int(conf, CONF_proxy_dns) == FORCE_ON || \
  15. (conf_get_int(conf, CONF_proxy_dns) == AUTO && \
  16. conf_get_int(conf, CONF_proxy_type) != PROXY_SOCKS4))
  17. static void proxy_negotiator_cleanup(ProxySocket *ps)
  18. {
  19. if (ps->pn) {
  20. proxy_negotiator_free(ps->pn);
  21. ps->pn = NULL;
  22. }
  23. if (ps->clientseat) {
  24. interactor_return_seat(ps->clientitr);
  25. ps->clientitr = NULL;
  26. ps->clientseat = NULL;
  27. }
  28. }
  29. /*
  30. * Call this when proxy negotiation is complete, so that this
  31. * socket can begin working normally.
  32. */
  33. static void proxy_activate(ProxySocket *ps)
  34. {
  35. size_t output_before, output_after;
  36. proxy_negotiator_cleanup(ps);
  37. plug_log(ps->plug, PLUGLOG_CONNECT_SUCCESS, NULL, 0, NULL, 0);
  38. /* we want to ignore new receive events until we have sent
  39. * all of our buffered receive data.
  40. */
  41. sk_set_frozen(ps->sub_socket, true);
  42. /* how many bytes of output have we buffered? */
  43. output_before = bufchain_size(&ps->pending_oob_output_data) +
  44. bufchain_size(&ps->pending_output_data);
  45. /* and keep track of how many bytes do not get sent. */
  46. output_after = 0;
  47. /* send buffered OOB writes */
  48. while (bufchain_size(&ps->pending_oob_output_data) > 0) {
  49. ptrlen data = bufchain_prefix(&ps->pending_oob_output_data);
  50. output_after += sk_write_oob(ps->sub_socket, data.ptr, data.len);
  51. bufchain_consume(&ps->pending_oob_output_data, data.len);
  52. }
  53. /* send buffered normal writes */
  54. while (bufchain_size(&ps->pending_output_data) > 0) {
  55. ptrlen data = bufchain_prefix(&ps->pending_output_data);
  56. output_after += sk_write(ps->sub_socket, data.ptr, data.len);
  57. bufchain_consume(&ps->pending_output_data, data.len);
  58. }
  59. /* if we managed to send any data, let the higher levels know. */
  60. if (output_after < output_before)
  61. plug_sent(ps->plug, output_after);
  62. /* if we have a pending EOF to send, send it */
  63. if (ps->pending_eof) sk_write_eof(ps->sub_socket);
  64. /* if the backend wanted the socket unfrozen, try to unfreeze.
  65. * our set_frozen handler will flush buffered receive data before
  66. * unfreezing the actual underlying socket.
  67. */
  68. if (!ps->freeze)
  69. sk_set_frozen(&ps->sock, false);
  70. }
  71. /* basic proxy socket functions */
  72. static Plug *sk_proxy_plug (Socket *s, Plug *p)
  73. {
  74. ProxySocket *ps = container_of(s, ProxySocket, sock);
  75. Plug *ret = ps->plug;
  76. if (p)
  77. ps->plug = p;
  78. return ret;
  79. }
  80. static void sk_proxy_close (Socket *s)
  81. {
  82. ProxySocket *ps = container_of(s, ProxySocket, sock);
  83. sk_close(ps->sub_socket);
  84. sk_addr_free(ps->proxy_addr);
  85. sk_addr_free(ps->remote_addr);
  86. proxy_negotiator_cleanup(ps);
  87. bufchain_clear(&ps->output_from_negotiator);
  88. sfree(ps);
  89. }
  90. static size_t sk_proxy_write (Socket *s, const void *data, size_t len)
  91. {
  92. ProxySocket *ps = container_of(s, ProxySocket, sock);
  93. if (ps->pn) {
  94. bufchain_add(&ps->pending_output_data, data, len);
  95. return bufchain_size(&ps->pending_output_data);
  96. }
  97. return sk_write(ps->sub_socket, data, len);
  98. }
  99. static size_t sk_proxy_write_oob (Socket *s, const void *data, size_t len)
  100. {
  101. ProxySocket *ps = container_of(s, ProxySocket, sock);
  102. if (ps->pn) {
  103. bufchain_clear(&ps->pending_output_data);
  104. bufchain_clear(&ps->pending_oob_output_data);
  105. bufchain_add(&ps->pending_oob_output_data, data, len);
  106. return len;
  107. }
  108. return sk_write_oob(ps->sub_socket, data, len);
  109. }
  110. static void sk_proxy_write_eof (Socket *s)
  111. {
  112. ProxySocket *ps = container_of(s, ProxySocket, sock);
  113. if (ps->pn) {
  114. ps->pending_eof = true;
  115. return;
  116. }
  117. sk_write_eof(ps->sub_socket);
  118. }
  119. static void sk_proxy_set_frozen (Socket *s, bool is_frozen)
  120. {
  121. ProxySocket *ps = container_of(s, ProxySocket, sock);
  122. if (ps->pn) {
  123. ps->freeze = is_frozen;
  124. return;
  125. }
  126. /* handle any remaining buffered recv data first */
  127. if (bufchain_size(&ps->pending_input_data) > 0) {
  128. ps->freeze = is_frozen;
  129. /* loop while we still have buffered data, and while we are
  130. * unfrozen. the plug_receive call in the loop could result
  131. * in a call back into this function refreezing the socket,
  132. * so we have to check each time.
  133. */
  134. while (!ps->freeze && bufchain_size(&ps->pending_input_data) > 0) {
  135. char databuf[512];
  136. ptrlen data = bufchain_prefix(&ps->pending_input_data);
  137. if (data.len > lenof(databuf))
  138. data.len = lenof(databuf);
  139. memcpy(databuf, data.ptr, data.len);
  140. bufchain_consume(&ps->pending_input_data, data.len);
  141. plug_receive(ps->plug, 0, databuf, data.len);
  142. }
  143. /* if we're still frozen, we'll have to wait for another
  144. * call from the backend to finish unbuffering the data.
  145. */
  146. if (ps->freeze) return;
  147. }
  148. sk_set_frozen(ps->sub_socket, is_frozen);
  149. }
  150. static const char *sk_proxy_socket_error (Socket *s)
  151. {
  152. ProxySocket *ps = container_of(s, ProxySocket, sock);
  153. if (ps->error != NULL || ps->sub_socket == NULL) {
  154. return ps->error;
  155. }
  156. return sk_socket_error(ps->sub_socket);
  157. }
  158. /* basic proxy plug functions */
  159. static void plug_proxy_log(Plug *plug, PlugLogType type, SockAddr *addr,
  160. int port, const char *error_msg, int error_code)
  161. {
  162. ProxySocket *ps = container_of(plug, ProxySocket, plugimpl);
  163. plug_log(ps->plug, type, addr, port, error_msg, error_code);
  164. }
  165. static void plug_proxy_closing(Plug *p, PlugCloseType type,
  166. const char *error_msg)
  167. {
  168. ProxySocket *ps = container_of(p, ProxySocket, plugimpl);
  169. proxy_negotiator_cleanup(ps);
  170. plug_closing(ps->plug, type, error_msg);
  171. }
  172. static void proxy_negotiate(ProxySocket *ps)
  173. {
  174. assert(ps->pn);
  175. proxy_negotiator_process_queue(ps->pn);
  176. if (ps->pn->error) {
  177. char *err = dupprintf("Proxy error: %s", ps->pn->error);
  178. sfree(ps->pn->error);
  179. proxy_negotiator_cleanup(ps);
  180. plug_closing_error(ps->plug, err);
  181. sfree(err);
  182. return;
  183. } else if (ps->pn->aborted) {
  184. proxy_negotiator_cleanup(ps);
  185. plug_closing_user_abort(ps->plug);
  186. return;
  187. }
  188. if (ps->pn->reconnect) {
  189. sk_close(ps->sub_socket);
  190. { // WINSCP
  191. SockAddr *proxy_addr = sk_addr_dup(ps->proxy_addr);
  192. ps->sub_socket = sk_new(proxy_addr, ps->proxy_port,
  193. ps->proxy_privport, ps->proxy_oobinline,
  194. ps->proxy_nodelay, ps->proxy_keepalive,
  195. &ps->plugimpl,
  196. #ifdef WINSCP
  197. conf_get_int(ps->conf, CONF_connect_timeout), conf_get_int(ps->conf, CONF_sndbuf),
  198. conf_get_str(ps->conf, CONF_srcaddr)
  199. #endif
  200. );
  201. ps->pn->reconnect = false;
  202. /* If the negotiator has asked us to reconnect, they are
  203. * expecting that on the next call their input queue will
  204. * consist entirely of data from the _new_ connection, without
  205. * any remaining data buffered from the old one. (If they'd
  206. * wanted the latter, they could have read it out of the input
  207. * queue before asking us to close the connection.) */
  208. bufchain_clear(&ps->pending_input_data);
  209. } // WINSCP
  210. }
  211. while (bufchain_size(&ps->output_from_negotiator)) {
  212. ptrlen data = bufchain_prefix(&ps->output_from_negotiator);
  213. sk_write(ps->sub_socket, data.ptr, data.len);
  214. bufchain_consume(&ps->output_from_negotiator, data.len);
  215. }
  216. if (ps->pn->done)
  217. proxy_activate(ps);
  218. }
  219. static void plug_proxy_receive(
  220. Plug *p, int urgent, const char *data, size_t len)
  221. {
  222. ProxySocket *ps = container_of(p, ProxySocket, plugimpl);
  223. if (ps->pn) {
  224. /* we will lose the urgentness of this data, but since most,
  225. * if not all, of this data will be consumed by the negotiation
  226. * process, hopefully it won't affect the protocol above us
  227. */
  228. bufchain_add(&ps->pending_input_data, data, len);
  229. proxy_negotiate(ps);
  230. } else {
  231. plug_receive(ps->plug, urgent, data, len);
  232. }
  233. }
  234. static void plug_proxy_sent (Plug *p, size_t bufsize)
  235. {
  236. ProxySocket *ps = container_of(p, ProxySocket, plugimpl);
  237. if (ps->pn)
  238. return;
  239. plug_sent(ps->plug, bufsize);
  240. }
  241. static int plug_proxy_accepting(Plug *p,
  242. accept_fn_t constructor, accept_ctx_t ctx)
  243. {
  244. unreachable("ProxySockets never create listening Sockets");
  245. }
  246. /*
  247. * This function can accept a NULL pointer as `addr', in which case
  248. * it will only check the host name.
  249. */
  250. static bool proxy_for_destination(SockAddr *addr, const char *hostname,
  251. int port, Conf *conf)
  252. {
  253. int s = 0, e = 0;
  254. char hostip[64];
  255. int hostip_len, hostname_len;
  256. const char *exclude_list;
  257. /*
  258. * Special local connections such as Unix-domain sockets
  259. * unconditionally cannot be proxied, even in proxy-localhost
  260. * mode. There just isn't any way to ask any known proxy type for
  261. * them.
  262. */
  263. if (addr && sk_address_is_special_local(addr))
  264. return false; /* do not proxy */
  265. /*
  266. * Check the host name and IP against the hard-coded
  267. * representations of `localhost'.
  268. */
  269. if (!conf_get_bool(conf, CONF_even_proxy_localhost) &&
  270. (sk_hostname_is_local(hostname) ||
  271. (addr && sk_address_is_local(addr))))
  272. return false; /* do not proxy */
  273. /* we want a string representation of the IP address for comparisons */
  274. if (addr) {
  275. sk_getaddr(addr, hostip, 64);
  276. hostip_len = strlen(hostip);
  277. } else
  278. hostip_len = 0; /* placate gcc; shouldn't be required */
  279. hostname_len = strlen(hostname);
  280. exclude_list = conf_get_str(conf, CONF_proxy_exclude_list);
  281. /* now parse the exclude list, and see if either our IP
  282. * or hostname matches anything in it.
  283. */
  284. while (exclude_list[s]) {
  285. while (exclude_list[s] &&
  286. (isspace((unsigned char)exclude_list[s]) ||
  287. exclude_list[s] == ',')) s++;
  288. if (!exclude_list[s]) break;
  289. e = s;
  290. while (exclude_list[e] &&
  291. (isalnum((unsigned char)exclude_list[e]) ||
  292. exclude_list[e] == '-' ||
  293. exclude_list[e] == '.' ||
  294. exclude_list[e] == '*')) e++;
  295. if (exclude_list[s] == '*') {
  296. /* wildcard at beginning of entry */
  297. if ((addr && strnicmp(hostip + hostip_len - (e - s - 1),
  298. exclude_list + s + 1, e - s - 1) == 0) ||
  299. strnicmp(hostname + hostname_len - (e - s - 1),
  300. exclude_list + s + 1, e - s - 1) == 0) {
  301. /* IP/hostname range excluded. do not use proxy. */
  302. return false;
  303. }
  304. } else if (exclude_list[e-1] == '*') {
  305. /* wildcard at end of entry */
  306. if ((addr && strnicmp(hostip, exclude_list + s, e - s - 1) == 0) ||
  307. strnicmp(hostname, exclude_list + s, e - s - 1) == 0) {
  308. /* IP/hostname range excluded. do not use proxy. */
  309. return false;
  310. }
  311. } else {
  312. /* no wildcard at either end, so let's try an absolute
  313. * match (ie. a specific IP)
  314. */
  315. if (addr && strnicmp(hostip, exclude_list + s, e - s) == 0)
  316. return false; /* IP/hostname excluded. do not use proxy. */
  317. if (strnicmp(hostname, exclude_list + s, e - s) == 0)
  318. return false; /* IP/hostname excluded. do not use proxy. */
  319. }
  320. s = e;
  321. /* Make sure we really have reached the next comma or end-of-string */
  322. while (exclude_list[s] &&
  323. !isspace((unsigned char)exclude_list[s]) &&
  324. exclude_list[s] != ',') s++;
  325. }
  326. /* no matches in the exclude list, so use the proxy */
  327. return true;
  328. }
  329. static char *dns_log_msg(const char *host, int addressfamily,
  330. const char *reason)
  331. {
  332. return dupprintf("Looking up host \"%s\"%s for %s", host,
  333. (addressfamily == ADDRTYPE_IPV4 ? " (IPv4)" :
  334. addressfamily == ADDRTYPE_IPV6 ? " (IPv6)" :
  335. ""), reason);
  336. }
  337. SockAddr *name_lookup(const char *host, int port, char **canonicalname,
  338. Conf *conf, int addressfamily, LogContext *logctx,
  339. const char *reason)
  340. {
  341. if (conf_get_int(conf, CONF_proxy_type) != PROXY_NONE &&
  342. do_proxy_dns(conf) &&
  343. proxy_for_destination(NULL, host, port, conf)) {
  344. if (logctx)
  345. logeventf(logctx, "Leaving host lookup to proxy of \"%s\""
  346. " (for %s)", host, reason);
  347. *canonicalname = dupstr(host);
  348. return sk_nonamelookup(host);
  349. } else {
  350. if (logctx)
  351. logevent_and_free(
  352. logctx, dns_log_msg(host, addressfamily, reason));
  353. return sk_namelookup(host, canonicalname, addressfamily);
  354. }
  355. }
  356. static const SocketVtable ProxySocket_sockvt = {
  357. // WINSCP
  358. /*.plug =*/ sk_proxy_plug,
  359. /*.close =*/ sk_proxy_close,
  360. /*.write =*/ sk_proxy_write,
  361. /*.write_oob =*/ sk_proxy_write_oob,
  362. /*.write_eof =*/ sk_proxy_write_eof,
  363. /*.set_frozen =*/ sk_proxy_set_frozen,
  364. /*.socket_error =*/ sk_proxy_socket_error,
  365. /*.peer_info =*/ NULL,
  366. };
  367. static const PlugVtable ProxySocket_plugvt = {
  368. // WINSCP
  369. /*.log =*/ plug_proxy_log,
  370. /*.closing =*/ plug_proxy_closing,
  371. /*.receive =*/ plug_proxy_receive,
  372. /*.sent =*/ plug_proxy_sent,
  373. /*.accepting =*/ plug_proxy_accepting
  374. };
  375. static char *proxy_description(Interactor *itr)
  376. {
  377. ProxySocket *ps = container_of(itr, ProxySocket, interactor);
  378. assert(ps->pn);
  379. return dupprintf("%s connection to %s port %d", ps->pn->vt->type,
  380. conf_get_str(ps->conf, CONF_proxy_host),
  381. conf_get_int(ps->conf, CONF_proxy_port));
  382. }
  383. static LogPolicy *proxy_logpolicy(Interactor *itr)
  384. {
  385. ProxySocket *ps = container_of(itr, ProxySocket, interactor);
  386. return ps->clientlp;
  387. }
  388. static Seat *proxy_get_seat(Interactor *itr)
  389. {
  390. ProxySocket *ps = container_of(itr, ProxySocket, interactor);
  391. return ps->clientseat;
  392. }
  393. static void proxy_set_seat(Interactor *itr, Seat *seat)
  394. {
  395. ProxySocket *ps = container_of(itr, ProxySocket, interactor);
  396. ps->clientseat = seat;
  397. }
  398. static const InteractorVtable ProxySocket_interactorvt = {
  399. // WINSCP
  400. /*.description =*/ proxy_description,
  401. /*.logpolicy =*/ proxy_logpolicy,
  402. /*.get_seat =*/ proxy_get_seat,
  403. /*.set_seat =*/ proxy_set_seat,
  404. };
  405. static void proxy_prompts_callback(void *ctx)
  406. {
  407. proxy_negotiate((ProxySocket *)ctx);
  408. }
  409. prompts_t *proxy_new_prompts(ProxySocket *ps)
  410. {
  411. prompts_t *prs = new_prompts();
  412. prs->callback = proxy_prompts_callback;
  413. prs->callback_ctx = ps;
  414. return prs;
  415. }
  416. void proxy_spr_abort(ProxyNegotiator *pn, SeatPromptResult spr)
  417. {
  418. if (spr.kind == SPRK_SW_ABORT) {
  419. pn->error = spr_get_error_message(spr);
  420. } else {
  421. assert(spr.kind == SPRK_USER_ABORT);
  422. pn->aborted = true;
  423. }
  424. }
  425. Socket *new_connection(SockAddr *addr, const char *hostname,
  426. int port, bool privport,
  427. bool oobinline, bool nodelay, bool keepalive,
  428. Plug *plug, Conf *conf, Interactor *itr)
  429. {
  430. int type = conf_get_int(conf, CONF_proxy_type);
  431. if (type != PROXY_NONE &&
  432. proxy_for_destination(addr, hostname, port, conf))
  433. {
  434. ProxySocket *ps;
  435. SockAddr *proxy_addr;
  436. char *proxy_canonical_name;
  437. Socket *sret;
  438. if ((type == PROXY_SSH_TCPIP ||
  439. type == PROXY_SSH_EXEC ||
  440. type == PROXY_SSH_SUBSYSTEM) &&
  441. (sret = sshproxy_new_connection(addr, hostname, port, privport,
  442. oobinline, nodelay, keepalive,
  443. plug, conf, itr)) != NULL)
  444. return sret;
  445. if ((sret = platform_new_connection(addr, hostname, port, privport,
  446. oobinline, nodelay, keepalive,
  447. plug, conf, itr)) != NULL)
  448. return sret;
  449. ps = snew(ProxySocket);
  450. ps->sock.vt = &ProxySocket_sockvt;
  451. ps->plugimpl.vt = &ProxySocket_plugvt;
  452. ps->interactor.vt = &ProxySocket_interactorvt;
  453. ps->conf = conf_copy(conf);
  454. ps->plug = plug;
  455. ps->remote_addr = addr; /* will need to be freed on close */
  456. ps->remote_port = port;
  457. ps->error = NULL;
  458. ps->pending_eof = false;
  459. ps->freeze = false;
  460. bufchain_init(&ps->pending_input_data);
  461. bufchain_init(&ps->pending_output_data);
  462. bufchain_init(&ps->pending_oob_output_data);
  463. bufchain_init(&ps->output_from_negotiator);
  464. ps->sub_socket = NULL;
  465. /*
  466. * If we've been given an Interactor by the caller, set ourselves
  467. * up to work with it.
  468. */
  469. if (itr) {
  470. ps->clientitr = itr;
  471. interactor_set_child(ps->clientitr, &ps->interactor);
  472. ps->clientlp = interactor_logpolicy(ps->clientitr);
  473. ps->clientseat = interactor_borrow_seat(ps->clientitr);
  474. }
  475. { // WINSCP
  476. const ProxyNegotiatorVT *vt;
  477. switch (type) {
  478. case PROXY_HTTP:
  479. vt = &http_proxy_negotiator_vt;
  480. break;
  481. case PROXY_SOCKS4:
  482. vt = &socks4_proxy_negotiator_vt;
  483. break;
  484. case PROXY_SOCKS5:
  485. vt = &socks5_proxy_negotiator_vt;
  486. break;
  487. case PROXY_TELNET:
  488. vt = &telnet_proxy_negotiator_vt;
  489. break;
  490. default:
  491. ps->error = "Proxy error: Unknown proxy method";
  492. return &ps->sock;
  493. }
  494. ps->pn = proxy_negotiator_new(vt);
  495. ps->pn->ps = ps;
  496. ps->pn->done = false;
  497. ps->pn->error = NULL;
  498. ps->pn->aborted = false;
  499. ps->pn->input = &ps->pending_input_data;
  500. /* Provide an Interactor to the negotiator if and only if we
  501. * are usefully able to ask interactive questions of the user */
  502. ps->pn->itr = ps->clientseat ? &ps->interactor : NULL;
  503. bufchain_sink_init(ps->pn->output, &ps->output_from_negotiator);
  504. {
  505. char *logmsg = dupprintf("Will use %s proxy at %s:%d to connect"
  506. " to %s:%d", vt->type,
  507. conf_get_str(conf, CONF_proxy_host),
  508. conf_get_int(conf, CONF_proxy_port),
  509. hostname, port);
  510. plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, logmsg, 0);
  511. sfree(logmsg);
  512. }
  513. {
  514. char *logmsg = dns_log_msg(conf_get_str(conf, CONF_proxy_host),
  515. conf_get_int(conf, CONF_addressfamily),
  516. "proxy");
  517. plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, logmsg, 0);
  518. sfree(logmsg);
  519. }
  520. /* look-up proxy */
  521. proxy_addr = sk_namelookup(conf_get_str(conf, CONF_proxy_host),
  522. &proxy_canonical_name,
  523. conf_get_int(conf, CONF_addressfamily));
  524. if (sk_addr_error(proxy_addr) != NULL) {
  525. ps->error = "Proxy error: Unable to resolve proxy host name";
  526. sk_addr_free(proxy_addr);
  527. return &ps->sock;
  528. }
  529. sfree(proxy_canonical_name);
  530. {
  531. char addrbuf[256], *logmsg;
  532. sk_getaddr(proxy_addr, addrbuf, lenof(addrbuf));
  533. logmsg = dupprintf("Connecting to %s proxy at %s port %d",
  534. vt->type, addrbuf,
  535. conf_get_int(conf, CONF_proxy_port));
  536. plug_log(plug, PLUGLOG_PROXY_MSG, NULL, 0, logmsg, 0);
  537. sfree(logmsg);
  538. }
  539. /* create the actual socket we will be using,
  540. * connected to our proxy server and port.
  541. */
  542. ps->proxy_addr = sk_addr_dup(proxy_addr);
  543. ps->proxy_port = conf_get_int(conf, CONF_proxy_port);
  544. ps->proxy_privport = privport;
  545. ps->proxy_oobinline = oobinline;
  546. ps->proxy_nodelay = nodelay;
  547. ps->proxy_keepalive = keepalive;
  548. ps->sub_socket = sk_new(proxy_addr, ps->proxy_port,
  549. ps->proxy_privport, ps->proxy_oobinline,
  550. ps->proxy_nodelay, ps->proxy_keepalive,
  551. &ps->plugimpl,
  552. #ifdef WINSCP
  553. conf_get_int(conf, CONF_connect_timeout), conf_get_int(conf, CONF_sndbuf),
  554. conf_get_str(conf, CONF_srcaddr)
  555. #endif
  556. );
  557. if (sk_socket_error(ps->sub_socket) != NULL)
  558. return &ps->sock;
  559. /* start the proxy negotiation process... */
  560. sk_set_frozen(ps->sub_socket, false);
  561. proxy_negotiate(ps);
  562. return &ps->sock;
  563. } // WINSCP
  564. }
  565. /* no proxy, so just return the direct socket */
  566. return sk_new(addr, port, privport, oobinline, nodelay, keepalive, plug,
  567. #ifdef WINSCP
  568. conf_get_int(conf, CONF_connect_timeout), conf_get_int(conf, CONF_sndbuf),
  569. conf_get_str(conf, CONF_srcaddr)
  570. #endif
  571. );
  572. }
  573. Socket *new_listener(const char *srcaddr, int port, Plug *plug,
  574. bool local_host_only, Conf *conf, int addressfamily)
  575. {
  576. /* TODO: SOCKS (and potentially others) support inbound
  577. * TODO: connections via the proxy. support them.
  578. */
  579. return sk_newlistener(srcaddr, port, plug, local_host_only, addressfamily);
  580. }
  581. #ifdef WINSCP
  582. ProxySocket * get_proxy_plug_socket(Plug * p)
  583. {
  584. return container_of(p, ProxySocket, plugimpl);
  585. }
  586. #endif