From c2662cc5d17fcb54ec6d04dd81a26bb427c55c74 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 28 Sep 2005 23:49:09 +0000 Subject: [PATCH] * add option to not enable SO_REUSEADDR for multicast sockets, effectively disallow multiple mDNS stacks running simultaneously * add a config file option for this and enable it by default * remove some bad \n in log messages git-svn-id: file:///home/lennart/svn/public/avahi/trunk@646 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/core.h | 1 + avahi-core/server.c | 5 +- avahi-core/socket.c | 98 +++++++++++++++++++--------------- avahi-core/socket.h | 4 +- avahi-daemon/avahi-daemon.conf | 1 + avahi-daemon/main.c | 3 ++ docs/TODO | 3 +- 7 files changed, 65 insertions(+), 50 deletions(-) diff --git a/avahi-core/core.h b/avahi-core/core.h index 705fa31..4883c6b 100644 --- a/avahi-core/core.h +++ b/avahi-core/core.h @@ -80,6 +80,7 @@ typedef struct AvahiServerConfig { int enable_wide_area; /**< Enable wide area support */ AvahiAddress wide_area_servers[AVAHI_MAX_WIDE_AREA_SERVERS]; /** Unicast DNS server to use for wide area lookup */ unsigned n_wide_area_servers; /**< Number of servers in wide_area_servers[] */ + int disallow_other_stacks; /**< Make sure that only one mDNS responder is run at the same time on the local machine. If this is enable Avahi will not set SO_REUSADDR on its sockets, effectively preventing other stacks from running on the local machine */ } AvahiServerConfig; /** Allocate a new mDNS responder object. */ diff --git a/avahi-core/server.c b/avahi-core/server.c index c4545c6..6b7571e 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -1324,8 +1324,8 @@ static int valid_server_config(const AvahiServerConfig *sc) { static int setup_sockets(AvahiServer *s) { assert(s); - s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4() : -1; - s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6() : -1; + s->fd_ipv4 = s->config.use_ipv4 ? avahi_open_socket_ipv4(s->config.disallow_other_stacks) : -1; + s->fd_ipv6 = s->config.use_ipv6 ? avahi_open_socket_ipv6(s->config.disallow_other_stacks) : -1; if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) return AVAHI_ERR_NO_NETWORK; @@ -2446,6 +2446,7 @@ AvahiServerConfig* avahi_server_config_init(AvahiServerConfig *c) { c->add_service_cookie = 1; c->enable_wide_area = 0; c->n_wide_area_servers = 0; + c->disallow_other_stacks = 0; return c; } diff --git a/avahi-core/socket.c b/avahi-core/socket.c index ecc137b..0323b37 100644 --- a/avahi-core/socket.c +++ b/avahi-core/socket.c @@ -93,7 +93,7 @@ int avahi_mdns_mcast_join_ipv4(int fd, int idx) { mreq.imr_ifindex = idx; if (setsockopt(fd, SOL_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { - avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s\n", strerror(errno)); + avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s", strerror(errno)); return -1; } @@ -111,7 +111,7 @@ int avahi_mdns_mcast_join_ipv6(int fd, int idx) { mreq6.ipv6mr_interface = idx; if (setsockopt(fd, SOL_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { - avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s", strerror(errno)); return -1; } @@ -129,7 +129,7 @@ int avahi_mdns_mcast_leave_ipv4(int fd, int idx) { mreq.imr_ifindex = idx; if (setsockopt(fd, SOL_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) { - avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s\n", strerror(errno)); + avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s", strerror(errno)); return -1; } @@ -147,7 +147,7 @@ int avahi_mdns_mcast_leave_ipv6(int fd, int idx) { mreq6.ipv6mr_interface = idx; if (setsockopt(fd, SOL_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) { - avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s", strerror(errno)); return -1; } @@ -164,7 +164,7 @@ static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { if (bind(fd, sa, l) < 0) { if (errno != EADDRINUSE) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + avahi_log_warn("bind() failed: %s", strerror(errno)); return -1; } @@ -174,12 +174,12 @@ static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { /* Try again, this time with SO_REUSEADDR set */ yes = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { - avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno)); + avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno)); return -1; } if (bind(fd, sa, l) < 0) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + avahi_log_warn("bind() failed: %s", strerror(errno)); return -1; } } else { @@ -190,7 +190,7 @@ static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { yes = 1; if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { - avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno)); + avahi_log_warn("SO_REUSEADDR failed: %s", strerror(errno)); return -1; } } @@ -198,30 +198,30 @@ static int bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { return 0; } -int avahi_open_socket_ipv4(void) { +int avahi_open_socket_ipv4(int no_reuse) { struct sockaddr_in local; - int fd = -1, ttl, yes; + int fd = -1, ttl, yes, r; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - avahi_log_warn("socket() failed: %s\n", strerror(errno)); + avahi_log_warn("socket() failed: %s", strerror(errno)); goto fail; } ttl = 255; if (setsockopt(fd, SOL_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0) { - avahi_log_warn("IP_MULTICAST_TTL failed: %s\n", strerror(errno)); + avahi_log_warn("IP_MULTICAST_TTL failed: %s", strerror(errno)); goto fail; } ttl = 255; if (setsockopt(fd, SOL_IP, IP_TTL, &ttl, sizeof(ttl)) < 0) { - avahi_log_warn("IP_TTL failed: %s\n", strerror(errno)); + avahi_log_warn("IP_TTL failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IP_MULTICAST_LOOP failed: %s\n", strerror(errno)); + avahi_log_warn("IP_MULTICAST_LOOP failed: %s", strerror(errno)); goto fail; } @@ -229,28 +229,33 @@ int avahi_open_socket_ipv4(void) { local.sin_family = AF_INET; local.sin_port = htons(AVAHI_MDNS_PORT); - if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0) + if (no_reuse) + r = bind(fd, (struct sockaddr*) &local, sizeof(local)); + else + r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)); + + if (r < 0) goto fail; yes = 1; if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno)); + avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno)); + avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno)); goto fail; } if (avahi_set_cloexec(fd) < 0) { - avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno)); + avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno)); goto fail; } if (avahi_set_nonblock(fd) < 0) { - avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno)); + avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno)); goto fail; } @@ -263,38 +268,38 @@ fail: return -1; } -int avahi_open_socket_ipv6(void) { +int avahi_open_socket_ipv6(int no_reuse) { struct sockaddr_in6 sa, local; - int fd = -1, ttl, yes; + int fd = -1, ttl, yes, r; mdns_mcast_group_ipv6(&sa); if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { - avahi_log_warn("socket() failed: %s\n", strerror(errno)); + avahi_log_warn("socket() failed: %s", strerror(errno)); goto fail; } ttl = 255; if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0) { - avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_MULTICAST_HOPS failed: %s", strerror(errno)); goto fail; } ttl = 255; if (setsockopt(fd, SOL_IPV6, IPV6_UNICAST_HOPS, &ttl, sizeof(ttl)) < 0) { - avahi_log_warn("IPV6_UNICAST_HOPS failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_UNICAST_HOPS failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_V6ONLY, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IPV6_V6ONLY failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_V6ONLY failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_MULTICAST_LOOP failed: %s", strerror(errno)); goto fail; } @@ -302,28 +307,33 @@ int avahi_open_socket_ipv6(void) { local.sin6_family = AF_INET6; local.sin6_port = htons(AVAHI_MDNS_PORT); - if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0) + if (no_reuse) + r = bind(fd, (struct sockaddr*) &local, sizeof(local)); + else + r = bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)); + + if (r < 0) goto fail; yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno)); goto fail; } if (avahi_set_cloexec(fd) < 0) { - avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno)); + avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno)); goto fail; } if (avahi_set_nonblock(fd) < 0) { - avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno)); + avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno)); goto fail; } @@ -346,7 +356,7 @@ static int sendmsg_loop(int fd, struct msghdr *msg, int flags) { break; if (errno != EAGAIN) { - avahi_log_debug("sendmsg() failed: %s\n", strerror(errno)); + avahi_log_debug("sendmsg() failed: %s", strerror(errno)); return -1; } @@ -631,7 +641,7 @@ int avahi_open_unicast_socket_ipv4(void) { int fd = -1, yes; if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { - avahi_log_warn("socket() failed: %s\n", strerror(errno)); + avahi_log_warn("socket() failed: %s", strerror(errno)); goto fail; } @@ -639,29 +649,29 @@ int avahi_open_unicast_socket_ipv4(void) { local.sin_family = AF_INET; if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + avahi_log_warn("bind() failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IP_RECVTTL failed: %s\n", strerror(errno)); + avahi_log_warn("IP_RECVTTL failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IP, IP_PKTINFO, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IP_PKTINFO failed: %s\n", strerror(errno)); + avahi_log_warn("IP_PKTINFO failed: %s", strerror(errno)); goto fail; } if (avahi_set_cloexec(fd) < 0) { - avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno)); + avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno)); goto fail; } if (avahi_set_nonblock(fd) < 0) { - avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno)); + avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno)); goto fail; } @@ -679,7 +689,7 @@ int avahi_open_unicast_socket_ipv6(void) { int fd = -1, yes; if ((fd = socket(AF_INET6, SOCK_DGRAM, 0)) < 0) { - avahi_log_warn("socket() failed: %s\n", strerror(errno)); + avahi_log_warn("socket() failed: %s", strerror(errno)); goto fail; } @@ -687,29 +697,29 @@ int avahi_open_unicast_socket_ipv6(void) { local.sin6_family = AF_INET6; if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + avahi_log_warn("bind() failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IPV6_HOPLIMIT failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_HOPLIMIT failed: %s", strerror(errno)); goto fail; } yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_PKTINFO, &yes, sizeof(yes)) < 0) { - avahi_log_warn("IPV6_PKTINFO failed: %s\n", strerror(errno)); + avahi_log_warn("IPV6_PKTINFO failed: %s", strerror(errno)); goto fail; } if (avahi_set_cloexec(fd) < 0) { - avahi_log_warn("FD_CLOEXEC failed: %s\n", strerror(errno)); + avahi_log_warn("FD_CLOEXEC failed: %s", strerror(errno)); goto fail; } if (avahi_set_nonblock(fd) < 0) { - avahi_log_warn("O_NONBLOCK failed: %s\n", strerror(errno)); + avahi_log_warn("O_NONBLOCK failed: %s", strerror(errno)); goto fail; } diff --git a/avahi-core/socket.h b/avahi-core/socket.h index 971e7e8..caf7066 100644 --- a/avahi-core/socket.h +++ b/avahi-core/socket.h @@ -32,8 +32,8 @@ #define AVAHI_IPV4_MCAST_GROUP "224.0.0.251" #define AVAHI_IPV6_MCAST_GROUP "ff02::fb" -int avahi_open_socket_ipv4(void); -int avahi_open_socket_ipv6(void); +int avahi_open_socket_ipv4(int no_reuse); +int avahi_open_socket_ipv6(int no_reuse); int avahi_open_unicast_socket_ipv4(void); int avahi_open_unicast_socket_ipv6(void); diff --git a/avahi-daemon/avahi-daemon.conf b/avahi-daemon/avahi-daemon.conf index 4761baa..b561f06 100644 --- a/avahi-daemon/avahi-daemon.conf +++ b/avahi-daemon/avahi-daemon.conf @@ -7,6 +7,7 @@ check-response-ttl=no use-iff-running=no enable-dbus=yes add-service-cookie=yes +disallow-other-stacks=yes [wide-area] enable-wide-area=yes diff --git a/avahi-daemon/main.c b/avahi-daemon/main.c index 5320b68..07d7127 100644 --- a/avahi-daemon/main.c +++ b/avahi-daemon/main.c @@ -393,6 +393,8 @@ static int load_config_file(DaemonConfig *c) { c->server_config.check_response_ttl = is_yes(p->value); else if (strcasecmp(p->key, "use-iff-running") == 0) c->server_config.use_iff_running = is_yes(p->value); + else if (strcasecmp(p->key, "disallow-other-stacks") == 0) + c->server_config.disallow_other_stacks = is_yes(p->value); else if (strcasecmp(p->key, "enable-dbus") == 0) { if (*(p->value) == 'w' || *(p->value) == 'W') { @@ -871,6 +873,7 @@ int main(int argc, char *argv[]) { init_rand_seed(); avahi_server_config_init(&config.server_config); + config.server_config.disallow_other_stacks = 1; config.command = DAEMON_RUN; config.daemonize = 0; config.config_file = NULL; diff --git a/docs/TODO b/docs/TODO index ed55e5f..6a8bd50 100644 --- a/docs/TODO +++ b/docs/TODO @@ -11,8 +11,6 @@ for 0.6: * Add static host configuration like static services [lathiat] * wrap avahi_server_add_record() via DBUS and in avahi-client [lathiat] -* add option to disable SO_REUSEADDR to disallow binding of multiple processes to port 5353 - * add flags argument to disable cookies-setting for local services * reset commit throttling for entry groups after a while @@ -87,3 +85,4 @@ done: * allow resolving of services without name * add sever version check to avahi-client * Passive observation of failures +* add option to disable SO_REUSEADDR to disallow binding of multiple processes to port 5353 -- 2.39.5