winhsock.c 11 KB

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