]> asedeno.scripts.mit.edu Git - PuTTY_svn.git/blob - windows/winhsock.c
701b46da5d193bc8b748e10e7c791bb0007589a6
[PuTTY_svn.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;
22     struct handle *send_h, *recv_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     char *error;
43
44     Plug plug;
45
46     void *privptr;
47 };
48
49 static int handle_gotdata(struct handle *h, void *data, int len)
50 {
51     Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
52
53     if (len < 0) {
54         return plug_closing(ps->plug, "Read error from handle",
55                             0, 0);
56     } else if (len == 0) {
57         return plug_closing(ps->plug, NULL, 0, 0);
58     } else {
59         assert(ps->frozen != FREEZING && ps->frozen != THAWING);
60         if (ps->frozen == FREEZING) {
61             /*
62              * If we've received data while this socket is supposed to
63              * be frozen (because the read winhandl.c started before
64              * sk_set_frozen was called has now returned) then buffer
65              * the data for when we unfreeze.
66              */
67             bufchain_add(&ps->inputdata, data, len);
68
69             /*
70              * And return a very large backlog, to prevent further
71              * data arriving from winhandl until we unfreeze.
72              */
73             return INT_MAX;
74         } else {
75             return plug_receive(ps->plug, 0, data, len);
76         }
77     }
78 }
79
80 static void handle_sentdata(struct handle *h, int new_backlog)
81 {
82     Handle_Socket ps = (Handle_Socket) handle_get_privdata(h);
83     
84     plug_sent(ps->plug, new_backlog);
85 }
86
87 static Plug sk_handle_plug(Socket s, Plug p)
88 {
89     Handle_Socket ps = (Handle_Socket) s;
90     Plug ret = ps->plug;
91     if (p)
92         ps->plug = p;
93     return ret;
94 }
95
96 static void sk_handle_close(Socket s)
97 {
98     Handle_Socket ps = (Handle_Socket) s;
99
100     handle_free(ps->send_h);
101     handle_free(ps->recv_h);
102     CloseHandle(ps->send_H);
103     if (ps->recv_H != ps->send_H)
104         CloseHandle(ps->recv_H);
105     bufchain_clear(&ps->inputdata);
106
107     sfree(ps);
108 }
109
110 static int sk_handle_write(Socket s, const char *data, int len)
111 {
112     Handle_Socket ps = (Handle_Socket) s;
113
114     return handle_write(ps->send_h, data, len);
115 }
116
117 static int sk_handle_write_oob(Socket s, const char *data, int len)
118 {
119     /*
120      * oob data is treated as inband; nasty, but nothing really
121      * better we can do
122      */
123     return sk_handle_write(s, data, len);
124 }
125
126 static void sk_handle_write_eof(Socket s)
127 {
128     Handle_Socket ps = (Handle_Socket) s;
129
130     handle_write_eof(ps->send_h);
131 }
132
133 static void sk_handle_flush(Socket s)
134 {
135     /* Handle_Socket ps = (Handle_Socket) s; */
136     /* do nothing */
137 }
138
139 static void sk_handle_set_private_ptr(Socket s, void *ptr)
140 {
141     Handle_Socket ps = (Handle_Socket) s;
142     ps->privptr = ptr;
143 }
144
145 static void *sk_handle_get_private_ptr(Socket s)
146 {
147     Handle_Socket ps = (Handle_Socket) s;
148     return ps->privptr;
149 }
150
151 static void handle_socket_unfreeze(void *psv)
152 {
153     Handle_Socket ps = (Handle_Socket) psv;
154     void *data;
155     int len, new_backlog;
156
157     /*
158      * If we've been put into a state other than THAWING since the
159      * last callback, then we're done.
160      */
161     if (ps->frozen != THAWING)
162         return;
163
164     /*
165      * Get some of the data we've buffered.
166      */
167     bufchain_prefix(&ps->inputdata, &data, &len);
168     assert(len > 0);
169
170     /*
171      * Hand it off to the plug.
172      */
173     new_backlog = plug_receive(ps->plug, 0, data, len);
174
175     if (bufchain_size(&ps->inputdata) > 0) {
176         /*
177          * If there's still data in our buffer, stay in THAWING state,
178          * and reschedule ourself.
179          */
180         queue_toplevel_callback(handle_socket_unfreeze, ps);
181     } else {
182         /*
183          * Otherwise, we've successfully thawed!
184          */
185         ps->frozen = UNFROZEN;
186         handle_unthrottle(ps->recv_h, new_backlog);
187     }
188 }
189
190 static void sk_handle_set_frozen(Socket s, int is_frozen)
191 {
192     Handle_Socket ps = (Handle_Socket) s;
193
194     if (is_frozen) {
195         switch (ps->frozen) {
196           case FREEZING:
197           case FROZEN:
198             return;                    /* nothing to do */
199
200           case THAWING:
201             /*
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.
206              */
207             ps->frozen = FROZEN;
208             break;
209
210           case UNFROZEN:
211             /*
212              * The normal case. Go to FREEZING, and expect one more
213              * load of data from winhandl if we're unlucky.
214              */
215             ps->frozen = FREEZING;
216             break;
217         }
218     } else {
219         switch (ps->frozen) {
220           case UNFROZEN:
221           case THAWING:
222             return;                    /* nothing to do */
223
224           case FREEZING:
225             /*
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.
229              */
230             assert(bufchain_size(&ps->inputdata) == 0);
231             ps->frozen = UNFROZEN;
232             break;
233
234           case FROZEN:
235             /*
236              * If we have buffered data, go to THAWING and start
237              * releasing it in top-level callbacks.
238              */
239             ps->frozen = THAWING;
240             queue_toplevel_callback(handle_socket_unfreeze, ps);
241         }
242     }
243 }
244
245 static const char *sk_handle_socket_error(Socket s)
246 {
247     Handle_Socket ps = (Handle_Socket) s;
248     return ps->error;
249 }
250
251 Socket make_handle_socket(HANDLE send_H, HANDLE recv_H, Plug plug,
252                           int overlapped)
253 {
254     static const struct socket_function_table socket_fn_table = {
255         sk_handle_plug,
256         sk_handle_close,
257         sk_handle_write,
258         sk_handle_write_oob,
259         sk_handle_write_eof,
260         sk_handle_flush,
261         sk_handle_set_private_ptr,
262         sk_handle_get_private_ptr,
263         sk_handle_set_frozen,
264         sk_handle_socket_error
265     };
266
267     Handle_Socket ret;
268     int flags = (overlapped ? HANDLE_FLAG_OVERLAPPED : 0);
269
270     ret = snew(struct Socket_handle_tag);
271     ret->fn = &socket_fn_table;
272     ret->plug = plug;
273     ret->error = NULL;
274     ret->frozen = UNFROZEN;
275     bufchain_init(&ret->inputdata);
276
277     ret->recv_H = recv_H;
278     ret->recv_h = handle_input_new(ret->recv_H, handle_gotdata, ret, flags);
279     ret->send_H = send_H;
280     ret->send_h = handle_output_new(ret->send_H, handle_sentdata, ret, flags);
281
282     return (Socket) ret;
283 }