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 */
66 int throttled
, throttle_override
;
69 struct pfwd_queue
*waiting
;
72 void pfd_close(Socket s
);
75 static int pfd_closing(Plug plug
, char *error_msg
, int error_code
,
78 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
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.
90 static int pfd_receive(Plug plug
, int urgent
, char *data
, int len
)
92 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
94 if (sshfwd_write(pr
->c
, data
, len
) > 0) {
96 sk_set_frozen(pr
->s
, 1);
102 static void pfd_sent(Plug plug
, int bufsize
)
104 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) plug
;
106 sshfwd_unthrottle(pr
->c
, bufsize
);
110 * Called when receiving a PORT OPEN from the server
112 char *pfd_newconnect(Socket
*s
, char *hostname
, int port
, void *c
)
114 static struct plug_function_table fn_table
= {
122 char *err
, *dummy_realhost
;
123 struct PFwdPrivate
*pr
;
128 addr
= sk_namelookup(hostname
, &dummy_realhost
);
129 if ((err
= sk_addr_error(addr
)))
135 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
137 pr
->throttled
= pr
->throttle_override
= 0;
141 pr
->s
= *s
= new_connection(addr
, dummy_realhost
, port
, 0, 1, 0, (Plug
) pr
);
142 if ((err
= sk_socket_error(*s
))) {
147 sk_set_private_ptr(*s
, pr
);
153 called when someone connects to the local port
156 static int pfd_accepting(Plug p
, void *sock
)
158 static struct plug_function_table fn_table
= {
164 struct PFwdPrivate
*pr
, *org
;
168 org
= (struct PFwdPrivate
*)p
;
169 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
174 pr
->s
= s
= sk_register(sock
, (Plug
) pr
);
175 if ((err
= sk_socket_error(s
))) {
180 pr
->c
= new_sock_channel(backhandle
, s
);
182 strcpy(pr
->hostname
, org
->hostname
);
183 pr
->port
= org
->port
;
184 pr
->throttled
= pr
->throttle_override
= 0;
188 sk_set_private_ptr(s
, pr
);
194 /* asks to forward to the specified host/port for this */
195 ssh_send_port_open(backhandle
, pr
->c
, pr
->hostname
,
196 pr
->port
, "forwarding");
203 /* Add a new forwarding from port -> desthost:destport
204 sets up a listener on the local machine on port
206 char *pfd_addforward(char *desthost
, int destport
, int port
)
208 static struct plug_function_table fn_table
= {
210 pfd_receive
, /* should not happen... */
211 pfd_sent
, /* also should not happen */
216 struct PFwdPrivate
*pr
;
222 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
225 strcpy(pr
->hostname
, desthost
);
227 pr
->throttled
= pr
->throttle_override
= 0;
231 pr
->s
= s
= new_listener(port
, (Plug
) pr
, !cfg
.lport_acceptall
);
232 if ((err
= sk_socket_error(s
))) {
237 sk_set_private_ptr(s
, pr
);
242 void pfd_close(Socket s
)
244 struct PFwdPrivate
*pr
;
249 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
256 void pfd_unthrottle(Socket s
)
258 struct PFwdPrivate
*pr
;
261 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
264 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
267 void pfd_override_throttle(Socket s
, int enable
)
269 struct PFwdPrivate
*pr
;
272 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
274 pr
->throttle_override
= enable
;
275 sk_set_frozen(s
, pr
->throttled
|| pr
->throttle_override
);
279 * Called to send data down the raw connection.
281 int pfd_send(Socket s
, char *data
, int len
)
285 return sk_write(s
, data
, len
);
289 void pfd_confirm(Socket s
)
291 struct PFwdPrivate
*pr
;
296 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
299 sk_write(s
, NULL
, 0);