winhsock.c 10 KB

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