winhsock.c 11 KB

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