user_list: Make list head static.
[paraslash.git] / udp_recv.c
index e276343..67846b1 100644 (file)
@@ -1,47 +1,35 @@
-/*
- * Copyright (C) 2005-2011 Andre Noll <maan@systemlinux.org>
- *
- * Licensed under the GPL v2. For licencing details see COPYING.
- */
+/* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
 /** \file udp_recv.c Paraslash's udp receiver */
 
+#include <netinet/in.h>
 #include <regex.h>
-#include <dirent.h>
 #include <sys/socket.h>
 #include <net/if.h>
+#include <sys/types.h>
+#include <arpa/inet.h>
+#include <sys/un.h>
+#include <netdb.h>
+#include <lopsub.h>
 
+#include "recv_cmd.lsg.h"
 #include "para.h"
 #include "error.h"
 #include "portable_io.h"
 #include "list.h"
 #include "sched.h"
-#include "ggo.h"
+#include "buffer_tree.h"
 #include "recv.h"
-#include "udp_recv.cmdline.h"
 #include "string.h"
 #include "net.h"
 #include "fd.h"
-#include "buffer_tree.h"
 
-/**
- * Data specific to the udp receiver.
- *
- * \sa \ref receiver, \ref receiver_node.
- */
-struct private_udp_recv_data {
-       /** The socket file descriptor. */
-       int fd;
-       struct btr_pool *btrp;
-};
-
-static void udp_recv_pre_select(struct sched *s, struct task *t)
+static void udp_recv_pre_select(struct sched *s, void *context)
 {
-       struct receiver_node *rn = container_of(t, struct receiver_node, task);
-       struct private_udp_recv_data *purd = rn->private_data;
+       struct receiver_node *rn = context;
 
-       if (generic_recv_pre_select(s, t) <= 0)
+       if (generic_recv_pre_select(s, rn) <= 0)
                return;
-       para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
+       para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
 }
 
 static int udp_check_eof(size_t sz, struct iovec iov[2])
@@ -56,30 +44,31 @@ static int udp_check_eof(size_t sz, struct iovec iov[2])
        }
        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,
+       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)
+static int udp_recv_post_select(__a_unused struct sched *s, void *context)
 {
-       struct receiver_node *rn = container_of(t, struct receiver_node, task);
-       struct private_udp_recv_data *purd = rn->private_data;
+       struct receiver_node *rn = context;
        struct btr_node *btrn = rn->btrn;
        size_t num_bytes;
        struct iovec iov[2];
        int ret, readv_ret, iovcnt;
 
-       t->error = 0;
+       ret = task_get_notification(rn->task);
+       if (ret < 0)
+               goto out;
        ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
        if (ret <= 0)
                goto out;
-       iovcnt = btr_pool_get_buffers(purd->btrp, iov);
+       iovcnt = btr_pool_get_buffers(rn->btrp, iov);
        ret = -E_UDP_OVERRUN;
        if (iovcnt == 0)
                goto out;
-       ret = readv_nonblock(purd->fd, iov, iovcnt, &s->rfds, &num_bytes);
+       ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
        if (num_bytes == 0)
                goto out;
        readv_ret = ret;
@@ -87,43 +76,27 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
        if (ret < 0)
                goto out;
        if (iov[0].iov_len >= num_bytes)
-               btr_add_output_pool(purd->btrp, num_bytes, btrn);
+               btr_add_output_pool(rn->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,
+               btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
+               btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len,
                        btrn);
        }
        ret = readv_ret;
 out:
-       if (ret >= 0)
-               return;
-       btr_remove_node(btrn);
-       t->error = ret;
-       close(purd->fd);
-       purd->fd = -1;
+       if (ret < 0) {
+               btr_remove_node(&rn->btrn);
+               close(rn->fd);
+               rn->fd = -1;
+       }
+       return ret;
 }
 
 static void udp_recv_close(struct receiver_node *rn)
 {
-       struct private_udp_recv_data *purd = rn->private_data;
-
-       if (purd->fd >= 0)
-               close(purd->fd);
-       btr_pool_free(purd->btrp);
-       free(rn->private_data);
-}
-
-static void *udp_recv_parse_config(int argc, char **argv)
-{
-       int ret;
-       struct udp_recv_args_info *tmp =
-               para_calloc(sizeof(struct udp_recv_args_info));
-
-       ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
-       if (ret >= 0)
-               return tmp;
-       free(tmp);
-       return NULL;
+       if (rn->fd >= 0)
+               close(rn->fd);
+       btr_pool_free(rn->btrp);
 }
 
 /*
@@ -189,66 +162,34 @@ err:
 
 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;
+       struct lls_parse_result *lpr = rn->lpr;
+       const char *iface = RECV_CMD_OPT_STRING_VAL(UDP, IFACE, lpr);
+       const char *host = RECV_CMD_OPT_STRING_VAL(UDP, HOST, lpr);
+       uint32_t port = RECV_CMD_OPT_UINT32_VAL(UDP, PORT, lpr);
        int ret;
 
-       rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
-       purd = rn->private_data;
-
-       ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
+       ret = makesock(IPPROTO_UDP, 1, host, port, NULL);
+       if (ret < 0)
+               return ret;
+       rn->fd = ret;
+       ret = mcast_receiver_setup(rn->fd, iface);
        if (ret < 0)
                goto err;
-       purd->fd = ret;
-
-       ret = mcast_receiver_setup(purd->fd, iface);
-       if (ret < 0) {
-               close(purd->fd);
-               goto err;
-       }
-
-       ret = mark_fd_nonblocking(purd->fd);
-       if (ret < 0) {
-               close(purd->fd);
+       ret = mark_fd_nonblocking(rn->fd);
+       if (ret < 0)
                goto err;
-       }
-       PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
-               c->port_arg, purd->fd);
-       purd->btrp = btr_pool_new("udp_recv", 320 * 1024);
-       return purd->fd;
+       PARA_INFO_LOG("receiving from %s:%u, fd=%d\n", host, port, rn->fd);
+       rn->btrp = btr_pool_new("udp_recv", 320 * 1024);
+       return rn->fd;
 err:
-       free(rn->private_data);
+       close(rn->fd);
        return ret;
 }
 
-static void udp_recv_free_config(void *conf)
-{
-       udp_recv_cmdline_parser_free(conf);
-       free(conf);
-}
-
-/**
- * The init function of the udp receiver.
- *
- * \param r Pointer to the receiver struct to initialize.
- *
- * Initialize all function pointers of \a r.
- */
-void udp_recv_init(struct receiver *r)
-{
-       struct udp_recv_args_info dummy;
-
-       udp_recv_cmdline_parser_init(&dummy);
-       r->open = udp_recv_open;
-       r->close = udp_recv_close;
-       r->pre_select = udp_recv_pre_select;
-       r->post_select = udp_recv_post_select;
-       r->parse_config = udp_recv_parse_config;
-       r->free_config = udp_recv_free_config;
-       r->help = (struct ggo_help) {
-               .short_help = udp_recv_args_info_help,
-               .detailed_help = udp_recv_args_info_detailed_help
-       };
-       udp_recv_cmdline_parser_free(&dummy);
-}
+/** See \ref recv_init(). */
+const struct receiver lsg_recv_cmd_com_udp_user_data = {
+       .open = udp_recv_open,
+       .close = udp_recv_close,
+       .pre_select = udp_recv_pre_select,
+       .post_select = udp_recv_post_select,
+};