winhsock.c 10 KB

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