winhsock.c 10 KB

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