]> git.meshlink.io Git - catta/commitdiff
handle architectures that miss "struct ip_mreq" better
authorLennart Poettering <lennart@poettering.net>
Thu, 20 Oct 2005 23:06:59 +0000 (23:06 +0000)
committerLennart Poettering <lennart@poettering.net>
Thu, 20 Oct 2005 23:06:59 +0000 (23:06 +0000)
git-svn-id: file:///home/lennart/svn/public/avahi/trunk@832 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe

avahi-core/iface.c
avahi-core/socket.c
avahi-core/socket.h

index 39d26c3a72d53779de41e2a1fcd28d04093e194e..7afd2513f472d9a10e9a9fc691c336ca287c71a3 100644 (file)
@@ -27,6 +27,7 @@
 #include <sys/socket.h>
 #include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include <avahi-common/error.h>
 #include <avahi-common/malloc.h>
@@ -330,6 +331,30 @@ AvahiInterfaceAddress *avahi_interface_address_new(AvahiInterfaceMonitor *m, Ava
     return a;
 }
 
+static int interface_mdns_mcast_join(AvahiInterface *i, int join) {
+
+    if (i->protocol == AVAHI_PROTO_INET6)
+        return avahi_mdns_mcast_join_ipv6(i->monitor->server->fd_ipv6, i->hardware->index, join);
+    else if (i->protocol == AVAHI_PROTO_INET) {
+
+#ifdef HAVE_STRUCT_IP_MREQN
+        return avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, i->hardware->index, join);
+
+#else
+        AvahiInterfaceAddress *ia;
+        int r = 0;
+
+        for (ia = i->addresses; ia; ia = ia->address_next)
+            r  |= avahi_mdns_mcast_join_ipv4(i->monitor->server->fd_ipv4, &ia->address, join);
+
+        return r;
+#endif
+    }
+
+    abort();
+}
+
+
 void avahi_interface_check_relevant(AvahiInterface *i) {
     int b;
     AvahiInterfaceMonitor *m;
@@ -342,10 +367,7 @@ void avahi_interface_check_relevant(AvahiInterface *i) {
     if (m->list_complete && b && !i->announcing) {
         avahi_log_info("New relevant interface %s.%s.", i->hardware->name, avahi_proto_to_string(i->protocol));
 
-        if (i->protocol == AVAHI_PROTO_INET)
-            avahi_mdns_mcast_join_ipv4(m->server->fd_ipv4, i->hardware->index);
-        if (i->protocol == AVAHI_PROTO_INET6)
-            avahi_mdns_mcast_join_ipv6(m->server->fd_ipv6, i->hardware->index);
+        interface_mdns_mcast_join(i, 1);
 
         i->announcing = 1;
         avahi_announce_interface(m->server, i);
@@ -353,10 +375,7 @@ void avahi_interface_check_relevant(AvahiInterface *i) {
     } else if (!b && i->announcing) {
         avahi_log_info("Interface %s.%s no longer relevant.", i->hardware->name, avahi_proto_to_string(i->protocol));
 
-        if (i->protocol == AVAHI_PROTO_INET)
-            avahi_mdns_mcast_leave_ipv4(m->server->fd_ipv4, i->hardware->index);
-        if (i->protocol == AVAHI_PROTO_INET6)
-            avahi_mdns_mcast_leave_ipv6(m->server->fd_ipv6, i->hardware->index);
+        interface_mdns_mcast_join(i, 0);
 
         avahi_goodbye_interface(m->server, i, 0, 1);
         avahi_querier_free_all(i);
index a91a7403ee1aec1b2844b5ca9a6e864b22e75489..d02925aaff9c343262af839027c22446b90a092b 100644 (file)
@@ -99,103 +99,37 @@ static void ipv6_address_to_sockaddr(struct sockaddr_in6 *ret_sa, const AvahiIPv
     memcpy(&ret_sa->sin6_addr, a, sizeof(AvahiIPv6Address));
 }
 
-int avahi_mdns_mcast_join_ipv4(int fd, int idx) {
-/*  FIXME: sebest : we must factorize this part of the code */
-    struct sockaddr_in sa;
 #ifdef HAVE_STRUCT_IP_MREQN
-    struct ip_mreqn mreq; 
-    memset(&mreq, 0, sizeof(mreq));
-    mreq.imr_ifindex = idx;
+int avahi_mdns_mcast_join_ipv4(int fd, int idx, int join) {
+    struct ip_mreqn mreq;
 #else
-    struct ip_mreq mreq; 
-    struct ifreq ifreq;
-
-    memset(&mreq, 0, sizeof(mreq));
-
-    if ((!if_indextoname(idx, ifreq.ifr_name))) {
-      avahi_log_warn("if_indextoname failed: %s", strerror(errno));
-      return -1;
-    }
-
-    if (ioctl(fd, SIOCGIFADDR, &ifreq) < 0) {
-      avahi_log_warn("SIOCGIFADDR failed: %s", strerror(errno));
-      return -1;
-    }
-
-    memcpy(&mreq.imr_interface,
-          &((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr,
-          sizeof(struct in_addr));
+int avahi_mdns_mcast_join_ipv4(int fd, const AvahiAddress *a, int join) {
+    struct ip_mreq mreq;
 #endif
-    mdns_mcast_group_ipv4 (&sa);
-    mreq.imr_multiaddr = sa.sin_addr;
-
-    if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
-        avahi_log_warn("IP_ADD_MEMBERSHIP failed: %s", strerror(errno));
-        return -1;
-    } 
-
-    return 0;
-}
-
-int avahi_mdns_mcast_join_ipv6(int fd, int idx) {
-    struct ipv6_mreq mreq6; 
-    struct sockaddr_in6 sa6;
-
-    mdns_mcast_group_ipv6 (&sa6);
-
-    memset(&mreq6, 0, sizeof(mreq6));
-    mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
-    mreq6.ipv6mr_interface = idx;
-
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
-        avahi_log_warn("IPV6_ADD_MEMBERSHIP failed: %s", strerror(errno));
-        return -1;
-    }
-
-    return 0;
-}
-
-int avahi_mdns_mcast_leave_ipv4(int fd, int idx) {
-/*  FIXME: sebest : we must factorize this part of the code */
+        
     struct sockaddr_in sa;
-#ifdef HAVE_STRUCT_IP_MREQN
-    struct ip_mreqn mreq; 
     memset(&mreq, 0, sizeof(mreq));
+
+#ifdef HAVE_STRUCT_IP_MREQN
     mreq.imr_ifindex = idx;
 #else
-    struct ip_mreq mreq; 
-    struct ifreq ifreq;
-
-    memset(&mreq, 0, sizeof(mreq));
-
-    if ((!if_indextoname(idx, ifreq.ifr_name))) {
-      avahi_log_warn("if_indextoname failed: %s", strerror(errno));
-      return -1;
-    }
-
-    if (ioctl(fd, SIOCGIFADDR, &ifreq) < 0) {
-      avahi_log_warn("SIOCGIFADDR failed: %s", strerror(errno));
-      return -1;
-    }
-
-    memcpy(&mreq.imr_interface,
-          &((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr,
-          sizeof(struct in_addr));
+    assert(a);
+    assert(a->proto == AVAHI_PROTO_INET);
+    mreq.imr_interface.s_addr = htonl(a->data.ipv4.address);
 #endif
-    mdns_mcast_group_ipv4 (&sa);
+    
+    mdns_mcast_group_ipv4(&sa);
     mreq.imr_multiaddr = sa.sin_addr;
 
-    if (setsockopt(fd, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
-        avahi_log_warn("IP_DROP_MEMBERSHIP failed: %s", strerror(errno));
+    if (setsockopt(fd, IPPROTO_IP, join ? IP_ADD_MEMBERSHIP : IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)) < 0) {
+        avahi_log_warn("%s failed: %s", join ? "IP_ADD_MEMBERSHIP" : "IP_DROP_MEMBERSHIP", strerror(errno));
         return -1;
-    }
+    } 
 
     return 0;
 }
 
-int avahi_mdns_mcast_leave_ipv6(int fd, int idx) {
+int avahi_mdns_mcast_join_ipv6(int fd, int idx, int join) {
     struct ipv6_mreq mreq6; 
     struct sockaddr_in6 sa6;
 
@@ -205,8 +139,8 @@ int avahi_mdns_mcast_leave_ipv6(int fd, int idx) {
     mreq6.ipv6mr_multiaddr = sa6.sin6_addr;
     mreq6.ipv6mr_interface = idx;
 
-    if (setsockopt(fd, IPPROTO_IPV6, IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
-        avahi_log_warn("IPV6_DROP_MEMBERSHIP failed: %s", strerror(errno));
+    if (setsockopt(fd, IPPROTO_IPV6, join ? IPV6_ADD_MEMBERSHIP : IPV6_DROP_MEMBERSHIP, &mreq6, sizeof(mreq6)) < 0) {
+        avahi_log_warn("%s failed: %s", join ? "IPV6_ADD_MEMBERSHIP" : "IPV6_DROP_MEMBERSHIP", strerror(errno));
         return -1;
     }
 
index caf70666c33c7d8cac24f636abcb1bbf33cad55a..02990b70313b49dc24240d635ada7a5537aab85a 100644 (file)
@@ -44,10 +44,12 @@ int avahi_send_dns_packet_ipv6(int fd, int iface, AvahiDnsPacket *p, const Avahi
 AvahiDnsPacket *avahi_recv_dns_packet_ipv4(int fd, struct sockaddr_in*ret_sa, AvahiIPv4Address *ret_dest_address, int *ret_iface, uint8_t *ret_ttl);
 AvahiDnsPacket *avahi_recv_dns_packet_ipv6(int fd, struct sockaddr_in6*ret_sa, AvahiIPv6Address *ret_dest_address, int *ret_iface, uint8_t *ret_ttl);
 
-int avahi_mdns_mcast_join_ipv4(int fd, int idx);
-int avahi_mdns_mcast_join_ipv6(int fd, int idx);
+#ifdef HAVE_STRUCT_IP_MREQN
+int avahi_mdns_mcast_join_ipv4(int fd, int idx, int join);
+#else
+int avahi_mdns_mcast_join_ipv4(int fd, const AvahiAddress *a, int join);
+#endif
 
-int avahi_mdns_mcast_leave_ipv4(int fd, int idx);
-int avahi_mdns_mcast_leave_ipv6(int fd, int idx);
+int avahi_mdns_mcast_join_ipv6(int fd, int idx, int join);
 
 #endif