winhsock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. typedef struct HandleSocket {
  12. HANDLE send_H, recv_H, stderr_H;
  13. struct handle *send_h, *recv_h, *stderr_h;
  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
  18. * it can't sensibly be interrupted. Hence, after the user tries
  19. * to freeze one of these sockets, it's unavoidable that we may
  20. * receive one more load of data before we manage to get
  21. * winhandl.c to stop reading.
  22. */
  23. enum {
  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. } frozen;
  29. /* We buffer data here if we receive it from winhandl while frozen. */
  30. bufchain inputdata;
  31. /* Handle logging proxy error messages from stderr_H, if we have one. */
  32. ProxyStderrBuf psb;
  33. bool defer_close, deferred_close; /* in case of re-entrance */
  34. char *error;
  35. Plug *plug;
  36. Socket sock;
  37. } HandleSocket;
  38. static size_t handle_gotdata(
  39. struct handle *h, const void *data, size_t len, int err)
  40. {
  41. HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
  42. if (err) {
  43. plug_closing(hs->plug, "Read error from handle", 0, 0);
  44. return 0;
  45. } else if (len == 0) {
  46. plug_closing(hs->plug, NULL, 0, 0);
  47. return 0;
  48. } else {
  49. assert(hs->frozen != FROZEN && hs->frozen != THAWING);
  50. if (hs->frozen == FREEZING) {
  51. /*
  52. * If we've received data while this socket is supposed to
  53. * be frozen (because the read winhandl.c started before
  54. * sk_set_frozen was called has now returned) then buffer
  55. * the data for when we unfreeze.
  56. */
  57. bufchain_add(&hs->inputdata, data, len);
  58. hs->frozen = FROZEN;
  59. /*
  60. * And return a very large backlog, to prevent further
  61. * data arriving from winhandl until we unfreeze.
  62. */
  63. return INT_MAX;
  64. } else {
  65. plug_receive(hs->plug, 0, data, len);
  66. return 0;
  67. }
  68. }
  69. }
  70. static size_t handle_stderr(
  71. struct handle *h, const void *data, size_t len, int err)
  72. {
  73. HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
  74. if (!err && len > 0)
  75. log_proxy_stderr(hs->plug, &hs->psb, data, len);
  76. return 0;
  77. }
  78. static void handle_sentdata(struct handle *h, size_t new_backlog, int err)
  79. {
  80. HandleSocket *hs = (HandleSocket *)handle_get_privdata(h);
  81. if (err) {
  82. plug_closing(hs->plug, win_strerror(err), err, 0);
  83. return;
  84. }
  85. plug_sent(hs->plug, new_backlog);
  86. }
  87. static Plug *sk_handle_plug(Socket *s, Plug *p)
  88. {
  89. HandleSocket *hs = container_of(s, HandleSocket, sock);
  90. Plug *ret = hs->plug;
  91. if (p)
  92. hs->plug = p;
  93. return ret;
  94. }
  95. static void sk_handle_close(Socket *s)
  96. {
  97. HandleSocket *hs = container_of(s, HandleSocket, sock);
  98. if (hs->defer_close) {
  99. hs->deferred_close = true;
  100. return;
  101. }
  102. handle_free(hs->send_h);
  103. handle_free(hs->recv_h);
  104. CloseHandle(hs->send_H);
  105. if (hs->recv_H != hs->send_H)
  106. CloseHandle(hs->recv_H);
  107. bufchain_clear(&hs->inputdata);
  108. sfree(hs);
  109. }
  110. static size_t sk_handle_write(Socket *s, const void *data, size_t len)
  111. {
  112. HandleSocket *hs = container_of(s, HandleSocket, sock);
  113. return handle_write(hs->send_h, data, len);
  114. }
  115. static size_t sk_handle_write_oob(Socket *s, const void *data, size_t len)
  116. {
  117. /*
  118. * oob data is treated as inband; nasty, but nothing really
  119. * better we can do
  120. */
  121. return sk_handle_write(s, data, len);
  122. }
  123. static void sk_handle_write_eof(Socket *s)
  124. {
  125. HandleSocket *hs = container_of(s, HandleSocket, sock);
  126. handle_write_eof(hs->send_h);
  127. }
  128. static void sk_handle_flush(Socket *s)
  129. {
  130. /* HandleSocket *hs = container_of(s, HandleSocket, sock); */
  131. /* do nothing */
  132. }
  133. static void handle_socket_unfreeze(void *hsv)
  134. {
  135. HandleSocket *hs = (HandleSocket *)hsv;
  136. /*
  137. * If we've been put into a state other than THAWING since the
  138. * last callback, then we're done.
  139. */
  140. if (hs->frozen != THAWING)
  141. return;
  142. /*
  143. * Get some of the data we've buffered.
  144. */
  145. ptrlen data = bufchain_prefix(&hs->inputdata);
  146. assert(data.len > 0);
  147. /*
  148. * Hand it off to the plug. Be careful of re-entrance - that might
  149. * have the effect of trying to close this socket.
  150. */
  151. hs->defer_close = true;
  152. plug_receive(hs->plug, 0, data.ptr, data.len);
  153. bufchain_consume(&hs->inputdata, data.len);
  154. hs->defer_close = false;
  155. if (hs->deferred_close) {
  156. sk_handle_close(&hs->sock);
  157. return;
  158. }
  159. if (bufchain_size(&hs->inputdata) > 0) {
  160. /*
  161. * If there's still data in our buffer, stay in THAWING state,
  162. * and reschedule ourself.
  163. */
  164. queue_toplevel_callback(handle_socket_unfreeze, hs);
  165. } else {
  166. /*
  167. * Otherwise, we've successfully thawed!
  168. */
  169. hs->frozen = UNFROZEN;
  170. handle_unthrottle(hs->recv_h, 0);
  171. }
  172. }
  173. static void sk_handle_set_frozen(Socket *s, bool is_frozen)
  174. {
  175. HandleSocket *hs = container_of(s, HandleSocket, sock);
  176. if (is_frozen) {
  177. switch (hs->frozen) {
  178. case FREEZING:
  179. case FROZEN:
  180. return; /* nothing to do */
  181. case THAWING:
  182. /*
  183. * We were in the middle of emptying our bufchain, and got
  184. * frozen again. In that case, winhandl.c is already
  185. * throttled, so just return to FROZEN state. The toplevel
  186. * callback will notice and disable itself.
  187. */
  188. hs->frozen = FROZEN;
  189. break;
  190. case UNFROZEN:
  191. /*
  192. * The normal case. Go to FREEZING, and expect one more
  193. * load of data from winhandl if we're unlucky.
  194. */
  195. hs->frozen = FREEZING;
  196. break;
  197. }
  198. } else {
  199. switch (hs->frozen) {
  200. case UNFROZEN:
  201. case THAWING:
  202. return; /* nothing to do */
  203. case FREEZING:
  204. /*
  205. * If winhandl didn't send us any data throughout the time
  206. * we were frozen, then we'll still be in this state and
  207. * can just unfreeze in the trivial way.
  208. */
  209. assert(bufchain_size(&hs->inputdata) == 0);
  210. hs->frozen = UNFROZEN;
  211. break;
  212. case FROZEN:
  213. /*
  214. * If we have buffered data, go to THAWING and start
  215. * releasing it in top-level callbacks.
  216. */
  217. hs->frozen = THAWING;
  218. queue_toplevel_callback(handle_socket_unfreeze, hs);
  219. }
  220. }
  221. }
  222. static const char *sk_handle_socket_error(Socket *s)
  223. {
  224. HandleSocket *hs = container_of(s, HandleSocket, sock);
  225. return hs->error;
  226. }
  227. static SocketPeerInfo *sk_handle_peer_info(Socket *s)
  228. {
  229. HandleSocket *hs = container_of(s, HandleSocket, sock);
  230. ULONG pid;
  231. static HMODULE kernel32_module;
  232. DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
  233. (HANDLE, PULONG));
  234. if (!kernel32_module) {
  235. kernel32_module = load_system32_dll("kernel32.dll");
  236. #if (defined _MSC_VER && _MSC_VER < 1900) || defined __MINGW32__ || defined COVERITY
  237. /* For older Visual Studio, and MinGW too (at least as of
  238. * Ubuntu 16.04), this function isn't available in the header
  239. * files to type-check. Ditto the toolchain I use for
  240. * Coveritying the Windows code. */
  241. GET_WINDOWS_FUNCTION_NO_TYPECHECK(
  242. kernel32_module, GetNamedPipeClientProcessId);
  243. #else
  244. GET_WINDOWS_FUNCTION(
  245. kernel32_module, GetNamedPipeClientProcessId);
  246. #endif
  247. }
  248. /*
  249. * Of course, not all handles managed by this module will be
  250. * server ends of named pipes, but if they are, then it's useful
  251. * to log what we can find out about the client end.
  252. */
  253. if (p_GetNamedPipeClientProcessId &&
  254. p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
  255. SocketPeerInfo *pi = snew(SocketPeerInfo);
  256. pi->addressfamily = ADDRTYPE_LOCAL;
  257. pi->addr_text = NULL;
  258. pi->port = -1;
  259. pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
  260. return pi;
  261. }
  262. return NULL;
  263. }
  264. static const SocketVtable HandleSocket_sockvt = {
  265. sk_handle_plug,
  266. sk_handle_close,
  267. sk_handle_write,
  268. sk_handle_write_oob,
  269. sk_handle_write_eof,
  270. sk_handle_flush,
  271. sk_handle_set_frozen,
  272. sk_handle_socket_error,
  273. sk_handle_peer_info,
  274. };
  275. Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  276. Plug *plug, bool overlapped)
  277. {
  278. HandleSocket *hs;
  279. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  280. hs = snew(HandleSocket);
  281. hs->sock.vt = &HandleSocket_sockvt;
  282. hs->plug = plug;
  283. hs->error = NULL;
  284. hs->frozen = UNFROZEN;
  285. bufchain_init(&hs->inputdata);
  286. psb_init(&hs->psb);
  287. hs->recv_H = recv_H;
  288. hs->recv_h = handle_input_new(hs->recv_H, handle_gotdata, hs, flags);
  289. hs->send_H = send_H;
  290. hs->send_h = handle_output_new(hs->send_H, handle_sentdata, hs, flags);
  291. hs->stderr_H = stderr_H;
  292. if (hs->stderr_H)
  293. hs->stderr_h = handle_input_new(hs->stderr_H, handle_stderr,
  294. hs, flags);
  295. hs->defer_close = hs->deferred_close = false;
  296. return &hs->sock;
  297. }