#include <sys/socket.h>
#include <errno.h>
#include <stdio.h>
+#include <stdlib.h>
#include <avahi-common/error.h>
#include <avahi-common/malloc.h>
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;
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);
} 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);
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;
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;
}
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