winhsock.c 11 KB

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