winhsock.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. char *error;
  41. Plug plug;
  42. };
  43. static int handle_gotdata(struct handle *h, void *data, int len)
  44. {
  45. Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
  46. if (len < 0) {
  47. return plug_closing(ps->plug, "Read error from handle",
  48. 0, 0);
  49. } else if (len == 0) {
  50. return plug_closing(ps->plug, NULL, 0, 0);
  51. } else {
  52. assert(ps->frozen != FREEZING && ps->frozen != THAWING);
  53. if (ps->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(&ps->inputdata, data, len);
  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. return plug_receive(ps->plug, 0, data, len);
  68. }
  69. }
  70. }
  71. static int handle_stderr(struct handle *h, void *data, int len)
  72. {
  73. Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
  74. if (len > 0)
  75. log_proxy_stderr(ps->plug, &ps->stderrdata, data, len);
  76. return 0;
  77. }
  78. static void handle_sentdata(struct handle *h, int new_backlog)
  79. {
  80. Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
  81. plug_sent(ps->plug, new_backlog);
  82. }
  83. static Plug sk_handle_plug(Socket s, Plug p)
  84. {
  85. Handle_Socket ps = (Handle_Socket) s;
  86. Plug ret = ps->plug;
  87. if (p)
  88. ps->plug = p;
  89. return ret;
  90. }
  91. static void sk_handle_close(Socket s)
  92. {
  93. Handle_Socket ps = (Handle_Socket) s;
  94. #ifdef MPEXT
  95. // WinSCP core uses do_select as signalization of connection up/down
  96. do_select(ps->plug, INVALID_SOCKET, 0);
  97. #endif
  98. handle_free(ps->send_h);
  99. handle_free(ps->recv_h);
  100. CloseHandle(ps->send_H);
  101. if (ps->recv_H != ps->send_H)
  102. CloseHandle(ps->recv_H);
  103. bufchain_clear(&ps->inputdata);
  104. bufchain_clear(&ps->stderrdata);
  105. sfree(ps);
  106. }
  107. static int sk_handle_write(Socket s, const char *data, int len)
  108. {
  109. Handle_Socket ps = (Handle_Socket) s;
  110. return handle_write(ps->send_h, data, len);
  111. }
  112. static int sk_handle_write_oob(Socket s, const char *data, int len)
  113. {
  114. /*
  115. * oob data is treated as inband; nasty, but nothing really
  116. * better we can do
  117. */
  118. return sk_handle_write(s, data, len);
  119. }
  120. static void sk_handle_write_eof(Socket s)
  121. {
  122. Handle_Socket ps = (Handle_Socket) s;
  123. handle_write_eof(ps->send_h);
  124. }
  125. static void sk_handle_flush(Socket s)
  126. {
  127. /* Handle_Socket ps = (Handle_Socket) s; */
  128. /* do nothing */
  129. }
  130. static void handle_socket_unfreeze(void *psv)
  131. {
  132. Handle_Socket ps = (Handle_Socket) psv;
  133. void *data;
  134. int len, new_backlog;
  135. /*
  136. * If we've been put into a state other than THAWING since the
  137. * last callback, then we're done.
  138. */
  139. if (ps->frozen != THAWING)
  140. return;
  141. /*
  142. * Get some of the data we've buffered.
  143. */
  144. bufchain_prefix(&ps->inputdata, &data, &len);
  145. assert(len > 0);
  146. /*
  147. * Hand it off to the plug.
  148. */
  149. new_backlog = plug_receive(ps->plug, 0, data, len);
  150. if (bufchain_size(&ps->inputdata) > 0) {
  151. /*
  152. * If there's still data in our buffer, stay in THAWING state,
  153. * and reschedule ourself.
  154. */
  155. queue_toplevel_callback(handle_socket_unfreeze, ps);
  156. } else {
  157. /*
  158. * Otherwise, we've successfully thawed!
  159. */
  160. ps->frozen = UNFROZEN;
  161. handle_unthrottle(ps->recv_h, new_backlog);
  162. }
  163. }
  164. static void sk_handle_set_frozen(Socket s, int is_frozen)
  165. {
  166. Handle_Socket ps = (Handle_Socket) s;
  167. if (is_frozen) {
  168. switch (ps->frozen) {
  169. case FREEZING:
  170. case FROZEN:
  171. return; /* nothing to do */
  172. case THAWING:
  173. /*
  174. * We were in the middle of emptying our bufchain, and got
  175. * frozen again. In that case, winhandl.c is already
  176. * throttled, so just return to FROZEN state. The toplevel
  177. * callback will notice and disable itself.
  178. */
  179. ps->frozen = FROZEN;
  180. break;
  181. case UNFROZEN:
  182. /*
  183. * The normal case. Go to FREEZING, and expect one more
  184. * load of data from winhandl if we're unlucky.
  185. */
  186. ps->frozen = FREEZING;
  187. break;
  188. }
  189. } else {
  190. switch (ps->frozen) {
  191. case UNFROZEN:
  192. case THAWING:
  193. return; /* nothing to do */
  194. case FREEZING:
  195. /*
  196. * If winhandl didn't send us any data throughout the time
  197. * we were frozen, then we'll still be in this state and
  198. * can just unfreeze in the trivial way.
  199. */
  200. assert(bufchain_size(&ps->inputdata) == 0);
  201. ps->frozen = UNFROZEN;
  202. break;
  203. case FROZEN:
  204. /*
  205. * If we have buffered data, go to THAWING and start
  206. * releasing it in top-level callbacks.
  207. */
  208. ps->frozen = THAWING;
  209. queue_toplevel_callback(handle_socket_unfreeze, ps);
  210. }
  211. }
  212. }
  213. static const char *sk_handle_socket_error(Socket s)
  214. {
  215. Handle_Socket ps = (Handle_Socket) s;
  216. return ps->error;
  217. }
  218. static char *sk_handle_peer_info(Socket s)
  219. {
  220. Handle_Socket ps = (Handle_Socket) s;
  221. ULONG pid;
  222. static HMODULE kernel32_module;
  223. DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
  224. (HANDLE, PULONG));
  225. if (!kernel32_module) {
  226. kernel32_module = load_system32_dll("kernel32.dll");
  227. GET_WINDOWS_FUNCTION(kernel32_module, GetNamedPipeClientProcessId);
  228. }
  229. /*
  230. * Of course, not all handles managed by this module will be
  231. * server ends of named pipes, but if they are, then it's useful
  232. * to log what we can find out about the client end.
  233. */
  234. if (p_GetNamedPipeClientProcessId &&
  235. p_GetNamedPipeClientProcessId(ps->send_H, &pid))
  236. return dupprintf("process id %lu", (unsigned long)pid);
  237. return NULL;
  238. }
  239. Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
  240. Plug plug, int overlapped)
  241. {
  242. static const struct socket_function_table socket_fn_table = {
  243. sk_handle_plug,
  244. sk_handle_close,
  245. sk_handle_write,
  246. sk_handle_write_oob,
  247. sk_handle_write_eof,
  248. sk_handle_flush,
  249. sk_handle_set_frozen,
  250. sk_handle_socket_error,
  251. sk_handle_peer_info,
  252. };
  253. Handle_Socket ret;
  254. int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
  255. ret = snew(struct Socket_handle_tag);
  256. ret->fn = &socket_fn_table;
  257. ret->plug = plug;
  258. ret->error = NULL;
  259. ret->frozen = UNFROZEN;
  260. bufchain_init(&ret->inputdata);
  261. bufchain_init(&ret->stderrdata);
  262. ret->recv_H = recv_H;
  263. ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
  264. ret->send_H = send_H;
  265. ret->send_h = handle_output_new(ret->send_H, handle_sentdata, ret, flags);
  266. ret->stderr_H = stderr_H;
  267. if (ret->stderr_H)
  268. ret->stderr_h = handle_input_new(ret->stderr_H, handle_stderr,
  269. ret, flags);
  270. #ifdef MPEXT
  271. // WinSCP core uses do_select as signalization of connection up/down
  272. do_select(plug, INVALID_SOCKET, 1);
  273. #endif
  274. return (Socket) ret;
  275. }