handle-socket.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  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->sock, &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 SocketEndpointInfo *sk_handle_endpoint_info(Socket *s, bool peer)
  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 (!peer)
  278. return NULL;
  279. if (!kernel32_module) {
  280. kernel32_module = load_system32_dll("kernel32.dll");
  281. #if !HAVE_GETNAMEDPIPECLIENTPROCESSID
  282. /* For older Visual Studio, and MinGW too (at least as of
  283. * Ubuntu 16.04), this function isn't available in the header
  284. * files to type-check. Ditto the toolchain I use for
  285. * Coveritying the Windows code. */
  286. GET_WINDOWS_FUNCTION_NO_TYPECHECK(
  287. kernel32_module, GetNamedPipeClientProcessId);
  288. #else
  289. GET_WINDOWS_FUNCTION(
  290. kernel32_module, GetNamedPipeClientProcessId);
  291. #endif
  292. }
  293. /*
  294. * Of course, not all handles managed by this module will be
  295. * server ends of named pipes, but if they are, then it's useful
  296. * to log what we can find out about the client end.
  297. */
  298. if (p_GetNamedPipeClientProcessId &&
  299. p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
  300. SocketEndpointInfo *pi = snew(SocketEndpointInfo);
  301. pi->addressfamily = ADDRTYPE_LOCAL;
  302. pi->addr_text = NULL;
  303. pi->port = -1;
  304. pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
  305. return pi;
  306. }
  307. return NULL;
  308. }
  309. static const SocketVtable HandleSocket_sockvt = {
  310. // WINSCP
  311. /*.plug =*/ sk_handle_plug,
  312. /*.close =*/ sk_handle_close,
  313. /*.write =*/ sk_handle_write,
  314. /*.write_oob =*/ sk_handle_write_oob,
  315. /*.write_eof =*/ sk_handle_write_eof,
  316. /*.set_frozen =*/ sk_handle_set_frozen,
  317. /*.socket_error =*/ sk_handle_socket_error,
  318. /*.endpoint_info =*/ sk_handle_endpoint_info,
  319. };
  320. static void sk_handle_connect_success_callback(void *ctx)
  321. {
  322. HandleSocket *hs = (HandleSocket *)ctx;
  323. plug_log(hs->plug, &hs->sock, PLUGLOG_CONNECT_SUCCESS, hs->addr, hs->port,
  324. NULL, 0);
  325. }
  326. Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  327. SockAddr *addr, int port, Plug *plug,
  328. bool overlapped)
  329. {
  330. HandleSocket *hs;
  331. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  332. struct callback_set * callback_set = get_callback_set(plug); // WINSCP
  333. hs = snew(HandleSocket);
  334. hs->sock.vt = &HandleSocket_sockvt;
  335. hs->addr = addr;
  336. hs->port = port;
  337. hs->plug = plug;
  338. hs->error = NULL;
  339. hs->frozen = UNFROZEN;
  340. bufchain_init(&hs->inputdata);
  341. psb_init(&hs->psb);
  342. hs->recv_H = recv_H;
  343. hs->recv_h = handle_input_new(callback_set, hs->recv_H, handle_gotdata, hs, flags); // WINSCP
  344. hs->send_H = send_H;
  345. hs->send_h = handle_output_new(callback_set, hs->send_H, handle_sentdata, hs, flags); // WINSCP
  346. hs->stderr_H = stderr_H;
  347. hs->stderr_h = NULL; // WINSCP
  348. if (hs->stderr_H)
  349. hs->stderr_h = handle_input_new(callback_set, hs->stderr_H, handle_stderr, // WINSCP
  350. hs, flags);
  351. hs->defer_close = hs->deferred_close = false;
  352. #ifdef MPEXT
  353. // WinSCP core uses do_select as signalization of connection up/down
  354. do_select(plug, INVALID_SOCKET, 1);
  355. #endif
  356. queue_toplevel_callback(sk_handle_connect_success_callback, hs);
  357. return &hs->sock;
  358. }
  359. void handle_socket_set_psb_prefix(Socket *s, const char *prefix)
  360. {
  361. HandleSocket *hs = container_of(s, HandleSocket, sock);
  362. assert(hs->sock.vt == &HandleSocket_sockvt);
  363. psb_set_prefix(&hs->psb, prefix);
  364. }
  365. static void sk_handle_deferred_close(Socket *s)
  366. {
  367. HandleSocket *hs = container_of(s, HandleSocket, sock);
  368. deferred_socket_opener_free(hs->opener);
  369. bufchain_clear(&hs->outputdata);
  370. if (hs->addr)
  371. sk_addr_free(hs->addr);
  372. delete_callbacks_for_context(get_callback_set(hs->plug), hs);
  373. sfree(hs);
  374. }
  375. static size_t sk_handle_deferred_write(Socket *s, const void *data, size_t len)
  376. {
  377. HandleSocket *hs = container_of(s, HandleSocket, sock);
  378. assert(!hs->output_eof_pending);
  379. bufchain_add(&hs->outputdata, data, len);
  380. return bufchain_size(&hs->outputdata);
  381. }
  382. static void sk_handle_deferred_write_eof(Socket *s)
  383. {
  384. HandleSocket *hs = container_of(s, HandleSocket, sock);
  385. assert(!hs->output_eof_pending);
  386. hs->output_eof_pending = true;
  387. }
  388. static void sk_handle_deferred_set_frozen(Socket *s, bool is_frozen)
  389. {
  390. HandleSocket *hs = container_of(s, HandleSocket, sock);
  391. hs->frozen = is_frozen;
  392. }
  393. static SocketEndpointInfo *sk_handle_deferred_endpoint_info(
  394. Socket *s, bool peer)
  395. {
  396. return NULL;
  397. }
  398. static const SocketVtable HandleSocket_deferred_sockvt = {
  399. // WINSCP
  400. /*.plug =*/ sk_handle_plug,
  401. /*.close =*/ sk_handle_deferred_close,
  402. /*.write =*/ sk_handle_deferred_write,
  403. /*.write_oob =*/ sk_handle_deferred_write,
  404. /*.write_eof =*/ sk_handle_deferred_write_eof,
  405. /*.set_frozen =*/ sk_handle_deferred_set_frozen,
  406. /*.socket_error =*/ sk_handle_socket_error,
  407. /*.endpoint_info =*/ sk_handle_deferred_endpoint_info,
  408. };
  409. Socket *make_deferred_handle_socket(DeferredSocketOpener *opener,
  410. SockAddr *addr, int port, Plug *plug)
  411. {
  412. HandleSocket *hs = snew(HandleSocket);
  413. hs->sock.vt = &HandleSocket_deferred_sockvt;
  414. hs->addr = addr;
  415. hs->port = port;
  416. hs->plug = plug;
  417. hs->error = NULL;
  418. hs->opener = opener;
  419. bufchain_init(&hs->outputdata);
  420. hs->output_eof_pending = false;
  421. hs->start_frozen = false;
  422. return &hs->sock;
  423. }
  424. void setup_handle_socket(Socket *s, HANDLE send_H, HANDLE recv_H,
  425. HANDLE stderr_H, bool overlapped)
  426. {
  427. HandleSocket *hs = container_of(s, HandleSocket, sock);
  428. pinitassert(hs->sock.vt == &HandleSocket_deferred_sockvt);
  429. struct callback_set * callback_set = get_callback_set(hs->plug);
  430. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  431. struct handle *recv_h = handle_input_new(
  432. callback_set, recv_H, handle_gotdata, hs, flags);
  433. struct handle *send_h = handle_output_new(
  434. callback_set, send_H, handle_sentdata, hs, flags);
  435. struct handle *stderr_h = !stderr_H ? NULL : handle_input_new(
  436. callback_set, stderr_H, handle_stderr, hs, flags);
  437. while (bufchain_size(&hs->outputdata)) {
  438. ptrlen data = bufchain_prefix(&hs->outputdata);
  439. handle_write(send_h, data.ptr, data.len);
  440. bufchain_consume(&hs->outputdata, data.len);
  441. }
  442. if (hs->output_eof_pending)
  443. handle_write_eof(send_h);
  444. { // WINSCP
  445. bool start_frozen = hs->start_frozen;
  446. deferred_socket_opener_free(hs->opener);
  447. bufchain_clear(&hs->outputdata);
  448. hs->sock.vt = &HandleSocket_sockvt;
  449. hs->frozen = start_frozen ? FREEZING : UNFROZEN;
  450. bufchain_init(&hs->inputdata);
  451. psb_init(&hs->psb);
  452. hs->recv_H = recv_H;
  453. hs->recv_h = recv_h;
  454. hs->send_H = send_H;
  455. hs->send_h = send_h;
  456. hs->stderr_H = stderr_H;
  457. hs->stderr_h = stderr_h;
  458. hs->defer_close = hs->deferred_close = false;
  459. #ifdef MPEXT
  460. // WinSCP core uses do_select as signalization of connection up/down
  461. do_select(hs->plug, INVALID_SOCKET, 1);
  462. #endif
  463. queue_toplevel_callback(sk_handle_connect_success_callback, hs);
  464. } // WINSCP
  465. }