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