From: Lennart Poettering Date: Thu, 20 Oct 2005 23:06:59 +0000 (+0000) Subject: handle architectures that miss "struct ip_mreq" better X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=8084854ef36a50506bd59639816a30130bd3477e;p=catta handle architectures that miss "struct ip_mreq" better git-svn-id: file:///home/lennart/svn/public/avahi/trunk@832 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-core/iface.c b/avahi-core/iface.c index 39d26c3..7afd251 100644 --- a/avahi-core/iface.c +++ b/avahi-core/iface.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -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); diff --git a/avahi-core/socket.c b/avahi-core/socket.c index a91a740..d02925a 100644 --- a/avahi-core/socket.c +++ b/avahi-core/socket.c @@ -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; } diff --git a/avahi-core/socket.h b/avahi-core/socket.h index caf7066..02990b7 100644 --- a/avahi-core/socket.h +++ b/avahi-core/socket.h @@ -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