handle-socket.c 14 KB

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