winhsock.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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. typedef struct Socket_handle_tag *Handle_Socket;
  13. #ifdef MPEXT
  14. extern char *do_select(Plug plug, SOCKET skt, int startup);
  15. #endif
  16. struct Socket_handle_tag {
  17. const struct socket_function_table *fn;
  18. /* the above variable absolutely *must* be the first in this structure */
  19. HANDLE send_H, recv_H, stderr_H;
  20. struct handle *send_h, *recv_h, *stderr_h;
  21. /*
  22. * Freezing one of these sockets is a slightly fiddly business,
  23. * because the reads from the handle are happening in a separate
  24. * thread as blocking system calls and so once one is in progress
  25. * it can't sensibly be interrupted. Hence, after the user tries
  26. * to freeze one of these sockets, it's unavoidable that we may
  27. * receive one more load of data before we manage to get
  28. * winhandl.c to stop reading.
  29. */
  30. enum {
  31. UNFROZEN, /* reading as normal */
  32. FREEZING, /* have been set to frozen but winhandl is still reading */
  33. FROZEN, /* really frozen - winhandl has been throttled */
  34. THAWING /* we're gradually releasing our remaining data */
  35. } frozen;
  36. /* We buffer data here if we receive it from winhandl while frozen. */
  37. bufchain inputdata;
  38. /* Data received from stderr_H, if we have one. */
  39. bufchain stderrdata;
  40. int defer_close, deferred_close; /* in case of re-entrance */
  41. char *error;
  42. Plug plug;
  43. };
  44. static int handle_gotdata(struct handle *h, void *data, int len)
  45. {
  46. Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
  47. if (len < 0) {
  48. plug_closing(ps->plug, "Read error from handle", 0, 0);
  49. return 0;
  50. } else if (len == 0) {
  51. plug_closing(ps->plug, NULL, 0, 0);
  52. return 0;
  53. } else {
  54. assert(ps->frozen != FROZEN && ps->frozen != THAWING);
  55. if (ps->frozen == FREEZING) {
  56. /*
  57. * If we've received data while this socket is supposed to
  58. * be frozen (because the read winhandl.c started before
  59. * sk_set_frozen was called has now returned) then buffer
  60. * the data for when we unfreeze.
  61. */
  62. bufchain_add(&ps->inputdata, data, len);
  63. ps->frozen = FROZEN;
  64. /*
  65. * And return a very large backlog, to prevent further
  66. * data arriving from winhandl until we unfreeze.
  67. */
  68. return INT_MAX;
  69. } else {
  70. plug_receive(ps->plug, 0, data, len);
  71. return 0;
  72. }
  73. }
  74. }
  75. static int handle_stderr(struct handle *h, void *data, int len)
  76. {
  77. Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
  78. if (len > 0)
  79. {
  80. log_proxy_stderr(ps->plug, &ps->stderrdata, data, len);
  81. }
  82. return 0;
  83. }
  84. static void handle_sentdata(struct handle *h, int new_backlog)
  85. {
  86. Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
  87. if (new_backlog < 0) {
  88. /* Special case: this is actually reporting an error writing
  89. * to the underlying handle, and our input value is the error
  90. * code itself, negated. */
  91. plug_closing(ps->plug, win_strerror(-new_backlog), -new_backlog, 0);
  92. return;
  93. }
  94. plug_sent(ps->plug, new_backlog);
  95. }
  96. static Plug sk_handle_plug(Socket s, Plug p)
  97. {
  98. Handle_Socket ps = (Handle_Socket) s;
  99. Plug ret = ps->plug;
  100. if (p)
  101. ps->plug = p;
  102. return ret;
  103. }
  104. static void sk_handle_close(Socket s)
  105. {
  106. Handle_Socket ps = (Handle_Socket) s;
  107. if (ps->defer_close) {
  108. ps->deferred_close = TRUE;
  109. return;
  110. }
  111. #ifdef MPEXT
  112. // WinSCP core uses do_select as signalization of connection up/down
  113. do_select(ps->plug, INVALID_SOCKET, 0);
  114. #endif
  115. handle_free(ps->send_h);
  116. handle_free(ps->recv_h);
  117. CloseHandle(ps->send_H);
  118. if (ps->recv_H != ps->send_H)
  119. CloseHandle(ps->recv_H);
  120. bufchain_clear(&ps->inputdata);
  121. bufchain_clear(&ps->stderrdata);
  122. sfree(ps);
  123. }
  124. static int sk_handle_write(Socket s, const char *data, int len)
  125. {
  126. Handle_Socket ps = (Handle_Socket) s;
  127. return handle_write(ps->send_h, data, len);
  128. }
  129. static int sk_handle_write_oob(Socket s, const char *data, int len)
  130. {
  131. /*
  132. * oob data is treated as inband; nasty, but nothing really
  133. * better we can do
  134. */
  135. return sk_handle_write(s, data, len);
  136. }
  137. static void sk_handle_write_eof(Socket s)
  138. {
  139. Handle_Socket ps = (Handle_Socket) s;
  140. handle_write_eof(ps->send_h);
  141. }
  142. static void sk_handle_flush(Socket s)
  143. {
  144. /* Handle_Socket ps = (Handle_Socket) s; */
  145. /* do nothing */
  146. }
  147. static void handle_socket_unfreeze(void *psv)
  148. {
  149. Handle_Socket ps = (Handle_Socket) psv;
  150. void *data;
  151. int len;
  152. /*
  153. * If we've been put into a state other than THAWING since the
  154. * last callback, then we're done.
  155. */
  156. if (ps->frozen != THAWING)
  157. return;
  158. /*
  159. * Get some of the data we've buffered.
  160. */
  161. bufchain_prefix(&ps->inputdata, &data, &len);
  162. assert(len > 0);
  163. /*
  164. * Hand it off to the plug. Be careful of re-entrance - that might
  165. * have the effect of trying to close this socket.
  166. */
  167. ps->defer_close = TRUE;
  168. plug_receive(ps->plug, 0, data, len);
  169. bufchain_consume(&ps->inputdata, len);
  170. ps->defer_close = FALSE;
  171. if (ps->deferred_close) {
  172. sk_handle_close(ps);
  173. return;
  174. }
  175. if (bufchain_size(&ps->inputdata) > 0) {
  176. /*
  177. * If there's still data in our buffer, stay in THAWING state,
  178. * and reschedule ourself.
  179. */
  180. queue_toplevel_callback(handle_socket_unfreeze, ps);
  181. } else {
  182. /*
  183. * Otherwise, we've successfully thawed!
  184. */
  185. ps->frozen = UNFROZEN;
  186. handle_unthrottle(ps->recv_h, 0);
  187. }
  188. }
  189. static void sk_handle_set_frozen(Socket s, int is_frozen)
  190. {
  191. Handle_Socket ps = (Handle_Socket) s;
  192. if (is_frozen) {
  193. switch (ps->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. ps->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. ps->frozen = FREEZING;
  212. break;
  213. }
  214. } else {
  215. switch (ps->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(&ps->inputdata) == 0);
  226. ps->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. ps->frozen = THAWING;
  234. queue_toplevel_callback(handle_socket_unfreeze, ps);
  235. }
  236. }
  237. }
  238. static const char *sk_handle_socket_error(Socket s)
  239. {
  240. Handle_Socket ps = (Handle_Socket) s;
  241. return ps->error;
  242. }
  243. static char *sk_handle_peer_info(Socket s)
  244. {
  245. Handle_Socket ps = (Handle_Socket) s;
  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__ || defined COVERITY
  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(ps->send_H, &pid))
  271. return dupprintf("process id %lu", (unsigned long)pid);
  272. return NULL;
  273. }
  274. Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  275. Plug plug, int overlapped)
  276. {
  277. static const struct socket_function_table socket_fn_table = {
  278. sk_handle_plug,
  279. sk_handle_close,
  280. sk_handle_write,
  281. sk_handle_write_oob,
  282. sk_handle_write_eof,
  283. sk_handle_flush,
  284. sk_handle_set_frozen,
  285. sk_handle_socket_error,
  286. sk_handle_peer_info,
  287. };
  288. Handle_Socket ret;
  289. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  290. ret = snew(struct Socket_handle_tag);
  291. ret->fn = &socket_fn_table;
  292. ret->plug = plug;
  293. ret->error = NULL;
  294. ret->frozen = UNFROZEN;
  295. bufchain_init(&ret->inputdata);
  296. bufchain_init(&ret->stderrdata);
  297. ret->recv_H = recv_H;
  298. ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
  299. ret->send_H = send_H;
  300. ret->send_h = handle_output_new(ret->send_H, handle_sentdata, ret, flags);
  301. ret->stderr_H = stderr_H;
  302. if (ret->stderr_H)
  303. ret->stderr_h = handle_input_new(ret->stderr_H, handle_stderr,
  304. ret, flags);
  305. ret->defer_close = ret->deferred_close = FALSE;
  306. #ifdef MPEXT
  307. // WinSCP core uses do_select as signalization of connection up/down
  308. do_select(plug, INVALID_SOCKET, 1);
  309. #endif
  310. return (Socket) ret;
  311. }