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