Add documentation to struct rc4_context.
[paraslash.git] / udp_recv.c
index 9ea35d8d53ed2db68a54bfd1bc5527866d4f3061..7bbd4b0c575a351055170bab1df3f186769c6783 100644 (file)
@@ -5,12 +5,14 @@
  */
 /** \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"
 
 /** 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;
-       /** 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)
@@ -62,53 +48,6 @@ static int enough_space(size_t nbytes, size_t loaded)
        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)
@@ -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];
-       uint16_t data_len;
-       char *data_buf;
        size_t packet_size;
-       struct udp_audio_header uah;
 
        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;
-       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;
-               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;
-       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;
 }
@@ -303,7 +208,6 @@ static int udp_recv_open(struct receiver_node *rn)
        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;