Merge branch 't/buffer_tree_improvements'
[paraslash.git] / dccp_recv.c
index 58c969a255335461d0481e8864ec79fb41269dac..c751f2f7ce5d85464d506c8c6582556bb424aa7d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2013 Andre Noll <maan@systemlinux.org>
  *
  * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
 #include <regex.h>
 #include <sys/types.h>
-#include <dirent.h>
 
 #include "para.h"
 #include "error.h"
 #include "list.h"
 #include "sched.h"
 #include "ggo.h"
+#include "buffer_tree.h"
 #include "recv.h"
 #include "string.h"
 #include "net.h"
 
 #include "dccp_recv.cmdline.h"
 
-/** the size of the output buffer */
-#define DCCP_BUFSIZE 40960
-
-/**
- * data specific to the dccp receiver
- *
- * \sa receiver receiver_node
- */
-struct private_dccp_recv_data {
-       /** the file descriptor for the dccp socket */
-       int fd;
-};
-
-
 static void dccp_recv_close(struct receiver_node *rn)
 {
-
-       struct private_dccp_recv_data *pdd = rn->private_data;
-
-       if (pdd && pdd->fd > 0)
-               close(pdd->fd);
-       free(rn->buf);
-       rn->buf = NULL;
-       free(rn->private_data);
-       rn->private_data = NULL;
+       if (rn->fd > 0)
+               close(rn->fd);
+       btr_pool_free(rn->btrp);
 }
 
-
 static int dccp_recv_open(struct receiver_node *rn)
 {
-       struct private_dccp_recv_data *pdd;
        struct dccp_recv_args_info *conf = rn->conf;
-       int fd, ret = makesock(AF_UNSPEC, IPPROTO_DCCP, 0, conf->host_arg,
-               conf->port_arg);
+       struct flowopts *fo = NULL;
+       uint8_t *ccids = NULL;
+       int fd, ret, i;
 
-       if (ret < 0)
-               return ret;
-       fd = ret;
+       /* Copy CCID preference list (u8 array required) */
+       if (conf->ccid_given) {
+               ccids = para_malloc(conf->ccid_given);
+               fo    = flowopt_new();
+
+               for (i = 0; i < conf->ccid_given; i++)
+                       ccids[i] = conf->ccid_arg[i];
+
+               OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i);
+       }
+
+       fd = makesock(IPPROTO_DCCP, 0, conf->host_arg, conf->port_arg, fo);
+       free(ccids);
+       if (fd < 0)
+               return fd;
        /*
         * Disable unused CCIDs: the receiver does not send any application
         * data to the server. By shutting down this unused path we reduce
@@ -78,62 +69,97 @@ static int dccp_recv_open(struct receiver_node *rn)
        ret = mark_fd_nonblocking(fd);
        if (ret < 0)
                goto err;
-       rn->buf = para_calloc(DCCP_BUFSIZE);
-       rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
-       pdd->fd = fd;
+       rn->btrp = btr_pool_new("dccp_recv", 320 * 1024);
+       rn->fd = fd;
        return 1;
 err:
        close(fd);
        return ret;
 }
 
-static void dccp_shutdown(void)
+/**
+ * Check whether the host supports the requested 'ccid' arguments.
+ * \param conf DCCP receiver arguments.
+ * \return True if all CCIDs requested in \a conf are supported.
+ */
+static bool dccp_recv_ccid_support_check(struct dccp_recv_args_info *conf)
 {
-       ; /* nothing to do */
+       uint8_t *ccids;
+       int i, j, nccids;
+
+       nccids = dccp_available_ccids(&ccids);
+       if (nccids <= 0)
+               return false;
+
+       for (i = 0; i < conf->ccid_given; i++) {
+               for (j = 0; j < nccids && ccids[j] != conf->ccid_arg[i]; j++)
+                       ;
+               if (j == nccids) {
+                       PARA_ERROR_LOG("'CCID-%d' not supported on this host.\n",
+                                       conf->ccid_arg[i]);
+                       return false;
+               }
+       }
+       return true;
 }
 
 static void *dccp_recv_parse_config(int argc, char **argv)
 {
-       struct dccp_recv_args_info *tmp = para_calloc(sizeof(struct dccp_recv_args_info));
+       struct dccp_recv_args_info *tmp = para_calloc(sizeof(*tmp));
 
-       if (!dccp_recv_cmdline_parser(argc, argv, tmp))
-               return tmp;
-       free(tmp);
-       return NULL;
+       dccp_recv_cmdline_parser(argc, argv, tmp);
+       if (!dccp_recv_ccid_support_check(tmp))
+               exit(EXIT_FAILURE);
+       return tmp;
 }
 
 static void dccp_recv_pre_select(struct sched *s, struct task *t)
 {
        struct receiver_node *rn = container_of(t, struct receiver_node, task);
-       struct private_dccp_recv_data *pdd = rn->private_data;
 
        t->error = 0;
-       para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
+       if (generic_recv_pre_select(s, t) <= 0)
+               return;
+       para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
 }
 
-static void dccp_recv_post_select(struct sched *s, struct task *t)
+static int dccp_recv_post_select(struct sched *s, struct task *t)
 {
        struct receiver_node *rn = container_of(t, struct receiver_node, task);
-       struct private_dccp_recv_data *pdd = rn->private_data;
+       struct btr_node *btrn = rn->btrn;
+       struct iovec iov[2];
+       int ret, iovcnt;
+       size_t num_bytes;
 
-       if (rn->output_error && *rn->output_error < 0) {
-               t->error = *rn->output_error;
-               return;
-       }
-       if (!FD_ISSET(pdd->fd, &s->rfds))
-               return; /* nothing to do */
-       if (rn->loaded >= DCCP_BUFSIZE) {
-               t->error = -E_DCCP_OVERRUN;
-               return;
-       }
-       t->error = recv_bin_buffer(pdd->fd, rn->buf + rn->loaded,
-               DCCP_BUFSIZE - rn->loaded);
-       if (t->error > 0) {
-               rn->loaded += t->error;
-               return;
+       ret = task_get_notification(t);
+       if (ret < 0)
+               goto out;
+       ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
+       if (ret <= 0)
+               goto out;
+       iovcnt = btr_pool_get_buffers(rn->btrp, iov);
+       ret = -E_DCCP_OVERRUN;
+       if (iovcnt == 0)
+               goto out;
+       ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
+       if (num_bytes == 0)
+               goto out;
+       if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
+               btr_add_output_pool(rn->btrp, num_bytes, btrn);
+       else { /* both buffers contain data */
+               btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
+               btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
        }
-       if (!t->error)
-               t->error = -E_RECV_EOF;
+out:
+       if (ret < 0)
+               btr_remove_node(&rn->btrn);
+       return ret;
+}
+
+static void dccp_recv_free_config(void *conf)
+{
+       dccp_recv_cmdline_parser_free(conf);
+       free(conf);
 }
 
 /**
@@ -148,15 +174,12 @@ void dccp_recv_init(struct receiver *r)
        struct dccp_recv_args_info dummy;
 
        dccp_recv_cmdline_parser_init(&dummy);
-       r->shutdown = dccp_shutdown;
        r->open = dccp_recv_open;
        r->close = dccp_recv_close;
        r->pre_select = dccp_recv_pre_select;
        r->post_select = dccp_recv_post_select;
        r->parse_config = dccp_recv_parse_config;
-       r->help = (struct ggo_help) {
-               .short_help = dccp_recv_args_info_help,
-               .detailed_help = dccp_recv_args_info_detailed_help
-       };
+       r->free_config = dccp_recv_free_config;
+       r->help = (struct ggo_help)DEFINE_GGO_HELP(dccp_recv);
        dccp_recv_cmdline_parser_free(&dummy);
 }