+ if(multicast(res->ai_addr)) {
+ /* Multicasting */
+ switch(res->ai_family) {
+ case PF_INET: {
+ const int mttl = config->multicast_ttl;
+ if(setsockopt(bfd, IPPROTO_IP, IP_MULTICAST_TTL, &mttl, sizeof mttl) < 0)
+ fatal(errno, "error setting IP_MULTICAST_TTL on multicast socket");
+ if(setsockopt(bfd, IPPROTO_IP, IP_MULTICAST_LOOP,
+ &config->multicast_loop, sizeof one) < 0)
+ fatal(errno, "error setting IP_MULTICAST_LOOP on multicast socket");
+ break;
+ }
+ case PF_INET6: {
+ const int mttl = config->multicast_ttl;
+ if(setsockopt(bfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
+ &mttl, sizeof mttl) < 0)
+ fatal(errno, "error setting IPV6_MULTICAST_HOPS on multicast socket");
+ if(setsockopt(bfd, IPPROTO_IP, IPV6_MULTICAST_LOOP,
+ &config->multicast_loop, sizeof (int)) < 0)
+ fatal(errno, "error setting IPV6_MULTICAST_LOOP on multicast socket");
+ break;
+ }
+ default:
+ fatal(0, "unsupported address family %d", res->ai_family);
+ }
+ info("multicasting on %s", sockname);
+ } else {
+ struct ifaddrs *ifs;
+
+ if(getifaddrs(&ifs) < 0)
+ fatal(errno, "error calling getifaddrs");
+ while(ifs) {
+ /* (At least on Darwin) IFF_BROADCAST might be set but ifa_broadaddr
+ * still a null pointer. It turns out that there's a subsequent entry
+ * for he same interface which _does_ have ifa_broadaddr though... */
+ if((ifs->ifa_flags & IFF_BROADCAST)
+ && ifs->ifa_broadaddr
+ && sockaddr_equal(ifs->ifa_broadaddr, res->ai_addr))
+ break;
+ ifs = ifs->ifa_next;
+ }
+ if(ifs) {
+ if(setsockopt(bfd, SOL_SOCKET, SO_BROADCAST, &one, sizeof one) < 0)
+ fatal(errno, "error setting SO_BROADCAST on broadcast socket");
+ info("broadcasting on %s (%s)", sockname, ifs->ifa_name);
+ } else
+ info("unicasting on %s", sockname);
+ }