server: Implement --listen-address for control service.
[paraslash.git] / net.c
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).
- * \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) {
@@ -526,6 +537,22 @@ int para_listen_simple(unsigned l4type, uint16_t port)
        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.