CPPFLAGS="$OLD_CPPFLAGS"
LDFLAGS="$OLD_LDFLAGS"
LIBS="$OLD_LIBS"
+
+########################################################################### ip_mreqn
+AC_MSG_CHECKING(for struct ip_mreqn (UDPv4 multicast))
+AC_TRY_LINK([
+ #include <netdb.h>
+ #include <net/if.h>
+],[
+ struct ip_mreqn mn;
+ mn.imr_ifindex = 0;
+],[have_ip_mreqn=yes],[have_ip_mreqn=no])
+AC_MSG_RESULT($have_ip_mreqn)
+if test ${have_ip_mreqn} = yes; then
+ AC_DEFINE(HAVE_IP_MREQN, 1, define to 1 you have struct ip_mreqn)
+fi
########################################################################### osx
AC_MSG_CHECKING(for CoreAudio (MacOs))
default="8000"
optional
+option "udp_mcast_iface" -
+#~~~~~~~~~~~~~~~~~~~~~~~~~~
+"outgoing udp multicast interface"
+string
+optional
+
option "udp_header_interval" H
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
"duration for sending header"
int typestr="portnumber"
default="8000"
optional
+
+option "iface" I "receiving udp multicast interface"
+string
+optional
*/
/** \file udp_header.h some macros used by udp_send.c and udp_recv.c. */
+#include <net/if.h>
/**
* Number of bytes of the paraslash udp header.
* 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)
+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;
- memset(&m4, 0, sizeof(m4));
- m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
+ 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;
}
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;
}
{
struct private_udp_recv_data *purd;
struct udp_recv_args_info *c = rn->conf;
+ char *iface = c->iface_given ? c->iface_arg : NULL;
int ret;
rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
goto err;
purd->fd = ret;
- ret = mcast_receiver_setup(purd->fd);
+ ret = mcast_receiver_setup(purd->fd, iface);
if (ret < 0) {
close(purd->fd);
return ret;
* \param fd The connected socket descriptor.
* \param ttl UDPv4 multicast TTL or UDPv6 multicast number of hops.
* Use -1 to mean default, 0..255 otherwise.
-
- ** \return Zero if okay, negative on error.
+ * \param iface The outgoing multicast interface, or NULL for the default.
+ *
+ * \return Zero if okay, negative on error.
*/
-static int mcast_sender_setup(struct udp_target *ut, int ttl)
+static int mcast_sender_setup(struct udp_target *ut, int ttl, char *iface)
{
struct sockaddr_storage ss;
socklen_t sslen = sizeof(ss);
const int on = 1;
+ int id = iface == NULL ? 0 : if_nametoindex(iface);
if (getpeername(ut->fd, (struct sockaddr *)&ss, &sslen) < 0)
goto err;
+ if (iface != NULL && id == 0)
+ PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
+
/* RFC 3493, 5.2: -1 means 'use kernel default' */
if (ttl < 0 || ttl > 255)
ttl = -1;
case AF_INET:
if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
return 0;
+ if (id != 0) {
+#ifdef HAVE_IP_MREQN
+ struct ip_mreqn mn;
+
+ memset(&mn, 0, sizeof(mn));
+ mn.imr_ifindex = id;
+ if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
+ goto err;
+#else
+ PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
+#endif
+ }
/*
* Enable receiving multicast messages generated on the local host
* At least on Linux, this is enabled by default.
case AF_INET6:
if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
return 0;
+ if (id != 0 &&
+ setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
+ break;
if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
break;
if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
static int udp_init_session(struct udp_target *ut)
{
int ret;
+ char *iface = NULL;
if (ut->fd >= 0) /* nothing to do */
return 0;
return ret;
ut->fd = ret;
- ret = mcast_sender_setup(ut, conf.udp_ttl_arg);
+ if (conf.udp_mcast_iface_given)
+ iface = conf.udp_mcast_iface_arg;
+
+ ret = mcast_sender_setup(ut, conf.udp_ttl_arg, iface);
if (ret < 0) {
close(ut->fd);
return ret;