handle-socket.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /*
  2. * General mechanism for wrapping up reading/writing of Windows
  3. * HANDLEs into a PuTTY Socket abstraction.
  4. */
  5. #include <stdio.h>
  6. #include <assert.h>
  7. #include <limits.h>
  8. #include "tree234.h"
  9. #include "putty.h"
  10. #include "network.h"
  11. #ifdef MPEXT
  12. #define queue_toplevel_callback(FN, CTX) queue_toplevel_callback(get_callback_set(CTX->plug), FN, CTX)
  13. #endif
  14. /*
  15. * Freezing one of these sockets is a slightly fiddly business,
  16. * because the reads from the handle are happening in a separate
  17. * thread as blocking system calls and so once one is in progress it
  18. * can't sensibly be interrupted. Hence, after the user tries to
  19. * freeze one of these sockets, it's unavoidable that we may receive
  20. * one more load of data before we manage to get handle-io.c to stop
  21. * reading.
  22. */
  23. typedef enum HandleSocketFreezeState {
  24. UNFROZEN, /* reading as normal */
  25. FREEZING, /* have been set to frozen but winhandl is still reading */
  26. FROZEN, /* really frozen - winhandl has been throttled */
  27. THAWING /* we're gradually releasing our remaining data */
  28. } HandleSocketFreezeState;
  29. typedef struct HandleSocket {
  30. union {
  31. struct {
  32. HANDLE send_H, recv_H, stderr_H;
  33. struct handle *send_h, *recv_h, *stderr_h;
  34. HandleSocketFreezeState frozen;
  35. /* We buffer data here if we receive it from winhandl
  36. * while frozen. */
  37. bufchain inputdata;
  38. /* Handle logging proxy error messages from stderr_H, if
  39. * we have one */
  40. ProxyStderrBuf psb;
  41. bool defer_close, deferred_close; /* in case of re-entrance */
  42. };
  43. struct {
  44. DeferredSocketOpener *opener;
  45. /* We buffer data here if we receive it via sk_write
  46. * before the socket is opened. */
  47. bufchain outputdata;
  48. bool output_eof_pending;
  49. bool start_frozen;
  50. };
  51. };
  52. char *error;
  53. SockAddr *addr;
  54. int port;
  55. Plug *plug;
  56. Socket sock;
  57. } HandleSocket;
  58. static size_t handle_gotdata(
  59. struct handle *h, const void *data, size_t len, int err)
  60. {
  61. HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
  62. if (err) {
  63. plug_closing_error(hs->plug, "Read error from handle");
  64. return 0;
  65. } else if (len == 0) {
  66. plug_closing_normal(hs->plug);
  67. return 0;
  68. } else {
  69. assert(hs->frozen != FROZEN && hs->frozen != THAWING);
  70. if (hs->frozen == FREEZING) {
  71. /*
  72. * If we've received data while this socket is supposed to
  73. * be frozen (because the read handle-io.c started before
  74. * sk_set_frozen was called has now returned) then buffer
  75. * the data for when we unfreeze.
  76. */
  77. bufchain_add(&hs->inputdata, data, len);
  78. hs->frozen = FROZEN;
  79. /*
  80. * And return a very large backlog, to prevent further
  81. * data arriving from winhandl until we unfreeze.
  82. */
  83. return INT_MAX;
  84. } else {
  85. plug_receive(hs->plug, 0, data, len);
  86. return 0;
  87. }
  88. }
  89. }
  90. static size_t handle_stderr(
  91. struct handle *h, const void *data, size_t len, int err)
  92. {
  93. HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
  94. if (!err && len > 0)
  95. log_proxy_stderr(hs->plug, &hs->psb, data, len);
  96. return 0;
  97. }
  98. static void handle_sentdata(struct handle *h, size_t new_backlog, int err,
  99. bool close)
  100. {
  101. HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
  102. if (close) {
  103. if (hs->send_H != INVALID_HANDLE_VALUE)
  104. CloseHandle(hs->send_H);
  105. if (hs->recv_H != INVALID_HANDLE_VALUE && hs->recv_H != hs->send_H)
  106. CloseHandle(hs->recv_H);
  107. hs->send_H = hs->recv_H = INVALID_HANDLE_VALUE;
  108. }
  109. if (err) {
  110. plug_closing_system_error(hs->plug, err);
  111. return;
  112. }
  113. plug_sent(hs->plug, new_backlog);
  114. }
  115. static Plug *sk_handle_plug(Socket *s, Plug *p)
  116. {
  117. HandleSocket *hs = container_of(s, HandleSocket, sock);
  118. Plug *ret = hs->plug;
  119. if (p)
  120. hs->plug = p;
  121. return ret;
  122. }
  123. static void sk_handle_close(Socket *s)
  124. {
  125. HandleSocket *hs = container_of(s, HandleSocket, sock);
  126. if (hs->defer_close) {
  127. hs->deferred_close = true;
  128. return;
  129. }
  130. #ifdef MPEXT
  131. // WinSCP core uses do_select as signalization of connection up/down
  132. do_select(hs->plug, INVALID_SOCKET, 0);
  133. #endif
  134. handle_free(hs->send_h);
  135. handle_free(hs->recv_h);
  136. if (hs->send_H != INVALID_HANDLE_VALUE)
  137. CloseHandle(hs->send_H);
  138. if (hs->recv_H != INVALID_HANDLE_VALUE && hs->recv_H != hs->send_H)
  139. CloseHandle(hs->recv_H);
  140. bufchain_clear(&hs->inputdata);
  141. #ifdef MPEXT
  142. if (hs->stderr_h)
  143. {
  144. handle_free(hs->stderr_h);
  145. }
  146. if (hs->stderr_H)
  147. {
  148. CloseHandle(hs->stderr_H);
  149. }
  150. #endif
  151. if (hs->addr)
  152. sk_addr_free(hs->addr);
  153. delete_callbacks_for_context(get_callback_set(hs->plug), hs);
  154. sfree(hs);
  155. }
  156. static size_t sk_handle_write(Socket *s, const void *data, size_t len)
  157. {
  158. HandleSocket *hs = container_of(s, HandleSocket, sock);
  159. return handle_write(hs->send_h, data, len);
  160. }
  161. static size_t sk_handle_write_oob(Socket *s, const void *data, size_t len)
  162. {
  163. /*
  164. * oob data is treated as inband; nasty, but nothing really
  165. * better we can do
  166. */
  167. return sk_handle_write(s, data, len);
  168. }
  169. static void sk_handle_write_eof(Socket *s)
  170. {
  171. HandleSocket *hs = container_of(s, HandleSocket, sock);
  172. handle_write_eof(hs->send_h);
  173. }
  174. static void handle_socket_unfreeze(void *hsv)
  175. {
  176. HandleSocket *hs = (HandleSocket *)hsv;
  177. /*
  178. * If we've been put into a state other than THAWING since the
  179. * last callback, then we're done.
  180. */
  181. if (hs->frozen != THAWING)
  182. return;
  183. /*
  184. * Get some of the data we've buffered.
  185. */
  186. { // WINSCP
  187. ptrlen data = bufchain_prefix(&hs->inputdata);
  188. assert(data.len > 0);
  189. /*
  190. * Hand it off to the plug. Be careful of re-entrance - that might
  191. * have the effect of trying to close this socket.
  192. */
  193. hs->defer_close = true;
  194. plug_receive(hs->plug, 0, data.ptr, data.len);
  195. bufchain_consume(&hs->inputdata, data.len);
  196. hs->defer_close = false;
  197. if (hs->deferred_close) {
  198. sk_handle_close(&hs->sock);
  199. return;
  200. }
  201. if (bufchain_size(&hs->inputdata) > 0) {
  202. /*
  203. * If there's still data in our buffer, stay in THAWING state,
  204. * and reschedule ourself.
  205. */
  206. queue_toplevel_callback(handle_socket_unfreeze, hs);
  207. } else {
  208. /*
  209. * Otherwise, we've successfully thawed!
  210. */
  211. hs->frozen = UNFROZEN;
  212. handle_unthrottle(hs->recv_h, 0);
  213. }
  214. } // WINSCP
  215. }
  216. static void sk_handle_set_frozen(Socket *s, bool is_frozen)
  217. {
  218. HandleSocket *hs = container_of(s, HandleSocket, sock);
  219. if (is_frozen) {
  220. switch (hs->frozen) {
  221. case FREEZING:
  222. case FROZEN:
  223. return; /* nothing to do */
  224. case THAWING:
  225. /*
  226. * We were in the middle of emptying our bufchain, and got
  227. * frozen again. In that case, handle-io.c is already
  228. * throttled, so just return to FROZEN state. The toplevel
  229. * callback will notice and disable itself.
  230. */
  231. hs->frozen = FROZEN;
  232. break;
  233. case UNFROZEN:
  234. /*
  235. * The normal case. Go to FREEZING, and expect one more
  236. * load of data from winhandl if we're unlucky.
  237. */
  238. hs->frozen = FREEZING;
  239. break;
  240. }
  241. } else {
  242. switch (hs->frozen) {
  243. case UNFROZEN:
  244. case THAWING:
  245. return; /* nothing to do */
  246. case FREEZING:
  247. /*
  248. * If winhandl didn't send us any data throughout the time
  249. * we were frozen, then we'll still be in this state and
  250. * can just unfreeze in the trivial way.
  251. */
  252. assert(bufchain_size(&hs->inputdata) == 0);
  253. hs->frozen = UNFROZEN;
  254. break;
  255. case FROZEN:
  256. /*
  257. * If we have buffered data, go to THAWING and start
  258. * releasing it in top-level callbacks.
  259. */
  260. hs->frozen = THAWING;
  261. queue_toplevel_callback(handle_socket_unfreeze, hs);
  262. }
  263. }
  264. }
  265. static const char *sk_handle_socket_error(Socket *s)
  266. {
  267. HandleSocket *hs = container_of(s, HandleSocket, sock);
  268. return hs->error;
  269. }
  270. static SocketPeerInfo *sk_handle_peer_info(Socket *s)
  271. {
  272. HandleSocket *hs = container_of(s, HandleSocket, sock);
  273. ULONG pid;
  274. static HMODULE kernel32_module;
  275. DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
  276. (HANDLE, PULONG));
  277. if (!kernel32_module) {
  278. kernel32_module = load_system32_dll("kernel32.dll");
  279. #if !HAVE_GETNAMEDPIPECLIENTPROCESSID
  280. /* For older Visual Studio, and MinGW too (at least as of
  281. * Ubuntu 16.04), this function isn't available in the header
  282. * files to type-check. Ditto the toolchain I use for
  283. * Coveritying the Windows code. */
  284. GET_WINDOWS_FUNCTION_NO_TYPECHECK(
  285. kernel32_module, GetNamedPipeClientProcessId);
  286. #else
  287. GET_WINDOWS_FUNCTION(
  288. kernel32_module, GetNamedPipeClientProcessId);
  289. #endif
  290. }
  291. /*
  292. * Of course, not all handles managed by this module will be
  293. * server ends of named pipes, but if they are, then it's useful
  294. * to log what we can find out about the client end.
  295. */
  296. if (p_GetNamedPipeClientProcessId &&
  297. p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
  298. SocketPeerInfo *pi = snew(SocketPeerInfo);
  299. pi->addressfamily = ADDRTYPE_LOCAL;
  300. pi->addr_text = NULL;
  301. pi->port = -1;
  302. pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
  303. return pi;
  304. }
  305. return NULL;
  306. }
  307. static const SocketVtable HandleSocket_sockvt = {
  308. // WINSCP
  309. /*.plug =*/ sk_handle_plug,
  310. /*.close =*/ sk_handle_close,
  311. /*.write =*/ sk_handle_write,
  312. /*.write_oob =*/ sk_handle_write_oob,
  313. /*.write_eof =*/ sk_handle_write_eof,
  314. /*.set_frozen =*/ sk_handle_set_frozen,
  315. /*.socket_error =*/ sk_handle_socket_error,
  316. /*.peer_info =*/ sk_handle_peer_info,
  317. };
  318. static void sk_handle_connect_success_callback(void *ctx)
  319. {
  320. HandleSocket *hs = (HandleSocket *)ctx;
  321. plug_log(hs->plug, PLUGLOG_CONNECT_SUCCESS, hs->addr, hs->port, NULL, 0);
  322. }
  323. Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  324. SockAddr *addr, int port, Plug *plug,
  325. bool overlapped)
  326. {
  327. HandleSocket *hs;
  328. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  329. struct callback_set * callback_set = get_callback_set(plug); // WINSCP
  330. hs = snew(HandleSocket);
  331. hs->sock.vt = &HandleSocket_sockvt;
  332. hs->addr = addr;
  333. hs->port = port;
  334. hs->plug = plug;
  335. hs->error = NULL;
  336. hs->frozen = UNFROZEN;
  337. bufchain_init(&hs->inputdata);
  338. psb_init(&hs->psb);
  339. hs->recv_H = recv_H;
  340. hs->recv_h = handle_input_new(callback_set, hs->recv_H, handle_gotdata, hs, flags); // WINSCP
  341. hs->send_H = send_H;
  342. hs->send_h = handle_output_new(callback_set, hs->send_H, handle_sentdata, hs, flags); // WINSCP
  343. hs->stderr_H = stderr_H;
  344. hs->stderr_h = NULL; // WINSCP
  345. if (hs->stderr_H)
  346. hs->stderr_h = handle_input_new(callback_set, hs->stderr_H, handle_stderr, // WINSCP
  347. hs, flags);
  348. hs->defer_close = hs->deferred_close = false;
  349. #ifdef MPEXT
  350. // WinSCP core uses do_select as signalization of connection up/down
  351. do_select(plug, INVALID_SOCKET, 1);
  352. #endif
  353. queue_toplevel_callback(sk_handle_connect_success_callback, hs);
  354. return &hs->sock;
  355. }
  356. void handle_socket_set_psb_prefix(Socket *s, const char *prefix)
  357. {
  358. HandleSocket *hs = container_of(s, HandleSocket, sock);
  359. assert(hs->sock.vt == &HandleSocket_sockvt);
  360. psb_set_prefix(&hs->psb, prefix);
  361. }
  362. static void sk_handle_deferred_close(Socket *s)
  363. {
  364. HandleSocket *hs = container_of(s, HandleSocket, sock);
  365. deferred_socket_opener_free(hs->opener);
  366. bufchain_clear(&hs->outputdata);
  367. if (hs->addr)
  368. sk_addr_free(hs->addr);
  369. delete_callbacks_for_context(get_callback_set(hs->plug), hs);
  370. sfree(hs);
  371. }
  372. static size_t sk_handle_deferred_write(Socket *s, const void *data, size_t len)
  373. {
  374. HandleSocket *hs = container_of(s, HandleSocket, sock);
  375. assert(!hs->output_eof_pending);
  376. bufchain_add(&hs->outputdata, data, len);
  377. return bufchain_size(&hs->outputdata);
  378. }
  379. static void sk_handle_deferred_write_eof(Socket *s)
  380. {
  381. HandleSocket *hs = container_of(s, HandleSocket, sock);
  382. assert(!hs->output_eof_pending);
  383. hs->output_eof_pending = true;
  384. }
  385. static void sk_handle_deferred_set_frozen(Socket *s, bool is_frozen)
  386. {
  387. HandleSocket *hs = container_of(s, HandleSocket, sock);
  388. hs->frozen = is_frozen;
  389. }
  390. static SocketPeerInfo *sk_handle_deferred_peer_info(Socket *s)
  391. {
  392. return NULL;
  393. }
  394. static const SocketVtable HandleSocket_deferred_sockvt = {
  395. // WINSCP
  396. /*.plug =*/ sk_handle_plug,
  397. /*.close =*/ sk_handle_deferred_close,
  398. /*.write =*/ sk_handle_deferred_write,
  399. /*.write_oob =*/ sk_handle_deferred_write,
  400. /*.write_eof =*/ sk_handle_deferred_write_eof,
  401. /*.set_frozen =*/ sk_handle_deferred_set_frozen,
  402. /*.socket_error =*/ sk_handle_socket_error,
  403. /*.peer_info =*/ sk_handle_deferred_peer_info,
  404. };
  405. Socket *make_deferred_handle_socket(DeferredSocketOpener *opener,
  406. SockAddr *addr, int port, Plug *plug)
  407. {
  408. HandleSocket *hs = snew(HandleSocket);
  409. hs->sock.vt = &HandleSocket_deferred_sockvt;
  410. hs->addr = addr;
  411. hs->port = port;
  412. hs->plug = plug;
  413. hs->error = NULL;
  414. hs->opener = opener;
  415. bufchain_init(&hs->outputdata);
  416. hs->output_eof_pending = false;
  417. hs->start_frozen = false;
  418. return &hs->sock;
  419. }
  420. void setup_handle_socket(Socket *s, HANDLE send_H, HANDLE recv_H,
  421. HANDLE stderr_H, bool overlapped)
  422. {
  423. HandleSocket *hs = container_of(s, HandleSocket, sock);
  424. pinitassert(hs->sock.vt == &HandleSocket_deferred_sockvt);
  425. struct callback_set * callback_set = get_callback_set(hs->plug);
  426. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  427. struct handle *recv_h = handle_input_new(
  428. callback_set, recv_H, handle_gotdata, hs, flags);
  429. struct handle *send_h = handle_output_new(
  430. callback_set, send_H, handle_sentdata, hs, flags);
  431. struct handle *stderr_h = !stderr_H ? NULL : handle_input_new(
  432. callback_set, stderr_H, handle_stderr, hs, flags);
  433. while (bufchain_size(&hs->outputdata)) {
  434. ptrlen data = bufchain_prefix(&hs->outputdata);
  435. handle_write(send_h, data.ptr, data.len);
  436. bufchain_consume(&hs->outputdata, data.len);
  437. }
  438. if (hs->output_eof_pending)
  439. handle_write_eof(send_h);
  440. { // WINSCP
  441. bool start_frozen = hs->start_frozen;
  442. deferred_socket_opener_free(hs->opener);
  443. bufchain_clear(&hs->outputdata);
  444. hs->sock.vt = &HandleSocket_sockvt;
  445. hs->frozen = start_frozen ? FREEZING : UNFROZEN;
  446. bufchain_init(&hs->inputdata);
  447. psb_init(&hs->psb);
  448. hs->recv_H = recv_H;
  449. hs->recv_h = recv_h;
  450. hs->send_H = send_H;
  451. hs->send_h = send_h;
  452. hs->stderr_H = stderr_H;
  453. hs->stderr_h = stderr_h;
  454. hs->defer_close = hs->deferred_close = false;
  455. #ifdef MPEXT
  456. // WinSCP core uses do_select as signalization of connection up/down
  457. do_select(hs->plug, INVALID_SOCKET, 1);
  458. #endif
  459. queue_toplevel_callback(sk_handle_connect_success_callback, hs);
  460. } // WINSCP
  461. }