winhsock.c 11 KB

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