1
0

handle-socket.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. tree234 * handles_by_evtomain = get_callback_set(hs->plug)->handles_by_evtomain; // WINSCP
  127. if (hs->defer_close) {
  128. hs->deferred_close = true;
  129. return;
  130. }
  131. #ifdef MPEXT
  132. // WinSCP core uses do_select as signalization of connection up/down
  133. do_select(hs->plug, INVALID_SOCKET, 0);
  134. #endif
  135. handle_free(handles_by_evtomain, hs->send_h); // WINSCP
  136. handle_free(handles_by_evtomain, hs->recv_h); // WINSCP
  137. if (hs->send_H != INVALID_HANDLE_VALUE)
  138. CloseHandle(hs->send_H);
  139. if (hs->recv_H != INVALID_HANDLE_VALUE && hs->recv_H != hs->send_H)
  140. CloseHandle(hs->recv_H);
  141. bufchain_clear(&hs->inputdata);
  142. #ifdef MPEXT
  143. if (hs->stderr_h)
  144. {
  145. handle_free(handles_by_evtomain, hs->stderr_h); // WINSCP
  146. }
  147. if (hs->stderr_H)
  148. {
  149. CloseHandle(hs->stderr_H);
  150. }
  151. #endif
  152. if (hs->addr)
  153. sk_addr_free(hs->addr);
  154. delete_callbacks_for_context(get_callback_set(hs->plug), hs);
  155. sfree(hs);
  156. }
  157. static size_t sk_handle_write(Socket *s, const void *data, size_t len)
  158. {
  159. HandleSocket *hs = container_of(s, HandleSocket, sock);
  160. return handle_write(hs->send_h, data, len);
  161. }
  162. static size_t sk_handle_write_oob(Socket *s, const void *data, size_t len)
  163. {
  164. /*
  165. * oob data is treated as inband; nasty, but nothing really
  166. * better we can do
  167. */
  168. return sk_handle_write(s, data, len);
  169. }
  170. static void sk_handle_write_eof(Socket *s)
  171. {
  172. HandleSocket *hs = container_of(s, HandleSocket, sock);
  173. handle_write_eof(hs->send_h);
  174. }
  175. static void handle_socket_unfreeze(void *hsv)
  176. {
  177. HandleSocket *hs = (HandleSocket *)hsv;
  178. /*
  179. * If we've been put into a state other than THAWING since the
  180. * last callback, then we're done.
  181. */
  182. if (hs->frozen != THAWING)
  183. return;
  184. /*
  185. * Get some of the data we've buffered.
  186. */
  187. { // WINSCP
  188. ptrlen data = bufchain_prefix(&hs->inputdata);
  189. assert(data.len > 0);
  190. /*
  191. * Hand it off to the plug. Be careful of re-entrance - that might
  192. * have the effect of trying to close this socket.
  193. */
  194. hs->defer_close = true;
  195. plug_receive(hs->plug, 0, data.ptr, data.len);
  196. bufchain_consume(&hs->inputdata, data.len);
  197. hs->defer_close = false;
  198. if (hs->deferred_close) {
  199. sk_handle_close(&hs->sock);
  200. return;
  201. }
  202. if (bufchain_size(&hs->inputdata) > 0) {
  203. /*
  204. * If there's still data in our buffer, stay in THAWING state,
  205. * and reschedule ourself.
  206. */
  207. queue_toplevel_callback(handle_socket_unfreeze, hs);
  208. } else {
  209. /*
  210. * Otherwise, we've successfully thawed!
  211. */
  212. hs->frozen = UNFROZEN;
  213. handle_unthrottle(hs->recv_h, 0);
  214. }
  215. } // WINSCP
  216. }
  217. static void sk_handle_set_frozen(Socket *s, bool is_frozen)
  218. {
  219. HandleSocket *hs = container_of(s, HandleSocket, sock);
  220. if (is_frozen) {
  221. switch (hs->frozen) {
  222. case FREEZING:
  223. case FROZEN:
  224. return; /* nothing to do */
  225. case THAWING:
  226. /*
  227. * We were in the middle of emptying our bufchain, and got
  228. * frozen again. In that case, handle-io.c is already
  229. * throttled, so just return to FROZEN state. The toplevel
  230. * callback will notice and disable itself.
  231. */
  232. hs->frozen = FROZEN;
  233. break;
  234. case UNFROZEN:
  235. /*
  236. * The normal case. Go to FREEZING, and expect one more
  237. * load of data from winhandl if we're unlucky.
  238. */
  239. hs->frozen = FREEZING;
  240. break;
  241. }
  242. } else {
  243. switch (hs->frozen) {
  244. case UNFROZEN:
  245. case THAWING:
  246. return; /* nothing to do */
  247. case FREEZING:
  248. /*
  249. * If winhandl didn't send us any data throughout the time
  250. * we were frozen, then we'll still be in this state and
  251. * can just unfreeze in the trivial way.
  252. */
  253. assert(bufchain_size(&hs->inputdata) == 0);
  254. hs->frozen = UNFROZEN;
  255. break;
  256. case FROZEN:
  257. /*
  258. * If we have buffered data, go to THAWING and start
  259. * releasing it in top-level callbacks.
  260. */
  261. hs->frozen = THAWING;
  262. queue_toplevel_callback(handle_socket_unfreeze, hs);
  263. }
  264. }
  265. }
  266. static const char *sk_handle_socket_error(Socket *s)
  267. {
  268. HandleSocket *hs = container_of(s, HandleSocket, sock);
  269. return hs->error;
  270. }
  271. static SocketPeerInfo *sk_handle_peer_info(Socket *s)
  272. {
  273. HandleSocket *hs = container_of(s, HandleSocket, sock);
  274. ULONG pid;
  275. static HMODULE kernel32_module;
  276. DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
  277. (HANDLE, PULONG));
  278. if (!kernel32_module) {
  279. kernel32_module = load_system32_dll("kernel32.dll");
  280. #if !HAVE_GETNAMEDPIPECLIENTPROCESSID
  281. /* For older Visual Studio, and MinGW too (at least as of
  282. * Ubuntu 16.04), this function isn't available in the header
  283. * files to type-check. Ditto the toolchain I use for
  284. * Coveritying the Windows code. */
  285. GET_WINDOWS_FUNCTION_NO_TYPECHECK(
  286. kernel32_module, GetNamedPipeClientProcessId);
  287. #else
  288. GET_WINDOWS_FUNCTION(
  289. kernel32_module, GetNamedPipeClientProcessId);
  290. #endif
  291. }
  292. /*
  293. * Of course, not all handles managed by this module will be
  294. * server ends of named pipes, but if they are, then it's useful
  295. * to log what we can find out about the client end.
  296. */
  297. if (p_GetNamedPipeClientProcessId &&
  298. p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
  299. SocketPeerInfo *pi = snew(SocketPeerInfo);
  300. pi->addressfamily = ADDRTYPE_LOCAL;
  301. pi->addr_text = NULL;
  302. pi->port = -1;
  303. pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
  304. return pi;
  305. }
  306. return NULL;
  307. }
  308. static const SocketVtable HandleSocket_sockvt = {
  309. // WINSCP
  310. /*.plug =*/ sk_handle_plug,
  311. /*.close =*/ sk_handle_close,
  312. /*.write =*/ sk_handle_write,
  313. /*.write_oob =*/ sk_handle_write_oob,
  314. /*.write_eof =*/ sk_handle_write_eof,
  315. /*.set_frozen =*/ sk_handle_set_frozen,
  316. /*.socket_error =*/ sk_handle_socket_error,
  317. /*.peer_info =*/ sk_handle_peer_info,
  318. };
  319. static void sk_handle_connect_success_callback(void *ctx)
  320. {
  321. HandleSocket *hs = (HandleSocket *)ctx;
  322. plug_log(hs->plug, PLUGLOG_CONNECT_SUCCESS, hs->addr, hs->port, NULL, 0);
  323. }
  324. Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  325. SockAddr *addr, int port, Plug *plug,
  326. bool overlapped)
  327. {
  328. HandleSocket *hs;
  329. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  330. tree234 * handles_by_evtomain = get_callback_set(plug)->handles_by_evtomain; // WINSCP
  331. hs = snew(HandleSocket);
  332. hs->sock.vt = &HandleSocket_sockvt;
  333. hs->addr = addr;
  334. hs->port = port;
  335. hs->plug = plug;
  336. hs->error = NULL;
  337. hs->frozen = UNFROZEN;
  338. bufchain_init(&hs->inputdata);
  339. psb_init(&hs->psb);
  340. hs->recv_H = recv_H;
  341. hs->recv_h = handle_input_new(handles_by_evtomain, hs->recv_H, handle_gotdata, hs, flags); // WINSCP
  342. hs->send_H = send_H;
  343. hs->send_h = handle_output_new(handles_by_evtomain, hs->send_H, handle_sentdata, hs, flags); // WINSCP
  344. hs->stderr_H = stderr_H;
  345. if (hs->stderr_H)
  346. hs->stderr_h = handle_input_new(handles_by_evtomain, 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. static void sk_handle_deferred_close(Socket *s)
  357. {
  358. HandleSocket *hs = container_of(s, HandleSocket, sock);
  359. deferred_socket_opener_free(hs->opener);
  360. bufchain_clear(&hs->outputdata);
  361. if (hs->addr)
  362. sk_addr_free(hs->addr);
  363. delete_callbacks_for_context(hs);
  364. sfree(hs);
  365. }
  366. static size_t sk_handle_deferred_write(Socket *s, const void *data, size_t len)
  367. {
  368. HandleSocket *hs = container_of(s, HandleSocket, sock);
  369. assert(!hs->output_eof_pending);
  370. bufchain_add(&hs->outputdata, data, len);
  371. return bufchain_size(&hs->outputdata);
  372. }
  373. static void sk_handle_deferred_write_eof(Socket *s)
  374. {
  375. HandleSocket *hs = container_of(s, HandleSocket, sock);
  376. assert(!hs->output_eof_pending);
  377. hs->output_eof_pending = true;
  378. }
  379. static void sk_handle_deferred_set_frozen(Socket *s, bool is_frozen)
  380. {
  381. HandleSocket *hs = container_of(s, HandleSocket, sock);
  382. hs->frozen = is_frozen;
  383. }
  384. static SocketPeerInfo *sk_handle_deferred_peer_info(Socket *s)
  385. {
  386. return NULL;
  387. }
  388. static const SocketVtable HandleSocket_deferred_sockvt = {
  389. .plug = sk_handle_plug,
  390. .close = sk_handle_deferred_close,
  391. .write = sk_handle_deferred_write,
  392. .write_oob = sk_handle_deferred_write,
  393. .write_eof = sk_handle_deferred_write_eof,
  394. .set_frozen = sk_handle_deferred_set_frozen,
  395. .socket_error = sk_handle_socket_error,
  396. .peer_info = sk_handle_deferred_peer_info,
  397. };
  398. Socket *make_deferred_handle_socket(DeferredSocketOpener *opener,
  399. SockAddr *addr, int port, Plug *plug)
  400. {
  401. HandleSocket *hs = snew(HandleSocket);
  402. hs->sock.vt = &HandleSocket_deferred_sockvt;
  403. hs->addr = addr;
  404. hs->port = port;
  405. hs->plug = plug;
  406. hs->error = NULL;
  407. hs->opener = opener;
  408. bufchain_init(&hs->outputdata);
  409. hs->output_eof_pending = false;
  410. hs->start_frozen = false;
  411. return &hs->sock;
  412. }
  413. void setup_handle_socket(Socket *s, HANDLE send_H, HANDLE recv_H,
  414. HANDLE stderr_H, bool overlapped)
  415. {
  416. HandleSocket *hs = container_of(s, HandleSocket, sock);
  417. assert(hs->sock.vt == &HandleSocket_deferred_sockvt);
  418. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  419. struct handle *recv_h = handle_input_new(
  420. recv_H, handle_gotdata, hs, flags);
  421. struct handle *send_h = handle_output_new(
  422. send_H, handle_sentdata, hs, flags);
  423. struct handle *stderr_h = !stderr_H ? NULL : handle_input_new(
  424. stderr_H, handle_stderr, hs, flags);
  425. while (bufchain_size(&hs->outputdata)) {
  426. ptrlen data = bufchain_prefix(&hs->outputdata);
  427. handle_write(send_h, data.ptr, data.len);
  428. bufchain_consume(&hs->outputdata, data.len);
  429. }
  430. if (hs->output_eof_pending)
  431. handle_write_eof(send_h);
  432. bool start_frozen = hs->start_frozen;
  433. deferred_socket_opener_free(hs->opener);
  434. bufchain_clear(&hs->outputdata);
  435. hs->sock.vt = &HandleSocket_sockvt;
  436. hs->frozen = start_frozen ? FREEZING : UNFROZEN;
  437. bufchain_init(&hs->inputdata);
  438. psb_init(&hs->psb);
  439. hs->recv_H = recv_H;
  440. hs->recv_h = recv_h;
  441. hs->send_H = send_H;
  442. hs->send_h = send_h;
  443. hs->stderr_H = stderr_H;
  444. hs->stderr_h = stderr_h;
  445. hs->defer_close = hs->deferred_close = false;
  446. queue_toplevel_callback(sk_handle_connect_success_callback, hs);
  447. }