2 * General mechanism for wrapping up reading/writing of Windows
3 * HANDLEs into a PuTTY Socket abstraction.
10 #define DEFINE_PLUG_METHOD_MACROS
15 typedef struct Socket_handle_tag *Handle_Socket;
17 struct Socket_handle_tag {
18 const struct socket_function_table *fn;
19 /* the above variable absolutely *must* be the first in this structure */
21 HANDLE send_H, recv_H, stderr_H;
22 struct handle *send_h, *recv_h, *stderr_h;
25 * Freezing one of these sockets is a slightly fiddly business,
26 * because the reads from the handle are happening in a separate
27 * thread as blocking system calls and so once one is in progress
28 * it can't sensibly be interrupted. Hence, after the user tries
29 * to freeze one of these sockets, it's unavoidable that we may
30 * receive one more load of data before we manage to get
31 * winhandl.c to stop reading.
34 UNFROZEN, /* reading as normal */
35 FREEZING, /* have been set to frozen but winhandl is still reading */
36 FROZEN, /* really frozen - winhandl has been throttled */
37 THAWING /* we're gradually releasing our remaining data */
39 /* We buffer data here if we receive it from winhandl while frozen. */
42 /* Data received from stderr_H, if we have one. */
50 static int handle_gotdata(struct handle *h, void *data, int len)
52 Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
55 return plug_closing(ps->plug, "Read error from handle",
57 } else if (len == 0) {
58 return plug_closing(ps->plug, NULL, 0, 0);
60 assert(ps->frozen != FREEZING && ps->frozen != THAWING);
61 if (ps->frozen == FREEZING) {
63 * If we've received data while this socket is supposed to
64 * be frozen (because the read winhandl.c started before
65 * sk_set_frozen was called has now returned) then buffer
66 * the data for when we unfreeze.
68 bufchain_add(&ps->inputdata, data, len);
71 * And return a very large backlog, to prevent further
72 * data arriving from winhandl until we unfreeze.
76 return plug_receive(ps->plug, 0, data, len);
81 static int handle_stderr(struct handle *h, void *data, int len)
83 Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
86 log_proxy_stderr(ps->plug, &ps->stderrdata, data, len);
91 static void handle_sentdata(struct handle *h, int new_backlog)
93 Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
95 plug_sent(ps->plug, new_backlog);
98 static Plug sk_handle_plug(Socket s, Plug p)
100 Handle_Socket ps = (Handle_Socket) s;
107 static void sk_handle_close(Socket s)
109 Handle_Socket ps = (Handle_Socket) s;
111 handle_free(ps->send_h);
112 handle_free(ps->recv_h);
113 CloseHandle(ps->send_H);
114 if (ps->recv_H != ps->send_H)
115 CloseHandle(ps->recv_H);
116 bufchain_clear(&ps->inputdata);
117 bufchain_clear(&ps->stderrdata);
122 static int sk_handle_write(Socket s, const char *data, int len)
124 Handle_Socket ps = (Handle_Socket) s;
126 return handle_write(ps->send_h, data, len);
129 static int sk_handle_write_oob(Socket s, const char *data, int len)
132 * oob data is treated as inband; nasty, but nothing really
135 return sk_handle_write(s, data, len);
138 static void sk_handle_write_eof(Socket s)
140 Handle_Socket ps = (Handle_Socket) s;
142 handle_write_eof(ps->send_h);
145 static void sk_handle_flush(Socket s)
147 /* Handle_Socket ps = (Handle_Socket) s; */
151 static void handle_socket_unfreeze(void *psv)
153 Handle_Socket ps = (Handle_Socket) psv;
155 int len, new_backlog;
158 * If we've been put into a state other than THAWING since the
159 * last callback, then we're done.
161 if (ps->frozen != THAWING)
165 * Get some of the data we've buffered.
167 bufchain_prefix(&ps->inputdata, &data, &len);
171 * Hand it off to the plug.
173 new_backlog = plug_receive(ps->plug, 0, data, len);
175 if (bufchain_size(&ps->inputdata) > 0) {
177 * If there's still data in our buffer, stay in THAWING state,
178 * and reschedule ourself.
180 queue_toplevel_callback(handle_socket_unfreeze, ps);
183 * Otherwise, we've successfully thawed!
185 ps->frozen = UNFROZEN;
186 handle_unthrottle(ps->recv_h, new_backlog);
190 static void sk_handle_set_frozen(Socket s, int is_frozen)
192 Handle_Socket ps = (Handle_Socket) s;
195 switch (ps->frozen) {
198 return; /* nothing to do */
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.
212 * The normal case. Go to FREEZING, and expect one more
213 * load of data from winhandl if we're unlucky.
215 ps->frozen = FREEZING;
219 switch (ps->frozen) {
222 return; /* nothing to do */
226 * If winhandl didn't send us any data throughout the time
227 * we were frozen, then we'll still be in this state and
228 * can just unfreeze in the trivial way.
230 assert(bufchain_size(&ps->inputdata) == 0);
231 ps->frozen = UNFROZEN;
236 * If we have buffered data, go to THAWING and start
237 * releasing it in top-level callbacks.
239 ps->frozen = THAWING;
240 queue_toplevel_callback(handle_socket_unfreeze, ps);
245 static const char *sk_handle_socket_error(Socket s)
247 Handle_Socket ps = (Handle_Socket) s;
251 static char *sk_handle_peer_info(Socket s)
253 Handle_Socket ps = (Handle_Socket) s;
255 static HMODULE kernel32_module;
256 DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
259 if (!kernel32_module) {
260 kernel32_module = load_system32_dll("kernel32.dll");
261 GET_WINDOWS_FUNCTION(kernel32_module, GetNamedPipeClientProcessId);
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.
269 if (p_GetNamedPipeClientProcessId &&
270 p_GetNamedPipeClientProcessId(ps->send_H, &pid))
271 return dupprintf("process id %lu", (unsigned long)pid);
276 Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
277 Plug plug, int overlapped)
279 static const struct socket_function_table socket_fn_table = {
286 sk_handle_set_frozen,
287 sk_handle_socket_error,
292 int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
294 ret = snew(struct Socket_handle_tag);
295 ret->fn = &socket_fn_table;
298 ret->frozen = UNFROZEN;
299 bufchain_init(&ret->inputdata);
300 bufchain_init(&ret->stderrdata);
302 ret->recv_H = recv_H;
303 ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
304 ret->send_H = send_H;
305 ret->send_h = handle_output_new(ret->send_H, handle_sentdata, ret, flags);
306 ret->stderr_H = stderr_H;
308 ret->stderr_h = handle_input_new(ret->stderr_H, handle_stderr,