Merge branch 'master' into next
[paraslash.git] / udp_recv.c
index ccd769b..f978298 100644 (file)
@@ -5,12 +5,14 @@
  */
 /** \file udp_recv.c Paraslash's udp receiver */
 
  */
 /** \file udp_recv.c Paraslash's udp receiver */
 
+#include <regex.h>
 #include <dirent.h>
 #include <dirent.h>
+#include <sys/socket.h>
+#include <net/if.h>
 
 #include "para.h"
 #include "error.h"
 #include "portable_io.h"
 
 #include "para.h"
 #include "error.h"
 #include "portable_io.h"
-#include "udp_header.h"
 #include "list.h"
 #include "sched.h"
 #include "ggo.h"
 #include "list.h"
 #include "sched.h"
 #include "ggo.h"
 
 /** The size of the receiver node buffer. */
 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
 
 /** The size of the receiver node buffer. */
 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
-
 /**
  * Data specific to the udp receiver.
  *
  * \sa \ref receiver, \ref receiver_node.
  */
 struct private_udp_recv_data {
 /**
  * Data specific to the udp receiver.
  *
  * \sa \ref receiver, \ref receiver_node.
  */
 struct private_udp_recv_data {
-       /**
-        * Whether a header was received.
-        *
-        * A flag indicating whether this receiver already received a packet
-        * which contains the audio file header.
-        *
-        * This flag has no effect if the audio stream indicates that no extra
-        * headers will be sent (mp3, aac).  Otherwise, all data packets are
-        * dropped until the header is received.
-        */
-       int have_header;
        /** The socket file descriptor. */
        int fd;
        /** The socket file descriptor. */
        int fd;
-       /** Non-zero on short reads. */
-       uint16_t need_more;
-       /** Copied from the first audio header received. */
-       uint16_t stream_type;
 };
 
 static void udp_recv_pre_select(struct sched *s, struct task *t)
 };
 
 static void udp_recv_pre_select(struct sched *s, struct task *t)
@@ -62,53 +48,6 @@ static int enough_space(size_t nbytes, size_t loaded)
        return nbytes + loaded < UDP_RECV_CHUNK_SIZE;
 }
 
        return nbytes + loaded < UDP_RECV_CHUNK_SIZE;
 }
 
-/*
- * Perform some sanity checks on an udp audio file header.
- *
- * return: negative on error, 0: discard data, 1: use data
- */
-static int examine_audio_header(struct private_udp_recv_data *purd,
-               struct udp_audio_header *uah, size_t packet_size)
-{
-       /* payload_len includes header */
-       if (uah->payload_len < uah->header_len)
-               return -E_UDP_BAD_HEADER;
-       switch (uah->packet_type) {
-       case UDP_EOF_PACKET:
-               return -E_RECV_EOF;
-       case UDP_BOF_PACKET:
-               purd->have_header = 1;
-               /* fall through */
-       case UDP_DATA_PACKET:
-               if (uah->header_len) /* header in no-header packet */
-                       return -E_UDP_BAD_HEADER;
-               break;
-       case UDP_HEADER_PACKET:
-               if (!uah->header_len) /** no header in header packet */
-                       return -E_UDP_BAD_HEADER;
-               break;
-       default: /* bad packet type */
-               return -E_UDP_BAD_HEADER;
-       }
-       /* check stream type */
-       if (uah->stream_type != UDP_PLAIN_STREAM &&
-                       uah->stream_type != UDP_HEADER_STREAM)
-               return -E_UDP_BAD_STREAM_TYPE;
-       if (purd->stream_type == UDP_UNKNOWN_STREAM)
-               purd->stream_type = uah->stream_type;
-       /* stream type must not change */
-       if (uah->stream_type != purd->stream_type)
-               return -E_UDP_BAD_STREAM_TYPE;
-       if (!purd->have_header && uah->stream_type == UDP_HEADER_STREAM)
-               /* can't use the data, wait for header packet */
-               return 0;
-       if (packet_size < uah->payload_len + UDP_AUDIO_HEADER_LEN)
-               /* we read only a part of the package */
-               purd->need_more = uah->payload_len
-                       + UDP_AUDIO_HEADER_LEN - packet_size;
-       return 1;
-}
-
 static int add_rn_output(struct receiver_node *rn, char *buf, size_t len)
 {
        if (!len)
 static int add_rn_output(struct receiver_node *rn, char *buf, size_t len)
 {
        if (!len)
@@ -126,10 +65,7 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
        struct private_udp_recv_data *purd = rn->private_data;
        int ret;
        char tmpbuf[UDP_RECV_CHUNK_SIZE];
        struct private_udp_recv_data *purd = rn->private_data;
        int ret;
        char tmpbuf[UDP_RECV_CHUNK_SIZE];
-       uint16_t data_len;
-       char *data_buf;
        size_t packet_size;
        size_t packet_size;
-       struct udp_audio_header uah;
 
        if (rn->output_error && *rn->output_error < 0) {
                t->error = *rn->output_error;
 
        if (rn->output_error && *rn->output_error < 0) {
                t->error = *rn->output_error;
@@ -148,43 +84,12 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
        if (!ret)
                return;
        packet_size = ret;
        if (!ret)
                return;
        packet_size = ret;
-       for (;;) {
-               uint16_t num;
-
-               if (!purd->need_more) {
-                       ret = read_udp_audio_header(tmpbuf, packet_size, &uah);
-                       if (ret >= 0)
-                               break;
-                       goto success; /* drop data */
-               }
-               num = PARA_MIN(purd->need_more, (uint16_t)packet_size);
-               assert(num > 0);
-               t->error = add_rn_output(rn, tmpbuf, num);
-               if (t->error < 0)
+       if (packet_size >= FEC_EOF_PACKET_LEN)
+               if (!memcmp(tmpbuf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN))
                        return;
                        return;
-               purd->need_more -= num;
-               if (packet_size <= num)
-                       goto success;
-               packet_size -= num;
-               memmove(tmpbuf, tmpbuf + num, packet_size);
-       }
-       assert(!purd->need_more);
-       t->error = examine_audio_header(purd, &uah, packet_size);
-       if (t->error <= 0)
+       t->error = add_rn_output(rn, tmpbuf, packet_size);
+       if (t->error < 0)
                return;
                return;
-       data_len = uah.payload_len;
-       data_buf = tmpbuf + UDP_AUDIO_HEADER_LEN;
-       if (uah.packet_type == UDP_HEADER_PACKET) {
-               if (purd->have_header) { /* skip header */
-                       data_buf += uah.header_len;
-                       data_len -= uah.header_len;
-               } else { /* only use the header */
-                       purd->have_header = 1;
-                       data_len = uah.header_len;
-               }
-       }
-       t->error = add_rn_output(rn, data_buf, data_len);
-       return;
 success:
        t->error = 1;
 }
 success:
        t->error = 1;
 }
@@ -221,24 +126,40 @@ static void *udp_recv_parse_config(int argc, char **argv)
  * Perform AF-independent joining of multicast receive addresses.
  *
  * \param fd   Bound socket descriptor.
  * 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.
  */
  *
  * \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);
 {
        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 (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))) {
        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;
 
                        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;
                }
                        if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
                                break;
                }
@@ -249,6 +170,7 @@ static int mcast_receiver_setup(int fd)
 
                        memset(&m6, 0, sizeof(m6));
                        memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
 
                        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;
                }
                        if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
                                break;
                }
@@ -265,6 +187,7 @@ static int udp_recv_open(struct receiver_node *rn)
 {
        struct private_udp_recv_data *purd;
        struct udp_recv_args_info *c = rn->conf;
 {
        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);
        int ret;
 
        rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
@@ -276,7 +199,7 @@ static int udp_recv_open(struct receiver_node *rn)
                goto err;
        purd->fd = ret;
 
                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;
        if (ret < 0) {
                close(purd->fd);
                return ret;
@@ -285,7 +208,6 @@ static int udp_recv_open(struct receiver_node *rn)
        ret = mark_fd_nonblocking(purd->fd);
        if (ret < 0)
                goto err;
        ret = mark_fd_nonblocking(purd->fd);
        if (ret < 0)
                goto err;
-       purd->stream_type = UDP_UNKNOWN_STREAM;
        PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
                c->port_arg, purd->fd);
        return purd->fd;
        PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
                c->port_arg, purd->fd);
        return purd->fd;
@@ -317,4 +239,5 @@ void udp_recv_init(struct receiver *r)
                .short_help = udp_recv_args_info_help,
                .detailed_help = udp_recv_args_info_detailed_help
        };
                .short_help = udp_recv_args_info_help,
                .detailed_help = udp_recv_args_info_detailed_help
        };
+       udp_recv_cmdline_parser_free(&dummy);
 }
 }