f26d5c33 |
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 | |
f26d5c33 |
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 */ |
6b78788a |
64 | void *backhandle; /* instance of SSH backend itself */ |
65 | /* Note that backhandle need not be filled in if c is non-NULL */ |
f26d5c33 |
66 | Socket s; |
67 | char hostname[128]; |
5471d09a |
68 | int throttled, throttle_override; |
f26d5c33 |
69 | int port; |
70 | int ready; |
71 | struct pfwd_queue *waiting; |
72 | }; |
73 | |
74 | void pfd_close(Socket s); |
75 | |
76 | |
77 | static int pfd_closing(Plug plug, char *error_msg, int error_code, |
78 | int calling_back) |
79 | { |
80 | struct PFwdPrivate *pr = (struct PFwdPrivate *) plug; |
81 | |
82 | /* |
83 | * We have no way to communicate down the forwarded connection, |
84 | * so if an error occurred on the socket, we just ignore it |
85 | * and treat it like a proper close. |
86 | */ |
87 | sshfwd_close(pr->c); |
88 | pfd_close(pr->s); |
89 | return 1; |
90 | } |
91 | |
92 | static int pfd_receive(Plug plug, int urgent, char *data, int len) |
93 | { |
94 | struct PFwdPrivate *pr = (struct PFwdPrivate *) plug; |
5471d09a |
95 | if (pr->ready) { |
96 | if (sshfwd_write(pr->c, data, len) > 0) { |
97 | pr->throttled = 1; |
98 | sk_set_frozen(pr->s, 1); |
99 | } |
100 | } |
f26d5c33 |
101 | return 1; |
102 | } |
103 | |
5471d09a |
104 | static void pfd_sent(Plug plug, int bufsize) |
105 | { |
106 | struct PFwdPrivate *pr = (struct PFwdPrivate *) plug; |
107 | |
108 | sshfwd_unthrottle(pr->c, bufsize); |
109 | } |
110 | |
f26d5c33 |
111 | /* |
112 | * Called when receiving a PORT OPEN from the server |
113 | */ |
114 | char *pfd_newconnect(Socket *s, char *hostname, int port, void *c) |
115 | { |
116 | static struct plug_function_table fn_table = { |
117 | pfd_closing, |
118 | pfd_receive, |
5471d09a |
119 | pfd_sent, |
f26d5c33 |
120 | NULL |
121 | }; |
122 | |
123 | SockAddr addr; |
124 | char *err, *dummy_realhost; |
125 | struct PFwdPrivate *pr; |
126 | |
127 | /* |
128 | * Try to find host. |
129 | */ |
130 | addr = sk_namelookup(hostname, &dummy_realhost); |
131 | if ((err = sk_addr_error(addr))) |
132 | return err; |
133 | |
134 | /* |
135 | * Open socket. |
136 | */ |
137 | pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate)); |
138 | pr->fn = &fn_table; |
5471d09a |
139 | pr->throttled = pr->throttle_override = 0; |
f26d5c33 |
140 | pr->ready = 1; |
141 | pr->c = c; |
6b78788a |
142 | pr->backhandle = NULL; /* we shouldn't need this */ |
f26d5c33 |
143 | |
8eebd221 |
144 | pr->s = *s = new_connection(addr, dummy_realhost, port, 0, 1, 0, (Plug) pr); |
f26d5c33 |
145 | if ((err = sk_socket_error(*s))) { |
146 | sfree(pr); |
147 | return err; |
148 | } |
149 | |
150 | sk_set_private_ptr(*s, pr); |
151 | sk_addr_free(addr); |
152 | return NULL; |
153 | } |
154 | |
155 | /* |
156 | called when someone connects to the local port |
157 | */ |
158 | |
bcce45ed |
159 | static int pfd_accepting(Plug p, void *sock) |
f26d5c33 |
160 | { |
f26d5c33 |
161 | static struct plug_function_table fn_table = { |
162 | pfd_closing, |
163 | pfd_receive, |
5471d09a |
164 | pfd_sent, |
f26d5c33 |
165 | NULL |
166 | }; |
167 | struct PFwdPrivate *pr, *org; |
f26d5c33 |
168 | Socket s; |
169 | char *err; |
170 | |
f26d5c33 |
171 | org = (struct PFwdPrivate *)p; |
172 | pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate)); |
173 | pr->fn = &fn_table; |
174 | |
175 | pr->c = NULL; |
6b78788a |
176 | pr->backhandle = org->backhandle; |
f26d5c33 |
177 | |
178 | pr->s = s = sk_register(sock, (Plug) pr); |
179 | if ((err = sk_socket_error(s))) { |
180 | sfree(pr); |
181 | return err != NULL; |
182 | } |
183 | |
6b78788a |
184 | pr->c = new_sock_channel(org->backhandle, s); |
f26d5c33 |
185 | |
186 | strcpy(pr->hostname, org->hostname); |
187 | pr->port = org->port; |
5471d09a |
188 | pr->throttled = pr->throttle_override = 0; |
f26d5c33 |
189 | pr->ready = 0; |
190 | pr->waiting = NULL; |
191 | |
192 | sk_set_private_ptr(s, pr); |
193 | |
194 | if (pr->c == NULL) { |
195 | sfree(pr); |
196 | return 1; |
197 | } else { |
198 | /* asks to forward to the specified host/port for this */ |
6b78788a |
199 | ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding"); |
f26d5c33 |
200 | } |
201 | |
202 | return 0; |
203 | } |
204 | |
205 | |
206 | /* Add a new forwarding from port -> desthost:destport |
bcce45ed |
207 | sets up a listener on the local machine on port |
f26d5c33 |
208 | */ |
6b78788a |
209 | char *pfd_addforward(char *desthost, int destport, int port, void *backhandle) |
f26d5c33 |
210 | { |
211 | static struct plug_function_table fn_table = { |
212 | pfd_closing, |
5471d09a |
213 | pfd_receive, /* should not happen... */ |
214 | pfd_sent, /* also should not happen */ |
f26d5c33 |
215 | pfd_accepting |
216 | }; |
217 | |
218 | char *err; |
219 | struct PFwdPrivate *pr; |
220 | Socket s; |
221 | |
222 | /* |
223 | * Open socket. |
224 | */ |
225 | pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate)); |
226 | pr->fn = &fn_table; |
227 | pr->c = NULL; |
228 | strcpy(pr->hostname, desthost); |
229 | pr->port = destport; |
5471d09a |
230 | pr->throttled = pr->throttle_override = 0; |
f26d5c33 |
231 | pr->ready = 0; |
232 | pr->waiting = NULL; |
6b78788a |
233 | pr->backhandle = backhandle; |
f26d5c33 |
234 | |
8eebd221 |
235 | pr->s = s = new_listener(port, (Plug) pr, !cfg.lport_acceptall); |
f26d5c33 |
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 | |
5471d09a |
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 | |
f26d5c33 |
282 | /* |
283 | * Called to send data down the raw connection. |
284 | */ |
5471d09a |
285 | int pfd_send(Socket s, char *data, int len) |
f26d5c33 |
286 | { |
f26d5c33 |
287 | if (s == NULL) |
5471d09a |
288 | return 0; |
289 | return sk_write(s, data, len); |
f26d5c33 |
290 | } |
291 | |
292 | |
293 | void pfd_confirm(Socket s) |
294 | { |
5f2e19fc |
295 | struct PFwdPrivate *pr; |
f26d5c33 |
296 | |
297 | if (s == NULL) |
298 | return; |
299 | |
5f2e19fc |
300 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
f26d5c33 |
301 | pr->ready = 1; |
302 | sk_set_frozen(s, 0); |
303 | sk_write(s, NULL, 0); |
304 | } |