Merge branch 'master' into next
[paraslash.git] / udp_recv.c
index b3ea83c..f978298 100644 (file)
@@ -5,17 +5,19 @@
  */
 /** \file udp_recv.c Paraslash's udp receiver */
 
+#include <regex.h>
 #include <dirent.h>
+#include <sys/socket.h>
+#include <net/if.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 "recv.h"
 #include "udp_recv.cmdline.h"
-#include "error.h"
 #include "audiod.h"
 #include "string.h"
 #include "net.h"
 
 /** 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 {
-       /**
-        * 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;
 };
@@ -53,9 +43,20 @@ static void udp_recv_pre_select(struct sched *s, struct task *t)
        para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
 }
 
-static int enough_space(size_t packet_size, size_t loaded)
+static int enough_space(size_t nbytes, size_t loaded)
 {
-       return packet_size + loaded < UDP_RECV_CHUNK_SIZE + UDP_AUDIO_HEADER_LEN;
+       return nbytes + loaded < UDP_RECV_CHUNK_SIZE;
+}
+
+static int add_rn_output(struct receiver_node *rn, char *buf, size_t len)
+{
+       if (!len)
+               return 1;
+       if (!enough_space(len, rn->loaded))
+               return -E_UDP_OVERRUN;
+       memcpy(rn->buf + rn->loaded, buf, len);
+       rn->loaded += len;
+       return 1;
 }
 
 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
@@ -65,7 +66,6 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
        int ret;
        char tmpbuf[UDP_RECV_CHUNK_SIZE];
        size_t packet_size;
-       uint8_t stream_type, packet_type;
 
        if (rn->output_error && *rn->output_error < 0) {
                t->error = *rn->output_error;
@@ -75,80 +75,23 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
                return;
        ret = recv_bin_buffer(purd->fd, tmpbuf, UDP_RECV_CHUNK_SIZE);
        if (ret < 0) {
-               t->error = 0;
-               if (errno != EINTR && errno != EAGAIN)
-                       t->error = -ERRNO_TO_PARA_ERROR(errno);
-               return;
-       }
-       packet_size = ret;
-       if (packet_size < UDP_AUDIO_HEADER_LEN) {
-               t->error = -E_UDP_SHORT_PACKET; /* FIXME: We shouldn't fail here */
-               return;
-       }
-       if (udp_check_magic(tmpbuf, packet_size) < 0) {
-               t->error = -E_UDP_NO_MAGIC;
+               if (is_errno(ret, EINTR) || is_errno(ret, EAGAIN))
+                       goto success;
+               t->error = ret;
                return;
        }
-       stream_type = udp_read_stream_type(tmpbuf);
-       packet_type = udp_read_packet_type(tmpbuf);
-//     PARA_INFO_LOG("packet type: %d, stream type: %d,"
-//             " loaded: %u\n", packet_type,
-//             (unsigned) stream_type, rn->loaded);
-       switch (packet_type) {
-               uint16_t header_len, payload_len;
-       case UDP_EOF_PACKET:
-               t->error = -E_RECV_EOF;
+       t->error = -E_RECV_EOF;
+       if (!ret)
                return;
-       case UDP_BOF_PACKET:
-               PARA_INFO_LOG("bof (%zu)\n", packet_size);
-               purd->have_header = 1;
-               /* fall through */
-       case UDP_DATA_PACKET:
-               if (!purd->have_header && stream_type == UDP_HEADER_STREAM)
-               /* can't use the data, wait for header */
-                       goto success;
-               if (!enough_space(packet_size, rn->loaded)) {
-                       t->error = -E_OVERRUN;
-                       return;
-               }
-               if (packet_size > UDP_AUDIO_HEADER_LEN) {
-                       memcpy(rn->buf + rn->loaded, tmpbuf + UDP_AUDIO_HEADER_LEN,
-                               packet_size - UDP_AUDIO_HEADER_LEN);
-                       rn->loaded += packet_size - UDP_AUDIO_HEADER_LEN;
-               }
-               goto success;
-       case UDP_HEADER_PACKET:
-               if (!enough_space(packet_size, rn->loaded)) {
-                       t->error = -E_OVERRUN;
-                       return;
-               }
-               header_len = udp_read_header_len(tmpbuf);
-               if (header_len + UDP_AUDIO_HEADER_LEN > packet_size) {
-                       t->error = -E_INVALID_HEADER;
-                       return;
-               }
-//             PARA_DEBUG_LOG("header packet (%zu bytes), header len: %d\n",
-//                     packet_size, header_len);
-               if (!purd->have_header) {
-                       purd->have_header = 1;
-                       rn->loaded = header_len;
-                       memcpy(rn->buf, tmpbuf + UDP_AUDIO_HEADER_LEN,
-                               rn->loaded);
-//                     sleep(1);
-                       goto success;
-               }
-               payload_len = packet_size - UDP_AUDIO_HEADER_LEN - header_len;
-               if (rn->loaded + payload_len > UDP_RECV_CHUNK_SIZE) {
-                       t->error = -E_OVERRUN;
+       packet_size = ret;
+       if (packet_size >= FEC_EOF_PACKET_LEN)
+               if (!memcmp(tmpbuf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN))
                        return;
-               }
-               if (payload_len)
-                       memcpy(rn->buf + rn->loaded, tmpbuf
-                               + (packet_size - payload_len), payload_len);
-               rn->loaded += payload_len;
-       }
+       t->error = add_rn_output(rn, tmpbuf, packet_size);
+       if (t->error < 0)
+               return;
 success:
-       t->error = 0;
+       t->error = 1;
 }
 
 static void udp_shutdown(void)
@@ -179,25 +122,99 @@ static void *udp_recv_parse_config(int argc, char **argv)
        return NULL;
 }
 
+/*
+ * 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);
+}
+
 static int udp_recv_open(struct receiver_node *rn)
 {
        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);
        rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
        purd = rn->private_data;
-       ret = create_udp_recv_socket(c->host_arg, c->port_arg);
+
+       ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
        if (ret < 0)
-               return ret;
+               goto err;
        purd->fd = ret;
+
+       ret = mcast_receiver_setup(purd->fd, iface);
+       if (ret < 0) {
+               close(purd->fd);
+               return ret;
+       }
+
        ret = mark_fd_nonblocking(purd->fd);
        if (ret < 0)
-               return ret;
+               goto err;
        PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
                c->port_arg, purd->fd);
        return purd->fd;
+err:
+       free(rn->private_data);
+       free(rn->buf);
+       return ret;
 }
 
 /**
@@ -222,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
        };
+       udp_recv_cmdline_parser_free(&dummy);
 }