From ad7b145ebe82c3a53632eaa6ec75b014d91a7bea Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Sun, 19 Jun 2005 13:44:50 +0000 Subject: * detect other running mDNS stacks * replace more g_message()/g_warning() calls with avahi_log_xxx() * fix configuration file paths * only load configuration file when running daemon * require uid == 0 * fix static service reloading * add new command line option for reloading -r git-svn-id: file:///home/lennart/svn/public/avahi/trunk@129 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/socket.c | 67 +++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 20 deletions(-) (limited to 'avahi-core/socket.c') diff --git a/avahi-core/socket.c b/avahi-core/socket.c index 55db704..8f3d896 100644 --- a/avahi-core/socket.c +++ b/avahi-core/socket.c @@ -156,6 +156,50 @@ int avahi_mdns_mcast_leave_ipv6 (int index, int fd) { return 0; } +static gint bind_with_warn(int fd, const struct sockaddr *sa, socklen_t l) { + gint yes; + + g_assert(fd >= 0); + g_assert(sa); + g_assert(l > 0); + + if (bind(fd, sa, l) < 0) { + + if (errno != EADDRINUSE) { + avahi_log_warn("bind() failed: %s\n", strerror(errno)); + return -1; + } + + avahi_log_warn("*** WARNING: Detected another %s mDNS stack running on this host. This makes mDNS unreliable and is thus not recommended. ***", + sa->sa_family == AF_INET ? "IPv4" : "IPv6"); + + /* 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)); + return -1; + } + + if (bind(fd, sa, l) < 0) { + avahi_log_warn("bind() failed: %s\n", strerror(errno)); + return -1; + } + } else { + + /* We enable SO_REUSEADDR afterwards, to make sure that the + * user may run other mDNS implementations if he really + * wants. */ + + yes = 1; + if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { + avahi_log_warn("SO_REUSEADDR failed: %s\n", strerror(errno)); + return -1; + } + } + + return 0; +} + gint avahi_open_socket_ipv4(void) { struct sockaddr_in local; int fd = -1, ttl, yes; @@ -177,27 +221,18 @@ gint avahi_open_socket_ipv4(void) { goto fail; } - yes = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { - avahi_log_warn("SO_REUSEADDR failed: %s\n", 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)); goto fail; } - memset(&local, 0, sizeof(local)); local.sin_family = AF_INET; local.sin_port = htons(AVAHI_MDNS_PORT); - - if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + + if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0) goto fail; - } yes = 1; if (setsockopt(fd, SOL_IP, IP_RECVTTL, &yes, sizeof(yes)) < 0) { @@ -253,12 +288,6 @@ gint avahi_open_socket_ipv6(void) { goto fail; } - yes = 1; - if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0) { - avahi_log_warn("SO_REUSEADDR failed: %s\n", 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)); @@ -275,10 +304,8 @@ gint avahi_open_socket_ipv6(void) { local.sin6_family = AF_INET6; local.sin6_port = htons(AVAHI_MDNS_PORT); - if (bind(fd, (struct sockaddr*) &local, sizeof(local)) < 0) { - avahi_log_warn("bind() failed: %s\n", strerror(errno)); + if (bind_with_warn(fd, (struct sockaddr*) &local, sizeof(local)) < 0) goto fail; - } yes = 1; if (setsockopt(fd, SOL_IPV6, IPV6_HOPLIMIT, &yes, sizeof(yes)) < 0) { -- cgit