Commit 14b915b1 authored by Niels Möller's avatar Niels Möller
Browse files

* src/tcpforward_commands.c (new_tcpip_channel): Initialize

max_window and rec_window_size.
(remote_port_install_continuation): Added a port variable.
Various bug fixes.

Rev: src/tcpforward_commands.c:1.5
parent baca9e80
......@@ -42,24 +42,31 @@
/* Forward declarations */
/* extern struct command_simple forward_start_io; */
extern struct collect_info_1 open_direct_tcp;
extern struct collect_info_1 open_direct_tcpip;
extern struct collect_info_1 remote_listen_command;
extern struct collect_info_1 open_forwarded_tcp;
extern struct collect_info_1 open_forwarded_tcpip;
extern struct command tcpip_start_io;
extern struct command tcpip_connect_io;
struct collect_info_1 install_forwarded_tcp_handler;
static struct command make_direct_tcp_handler;
struct collect_info_1 install_direct_tcpip_handler;
struct collect_info_1 install_forwared_tcpip_handler;
struct collect_info_1 install_tcpip_forward_request_handler;
static struct command make_forward_tcpip_handler;
static struct command make_direct_tcpip_handler;
#if 0
struct collect_info_1 install_forwarded_tcpip_handler;
static struct command make_forwarded_tcpip_handler;
#endif
struct collect_info_1 install_tcpip_forward_handler;
static struct command make_tcpip_forward_handler;
/* #define FORWARD_START_IO (&forward_start_io.super.super) */
#define OPEN_DIRECT_TCP (&open_direct_tcp.super.super.super)
#define OPEN_DIRECT_TCPIP (&open_direct_tcpip.super.super.super)
#define REMOTE_LISTEN (&remote_listen_command.super.super.super)
#define TCPIP_START_IO (&tcpip_start_io.super)
#define TCPIP_CONNECT_IO (&tcpip_connect_io.super)
#define OPEN_FORWARDED_TCP (&open_forwarded_tcp.super.super.super)
#define OPEN_FORWARDED_TCPIP (&open_forwarded_tcpip.super.super.super)
#include "tcpforward_commands.c.x"
......@@ -69,21 +76,22 @@ static struct command make_forward_tcpip_handler;
* the party receiving a open-tcp request, when a channel to the
* target has been opened. */
#define TCPIP_WINDOW_SIZE (SSH_MAX_PACKET << 3)
/* NOTE: This command does not do any remembering. */
static int do_tcpip_connect_io(struct command *ignored UNUSED,
struct lsh_object *x,
struct command_continuation *c)
{
CAST(io_fd, socket, x);
struct ssh_channel *channel;
debug("tcpforward_commands.c: do_tcpip_connect_io, socket is %z\n",
socket ? "valid" : "invalid");
if (!socket)
COMMAND_RETURN(c, NULL);
channel = make_tcpip_channel(socket);
tcpip_channel_start_io(channel);
return COMMAND_RETURN(c, channel);
return COMMAND_RETURN(c, make_tcpip_channel(socket, TCPIP_WINDOW_SIZE));
}
struct command tcpip_connect_io = STATIC_COMMAND(do_tcpip_connect_io);
......@@ -99,6 +107,10 @@ do_tcpip_start_io(struct command *s UNUSED,
struct command_continuation *c)
{
CAST_SUBTYPE(ssh_channel, channel, x);
debug("tcpforward_commands.c: do_tcpip_start_io, channel is %z\n",
channel ? "valid" : "invalid");
if (!channel)
{
verbose("Error opening channel.\n");
......@@ -127,6 +139,7 @@ struct command tcpip_start_io =
(vars
; ATOM_FORWARDED_TCPIP or ATOM_DIRECT_TCPIP
(type . int)
(max_window . UINT32)
; For forwarded-tcpip, port is the port listened to.
; For direct-tcpip, poprt is the port to conect to.
; In both cases, it's a port used on the server end.
......@@ -144,12 +157,17 @@ new_tcpip_channel(struct channel_open_command *c,
/* NOTE: All accepted fd:s must end up in this function, so it
* should be ok to delay the REMEMBER() call until here. */
debug("tcpforward_commands.c: new_tcpip_channel()\n");
REMEMBER_RESOURCE(connection->resources, &self->peer->fd->super.super);
channel = make_tcpip_channel(self->peer->fd);
channel = make_tcpip_channel(self->peer->fd, TCPIP_WINDOW_SIZE);
channel->write = connection->write;
channel->max_window = self->max_window;
channel->rec_window_size = self->max_window;
*request = prepare_channel_open(connection->channels, self->type,
channel,
"%S%i%S%i",
......@@ -160,16 +178,20 @@ new_tcpip_channel(struct channel_open_command *c,
}
static struct command *
make_open_tcpip_command(int type,
make_open_tcpip_command(int type, UINT32 max_window,
struct address_info *port,
struct listen_value *peer)
{
NEW(open_tcpip_command, self);
debug("tcpforward_commands.c: make_open_tcpip_command()\n");
self->super.super.call = do_channel_open_command;
self->super.new_channel = new_tcpip_channel;
self->type = type;
self->max_window = max_window;
self->port = port;
self->peer = peer;
......@@ -177,44 +199,44 @@ make_open_tcpip_command(int type,
}
static struct lsh_object *
collect_open_forwarded_tcp(struct collect_info_2 *info,
struct lsh_object *a,
struct lsh_object *b)
collect_open_forwarded_tcpip(struct collect_info_2 *info,
struct lsh_object *a,
struct lsh_object *b)
{
CAST(address_info, local, a);
CAST(listen_value, peer, b);
assert(!info->next);
return &make_open_tcpip_command(ATOM_FORWARDED_TCPIP,
return &make_open_tcpip_command(ATOM_FORWARDED_TCPIP, TCPIP_WINDOW_SIZE,
local, peer)->super;
}
static struct collect_info_2 collect_open_forwarded_tcp_2 =
STATIC_COLLECT_2_FINAL(collect_open_forwarded_tcp);
static struct collect_info_2 collect_open_forwarded_tcpip_2 =
STATIC_COLLECT_2_FINAL(collect_open_forwarded_tcpip);
struct collect_info_1 open_forwarded_tcp =
STATIC_COLLECT_1(&collect_open_forwarded_tcp_2);
struct collect_info_1 open_forwarded_tcpip =
STATIC_COLLECT_1(&collect_open_forwarded_tcpip_2);
static struct lsh_object *
collect_open_direct_tcp(struct collect_info_2 *info,
struct lsh_object *a,
struct lsh_object *b)
collect_open_direct_tcpip(struct collect_info_2 *info,
struct lsh_object *a,
struct lsh_object *b)
{
CAST(address_info, local, a);
CAST(listen_value, peer, b);
assert(!info->next);
return &make_open_tcpip_command(ATOM_DIRECT_TCPIP,
return &make_open_tcpip_command(ATOM_DIRECT_TCPIP, TCPIP_WINDOW_SIZE,
local, peer)->super;
}
static struct collect_info_2 collect_open_direct_tcp_2 =
STATIC_COLLECT_2_FINAL(collect_open_direct_tcp);
static struct collect_info_2 collect_open_direct_tcpip_2 =
STATIC_COLLECT_2_FINAL(collect_open_direct_tcpip);
struct collect_info_1 open_direct_tcp =
STATIC_COLLECT_1(&collect_open_direct_tcp_2);
struct collect_info_1 open_direct_tcpip =
STATIC_COLLECT_1(&collect_open_direct_tcpip_2);
/* Requesting remote forwarding of a port */
......@@ -224,6 +246,7 @@ STATIC_COLLECT_1(&collect_open_direct_tcp_2);
(name remote_port_install_continuation)
(super command_frame)
(vars
(port object remote_port)
(callback object command)))
*/
......@@ -231,21 +254,37 @@ static int do_remote_port_install_continuation(struct command_continuation *s,
struct lsh_object *x)
{
CAST(remote_port_install_continuation, self, s);
CAST(remote_port, port, x);
CAST(ssh_connection, connection, x);
if (connection)
{
debug("tcpforward_commands.c: do_remote_port_install_continuation(), success.\n");
port->callback = self->callback;
self->port->callback = self->callback;
}
else
{
debug("tcpforward_commands.c: do_remote_port_install_continuation(), failed.\n");
/* FIXME: Cleanup, and delete the port from the remote_ports list. */
}
return COMMAND_RETURN(self->super.up, x);
}
static struct command_continuation *
make_remote_port_install_continuation(struct command *callback,
make_remote_port_install_continuation(struct remote_port *port,
struct command *callback,
struct command_continuation *c)
{
NEW(remote_port_install_continuation, self);
debug("tcpforward_commands.c: make_remote_port_install_continuation()\n");
self->super.super.c = do_remote_port_install_continuation;
self->super.up = c;
self->port = port;
self->callback = callback;
return &self->super.super;
......@@ -279,11 +318,13 @@ do_format_request_tcpip_forward(struct global_request_command *s,
CAST(request_tcpip_forward_command, self, s);
struct remote_port *port;
int want_reply;
debug("tcpforward_commands.c: do_format_request_tcpip_forward()\n");
if (c)
{
port = make_remote_port(self->port, NULL);
*c = make_remote_port_install_continuation(self->callback, *c);
*c = make_remote_port_install_continuation(port, self->callback, *c);
want_reply = 1;
}
else
......@@ -304,6 +345,9 @@ make_request_tcpip_forward_command(struct command *callback,
struct address_info *listen)
{
NEW(request_tcpip_forward_command, self);
debug("tcpforward_commands.c: make_request_tcpip_forward_command()\n");
self->super.super.call = do_channel_global_command;
self->super.format_request = do_format_request_tcpip_forward;
......@@ -338,11 +382,11 @@ STATIC_COLLECT_1(&collect_info_remote_listen_2);
/* GABA:
(expr
(name make_forward_local_port)
(name forward_local_port)
(globals
(listen LISTEN_COMMAND)
(start_io TCPIP_START_IO)
(open_direct_tcp OPEN_DIRECT_TCP))
(open_direct_tcpip OPEN_DIRECT_TCPIP))
(params
(backend object io_backend)
(local object address_info)
......@@ -350,69 +394,76 @@ STATIC_COLLECT_1(&collect_info_remote_listen_2);
(expr
(lambda (connection)
(listen (lambda (peer)
(start_io (open_direct_tcp target peer connection)))
(start_io (open_direct_tcpip target peer connection)))
backend
local))))
*/
struct command *forward_local_port(struct io_backend *backend,
struct address_info *local,
struct address_info *target)
struct command *
make_forward_local_port(struct io_backend *backend,
struct address_info *local,
struct address_info *target)
{
CAST_SUBTYPE(command, res,
make_forward_local_port(backend, local, target));
forward_local_port(backend, local, target));
debug("tcpforward_commands.c: forward_local_port()\n");
return res;
}
/* GABA:
(expr
(name make_forward_remote_port)
(name forward_remote_port)
(globals
(remote_listen REMOTE_LISTEN)
;; (connection_remember CONNECTION_REMEMBER)
(start_io TCPIP_CONNECT_IO))
(params
(connect object command)
(remote object address_info)
(target object address_info))
(remote object address_info))
(expr
(lambda (connection)
(remote_listen (lambda (peer)
(start_io peer
(connect target connection)))
;; FIXME: This calls connect too early
;; Let it take peer as argument
(connect (K connection peer))))
remote
connection))))
*/
struct command *forward_remote_port(struct io_backend *backend,
struct address_info *local,
struct address_info *target)
struct command *
make_forward_remote_port(struct io_backend *backend,
struct address_info *remote,
struct address_info *target)
{
CAST_SUBTYPE(command, connect,
COMMAND_SIMPLE(&connect_with_connection.super, backend));
CAST_SUBTYPE(command, res,
make_forward_remote_port(connect, local, target));
forward_remote_port(make_connect_port(backend, target), remote));
debug("tcpforward_commands.c: forward_remote_port()\n");
return res;
}
/* Takes a callback function and returns a channel_open
* handler. */
static int
do_make_direct_tcp_handler(struct command *s UNUSED,
do_make_direct_tcpip_handler(struct command *s UNUSED,
struct lsh_object *x,
struct command_continuation *c)
{
CAST_SUBTYPE(command, callback, x);
trace("tcpforward_commands.c: do_make_open_tcp_handler()\n");
return
COMMAND_RETURN(c,
&make_channel_open_direct_tcpip(callback)->super);
}
static struct command
make_direct_tcp_handler = STATIC_COMMAND(do_make_direct_tcp_handler);
make_direct_tcpip_handler = STATIC_COMMAND(do_make_direct_tcpip_handler);
/* Takes a callback function and returns a global_request handler. */
static int
......@@ -422,28 +473,30 @@ do_make_tcpip_forward_handler(struct command *s UNUSED,
{
CAST_SUBTYPE(command, callback, x);
debug("tcpforward_commands.c: do_make_tcpip_forward_handler()\n");
return
COMMAND_RETURN(c,
&make_tcpip_forward_request(callback)->super);
}
static struct command
make_forward_tcpip_handler
make_tcpip_forward_handler
= STATIC_COMMAND(do_make_tcpip_forward_handler);
/* Commands to install open hadnlers */
struct install_info install_direct_tcp_info_2 =
/* Commands to install handlers */
struct install_info install_direct_tcpip_info_2 =
STATIC_INSTALL_OPEN_HANDLER(ATOM_DIRECT_TCPIP);
struct collect_info_1 install_direct_tcp_handler =
STATIC_COLLECT_1(&install_direct_tcp_info_2.super);
struct collect_info_1 install_direct_tcpip_handler =
STATIC_COLLECT_1(&install_direct_tcpip_info_2.super);
struct install_info install_forwarded_tcp_info_2 =
struct install_info install_forwarded_tcpip_info_2 =
STATIC_INSTALL_OPEN_HANDLER(ATOM_FORWARDED_TCPIP);
struct collect_info_1 install_forwarded_tcp_handler =
STATIC_COLLECT_1(&install_forwarded_tcp_info_2.super);
struct collect_info_1 install_forwarded_tcpip_handler =
STATIC_COLLECT_1(&install_forwarded_tcpip_info_2.super);
/* Server side callbacks */
......@@ -452,16 +505,16 @@ STATIC_COLLECT_1(&install_forwarded_tcp_info_2.super);
(expr
(name direct_tcpip_hook)
(globals
(install "&install_forwarded_tcp_handler.super.super.super")
(handler "&make_direct_tcp_handler.super")
(start_io TCPIP_START_IO))
(install "&install_direct_tcpip_handler.super.super.super")
(handler "&make_direct_tcpip_handler.super")
(connect_io TCPIP_CONNECT_IO))
(params
(connect object command))
(expr
(lambda (connection)
(install connection
(handler (lambda (port)
(start_io (connect connection port))))))))
(connect_io (connect connection port))))))))
*/
struct command *
......@@ -470,50 +523,66 @@ make_direct_tcpip_hook(struct io_backend *backend)
CAST_SUBTYPE(command, res,
direct_tcpip_hook(make_connect_connection(backend)));
debug("tcpforward_commands.c: make_direct_tcpip_hook()\n");
return res;
}
/* Almost identical to the direct_tcpip-hook, but we have to
* examine the list of remotely-forwarded ports to find out
* where to forward the connection (if at all). */
/* ;; GABA:
(expr
(name forwarded_tcpip_hook)
(globals
(install "&install_forwarded_tcpip_handler.super.super.super")
(handler "&make_open_tcpip_handler.super")
(start_io TCPIP_START_IO))
(expr
(lambda (connection)
())))
(install connection
(handler (lambda (port)
(start_io (connect connection port))))))))
*/
struct install_info install_tcpip_forward_request_info_2 =
STATIC_INSTALL_OPEN_HANDLER(ATOM_TCPIP_FORWARD);
struct install_info install_tcpip_forward_info_2 =
STATIC_INSTALL_GLOBAL_HANDLER(ATOM_TCPIP_FORWARD);
struct collect_info_1 install_tcpip_forward_request_handler =
STATIC_COLLECT_1(&install_tcpip_forward_request_info_2.super);
struct collect_info_1 install_tcpip_forward_handler =
STATIC_COLLECT_1(&install_tcpip_forward_info_2.super);
/* GABA:
(expr
(name make_tcpip_forward_hook)
(name tcpip_forward_hook)
(globals
(install "&install_tcpip_forward_request_handler.super.super.super")
(handler "&make_forward_tcpip_handler.super")
(install "&install_tcpip_forward_handler.super.super.super")
(handler "&make_tcpip_forward_handler.super")
(start_io TCPIP_START_IO)
(open_forwarded_tcp OPEN_FORWARDED_TCP)
(open_forwarded_tcpip OPEN_FORWARDED_TCPIP)
(listen LISTEN_COMMAND))
(params
(backend object io_backend))
(expr
(lambda (connection)
;; Called when the ssh-connection is established
(install connection
(handler (lambda (port)
;; Called when the client requests remote forwarding
(listen (lambda (peer)
(start_io (open_forwarded_tcp port peer
connection)))
;; Called when someone connects to the
;; forwarded port.
(start_io (open_forwarded_tcpip port peer
connection)))
backend port)))))))
*/
struct command *
tcpip_forward_hook(struct io_backend *backend)
make_tcpip_forward_hook(struct io_backend *backend)
{
CAST_SUBTYPE(command, res, make_tcpip_forward_hook(backend));
CAST_SUBTYPE(command, res, tcpip_forward_hook(backend));
debug("tcpforward_commands.c: tcpip_forward_hook()\n");
return res;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment