[sheepdog] [PATCH] Add command line option for binding to a specified interface.

Bryan D. Payne bdpayne at acm.org
Thu Sep 27 15:35:24 CEST 2012


Currently sheep will try to bind to all interfaces.  This patch
provides a command line option allowing you to specify the ip address
of the interface where you could like sheep to bind.  If you don't
provide this option, sheep continues binding to all interfaces as
usual.


Signed-off-by: Bryan D. Payne <bdpayne at acm.org>
---
 include/net.h      |    3 ++-
 lib/net.c          |    5 +++--
 sheep/request.c    |    4 ++--
 sheep/sheep.c      |   23 +++++++++++++++++++++--
 sheep/sheep_priv.h |    2 +-
 5 files changed, 29 insertions(+), 8 deletions(-)

diff --git a/include/net.h b/include/net.h
index 15ee703..4e1e7f6 100644
--- a/include/net.h
+++ b/include/net.h
@@ -44,7 +44,8 @@ int connect_to(const char *name, int port);
 int send_req(int sockfd, struct sd_req *hdr, void *data, unsigned int *wlen);
 int exec_req(int sockfd, struct sd_req *hdr, void *data,
             unsigned int *wlen, unsigned int *rlen);
-int create_listen_ports(int port, int (*callback)(int fd, void *), void *data);
+int create_listen_ports(char *bindaddr, int port,
+                       int (*callback)(int fd, void *), void *data);

 char *addr_to_str(char *str, int size, uint8_t *addr, uint16_t port);
 uint8_t *str_to_addr(int af, const char *ipstr, uint8_t *addr);
diff --git a/lib/net.c b/lib/net.c
index 70954f0..70bb0c3 100644
--- a/lib/net.c
+++ b/lib/net.c
@@ -113,7 +113,8 @@ notrace int tx(struct connection *conn, enum
conn_state next_state, int flags)
        return ret;
 }

-int create_listen_ports(int port, int (*callback)(int fd, void *), void *data)
+int create_listen_ports(char *bindaddr, int port,
+               int (*callback)(int fd, void *), void *data)
 {
        char servname[64];
        int fd, ret, opt;
@@ -127,7 +128,7 @@ int create_listen_ports(int port, int
(*callback)(int fd, void *), void *data)
        hints.ai_socktype = SOCK_STREAM;
        hints.ai_flags = AI_PASSIVE;

-       ret = getaddrinfo(NULL, servname, &hints, &res0);
+       ret = getaddrinfo(bindaddr, servname, &hints, &res0);
        if (ret) {
                eprintf("failed to get address info: %m\n");
                return 1;
diff --git a/sheep/request.c b/sheep/request.c
index 0923b8c..40a01d5 100644
--- a/sheep/request.c
+++ b/sheep/request.c
@@ -876,9 +876,9 @@ static int create_listen_port_fn(int fd, void *data)
        return register_event(fd, listen_handler, data);
 }

-int create_listen_port(int port, void *data)
+int create_listen_port(char *bindaddr, int port, void *data)
 {
-       return create_listen_ports(port, create_listen_port_fn, data);
+       return create_listen_ports(bindaddr, port, create_listen_port_fn, data);
 }

 static void req_handler(int listen_fd, int events, void *data)
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 7fa8249..6c42a83 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -26,6 +26,7 @@
 #include <sys/eventfd.h>
 #include <fcntl.h>
 #include <errno.h>
+#include <arpa/inet.h>

 #include "sheep_priv.h"
 #include "trace/trace.h"
@@ -38,6 +39,7 @@ LIST_HEAD(cluster_drivers);
 static char program_name[] = "sheep";

 static struct option const long_options[] = {
+       {"bindaddr", required_argument, NULL, 'b'},
        {"cluster", required_argument, NULL, 'c'},
        {"debug", no_argument, NULL, 'd'},
        {"foreground", no_argument, NULL, 'f'},
@@ -55,7 +57,7 @@ static struct option const long_options[] = {
        {NULL, 0, NULL, 0},
 };

-static const char *short_options = "c:dDfghjl:op:P:s:w:y:z:";
+static const char *short_options = "b:c:dDfghjl:op:P:s:w:y:z:";

 static void usage(int status)
 {
@@ -67,6 +69,7 @@ static void usage(int status)
 Sheepdog daemon (version %s)\n\
 Usage: %s [OPTION]... [PATH]\n\
 Options:\n\
+  -b, --bindaddr          specify IP address of interface to listen on\n\
   -c, --cluster           specify the cluster driver\n\
   -d, --debug             include debug messages in the log\n\
   -f, --foreground        make the program run in the foreground\n\
@@ -322,6 +325,10 @@ int main(int argc, char **argv)
        char *p;
        struct cluster_driver *cdrv;
        char *pid_file = NULL;
+       char *bindaddr = NULL;
+       unsigned char buf[sizeof(struct in6_addr)];
+       int ipv4 = 0;
+       int ipv6 = 0;

        signal(SIGPIPE, SIG_IGN);

@@ -415,6 +422,18 @@ int main(int argc, char **argv)
                case 'j':
                        sys->use_journal = true;
                        break;
+               case 'b':
+                       /* validate provided address using inet_pton */
+                       ipv4 = inet_pton(AF_INET, optarg, buf);
+                       ipv6 = inet_pton(AF_INET6, optarg, buf);
+                       if (ipv4 || ipv6) {
+                               bindaddr = optarg;
+                       } else {
+                               fprintf(stderr,
+                                       "Invalid bind address '%s'\n", optarg);
+                               exit(1);
+                       }
+                       break;
                case 'h':
                        usage(0);
                        break;
@@ -454,7 +473,7 @@ int main(int argc, char **argv)
        if (ret)
                exit(1);

-       ret = create_listen_port(port, sys);
+       ret = create_listen_port(bindaddr, port, sys);
        if (ret)
                exit(1);

diff --git a/sheep/sheep_priv.h b/sheep/sheep_priv.h
index cad25ce..fcc4278 100644
--- a/sheep/sheep_priv.h
+++ b/sheep/sheep_priv.h
@@ -213,7 +213,7 @@ static inline uint32_t sys_epoch(void)
        return uatomic_read(&sys->epoch);
 }

-int create_listen_port(int port, void *data);
+int create_listen_port(char *bindaddr, int port, void *data);

 int init_store(const char *dir);
 int init_base_path(const char *dir);
--
1.7.5.4



More information about the sheepdog mailing list