+/*
+ * Perform AF-independent joining of multicast receive addresses.
+ *
+ * \param fd Bound socket descriptor.
+ * \param iface The receiving multicast interface, or NULL for the default.
+ *
+ * \return Zero if okay, negative on error.
+ */
+static int mcast_receiver_setup(int fd, const char *iface)
+{
+ struct sockaddr_storage ss;
+ socklen_t sslen = sizeof(ss);
+ int id = iface == NULL ? 0 : if_nametoindex(iface);
+
+ if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
+ goto err;
+
+ if (iface != NULL && id == 0)
+ PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
+
+ switch (ss.ss_family) {
+ case AF_INET:
+ if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
+#ifdef HAVE_IP_MREQN
+ struct ip_mreqn m4;
+
+ m4.imr_address.s_addr = INADDR_ANY;
+ m4.imr_ifindex = id;
+#else
+ struct ip_mreq m4;
+
+ m4.imr_interface.s_addr = INADDR_ANY;
+ if (id != 0)
+ PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
+
+#endif
+ m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
+
+ if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
+ break;
+ }
+ return 0;
+ case AF_INET6:
+ if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
+ struct ipv6_mreq m6;
+
+ memset(&m6, 0, sizeof(m6));
+ memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
+ m6.ipv6mr_interface = id;
+ if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
+ break;
+ }
+ return 0;
+ default:
+ PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
+ return -E_ADDRESS_LOOKUP;
+ }
+err:
+ return -ERRNO_TO_PARA_ERROR(errno);
+}
+