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