proxy.c 24 KB

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