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 */ |
64 | Socket s; |
65 | char hostname[128]; |
5471d09a |
66 | int throttled, throttle_override; |
f26d5c33 |
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; |
5471d09a |
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 | } |
f26d5c33 |
99 | return 1; |
100 | } |
101 | |
5471d09a |
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 | |
f26d5c33 |
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, |
5471d09a |
117 | pfd_sent, |
f26d5c33 |
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; |
5471d09a |
137 | pr->throttled = pr->throttle_override = 0; |
f26d5c33 |
138 | pr->ready = 1; |
139 | pr->c = c; |
140 | |
8eebd221 |
141 | pr->s = *s = new_connection(addr, dummy_realhost, port, 0, 1, 0, (Plug) pr); |
f26d5c33 |
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 | |
bcce45ed |
156 | static int pfd_accepting(Plug p, void *sock) |
f26d5c33 |
157 | { |
f26d5c33 |
158 | static struct plug_function_table fn_table = { |
159 | pfd_closing, |
160 | pfd_receive, |
5471d09a |
161 | pfd_sent, |
f26d5c33 |
162 | NULL |
163 | }; |
164 | struct PFwdPrivate *pr, *org; |
f26d5c33 |
165 | Socket s; |
166 | char *err; |
167 | |
f26d5c33 |
168 | org = (struct PFwdPrivate *)p; |
169 | pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate)); |
170 | pr->fn = &fn_table; |
171 | |
172 | pr->c = NULL; |
173 | |
174 | pr->s = s = sk_register(sock, (Plug) pr); |
175 | if ((err = sk_socket_error(s))) { |
176 | sfree(pr); |
177 | return err != NULL; |
178 | } |
179 | |
51470298 |
180 | pr->c = new_sock_channel(backhandle, s); |
f26d5c33 |
181 | |
182 | strcpy(pr->hostname, org->hostname); |
183 | pr->port = org->port; |
5471d09a |
184 | pr->throttled = pr->throttle_override = 0; |
f26d5c33 |
185 | pr->ready = 0; |
186 | pr->waiting = NULL; |
187 | |
188 | sk_set_private_ptr(s, pr); |
189 | |
190 | if (pr->c == NULL) { |
191 | sfree(pr); |
192 | return 1; |
193 | } else { |
194 | /* asks to forward to the specified host/port for this */ |
51470298 |
195 | ssh_send_port_open(backhandle, pr->c, pr->hostname, |
196 | pr->port, "forwarding"); |
f26d5c33 |
197 | } |
198 | |
199 | return 0; |
200 | } |
201 | |
202 | |
203 | /* Add a new forwarding from port -> desthost:destport |
bcce45ed |
204 | sets up a listener on the local machine on port |
f26d5c33 |
205 | */ |
206 | char *pfd_addforward(char *desthost, int destport, int port) |
207 | { |
208 | static struct plug_function_table fn_table = { |
209 | pfd_closing, |
5471d09a |
210 | pfd_receive, /* should not happen... */ |
211 | pfd_sent, /* also should not happen */ |
f26d5c33 |
212 | pfd_accepting |
213 | }; |
214 | |
215 | char *err; |
216 | struct PFwdPrivate *pr; |
217 | Socket s; |
218 | |
219 | /* |
220 | * Open socket. |
221 | */ |
222 | pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate)); |
223 | pr->fn = &fn_table; |
224 | pr->c = NULL; |
225 | strcpy(pr->hostname, desthost); |
226 | pr->port = destport; |
5471d09a |
227 | pr->throttled = pr->throttle_override = 0; |
f26d5c33 |
228 | pr->ready = 0; |
229 | pr->waiting = NULL; |
230 | |
8eebd221 |
231 | pr->s = s = new_listener(port, (Plug) pr, !cfg.lport_acceptall); |
f26d5c33 |
232 | if ((err = sk_socket_error(s))) { |
233 | sfree(pr); |
234 | return err; |
235 | } |
236 | |
237 | sk_set_private_ptr(s, pr); |
238 | |
239 | return NULL; |
240 | } |
241 | |
242 | void pfd_close(Socket s) |
243 | { |
244 | struct PFwdPrivate *pr; |
245 | |
246 | if (!s) |
247 | return; |
248 | |
249 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
250 | |
251 | sfree(pr); |
252 | |
253 | sk_close(s); |
254 | } |
255 | |
5471d09a |
256 | void pfd_unthrottle(Socket s) |
257 | { |
258 | struct PFwdPrivate *pr; |
259 | if (!s) |
260 | return; |
261 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
262 | |
263 | pr->throttled = 0; |
264 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
265 | } |
266 | |
267 | void pfd_override_throttle(Socket s, int enable) |
268 | { |
269 | struct PFwdPrivate *pr; |
270 | if (!s) |
271 | return; |
272 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
273 | |
274 | pr->throttle_override = enable; |
275 | sk_set_frozen(s, pr->throttled || pr->throttle_override); |
276 | } |
277 | |
f26d5c33 |
278 | /* |
279 | * Called to send data down the raw connection. |
280 | */ |
5471d09a |
281 | int pfd_send(Socket s, char *data, int len) |
f26d5c33 |
282 | { |
f26d5c33 |
283 | if (s == NULL) |
5471d09a |
284 | return 0; |
285 | return sk_write(s, data, len); |
f26d5c33 |
286 | } |
287 | |
288 | |
289 | void pfd_confirm(Socket s) |
290 | { |
5f2e19fc |
291 | struct PFwdPrivate *pr; |
f26d5c33 |
292 | |
293 | if (s == NULL) |
294 | return; |
295 | |
5f2e19fc |
296 | pr = (struct PFwdPrivate *) sk_get_private_ptr(s); |
f26d5c33 |
297 | pr->ready = 1; |
298 | sk_set_frozen(s, 0); |
299 | sk_write(s, NULL, 0); |
300 | } |