14 #define GET_32BIT_LSB_FIRST(cp) \
15 (((unsigned long)(unsigned char)(cp)[0]) | \
16 ((unsigned long)(unsigned char)(cp)[1] << 8) | \
17 ((unsigned long)(unsigned char)(cp)[2] << 16) | \
18 ((unsigned long)(unsigned char)(cp)[3] << 24))
20 #define PUT_32BIT_LSB_FIRST(cp, value) ( \
22 (cp)[1] = (value) >> 8, \
23 (cp)[2] = (value) >> 16, \
24 (cp)[3] = (value) >> 24 )
26 #define GET_16BIT_LSB_FIRST(cp) \
27 (((unsigned long)(unsigned char)(cp)[0]) | \
28 ((unsigned long)(unsigned char)(cp)[1] << 8))
30 #define PUT_16BIT_LSB_FIRST(cp, value) ( \
32 (cp)[1] = (value) >> 8 )
34 #define GET_32BIT_MSB_FIRST(cp) \
35 (((unsigned long)(unsigned char)(cp)[0] << 24) | \
36 ((unsigned long)(unsigned char)(cp)[1] << 16) | \
37 ((unsigned long)(unsigned char)(cp)[2] << 8) | \
38 ((unsigned long)(unsigned char)(cp)[3]))
40 #define PUT_32BIT_MSB_FIRST(cp, value) ( \
41 (cp)[0] = (value) >> 24, \
42 (cp)[1] = (value) >> 16, \
43 (cp)[2] = (value) >> 8, \
46 #define GET_16BIT_MSB_FIRST(cp) \
47 (((unsigned long)(unsigned char)(cp)[0] << 8) | \
48 ((unsigned long)(unsigned char)(cp)[1]))
50 #define PUT_16BIT_MSB_FIRST(cp, value) ( \
51 (cp)[0] = (value) >> 8, \
55 struct pfwd_queue *next;
60 struct plug_function_table *fn;
61 /* the above variable absolutely *must* be the first in this structure */
62 void *c; /* (channel) data used by ssh.c */
63 void *backhandle; /* instance of SSH backend itself */
64 /* Note that backhandle need not be filled in if c is non-NULL */
67 int throttled, throttle_override;
70 struct pfwd_queue *waiting;
74 static int pfd_closing(Plug plug, char *error_msg, int error_code,
77 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
80 * We have no way to communicate down the forwarded connection,
81 * so if an error occurred on the socket, we just ignore it
82 * and treat it like a proper close.
89 static int pfd_receive(Plug plug, int urgent, char *data, int len)
91 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
93 if (sshfwd_write(pr->c, data, len) > 0) {
95 sk_set_frozen(pr->s, 1);
101 static void pfd_sent(Plug plug, int bufsize)
103 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
105 sshfwd_unthrottle(pr->c, bufsize);
109 * Called when receiving a PORT OPEN from the server
111 char *pfd_newconnect(Socket *s, char *hostname, int port, void *c)
113 static struct plug_function_table fn_table = {
121 char *err, *dummy_realhost;
122 struct PFwdPrivate *pr;
127 addr = name_lookup(hostname, port, &dummy_realhost);
128 if ((err = sk_addr_error(addr)) != NULL)
134 pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
136 pr->throttled = pr->throttle_override = 0;
139 pr->backhandle = NULL; /* we shouldn't need this */
141 pr->s = *s = new_connection(addr, dummy_realhost, port, 0, 1, 0, (Plug) pr);
142 if ((err = sk_socket_error(*s)) != NULL) {
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));
173 pr->backhandle = org->backhandle;
175 pr->s = s = sk_register(sock, (Plug) pr);
176 if ((err = sk_socket_error(s)) != NULL) {
181 pr->c = new_sock_channel(org->backhandle, s);
183 strcpy(pr->hostname, org->hostname);
184 pr->port = org->port;
185 pr->throttled = pr->throttle_override = 0;
189 sk_set_private_ptr(s, pr);
195 /* asks to forward to the specified host/port for this */
196 ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
203 /* Add a new forwarding from port -> desthost:destport
204 sets up a listener on the local machine on (srcaddr:)port
206 char *pfd_addforward(char *desthost, int destport, char *srcaddr, int port,
209 static struct plug_function_table fn_table = {
211 pfd_receive, /* should not happen... */
212 pfd_sent, /* also should not happen */
217 struct PFwdPrivate *pr;
223 pr = (struct PFwdPrivate *) smalloc(sizeof(struct PFwdPrivate));
226 strcpy(pr->hostname, desthost);
228 pr->throttled = pr->throttle_override = 0;
231 pr->backhandle = backhandle;
233 pr->s = s = new_listener(srcaddr, port, (Plug) pr, !cfg.lport_acceptall);
234 if ((err = sk_socket_error(s)) != NULL) {
239 sk_set_private_ptr(s, pr);
244 void pfd_close(Socket s)
246 struct PFwdPrivate *pr;
251 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
258 void pfd_unthrottle(Socket s)
260 struct PFwdPrivate *pr;
263 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
266 sk_set_frozen(s, pr->throttled || pr->throttle_override);
269 void pfd_override_throttle(Socket s, int enable)
271 struct PFwdPrivate *pr;
274 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
276 pr->throttle_override = enable;
277 sk_set_frozen(s, pr->throttled || pr->throttle_override);
281 * Called to send data down the raw connection.
283 int pfd_send(Socket s, char *data, int len)
287 return sk_write(s, data, len);
291 void pfd_confirm(Socket s)
293 struct PFwdPrivate *pr;
298 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
301 sk_write(s, NULL, 0);