Commit 5fedf166 authored by Niels Möller's avatar Niels Möller
Browse files

(class lshd_port): Inherit io_listen_port.

(kill_port): Deleted function, it was identical with
kill_io_fd_resource.
(lshd_port_accept): Converted from an oop callback to an accept
method for the io_listen_port class. Use SERVER_VERSION_LINE.
(make_lshd_port): Changed return type to struct resource *. Take a
sockaddr as argument, rather than an fd. Create and initialize the
socket. Use io_listen.
(open_ports): Creation of socket moved to make_lshd_port.
(main): Simplified logic slightly for the daemon mode.

Rev: src/lshd.c:1.180
parent 9bfe742d
...@@ -281,66 +281,63 @@ lshd_packet_handler(struct transport_connection *connection, ...@@ -281,66 +281,63 @@ lshd_packet_handler(struct transport_connection *connection,
/* GABA: /* GABA:
(class (class
(name lshd_port) (name lshd_port)
(super resource) (super io_listen_port)
(vars (vars
(ctx object lshd_context) (ctx object lshd_context)))
(fd . int)))
*/ */
static void static void
kill_port(struct resource *s) lshd_port_accept(struct io_listen_port *s,
int fd,
socklen_t addr_len, const struct sockaddr *addr)
{ {
CAST(lshd_port, self, s); CAST(lshd_port, self, s);
if (self->super.alive) struct transport_forward *connection;
{
self->super.alive = 0;
io_close_fd(self->fd);
self->fd = -1;
}
};
static struct lshd_port *
make_lshd_port(struct lshd_context *ctx, int fd)
{
NEW(lshd_port, self);
init_resource(&self->super, kill_port);
io_register_fd(fd, "lshd listen port"); /* FIXME: Use the provided address, for logging or tcpwrappers. */
self->ctx = ctx; connection = make_transport_forward(kill_lshd_connection,
self->fd = fd; &self->ctx->super, fd, fd,
lshd_event_handler);
gc_global(&connection->super.super);
return self; transport_handshake(&connection->super, make_string(SERVER_VERSION_LINE),
lshd_line_handler);
} }
static void * static struct resource *
lshd_port_accept(oop_source *source UNUSED, make_lshd_port(struct lshd_context *ctx, socklen_t addr_len, struct sockaddr *addr)
int fd, oop_event event, void *state)
{ {
CAST(lshd_port, self, (struct lsh_object *) state); NEW(lshd_port, self);
struct transport_forward *connection; int yes = 1;
struct sockaddr_in peer; int fd = socket(addr->sa_family, SOCK_STREAM, 0);
socklen_t peer_length = sizeof(peer);
int s;
assert(event == OOP_READ); init_io_listen_port(&self->super, fd, lshd_port_accept);
assert(self->fd == fd); self->ctx = ctx;
s = accept(self->fd, (struct sockaddr *) &peer, &peer_length); if (fd < 0)
if (s < 0)
{ {
werror("accept failed: %e\n", errno); werror("socket failed: %e\n", errno);
return OOP_CONTINUE; fail:
KILL_RESOURCE(&self->super.super.super);
return NULL;
} }
connection = make_transport_forward(kill_lshd_connection, if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (yes)) <0)
&self->ctx->super, s, s, werror("setsockopt SO_REUSEADDR failed: %e\n", errno);
lshd_event_handler);
gc_global(&connection->super.super);
transport_handshake(&connection->super, make_string("SSH-2.0-lshd-ng"), if (bind(fd, addr, addr_len) < 0)
lshd_line_handler); {
werror("bind failed: %e\n", errno);
goto fail;
}
if (!io_listen(&self->super))
{
werror("listen failed: %e\n", errno);
goto fail;
}
return OOP_CONTINUE; return &self->super.super.super;
} }
/* FIXME: Should use getaddrinfo, and should allow multiple ports and /* FIXME: Should use getaddrinfo, and should allow multiple ports and
...@@ -351,10 +348,8 @@ open_ports(struct lshd_context *ctx, struct resource_list *resources, ...@@ -351,10 +348,8 @@ open_ports(struct lshd_context *ctx, struct resource_list *resources,
const char *portid) const char *portid)
{ {
struct sockaddr_in sin; struct sockaddr_in sin;
struct lshd_port *port; struct resource *port;
int yes = 1;
int s;
/* In network byte order */ /* In network byte order */
short port_number; short port_number;
...@@ -389,42 +384,19 @@ open_ports(struct lshd_context *ctx, struct resource_list *resources, ...@@ -389,42 +384,19 @@ open_ports(struct lshd_context *ctx, struct resource_list *resources,
} }
} }
s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
{
werror("socket failed: %e\n", errno);
return 0;
}
io_set_nonblocking(s);
io_set_close_on_exec(s);
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof (yes)) <0)
werror("setsockopt SO_REUSEADDR failed: %e\n", errno);
memset(&sin, 0, sizeof(sin)); memset(&sin, 0, sizeof(sin));
sin.sin_family = AF_INET; sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
sin.sin_port = port_number; sin.sin_port = port_number;
if (bind(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) port = make_lshd_port(ctx, sizeof(sin), (struct sockaddr *) &sin);
{ if (port)
werror("bind failed: %e\n", errno);
return 0;
}
if (listen(s, 256) < 0)
{ {
werror("listen failed: %e\n", errno); remember_resource(resources, port);
return 0; return 1;
} }
port = make_lshd_port(ctx, s); else
remember_resource(resources, &port->super); return 0;
global_oop_source->on_fd(global_oop_source, s,
OOP_READ, lshd_port_accept, port);
return 1;
} }
static struct lshd_context * static struct lshd_context *
...@@ -639,7 +611,7 @@ read_host_key(const char *file, ...@@ -639,7 +611,7 @@ read_host_key(const char *file,
} }
const char *argp_program_version const char *argp_program_version
= "lshd (" PACKAGE_STRING "), secsh protocol version " SERVER_PROTOCOL_VERSION; = "lshd (" PACKAGE_STRING ")";
const char *argp_program_bug_address = BUG_ADDRESS; const char *argp_program_bug_address = BUG_ADDRESS;
...@@ -930,8 +902,7 @@ main(int argc, char **argv) ...@@ -930,8 +902,7 @@ main(int argc, char **argv)
werror("Spawning from inetd not yet supported.\n"); werror("Spawning from inetd not yet supported.\n");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
else
if (mode != DAEMON_INETD)
{ {
if (!daemon_dup_null(STDIN_FILENO) if (!daemon_dup_null(STDIN_FILENO)
|| !daemon_dup_null(STDOUT_FILENO)) || !daemon_dup_null(STDOUT_FILENO))
......
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