]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
server: Implement --listen-address for control service.
authorAndre Noll <maan@tuebingen.mpg.de>
Wed, 7 Mar 2018 11:32:15 +0000 (12:32 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Tue, 17 Apr 2018 07:29:57 +0000 (09:29 +0200)
For hosts with multiple IP addresses one might want to configure the
listening sockets so that connections can only arrive on a subset of
the host's addresses. This patch implements this feature.

Unlike para_listen_simple() the new para_listen() receives an optional
argument to let the caller specify the listening address in addition
to the port number. para_listen_simple() is now a simple wrapper
that passes a NULL pointer as the new argument which indicates that
the socket should listen on all local addresses, just like prior to
this change.

The set of listening addresses for the control service of para_server
can be specified via the new --listen-address option. This option
can be given multiple times, once for each listening address.

Due to this change the server needs to maintain more than a single
file descriptor to dispatch incoming connections. Hence the integer
->listen_fd of struct server_command_task has to be replaced by an
array of file descriptors and the {pre,post}_select methods of the
command task have iterate over all descriptors in the array.

The meaning of the --port option has changed due to the new option:
since the argument to --listen-address can also contain a port number,
the argument to --port is only used for addresses with no port number,
or if --listen-address is not given at all.

Although the http and dccp senders also open a listening socket, this
commit affects only the control service of para_server (listening on
TCP 2990 by default). Senders will be covered in the next commit.

m4/lls/server.suite.m4
net.c
net.h
server.c

index 5bba85d4348c3254ad19d2ac91007dc0e3fefc53..89cf3521bf350e2273652b139d3e78c73ff7fcfa 100644 (file)
@@ -34,18 +34,45 @@ version-string = GIT_VERSION()
        m4_include(log-timing.m4)
        m4_include(color.m4)
        m4_include(per-command-options-section.m4)
        m4_include(log-timing.m4)
        m4_include(color.m4)
        m4_include(per-command-options-section.m4)
+       [option listen-address]
+               summary = local listening addresses for the control service
+               arg_info = required_arg
+               arg_type = string
+               typestr = addr
+               flag multiple
+               [help]
+                       para_server listens on a TCP socket for incoming connections from
+                       para_client or para_audiod. This option controls on which addresses
+                       the server should listen. If the option is not given, the server
+                       listens on all local addresses (INADDR_ANY for IPv4 addresses,
+                       IN6ADDR_ANY_INIT for IPv6 addresses).
+
+                       The argument specifies an IPv4 or an IPv6 address, either a numerical
+                       network address (for IPv4, numbers-and-dots notation as supported
+                       by inet_aton(3); for IPv6, hexadecimal string format as supported
+                       by inet_pton(3)), or a network hostname, whose network addresses is
+                       looked up and resolved. The address can optionally include a port
+                       number. For addresses for which no port number is given, the argument
+                       of the --port option (see below) is implied.
+
+                       This option may be given multiple times. The server will then listen
+                       on each of the specified addresses.
+
+                       Examples: 10.10.1.1, 10.10.1.2:2991, localhost, localhost:2991,
+                       [::1]:2991, [badc0de::1].
+               [/help]
        [option port]
                short_opt = p
        [option port]
                short_opt = p
-               summary = listening port of the paraslash control service
+               summary = listening port of the control service
                arg_info = required_arg
                arg_type = uint32
                typestr = portnumber
                default_val = 2990
                [help]
                arg_info = required_arg
                arg_type = uint32
                typestr = portnumber
                default_val = 2990
                [help]
-                       para_server listens on this TCP port for incoming connections
-                       from clients such as para_client. If the default port is changed,
-                       the corresponding option of para_client must be used to connect
-                       to para_server.
+                       This option applies only to addresses given to --listen-address
+                       (see above) which do no include a port number. If the default port
+                       is changed, the corresponding option of para_client must be used to
+                       connect to para_server.
                [/help]
        [option user-list]
                summary = file which contains user names and credentials
                [/help]
        [option user-list]
                summary = file which contains user names and credentials
diff --git a/net.c b/net.c
index 1fece043586cddc782791d0ec18af45881797c5f..672e09e971717a742eb85b9c57b05f661f0b97dd 100644 (file)
--- a/net.c
+++ b/net.c
@@ -502,17 +502,28 @@ int makesock(unsigned l4type, bool passive, const char *host, uint16_t port_numb
  * Create a passive / listening socket.
  *
  * \param l4type The transport-layer type (\p IPPROTO_xxx).
  * Create a passive / listening socket.
  *
  * \param l4type The transport-layer type (\p IPPROTO_xxx).
- * \param port The decimal port number to listen on.
+ * \param addr Passed to \ref parse_url() if not NULL.
+ * \param port Ignored if addr contains a port number.
  *
  * \return Positive integer (socket descriptor) on success, negative value
  * otherwise.
  *
  * \sa \ref makesock(), ip(7), ipv6(7), bind(2), listen(2).
  */
  *
  * \return Positive integer (socket descriptor) on success, negative value
  * otherwise.
  *
  * \sa \ref makesock(), ip(7), ipv6(7), bind(2), listen(2).
  */
-int para_listen_simple(unsigned l4type, uint16_t port)
+int para_listen(unsigned l4type, const char *addr, uint16_t port)
 {
 {
-       int ret, fd = makesock(l4type, 1, NULL, port, NULL);
-
+       char host[MAX_HOSTLEN];
+       int ret, fd, addr_port;
+
+       if (addr) {
+               if (!parse_url(addr, host, sizeof(host), &addr_port))
+                       return -ERRNO_TO_PARA_ERROR(EINVAL);
+               if (addr_port > 0)
+                       port = addr_port;
+               addr = host;
+       }
+       fd = makesock(l4type, true /* passive */, addr, port,
+               NULL /* no flowopts */);
        if (fd > 0) {
                ret = listen(fd, BACKLOG);
                if (ret < 0) {
        if (fd > 0) {
                ret = listen(fd, BACKLOG);
                if (ret < 0) {
@@ -526,6 +537,22 @@ int para_listen_simple(unsigned l4type, uint16_t port)
        return fd;
 }
 
        return fd;
 }
 
+/**
+ * Create a socket which listens on all network addresses.
+ *
+ * \param l4type See \ref para_listen().
+ * \param port See \ref para_listen().
+ *
+ * This is a simple wrapper for \ref para_listen() which passes a NULL pointer
+ * as the address information.
+ *
+ * \return See \ref para_listen().
+ */
+int para_listen_simple(unsigned l4type, uint16_t port)
+{
+       return para_listen(l4type, NULL, port);
+}
+
 /**
  * Determine IPv4/v6 socket address length.
  * \param sa Container of IPv4 or IPv6 address.
 /**
  * Determine IPv4/v6 socket address length.
  * \param sa Container of IPv4 or IPv6 address.
diff --git a/net.h b/net.h
index 143fb812f4c1be20368b93cbb60be26eaeb826fe..0e93653c033640848b8787a092ff71c37f19d824 100644 (file)
--- a/net.h
+++ b/net.h
@@ -128,6 +128,7 @@ bool sockaddr_equal(const struct sockaddr *sa1, const struct sockaddr *sa2);
 /** How many pending connections queue of a listening server will hold. */
 #define BACKLOG        10
 
 /** How many pending connections queue of a listening server will hold. */
 #define BACKLOG        10
 
+int para_listen(unsigned l4type, const char *addr, uint16_t port);
 int para_listen_simple(unsigned l4type, uint16_t port);
 
 /** Pretty-printing of IPv4/6 socket addresses */
 int para_listen_simple(unsigned l4type, uint16_t port);
 
 /** Pretty-printing of IPv4/6 socket addresses */
index 66c93ab259d1c26e26a904c312cd45916407cb2a..9c11b23cbb4801f15e08ae3c1a0a559322b38578 100644 (file)
--- a/server.c
+++ b/server.c
@@ -108,8 +108,9 @@ pid_t afs_pid = 0;
 
 /** The task responsible for server command handling. */
 struct server_command_task {
 
 /** The task responsible for server command handling. */
 struct server_command_task {
-       /** TCP port on which para_server listens for connections. */
-       int listen_fd;
+       unsigned num_listen_fds; /* only one by default */
+       /** TCP socket(s) on which para_server listens for connections. */
+       int *listen_fds;
        /** Copied from para_server's main function. */
        int argc;
        /** Argument vector passed to para_server's main function. */
        /** Copied from para_server's main function. */
        int argc;
        /** Argument vector passed to para_server's main function. */
@@ -346,20 +347,22 @@ static void init_signal_task(void)
 
 static void command_pre_select(struct sched *s, void *context)
 {
 
 static void command_pre_select(struct sched *s, void *context)
 {
+       unsigned n;
        struct server_command_task *sct = context;
        struct server_command_task *sct = context;
-       para_fd_set(sct->listen_fd, &s->rfds, &s->max_fileno);
+
+       for (n = 0; n < sct->num_listen_fds; n++)
+               para_fd_set(sct->listen_fds[n], &s->rfds, &s->max_fileno);
 }
 
 }
 
-static int command_post_select(struct sched *s, void *context)
+static int command_task_accept(unsigned listen_idx, fd_set *rfds,
+               struct server_command_task *sct)
 {
 {
-       struct server_command_task *sct = context;
-
        int new_fd, ret, i;
        char *peer_name;
        pid_t child_pid;
        uint32_t *chunk_table;
 
        int new_fd, ret, i;
        char *peer_name;
        pid_t child_pid;
        uint32_t *chunk_table;
 
-       ret = para_accept(sct->listen_fd, &s->rfds, NULL, 0, &new_fd);
+       ret = para_accept(sct->listen_fds[listen_idx], rfds, NULL, 0, &new_fd);
        if (ret <= 0)
                goto out;
        mmd->num_connects++;
        if (ret <= 0)
                goto out;
        mmd->num_connects++;
@@ -410,23 +413,61 @@ out:
        return 0;
 }
 
        return 0;
 }
 
+static int command_post_select(struct sched *s, void *context)
+{
+       struct server_command_task *sct = context;
+       unsigned n;
+       int ret;
+
+       for (n = 0; n < sct->num_listen_fds; n++) {
+               ret = command_task_accept(n, &s->rfds, sct);
+               if (ret < 0) {
+                       free(sct->listen_fds);
+                       return ret;
+               }
+       }
+       return 0;
+}
+
 static void init_server_command_task(int argc, char **argv)
 {
        int ret;
        static struct server_command_task server_command_task_struct,
                *sct = &server_command_task_struct;
 static void init_server_command_task(int argc, char **argv)
 {
        int ret;
        static struct server_command_task server_command_task_struct,
                *sct = &server_command_task_struct;
+       unsigned n;
+       uint32_t port = OPT_UINT32_VAL(PORT);
+
 
        PARA_NOTICE_LOG("initializing tcp command socket\n");
        sct->argc = argc;
        sct->argv = argv;
 
        PARA_NOTICE_LOG("initializing tcp command socket\n");
        sct->argc = argc;
        sct->argv = argv;
-       ret = para_listen_simple(IPPROTO_TCP, OPT_UINT32_VAL(PORT));
-       if (ret < 0)
-               goto err;
-       sct->listen_fd = ret;
-       ret = mark_fd_nonblocking(sct->listen_fd);
-       if (ret < 0)
-               goto err;
-       add_close_on_fork_list(sct->listen_fd); /* child doesn't need the listener */
+       if (!OPT_GIVEN(LISTEN_ADDRESS)) {
+               sct->num_listen_fds = 1;
+               sct->listen_fds = para_malloc(sizeof(int));
+               ret = para_listen_simple(IPPROTO_TCP, port);
+               if (ret < 0)
+                       goto err;
+               sct->listen_fds[0] = ret;
+       } else {
+               sct->num_listen_fds = OPT_GIVEN(LISTEN_ADDRESS);
+               sct->listen_fds = para_malloc(sct->num_listen_fds * sizeof(int));
+               for (n = 0; n < OPT_GIVEN(LISTEN_ADDRESS); n++) {
+                       const char *arg;
+                       arg = lls_string_val(n, OPT_RESULT(LISTEN_ADDRESS));
+                       ret = para_listen(IPPROTO_TCP, arg, port);
+                       if (ret < 0)
+                               goto err;
+                       sct->listen_fds[n] = ret;
+               }
+       }
+       for (n = 0; n < sct->num_listen_fds; n++) {
+               ret = mark_fd_nonblocking(sct->listen_fds[n]);
+               if (ret < 0)
+                       goto err;
+               /* child doesn't need the listener */
+               add_close_on_fork_list(sct->listen_fds[n]);
+       }
+
        sct->task = task_register(&(struct task_info) {
                .name = "server command",
                .pre_select = command_pre_select,
        sct->task = task_register(&(struct task_info) {
                .name = "server command",
                .pre_select = command_pre_select,