winhsock.c 11 KB

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