winhsock.c 10 KB

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