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))
21 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
23 (cp)[1] = (value) >> 8, \
24 (cp)[2] = (value) >> 16, \
25 (cp)[3] = (value) >> 24 )
27 #define GET_16BIT_LSB_FIRST(cp) \
28 (((unsigned long)(unsigned char)(cp)[0]) | \
29 ((unsigned long)(unsigned char)(cp)[1] << 8))
31 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
33 (cp)[1] = (value) >> 8 )
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]))
41 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
42 (cp)[0] = (value) >> 24, \
43 (cp)[1] = (value) >> 16, \
44 (cp)[2] = (value) >> 8, \
47 #define GET_16BIT_MSB_FIRST(cp) \
48 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
49 ((unsigned long)(unsigned char)(cp)[1]))
51 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
52 (cp)[0] = (value) >> 8, \
56 struct pfwd_queue
*next
;
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 void *backhandle
; /* instance of SSH backend itself */
65 /* Note that backhandle need not be filled in if c is non-NULL */
68 int throttled
, throttle_override
;
71 struct pfwd_queue
*waiting
;
74 void pfd_close(Socket s
);
77 static int pfd_closing(Plug plug
, char *error_msg
, int error_code
,
80 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
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.
92 static int pfd_receive(Plug plug
, int urgent
, char *data
, int len
)
94 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
96 if (sshfwd_write(pr
->c
, data
, len
) > 0) {
98 sk_set_frozen(pr
->s
, 1);
104 static void pfd_sent(Plug plug
, int bufsize
)
106 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
108 sshfwd_unthrottle(pr
->c
, bufsize
);
112 * Called when receiving a PORT OPEN from the server
114 char *pfd_newconnect(Socket
*s
, char *hostname
, int port
, void *c
)
116 static struct plug_function_table fn_table
= {
124 char *err
, *dummy_realhost
;
125 struct PFwdPrivate
*pr
;
130 addr
= sk_namelookup(hostname
, &dummy_realhost
);
131 if ((err
= sk_addr_error(addr
)))
137 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
139 pr
->throttled
= pr
->throttle_override
= 0;
142 pr
->backhandle
= NULL
; /* we shouldn't need this */
144 pr
->s
= *s
= new_connection(addr
, dummy_realhost
, port
, 0, 1, 0, (Plug
) pr
);
145 if ((err
= sk_socket_error(*s
))) {
150 sk_set_private_ptr(*s
, pr
);
156 called when someone connects to the local port
159 static int pfd_accepting(Plug p
, void *sock
)
161 static struct plug_function_table fn_table
= {
167 struct PFwdPrivate
*pr
, *org
;
171 org
= (struct PFwdPrivate
*)p
;
172 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
176 pr
->backhandle
= org
->backhandle
;
178 pr
->s
= s
= sk_register(sock
, (Plug
) pr
);
179 if ((err
= sk_socket_error(s
))) {
184 pr
->c
= new_sock_channel(org
->backhandle
, s
);
186 strcpy(pr
->hostname
, org
->hostname
);
187 pr
->port
= org
->port
;
188 pr
->throttled
= pr
->throttle_override
= 0;
192 sk_set_private_ptr(s
, pr
);
198 /* asks to forward to the specified host/port for this */
199 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
206 /* Add a new forwarding from port -> desthost:destport
207 sets up a listener on the local machine on port
209 char *pfd_addforward(char *desthost
, int destport
, int port
, void *backhandle
)
211 static struct plug_function_table fn_table
= {
213 pfd_receive
, /* should not happen... */
214 pfd_sent
, /* also should not happen */
219 struct PFwdPrivate
*pr
;
225 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
228 strcpy(pr
->hostname
, desthost
);
230 pr
->throttled
= pr
->throttle_override
= 0;
233 pr
->backhandle
= backhandle
;
235 pr
->s
= s
= new_listener(port
, (Plug
) pr
, !cfg
.lport_acceptall
);
236 if ((err
= sk_socket_error(s
))) {
241 sk_set_private_ptr(s
, pr
);
246 void pfd_close(Socket s
)
248 struct PFwdPrivate
*pr
;
253 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
260 void pfd_unthrottle(Socket s
)
262 struct PFwdPrivate
*pr
;
265 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
268 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
271 void pfd_override_throttle(Socket s
, int enable
)
273 struct PFwdPrivate
*pr
;
276 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
278 pr
->throttle_override
= enable
;
279 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
283 * Called to send data down the raw connection.
285 int pfd_send(Socket s
, char *data
, int len
)
289 return sk_write(s
, data
, len
);
293 void pfd_confirm(Socket s
)
295 struct PFwdPrivate
*pr
;
300 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
303 sk_write(s
, NULL
, 0);