handle-socket.c 15 KB

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