]> git.meshlink.io Git - catta/blobdiff - avahi-core/socket.c
* Improve error message when /etc/avahi/services is not available
[catta] / avahi-core / socket.c
index 55db70408d823dd4dc52775fe27a986e5c2b48b6..b635dca467bab68d22548c0c47631195ab3ab6eb 100644 (file)
@@ -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) {
@@ -425,7 +452,7 @@ AvahiDnsPacket* avahi_recv_dns_packet_ipv4(gint fd, struct sockaddr_in *ret_sa,
     ssize_t l;
     struct cmsghdr *cmsg;
     gboolean found_ttl = FALSE, found_iface = FALSE;
-    guint ms;
+    gint ms;
 
     g_assert(fd >= 0);
     g_assert(ret_sa);
@@ -509,7 +536,7 @@ AvahiDnsPacket* avahi_recv_dns_packet_ipv6(gint fd, struct sockaddr_in6 *ret_sa,
     struct iovec io;
     uint8_t aux[64];
     ssize_t l;
-    guint ms;
+    gint ms;
     
     struct cmsghdr *cmsg;
     gboolean found_ttl = FALSE, found_iface = FALSE;