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, \
55 extern void sshfwd_close(void *);
56 extern void sshfwd_write(void *, char *, int);
59 struct pfwd_queue
*next
;
64 struct plug_function_table
*fn
;
65 /* the above variable absolutely *must* be the first in this structure */
66 void *c
; /* (channel) data used by ssh.c */
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
;
97 sshfwd_write(pr
->c
, data
, len
);
102 * Called when receiving a PORT OPEN from the server
104 char *pfd_newconnect(Socket
*s
, char *hostname
, int port
, void *c
)
106 static struct plug_function_table fn_table
= {
113 char *err
, *dummy_realhost
;
114 struct PFwdPrivate
*pr
;
119 addr
= sk_namelookup(hostname
, &dummy_realhost
);
120 if ((err
= sk_addr_error(addr
)))
126 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
131 pr
->s
= *s
= sk_new(addr
, port
, 0, 1, (Plug
) pr
);
132 if ((err
= sk_socket_error(*s
))) {
137 sk_set_private_ptr(*s
, pr
);
143 called when someone connects to the local port
146 static int pfd_accepting(Plug p
, struct sockaddr
*addr
, void *sock
)
148 /* for now always accept this socket */
149 static struct plug_function_table fn_table
= {
154 struct PFwdPrivate
*pr
, *org
;
155 struct sockaddr_in
*sin
= (struct sockaddr_in
*)addr
;
159 if (ntohl(sin
->sin_addr
.s_addr
) != 0x7F000001 && !cfg
.lport_acceptall
)
160 return 1; /* denied */
162 org
= (struct PFwdPrivate
*)p
;
163 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
168 pr
->s
= s
= sk_register(sock
, (Plug
) pr
);
169 if ((err
= sk_socket_error(s
))) {
174 pr
->c
= new_sock_channel(s
);
176 strcpy(pr
->hostname
, org
->hostname
);
177 pr
->port
= org
->port
;
181 sk_set_private_ptr(s
, pr
);
187 /* asks to forward to the specified host/port for this */
188 ssh_send_port_open(pr
->c
, pr
->hostname
, pr
->port
, "forwarding");
195 /* Add a new forwarding from port -> desthost:destport
196 sets up a listenner on the local machine on port
198 char *pfd_addforward(char *desthost
, int destport
, int port
)
200 static struct plug_function_table fn_table
= {
202 pfd_receive
, /* should not happen... */
207 struct PFwdPrivate
*pr
;
213 pr
= (struct PFwdPrivate
*) smalloc(sizeof(struct PFwdPrivate
));
216 strcpy(pr
->hostname
, desthost
);
221 pr
->s
= s
= sk_newlistenner(port
, (Plug
) pr
);
222 if ((err
= sk_socket_error(s
))) {
227 sk_set_private_ptr(s
, pr
);
232 void pfd_close(Socket s
)
234 struct PFwdPrivate
*pr
;
239 pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
247 * Called to send data down the raw connection.
249 void pfd_send(Socket s
, char *data
, int len
)
251 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
256 sk_write(s
, data
, len
);
260 void pfd_confirm(Socket s
)
262 struct PFwdPrivate
*pr
= (struct PFwdPrivate
*) sk_get_private_ptr(s
);
269 sk_write(s
, NULL
, 0);