]> asedeno.scripts.mit.edu Git - PuTTY.git/blob - portfwd.c
Extensive changes that _should_ fix the socket buffering problems,
[PuTTY.git] / portfwd.c
1 #include <windows.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include "putty.h"
6 #include "ssh.h"
7
8 #ifndef FALSE
9 #define FALSE 0
10 #endif
11 #ifndef TRUE
12 #define TRUE 1
13 #endif
14
15 #define GET_32BIT_LSB_FIRST(cp) \
16   (((unsigned long)(unsigned char)(cp)[0]) | \
17   ((unsigned long)(unsigned char)(cp)[1] << 8) | \
18   ((unsigned long)(unsigned char)(cp)[2] << 16) | \
19   ((unsigned long)(unsigned char)(cp)[3] << 24))
20
21 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
22   (cp)[0] = (value), \
23   (cp)[1] = (value) >> 8, \
24   (cp)[2] = (value) >> 16, \
25   (cp)[3] = (value) >> 24 )
26
27 #define GET_16BIT_LSB_FIRST(cp) \
28   (((unsigned long)(unsigned char)(cp)[0]) | \
29   ((unsigned long)(unsigned char)(cp)[1] << 8))
30
31 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
32   (cp)[0] = (value), \
33   (cp)[1] = (value) >> 8 )
34
35 #define GET_32BIT_MSB_FIRST(cp) \
36   (((unsigned long)(unsigned char)(cp)[0] << 24) | \
37   ((unsigned long)(unsigned char)(cp)[1] << 16) | \
38   ((unsigned long)(unsigned char)(cp)[2] << 8) | \
39   ((unsigned long)(unsigned char)(cp)[3]))
40
41 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
42   (cp)[0] = (value) >> 24, \
43   (cp)[1] = (value) >> 16, \
44   (cp)[2] = (value) >> 8, \
45   (cp)[3] = (value) )
46
47 #define GET_16BIT_MSB_FIRST(cp) \
48   (((unsigned long)(unsigned char)(cp)[0] << 8) | \
49   ((unsigned long)(unsigned char)(cp)[1]))
50
51 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
52   (cp)[0] = (value) >> 8, \
53   (cp)[1] = (value) )
54
55 struct pfwd_queue {
56     struct pfwd_queue *next;
57     char *buf;
58 };
59
60 struct PFwdPrivate {
61     struct plug_function_table *fn;
62     /* the above variable absolutely *must* be the first in this structure */
63     void *c;                           /* (channel) data used by ssh.c */
64     Socket s;
65     char hostname[128];
66     int throttled, throttle_override;
67     int port;
68     int ready;
69     struct pfwd_queue *waiting;
70 };
71
72 void pfd_close(Socket s);
73
74
75 static int pfd_closing(Plug plug, char *error_msg, int error_code,
76                        int calling_back)
77 {
78     struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
79
80     /*
81      * We have no way to communicate down the forwarded connection,
82      * so if an error occurred on the socket, we just ignore it
83      * and treat it like a proper close.
84      */
85     sshfwd_close(pr->c);
86     pfd_close(pr->s);
87     return 1;
88 }
89
90 static int pfd_receive(Plug plug, int urgent, char *data, int len)
91 {
92     struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
93     if (pr->ready) {
94         if (sshfwd_write(pr->c, data, len) > 0) {
95             pr->throttled = 1;
96             sk_set_frozen(pr->s, 1);
97         }
98     }
99     return 1;
100 }
101
102 static void pfd_sent(Plug plug, int bufsize)
103 {
104     struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
105
106     sshfwd_unthrottle(pr->c, bufsize);
107 }
108
109 /*
110  * Called when receiving a PORT OPEN from the server
111  */
112 char *pfd_newconnect(Socket *s, char *hostname, int port, void *c)
113 {
114     static struct plug_function_table fn_table = {
115         pfd_closing,
116         pfd_receive,
117         pfd_sent,
118         NULL
119     };
120
121     SockAddr addr;
122     char *err, *dummy_realhost;
123     struct PFwdPrivate *pr;
124
125     /*
126      * Try to find host.
127      */
128     addr = sk_namelookup(hostname, &dummy_realhost);
129     if ((err = sk_addr_error(addr)))
130         return err;
131
132     /*
133      * Open socket.
134      */
135     pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
136     pr->fn = &fn_table;
137     pr->throttled = pr->throttle_override = 0;
138     pr->ready = 1;
139     pr->c = c;
140
141     pr->s = *s = sk_new(addr, port, 0, 1, (Plug) pr);
142     if ((err = sk_socket_error(*s))) {
143         sfree(pr);
144         return err;
145     }
146
147     sk_set_private_ptr(*s, pr);
148     sk_addr_free(addr);
149     return NULL;
150 }
151
152 /*
153  called when someone connects to the local port
154  */
155
156 static int pfd_accepting(Plug p, struct sockaddr *addr, void *sock)
157 {
158     /* for now always accept this socket */
159     static struct plug_function_table fn_table = {
160         pfd_closing,
161         pfd_receive,
162         pfd_sent,
163         NULL
164     };
165     struct PFwdPrivate *pr, *org;
166     struct sockaddr_in *sin = (struct sockaddr_in *)addr;
167     Socket s;
168     char *err;
169
170     if (ntohl(sin->sin_addr.s_addr) != 0x7F000001 && !cfg.lport_acceptall)
171         return 1; /* denied */
172
173     org = (struct PFwdPrivate *)p;
174     pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
175     pr->fn = &fn_table;
176
177     pr->c = NULL;
178
179     pr->s = s = sk_register(sock, (Plug) pr);
180     if ((err = sk_socket_error(s))) {
181         sfree(pr);
182         return err != NULL;
183     }
184
185     pr->c = new_sock_channel(s);
186
187     strcpy(pr->hostname, org->hostname);
188     pr->port = org->port;
189     pr->throttled = pr->throttle_override = 0;
190     pr->ready = 0;
191     pr->waiting = NULL;
192
193     sk_set_private_ptr(s, pr);
194
195     if (pr->c == NULL) {
196         sfree(pr);
197         return 1;
198     } else {
199         /* asks to forward to the specified host/port for this */
200         ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
201     }
202
203     return 0;
204 }
205
206
207 /* Add a new forwarding from port -> desthost:destport
208  sets up a listenner on the local machine on port
209  */
210 char *pfd_addforward(char *desthost, int destport, int port)
211 {
212     static struct plug_function_table fn_table = {
213         pfd_closing,
214         pfd_receive,                   /* should not happen... */
215         pfd_sent,                      /* also should not happen */
216         pfd_accepting
217     };
218
219     char *err;
220     struct PFwdPrivate *pr;
221     Socket s;
222
223     /*
224      * Open socket.
225      */
226     pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
227     pr->fn = &fn_table;
228     pr->c = NULL;
229     strcpy(pr->hostname, desthost);
230     pr->port = destport;
231     pr->throttled = pr->throttle_override = 0;
232     pr->ready = 0;
233     pr->waiting = NULL;
234
235     pr->s = s = sk_newlistenner(port, (Plug) pr);
236     if ((err = sk_socket_error(s))) {
237         sfree(pr);
238         return err;
239     }
240
241     sk_set_private_ptr(s, pr);
242
243     return NULL;
244 }
245
246 void pfd_close(Socket s)
247 {
248     struct PFwdPrivate *pr;
249
250     if (!s)
251         return;
252
253     pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
254
255     sfree(pr);
256
257     sk_close(s);
258 }
259
260 void pfd_unthrottle(Socket s)
261 {
262     struct PFwdPrivate *pr;
263     if (!s)
264         return;
265     pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
266
267     pr->throttled = 0;
268     sk_set_frozen(s, pr->throttled || pr->throttle_override);
269 }
270
271 void pfd_override_throttle(Socket s, int enable)
272 {
273     struct PFwdPrivate *pr;
274     if (!s)
275         return;
276     pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
277
278     pr->throttle_override = enable;
279     sk_set_frozen(s, pr->throttled || pr->throttle_override);
280 }
281
282 /*
283  * Called to send data down the raw connection.
284  */
285 int pfd_send(Socket s, char *data, int len)
286 {
287     if (s == NULL)
288         return 0;
289     return sk_write(s, data, len);
290 }
291
292
293 void pfd_confirm(Socket s)
294 {
295     struct PFwdPrivate *pr;
296
297     if (s == NULL)
298         return;
299
300     pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
301     pr->ready = 1;
302     sk_set_frozen(s, 0);
303     sk_write(s, NULL, 0);
304 }