]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - windows/winhsock.c
Implement deferred closing of Windows handle-sockets.
[PuTTY.git] / windows / winhsock.c
1 /*
2  * General mechanism for wrapping up reading/writing of Windows
3  * HANDLEs into a PuTTY Socket abstraction.
4  */
5
6 #include <stdio.h>
7 #include <assert.h>
8 #include <limits.h>
9
10 #define DEFINE_PLUG_METHOD_MACROS
11 #include "tree234.h"
12 #include "putty.h"
13 #include "network.h"
14
15 typedef struct Socket_handle_tag *Handle_Socket;
16
17 struct Socket_handle_tag {
18     const struct socket_function_table *fn;
19     /* the above variable absolutely *must* be the first in this structure */
20
21     HANDLE send_H, recv_H, stderr_H;
22     struct handle *send_h, *recv_h, *stderr_h;
23
24     /*
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.
32      */
33     enum {
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 */
38     } frozen;
39     /* We buffer data here if we receive it from winhandl while frozen. */
40     bufchain inputdata;
41
42     /* Data received from stderr_H, if we have one. */
43     bufchain stderrdata;
44
45     int defer_close, deferred_close;   /* in case of re-entrance */
46
47     char *error;
48
49     Plug plug;
50 };
51
52 static int handle_gotdata(struct handle *h, void *data, int len)
53 {
54     Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
55
56     if (len < 0) {
57         return plug_closing(ps->plug, "Read error from handle",
58                             0, 0);
59     } else if (len == 0) {
60         return plug_closing(ps->plug, NULL, 0, 0);
61     } else {
62         assert(ps->frozen != FROZEN && ps->frozen != THAWING);
63         if (ps->frozen == FREEZING) {
64             /*
65              * If we've received data while this socket is supposed to
66              * be frozen (because the read winhandl.c started before
67              * sk_set_frozen was called has now returned) then buffer
68              * the data for when we unfreeze.
69              */
70             bufchain_add(&ps->inputdata, data, len);
71             ps->frozen = FROZEN;
72
73             /*
74              * And return a very large backlog, to prevent further
75              * data arriving from winhandl until we unfreeze.
76              */
77             return INT_MAX;
78         } else {
79             return plug_receive(ps->plug, 0, data, len);
80         }
81     }
82 }
83
84 static int handle_stderr(struct handle *h, void *data, int len)
85 {
86     Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
87
88     if (len > 0)
89         log_proxy_stderr(ps->plug, &ps->stderrdata, data, len);
90
91     return 0;
92 }
93
94 static void handle_sentdata(struct handle *h, int new_backlog)
95 {
96     Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
97     
98     plug_sent(ps->plug, new_backlog);
99 }
100
101 static Plug sk_handle_plug(Socket s, Plug p)
102 {
103     Handle_Socket ps = (Handle_Socket) s;
104     Plug ret = ps->plug;
105     if (p)
106         ps->plug = p;
107     return ret;
108 }
109
110 static void sk_handle_close(Socket s)
111 {
112     Handle_Socket ps = (Handle_Socket) s;
113
114     if (ps->defer_close) {
115         ps->deferred_close = TRUE;
116         return;
117     }
118
119     handle_free(ps->send_h);
120     handle_free(ps->recv_h);
121     CloseHandle(ps->send_H);
122     if (ps->recv_H != ps->send_H)
123         CloseHandle(ps->recv_H);
124     bufchain_clear(&ps->inputdata);
125     bufchain_clear(&ps->stderrdata);
126
127     sfree(ps);
128 }
129
130 static int sk_handle_write(Socket s, const char *data, int len)
131 {
132     Handle_Socket ps = (Handle_Socket) s;
133
134     return handle_write(ps->send_h, data, len);
135 }
136
137 static int sk_handle_write_oob(Socket s, const char *data, int len)
138 {
139     /*
140      * oob data is treated as inband; nasty, but nothing really
141      * better we can do
142      */
143     return sk_handle_write(s, data, len);
144 }
145
146 static void sk_handle_write_eof(Socket s)
147 {
148     Handle_Socket ps = (Handle_Socket) s;
149
150     handle_write_eof(ps->send_h);
151 }
152
153 static void sk_handle_flush(Socket s)
154 {
155     /* Handle_Socket ps = (Handle_Socket) s; */
156     /* do nothing */
157 }
158
159 static void handle_socket_unfreeze(void *psv)
160 {
161     Handle_Socket ps = (Handle_Socket) psv;
162     void *data;
163     int len, new_backlog;
164
165     /*
166      * If we've been put into a state other than THAWING since the
167      * last callback, then we're done.
168      */
169     if (ps->frozen != THAWING)
170         return;
171
172     /*
173      * Get some of the data we've buffered.
174      */
175     bufchain_prefix(&ps->inputdata, &data, &len);
176     assert(len > 0);
177
178     /*
179      * Hand it off to the plug. Be careful of re-entrance - that might
180      * have the effect of trying to close this socket.
181      */
182     ps->defer_close = TRUE;
183     new_backlog = plug_receive(ps->plug, 0, data, len);
184     bufchain_consume(&ps->inputdata, len);
185     ps->defer_close = FALSE;
186     if (ps->deferred_close) {
187         sk_handle_close(ps);
188         return;
189     }
190
191     if (bufchain_size(&ps->inputdata) > 0) {
192         /*
193          * If there's still data in our buffer, stay in THAWING state,
194          * and reschedule ourself.
195          */
196         queue_toplevel_callback(handle_socket_unfreeze, ps);
197     } else {
198         /*
199          * Otherwise, we've successfully thawed!
200          */
201         ps->frozen = UNFROZEN;
202         handle_unthrottle(ps->recv_h, new_backlog);
203     }
204 }
205
206 static void sk_handle_set_frozen(Socket s, int is_frozen)
207 {
208     Handle_Socket ps = (Handle_Socket) s;
209
210     if (is_frozen) {
211         switch (ps->frozen) {
212           case FREEZING:
213           case FROZEN:
214             return;                    /* nothing to do */
215
216           case THAWING:
217             /*
218              * We were in the middle of emptying our bufchain, and got
219              * frozen again. In that case, winhandl.c is already
220              * throttled, so just return to FROZEN state. The toplevel
221              * callback will notice and disable itself.
222              */
223             ps->frozen = FROZEN;
224             break;
225
226           case UNFROZEN:
227             /*
228              * The normal case. Go to FREEZING, and expect one more
229              * load of data from winhandl if we're unlucky.
230              */
231             ps->frozen = FREEZING;
232             break;
233         }
234     } else {
235         switch (ps->frozen) {
236           case UNFROZEN:
237           case THAWING:
238             return;                    /* nothing to do */
239
240           case FREEZING:
241             /*
242              * If winhandl didn't send us any data throughout the time
243              * we were frozen, then we'll still be in this state and
244              * can just unfreeze in the trivial way.
245              */
246             assert(bufchain_size(&ps->inputdata) == 0);
247             ps->frozen = UNFROZEN;
248             break;
249
250           case FROZEN:
251             /*
252              * If we have buffered data, go to THAWING and start
253              * releasing it in top-level callbacks.
254              */
255             ps->frozen = THAWING;
256             queue_toplevel_callback(handle_socket_unfreeze, ps);
257         }
258     }
259 }
260
261 static const char *sk_handle_socket_error(Socket s)
262 {
263     Handle_Socket ps = (Handle_Socket) s;
264     return ps->error;
265 }
266
267 static char *sk_handle_peer_info(Socket s)
268 {
269     Handle_Socket ps = (Handle_Socket) s;
270     ULONG pid;
271     static HMODULE kernel32_module;
272     DECL_WINDOWS_FUNCTION(static, BOOL, GetNamedPipeClientProcessId,
273                           (HANDLE, PULONG));
274
275     if (!kernel32_module) {
276         kernel32_module = load_system32_dll("kernel32.dll");
277         GET_WINDOWS_FUNCTION(kernel32_module, GetNamedPipeClientProcessId);
278     }
279
280     /*
281      * Of course, not all handles managed by this module will be
282      * server ends of named pipes, but if they are, then it's useful
283      * to log what we can find out about the client end.
284      */
285     if (p_GetNamedPipeClientProcessId &&
286         p_GetNamedPipeClientProcessId(ps->send_H, &pid))
287         return dupprintf("process id %lu", (unsigned long)pid);
288
289     return NULL;
290 }
291
292 Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, HANDLE stderr_H,
293                           Plug plug, int overlapped)
294 {
295     static const struct socket_function_table socket_fn_table = {
296         sk_handle_plug,
297         sk_handle_close,
298         sk_handle_write,
299         sk_handle_write_oob,
300         sk_handle_write_eof,
301         sk_handle_flush,
302         sk_handle_set_frozen,
303         sk_handle_socket_error,
304         sk_handle_peer_info,
305     };
306
307     Handle_Socket ret;
308     int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
309
310     ret = snew(struct Socket_handle_tag);
311     ret->fn = &socket_fn_table;
312     ret->plug = plug;
313     ret->error = NULL;
314     ret->frozen = UNFROZEN;
315     bufchain_init(&ret->inputdata);
316     bufchain_init(&ret->stderrdata);
317
318     ret->recv_H = recv_H;
319     ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
320     ret->send_H = send_H;
321     ret->send_h = handle_output_new(ret->send_H, handle_sentdata, ret, flags);
322     ret->stderr_H = stderr_H;
323     if (ret->stderr_H)
324         ret->stderr_h = handle_input_new(ret->stderr_H, handle_stderr,
325                                          ret, flags);
326
327     ret->defer_close = ret->deferred_close = FALSE;
328
329     return (Socket) ret;
330 }