winhsock.c 10 KB

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