winhsock.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. delete_callbacks_for_context(hs);
  109. sfree(hs);
  110. }
  111. static size_t sk_handle_write(Socket *s, const void *data, size_t len)
  112. {
  113. HandleSocket *hs = container_of(s, HandleSocket, sock);
  114. return handle_write(hs->send_h, data, len);
  115. }
  116. static size_t sk_handle_write_oob(Socket *s, const void *data, size_t len)
  117. {
  118. /*
  119. * oob data is treated as inband; nasty, but nothing really
  120. * better we can do
  121. */
  122. return sk_handle_write(s, data, len);
  123. }
  124. static void sk_handle_write_eof(Socket *s)
  125. {
  126. HandleSocket *hs = container_of(s, HandleSocket, sock);
  127. handle_write_eof(hs->send_h);
  128. }
  129. static void handle_socket_unfreeze(void *hsv)
  130. {
  131. HandleSocket *hs = (HandleSocket *)hsv;
  132. /*
  133. * If we've been put into a state other than THAWING since the
  134. * last callback, then we're done.
  135. */
  136. if (hs->frozen != THAWING)
  137. return;
  138. /*
  139. * Get some of the data we've buffered.
  140. */
  141. ptrlen data = bufchain_prefix(&hs->inputdata);
  142. assert(data.len > 0);
  143. /*
  144. * Hand it off to the plug. Be careful of re-entrance - that might
  145. * have the effect of trying to close this socket.
  146. */
  147. hs->defer_close = true;
  148. plug_receive(hs->plug, 0, data.ptr, data.len);
  149. bufchain_consume(&hs->inputdata, data.len);
  150. hs->defer_close = false;
  151. if (hs->deferred_close) {
  152. sk_handle_close(&hs->sock);
  153. return;
  154. }
  155. if (bufchain_size(&hs->inputdata) > 0) {
  156. /*
  157. * If there's still data in our buffer, stay in THAWING state,
  158. * and reschedule ourself.
  159. */
  160. queue_toplevel_callback(handle_socket_unfreeze, hs);
  161. } else {
  162. /*
  163. * Otherwise, we've successfully thawed!
  164. */
  165. hs->frozen = UNFROZEN;
  166. handle_unthrottle(hs->recv_h, 0);
  167. }
  168. }
  169. static void sk_handle_set_frozen(Socket *s, bool is_frozen)
  170. {
  171. HandleSocket *hs = container_of(s, HandleSocket, sock);
  172. if (is_frozen) {
  173. switch (hs->frozen) {
  174. case FREEZING:
  175. case FROZEN:
  176. return; /* nothing to do */
  177. case THAWING:
  178. /*
  179. * We were in the middle of emptying our bufchain, and got
  180. * frozen again. In that case, winhandl.c is already
  181. * throttled, so just return to FROZEN state. The toplevel
  182. * callback will notice and disable itself.
  183. */
  184. hs->frozen = FROZEN;
  185. break;
  186. case UNFROZEN:
  187. /*
  188. * The normal case. Go to FREEZING, and expect one more
  189. * load of data from winhandl if we're unlucky.
  190. */
  191. hs->frozen = FREEZING;
  192. break;
  193. }
  194. } else {
  195. switch (hs->frozen) {
  196. case UNFROZEN:
  197. case THAWING:
  198. return; /* nothing to do */
  199. case FREEZING:
  200. /*
  201. * If winhandl didn't send us any data throughout the time
  202. * we were frozen, then we'll still be in this state and
  203. * can just unfreeze in the trivial way.
  204. */
  205. assert(bufchain_size(&hs->inputdata) == 0);
  206. hs->frozen = UNFROZEN;
  207. break;
  208. case FROZEN:
  209. /*
  210. * If we have buffered data, go to THAWING and start
  211. * releasing it in top-level callbacks.
  212. */
  213. hs->frozen = THAWING;
  214. queue_toplevel_callback(handle_socket_unfreeze, hs);
  215. }
  216. }
  217. }
  218. static const char *sk_handle_socket_error(Socket *s)
  219. {
  220. HandleSocket *hs = container_of(s, HandleSocket, sock);
  221. return hs->error;
  222. }
  223. static SocketPeerInfo *sk_handle_peer_info(Socket *s)
  224. {
  225. HandleSocket *hs = container_of(s, HandleSocket, sock);
  226. ULONG pid;
  227. static HMODULE kernel32_module;
  228. DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
  229. (HANDLE, PULONG));
  230. if (!kernel32_module) {
  231. kernel32_module = load_system32_dll("kernel32.dll");
  232. #if (defined _MSC_VER && _MSC_VER < 1900) || defined __MINGW32__ || defined COVERITY
  233. /* For older Visual Studio, and MinGW too (at least as of
  234. * Ubuntu 16.04), this function isn't available in the header
  235. * files to type-check. Ditto the toolchain I use for
  236. * Coveritying the Windows code. */
  237. GET_WINDOWS_FUNCTION_NO_TYPECHECK(
  238. kernel32_module, GetNamedPipeClientProcessId);
  239. #else
  240. GET_WINDOWS_FUNCTION(
  241. kernel32_module, GetNamedPipeClientProcessId);
  242. #endif
  243. }
  244. /*
  245. * Of course, not all handles managed by this module will be
  246. * server ends of named pipes, but if they are, then it's useful
  247. * to log what we can find out about the client end.
  248. */
  249. if (p_GetNamedPipeClientProcessId &&
  250. p_GetNamedPipeClientProcessId(hs->send_H, &pid)) {
  251. SocketPeerInfo *pi = snew(SocketPeerInfo);
  252. pi->addressfamily = ADDRTYPE_LOCAL;
  253. pi->addr_text = NULL;
  254. pi->port = -1;
  255. pi->log_text = dupprintf("process id %lu", (unsigned long)pid);
  256. return pi;
  257. }
  258. return NULL;
  259. }
  260. static const SocketVtable HandleSocket_sockvt = {
  261. sk_handle_plug,
  262. sk_handle_close,
  263. sk_handle_write,
  264. sk_handle_write_oob,
  265. sk_handle_write_eof,
  266. sk_handle_set_frozen,
  267. sk_handle_socket_error,
  268. sk_handle_peer_info,
  269. };
  270. Socket *make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  271. Plug *plug, bool overlapped)
  272. {
  273. HandleSocket *hs;
  274. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  275. hs = snew(HandleSocket);
  276. hs->sock.vt = &HandleSocket_sockvt;
  277. hs->plug = plug;
  278. hs->error = NULL;
  279. hs->frozen = UNFROZEN;
  280. bufchain_init(&hs->inputdata);
  281. psb_init(&hs->psb);
  282. hs->recv_H = recv_H;
  283. hs->recv_h = handle_input_new(hs->recv_H, handle_gotdata, hs, flags);
  284. hs->send_H = send_H;
  285. hs->send_h = handle_output_new(hs->send_H, handle_sentdata, hs, flags);
  286. hs->stderr_H = stderr_H;
  287. if (hs->stderr_H)
  288. hs->stderr_h = handle_input_new(hs->stderr_H, handle_stderr,
  289. hs, flags);
  290. hs->defer_close = hs->deferred_close = false;
  291. return &hs->sock;
  292. }