client: Do not leak buffer tree node on exit.
[paraslash.git] / udp_recv.c
index 61b5312..45d24ea 100644 (file)
@@ -1,12 +1,11 @@
 /*
- * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 /** \file udp_recv.c Paraslash's udp receiver */
 
 #include <regex.h>
-#include <dirent.h>
 #include <sys/socket.h>
 #include <net/if.h>
 
@@ -23,8 +22,6 @@
 #include "fd.h"
 #include "buffer_tree.h"
 
-/** The size of the receiver node buffer. */
-#define UDP_RECV_CHUNK_SIZE (128 * 1024)
 /**
  * Data specific to the udp receiver.
  *
@@ -46,43 +43,59 @@ static void udp_recv_pre_select(struct sched *s, struct task *t)
        para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
 }
 
+static int udp_check_eof(size_t sz, struct iovec iov[2])
+{
+       if (sz < FEC_EOF_PACKET_LEN)
+               return 0;
+       if (iov[0].iov_len >= FEC_EOF_PACKET_LEN) {
+               if (memcmp(iov[0].iov_base, FEC_EOF_PACKET,
+                               FEC_EOF_PACKET_LEN) != 0)
+                       return 0;
+               return -E_RECV_EOF;
+       }
+       if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, iov[0].iov_len) != 0)
+               return 0;
+       if (memcmp(iov[1].iov_base, FEC_EOF_PACKET + iov[0].iov_len,
+                       FEC_EOF_PACKET_LEN - iov[0].iov_len) != 0)
+               return 0;
+       return -E_RECV_EOF;
+}
+
 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
 {
        struct receiver_node *rn = container_of(t, struct receiver_node, task);
        struct private_udp_recv_data *purd = rn->private_data;
        struct btr_node *btrn = rn->btrn;
-       int ret;
-       char *buf = NULL;
-       size_t packet_size, bufsize;
+       size_t num_bytes;
+       struct iovec iov[2];
+       int ret, readv_ret, iovcnt;
 
        t->error = 0;
        ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
-       if (ret < 0)
-               goto err;
-       if (ret == 0)
-               return;
-       if (!FD_ISSET(purd->fd, &s->rfds))
-               return;
-       bufsize = btr_pool_get_buffer(purd->btrp, &buf);
+       if (ret <= 0)
+               goto out;
+       iovcnt = btr_pool_get_buffers(purd->btrp, iov);
        ret = -E_UDP_OVERRUN;
-       if (bufsize == 0)
-               goto err;
-       ret = recv_bin_buffer(purd->fd, buf, bufsize);
-       if (ret == 0)
-               ret = -E_RECV_EOF;
+       if (iovcnt == 0)
+               goto out;
+       ret = readv_nonblock(purd->fd, iov, iovcnt, &s->rfds, &num_bytes);
+       if (num_bytes == 0)
+               goto out;
+       readv_ret = ret;
+       ret = udp_check_eof(num_bytes, iov);
        if (ret < 0)
-               goto err;
-       packet_size = ret;
-       if (packet_size >= FEC_EOF_PACKET_LEN) {
-               if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) {
-                       PARA_INFO_LOG("received eof packet\n");
-                       ret = -E_RECV_EOF;
-                       goto err;
-               }
+               goto out;
+       if (iov[0].iov_len >= num_bytes)
+               btr_add_output_pool(purd->btrp, num_bytes, btrn);
+       else { /* both buffers contain data */
+               btr_add_output_pool(purd->btrp, iov[0].iov_len, btrn);
+               btr_add_output_pool(purd->btrp, num_bytes - iov[0].iov_len,
+                       btrn);
        }
-       btr_add_output_pool(purd->btrp, packet_size, btrn);
-       return;
-err:
+       ret = readv_ret;
+out:
+       if (ret >= 0)
+               return;
        btr_remove_node(btrn);
        t->error = ret;
        close(purd->fd);
@@ -97,7 +110,6 @@ static void udp_recv_close(struct receiver_node *rn)
                close(purd->fd);
        btr_pool_free(purd->btrp);
        free(rn->private_data);
-       free(rn->buf);
 }
 
 static void *udp_recv_parse_config(int argc, char **argv)
@@ -129,9 +141,11 @@ static int mcast_receiver_setup(int fd, const char *iface)
 
        if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
                goto err;
+       assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
 
        if (iface != NULL && id == 0)
-               PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
+               PARA_WARNING_LOG("could not resolve interface %s, using default\n",
+                       iface);
 
        switch (ss.ss_family) {
        case AF_INET:
@@ -146,12 +160,13 @@ static int mcast_receiver_setup(int fd, const char *iface)
 
                        m4.imr_interface.s_addr = INADDR_ANY;
                        if (id != 0)
-                               PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
+                               PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported\n");
 
 #endif
-                       m4.imr_multiaddr        = ((struct sockaddr_in *)&ss)->sin_addr;
+                       m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
 
-                       if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
+                       if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
+                                       &m4, sizeof(m4)) < 0)
                                break;
                }
                return 0;
@@ -166,9 +181,6 @@ static int mcast_receiver_setup(int fd, const char *iface)
                                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);
@@ -181,11 +193,10 @@ static int udp_recv_open(struct receiver_node *rn)
        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 = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
+       ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
        if (ret < 0)
                goto err;
        purd->fd = ret;
@@ -193,7 +204,7 @@ static int udp_recv_open(struct receiver_node *rn)
        ret = mcast_receiver_setup(purd->fd, iface);
        if (ret < 0) {
                close(purd->fd);
-               return ret;
+               goto err;
        }
 
        ret = mark_fd_nonblocking(purd->fd);
@@ -207,13 +218,13 @@ static int udp_recv_open(struct receiver_node *rn)
        return purd->fd;
 err:
        free(rn->private_data);
-       free(rn->buf);
        return ret;
 }
 
 static void udp_recv_free_config(void *conf)
 {
        udp_recv_cmdline_parser_free(conf);
+       free(conf);
 }
 
 /**