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