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 const struct plug_function_table *fn;
56 /* the above variable absolutely *must* be the first in this structure */
57 void *c; /* (channel) data used by ssh.c */
58 void *backhandle; /* instance of SSH backend itself */
59 /* Note that backhandle need not be filled in if c is non-NULL */
61 int throttled, throttle_override;
64 * `dynamic' does double duty. It's set to 0 for an ordinary
65 * forwarded port, and nonzero for SOCKS-style dynamic port
66 * forwarding; but it also represents the state of the SOCKS
71 * `hostname' and `port' are the real hostname and port, once
72 * we know what we're connecting to; they're unused for this
73 * purpose while conducting a local SOCKS exchange, which means
74 * we can also use them as a buffer and pointer for reading
75 * data from the SOCKS client.
80 * When doing dynamic port forwarding, we can receive
81 * connection data before we are actually able to send it; so
82 * we may have to temporarily hold some in a dynamically
83 * allocated buffer here.
89 static int pfd_closing(Plug plug, const char *error_msg, int error_code,
92 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
95 * We have no way to communicate down the forwarded connection,
96 * so if an error occurred on the socket, we just ignore it
97 * and treat it like a proper close.
105 static int pfd_receive(Plug plug, int urgent, char *data, int len)
107 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
110 if (pr->port >= lenof(pr->hostname)) {
111 if ((pr->dynamic >> 12) == 4) {
112 /* Send back a SOCKS 4 error before closing. */
114 memset(data, 0, sizeof(data));
115 data[1] = 91; /* generic `request rejected' */
116 sk_write(pr->s, data, 8);
121 pr->hostname[pr->port++] = *data++;
124 * Now check what's in the buffer to see if it's a
125 * valid and complete message in the SOCKS exchange.
127 if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 4) &&
128 pr->hostname[0] == 4) {
132 if (pr->dynamic == 1)
133 pr->dynamic = 0x4000;
134 if (pr->port < 2) continue;/* don't have command code yet */
135 if (pr->hostname[1] != 1) {
136 /* Send back a SOCKS 4 error before closing. */
138 memset(data, 0, sizeof(data));
139 data[1] = 91; /* generic `request rejected' */
140 sk_write(pr->s, data, 8);
144 if (pr->port < 8) continue; /* haven't started username */
145 if (pr->hostname[pr->port-1] != 0)
146 continue; /* haven't _finished_ username */
148 * Now we have a full SOCKS 4 request. Check it to
149 * see if it's a SOCKS 4A request.
151 if (pr->hostname[4] == 0 && pr->hostname[5] == 0 &&
152 pr->hostname[6] == 0 && pr->hostname[7] != 0) {
154 * It's SOCKS 4A. So if we haven't yet
155 * collected the host name, we should continue
156 * waiting for data in order to do so; if we
157 * have, we can go ahead.
160 if (pr->dynamic == 0x4000) {
161 pr->dynamic = 0x4001;
162 pr->port = 8; /* reset buffer to overwrite name */
165 pr->hostname[0] = 0; /* reply version code */
166 pr->hostname[1] = 90; /* request granted */
167 sk_write(pr->s, pr->hostname, 8);
169 pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);
170 memmove(pr->hostname, pr->hostname + 8, len);
174 * It's SOCKS 4, which means we should format
175 * the IP address into the hostname string and
178 pr->hostname[0] = 0; /* reply version code */
179 pr->hostname[1] = 90; /* request granted */
180 sk_write(pr->s, pr->hostname, 8);
181 pr->port = GET_16BIT_MSB_FIRST(pr->hostname+2);
182 sprintf(pr->hostname, "%d.%d.%d.%d",
183 (unsigned char)pr->hostname[4],
184 (unsigned char)pr->hostname[5],
185 (unsigned char)pr->hostname[6],
186 (unsigned char)pr->hostname[7]);
191 if ((pr->dynamic == 1 || (pr->dynamic >> 12) == 5) &&
192 pr->hostname[0] == 5) {
196 if (pr->dynamic == 1)
197 pr->dynamic = 0x5000;
199 if (pr->dynamic == 0x5000) {
203 * We're receiving a set of method identifiers.
205 if (pr->port < 2) continue;/* no method count yet */
206 if (pr->port < 2 + (unsigned char)pr->hostname[1])
207 continue; /* no methods yet */
208 method = 0xFF; /* invalid */
209 for (i = 0; i < (unsigned char)pr->hostname[1]; i++)
210 if (pr->hostname[2+i] == 0) {
211 method = 0;/* no auth */
216 sk_write(pr->s, data, 2);
217 pr->dynamic = 0x5001;
218 pr->port = 0; /* re-empty the buffer */
222 if (pr->dynamic == 0x5001) {
224 if (pr->port < 6) continue;
225 atype = (unsigned char)pr->hostname[3];
226 if (atype == 1) /* IPv4 address */
228 if (atype == 4) /* IPv6 address */
230 if (atype == 3) /* domain name has leading length */
231 alen = 1 + (unsigned char)pr->hostname[4];
232 if (pr->port < 6 + alen) continue;
233 if (pr->hostname[1] != 1 || pr->hostname[2] != 0) {
234 pr->hostname[1] = 1; /* generic failure */
235 pr->hostname[2] = 0; /* reserved */
236 sk_write(pr->s, pr->hostname, pr->port);
241 * Now we have a viable connect request. Switch
244 pr->port = GET_16BIT_MSB_FIRST(pr->hostname+4+alen);
246 pr->hostname[1] = 0; /* succeeded */
247 sk_write(pr->s, pr->hostname, alen + 6);
248 sprintf(pr->hostname, "%d.%d.%d.%d",
249 (unsigned char)pr->hostname[4],
250 (unsigned char)pr->hostname[5],
251 (unsigned char)pr->hostname[6],
252 (unsigned char)pr->hostname[7]);
254 } else if (atype == 3) {
255 pr->hostname[1] = 0; /* succeeded */
256 sk_write(pr->s, pr->hostname, alen + 6);
257 memmove(pr->hostname, pr->hostname + 5, alen-1);
258 pr->hostname[alen-1] = '\0';
262 * Unknown address type. (FIXME: support IPv6!)
264 pr->hostname[1] = 8; /* atype not supported */
265 sk_write(pr->s, pr->hostname, pr->port);
273 * If we get here without either having done `continue'
274 * or `goto connect', it must be because there is no
275 * sensible interpretation of what's in our buffer. So
276 * close the connection rudely.
284 * We come here when we're ready to make an actual
289 pr->c = new_sock_channel(pr->backhandle, pr->s);
294 /* asks to forward to the specified host/port for this */
295 ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
300 * Now freeze the socket until the SSH server confirms the
303 sk_set_frozen(pr->s, 1);
305 * If there's any data remaining in our current buffer,
306 * save it to be sent on pfd_confirm().
309 pr->buffer = snewn(len, char);
310 memcpy(pr->buffer, data, len);
315 if (sshfwd_write(pr->c, data, len) > 0) {
317 sk_set_frozen(pr->s, 1);
323 static void pfd_sent(Plug plug, int bufsize)
325 struct PFwdPrivate *pr = (struct PFwdPrivate *) plug;
328 sshfwd_unthrottle(pr->c, bufsize);
332 * Called when receiving a PORT OPEN from the server
334 const char *pfd_newconnect(Socket *s, char *hostname, int port,
335 void *c, const Config *cfg)
337 static const struct plug_function_table fn_table = {
346 char *dummy_realhost;
347 struct PFwdPrivate *pr;
352 addr = name_lookup(hostname, port, &dummy_realhost, cfg);
353 if ((err = sk_addr_error(addr)) != NULL)
359 pr = snew(struct PFwdPrivate);
362 pr->throttled = pr->throttle_override = 0;
365 pr->backhandle = NULL; /* we shouldn't need this */
368 pr->s = *s = new_connection(addr, dummy_realhost, port,
369 0, 1, 0, (Plug) pr, cfg);
370 if ((err = sk_socket_error(*s)) != NULL) {
375 sk_set_private_ptr(*s, pr);
381 called when someone connects to the local port
384 static int pfd_accepting(Plug p, OSSocket sock)
386 static const struct plug_function_table fn_table = {
392 struct PFwdPrivate *pr, *org;
396 org = (struct PFwdPrivate *)p;
397 pr = snew(struct PFwdPrivate);
402 pr->backhandle = org->backhandle;
404 pr->s = s = sk_register(sock, (Plug) pr);
405 if ((err = sk_socket_error(s)) != NULL) {
410 sk_set_private_ptr(s, pr);
412 pr->throttled = pr->throttle_override = 0;
417 pr->port = 0; /* hostname buffer is so far empty */
418 sk_set_frozen(s, 0); /* we want to receive SOCKS _now_! */
421 strcpy(pr->hostname, org->hostname);
422 pr->port = org->port;
423 pr->c = new_sock_channel(org->backhandle, s);
429 /* asks to forward to the specified host/port for this */
430 ssh_send_port_open(pr->c, pr->hostname, pr->port, "forwarding");
438 /* Add a new forwarding from port -> desthost:destport
439 sets up a listener on the local machine on (srcaddr:)port
441 const char *pfd_addforward(char *desthost, int destport, char *srcaddr,
442 int port, void *backhandle, const Config *cfg)
444 static const struct plug_function_table fn_table = {
446 pfd_receive, /* should not happen... */
447 pfd_sent, /* also should not happen */
452 struct PFwdPrivate *pr;
458 pr = snew(struct PFwdPrivate);
463 strcpy(pr->hostname, desthost);
468 pr->throttled = pr->throttle_override = 0;
470 pr->backhandle = backhandle;
472 pr->s = s = new_listener(srcaddr, port, (Plug) pr,
473 !cfg->lport_acceptall, cfg);
474 if ((err = sk_socket_error(s)) != NULL) {
479 sk_set_private_ptr(s, pr);
484 void pfd_close(Socket s)
486 struct PFwdPrivate *pr;
491 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
499 void pfd_unthrottle(Socket s)
501 struct PFwdPrivate *pr;
504 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
507 sk_set_frozen(s, pr->throttled || pr->throttle_override);
510 void pfd_override_throttle(Socket s, int enable)
512 struct PFwdPrivate *pr;
515 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
517 pr->throttle_override = enable;
518 sk_set_frozen(s, pr->throttled || pr->throttle_override);
522 * Called to send data down the raw connection.
524 int pfd_send(Socket s, char *data, int len)
528 return sk_write(s, data, len);
532 void pfd_confirm(Socket s)
534 struct PFwdPrivate *pr;
539 pr = (struct PFwdPrivate *) sk_get_private_ptr(s);
542 sk_write(s, NULL, 0);
544 sshfwd_write(pr->c, pr->buffer, pr->buflen);