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
- 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]
- 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
* 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).
*/
-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) {
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.
/** 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 */
/** 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. */
static void command_pre_select(struct sched *s, void *context)
{
+ unsigned n;
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;
- 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++;
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;
+ unsigned n;
+ uint32_t port = OPT_UINT32_VAL(PORT);
+
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,