Kill afhi->header_offset.
[paraslash.git] / dccp_send.c
index dd495f98eccb190fc8aff579cf7851d5bcf627d8..304a42e749a647d5d6a568ae0e9850d183907979 100644 (file)
 /*
- * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
+/** \file dccp_send.c Paraslash's dccp sender. */
+
+/*
+ * based on server.c of dccp-cs-0.01.tar.bz2,
+ * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
+ */
+
+#include <regex.h>
+#include <sys/types.h>
+#include <dirent.h>
+#include <osl.h>
+
+#include "para.h"
+#include "error.h"
+#include "string.h"
+#include "afh.h"
+#include "afs.h"
 #include "server.h"
 #include "net.h"
 #include "list.h"
-#include "afs.h"
 #include "send.h"
-#include "dccp.h"
-#include "error.h"
-#include "string.h"
+#include "vss.h"
+#include "fd.h"
+#include "close_on_fork.h"
+#include "chunk_queue.h"
 #include "server.cmdline.h"
-extern struct gengetopt_args_info conf;
-/** the list of connected clients **/
-static struct list_head clients;
-static int listen_fd = -1;
-static struct sender *self;
-
-/** describes one connected client */
-struct dccp_client {
-       /** the dccp socket */
-       int fd;
-       /** address information about the client */
-       struct sockaddr_in addr;
-       /** the position of this client in the client list */
-       struct list_head node;
-       int header_sent; /* non-zero if audio file header has been sent */
+#include "acl.h"
+
+static struct sender_status dccp_sender_status, *dss = &dccp_sender_status;
+
+struct dccp_fec_client {
+       struct fec_client_parms fcp;
+       struct fec_client *fc;
 };
 
-static void dccp_pre_select(__unused struct audio_format *af, int *max_fileno, fd_set *rfds,
-               __unused fd_set *wfds)
+static void dccp_pre_select(int *max_fileno, fd_set *rfds,
+               __a_unused fd_set *wfds)
 {
-       if (listen_fd < 0)
-               return;
-       FD_SET(listen_fd, rfds);
-       *max_fileno = MAX(*max_fileno, listen_fd);
+       if (dss->listen_fd >= 0)
+               para_fd_set(dss->listen_fd, rfds, max_fileno);
 }
 
-static void dccp_post_select(__unused struct audio_format *af, fd_set *rfds,
-               __unused fd_set *wfds)
+/**
+ * Query the TX CCID used on the sender-client half connection.
+ * \param sockfd Server socket descriptor to query (after accept(2)).
+ * \return CCID number > 0, -1 on error/incompatibility.
+ *
+ * NB: This feature is only available on Linux > 2.6.30; on older kernels
+ * ENOPROTOOPT ("Protocol not available") will be returned.
+ */
+static int dccp_get_tx_ccid(int sockfd)
 {
-       struct dccp_client *dc;
-       int ret;
+       int tx_ccid;
+       socklen_t len = sizeof(tx_ccid);
 
-       if (!FD_ISSET(listen_fd, rfds))
-               return;
-       PARA_NOTICE_LOG("%s", "accepting...\n");
-       dc = para_calloc(sizeof(struct dccp_client));
-       ret = para_accept(listen_fd, &dc->addr, sizeof(struct sockaddr_in));
-       if (ret < 0) {
-               PARA_ERROR_LOG("%s", PARA_STRERROR(-ret));
-               return;
+       if (getsockopt(sockfd, SOL_DCCP,
+                      DCCP_SOCKOPT_TX_CCID, &tx_ccid, &len) < 0) {
+               PARA_WARNING_LOG("DCCP_SOCKOPT_TX_CCID: %s\n", strerror(errno));
+               return -1;
        }
-       PARA_NOTICE_LOG("%s", "connection\n");
-       dc->fd = ret;
-       list_add(&dc->node, &clients);
+       return tx_ccid;
 }
 
-static int dccp_open(void)
+static void dccp_shutdown_client(struct sender_client *sc)
 {
-       struct sockaddr_in servaddr;
-       int ret;
+       struct dccp_fec_client *dfc = sc->private_data;
 
-       ret = dccp_get_socket();
-       if (ret < 0)
-               return ret;
-       listen_fd = ret;
-
-       bzero(&servaddr, sizeof(servaddr));
-       servaddr.sin_family = AF_INET;
-       servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
-       servaddr.sin_port = htons(conf.dccp_port_arg);
-       ret = bind(listen_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
-       if (ret < 0)
-               return -E_DCCP_BIND;
-       ret = dccp_set_socket(listen_fd);
-       if (ret < 0)
-               return ret;
-       ret = listen(listen_fd, 0);
-       if (ret < 0)
-               return -E_DCCP_LISTEN;
-       PARA_DEBUG_LOG("listening on fd %d\n", listen_fd);
-       return 1;
+       vss_del_fec_client(dfc->fc);
+       shutdown_client(sc, dss);
+}
+
+static void dccp_shutdown_clients(void)
+{
+       struct sender_client *sc, *tmp;
+
+       list_for_each_entry_safe(sc, tmp, &dss->client_list, node)
+               dccp_shutdown_client(sc);
+}
+
+/** * Obtain current MPS according to RFC 4340, sec. 14. */
+static int dccp_init_fec(struct sender_client *sc)
+{
+       int mps, ret;
+       socklen_t ml = sizeof(mps);
+
+       /* If call fails, return some sensible minimum value */
+       ret = getsockopt(sc->fd, SOL_DCCP, DCCP_SOCKOPT_GET_CUR_MPS, &mps, &ml);
+       if (ret < 0) {
+               PARA_NOTICE_LOG("can not determine MPS: %s\n", strerror(errno));
+               mps = generic_max_transport_msg_size(sc->fd) - DCCP_MAX_HEADER;
+       }
+       PARA_INFO_LOG("current MPS = %d bytes\n", mps);
+       assert(mps > 0);
+       if (conf.dccp_max_slice_size_arg > 0)
+               mps = PARA_MIN(mps, conf.dccp_max_slice_size_arg);
+       return mps;
 }
 
-static void dccp_shutdown_client(struct dccp_client *dc)
+static int dccp_send_fec(struct sender_client *sc, char *buf, size_t len)
 {
-       close(dc->fd);
-       list_del(&dc->node);
-       free(dc);
+       int ret = write_nonblock(sc->fd, buf, len);
+
+       if (ret < 0)
+               dccp_shutdown_client(sc);
+       return ret;
 }
 
-static void dccp_send(__unused struct audio_format *af,
-               long unsigned current_chunk,
-               __unused long unsigned chunks_sent, const char *buf, size_t len)
+static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
 {
-       struct dccp_client *dc, *tmp;
-       int ret, header_len;
-       char *header_buf;
+       struct sender_client *sc;
+       struct dccp_fec_client *dfc;
+       int tx_ccid;
 
-       if (listen_fd < 0 || !len)
+       sc = accept_sender_client(dss, rfds);
+       if (!sc)
                return;
 
-       list_for_each_entry_safe(dc, tmp, &clients, node) {
-               if (!_write_ok(dc->fd))
-                       continue;
-               if (!dc->header_sent && af->get_header_info && current_chunk) {
-                       header_buf = af->get_header_info(&header_len);
-                       if (!header_buf || header_len <= 0)
-                               continue; /* header not yet available */
-                       ret = write(dc->fd, header_buf, header_len);
-                       if (ret != header_len) {
-                               dccp_shutdown_client(dc);
-                               continue;
-                       }
-                       if (!_write_ok(dc->fd))
-                               continue;
-               }
-               PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
-               ret = write(dc->fd, buf, len);
-               if (ret != len)
-                       dccp_shutdown_client(dc);
+       /* If CCID identifiable, present client as <host>#<port>~<ccid> */
+       tx_ccid = dccp_get_tx_ccid(sc->fd);
+       if (tx_ccid != -1) {
+               char *tmp = make_message("%s~%d", sc->name, tx_ccid);
+
+               free(sc->name);
+               sc->name = tmp;
+       }
+       /*
+        * Bypass unused CCID paths: the sender does not receive application data
+        * from the client; by shutting down this unused communication path we can
+        * reduce processing costs a bit. See analogous comment in dccp_recv.c.
+        */
+       if (shutdown(sc->fd, SHUT_RD) < 0) {
+               PARA_WARNING_LOG("%s\n", strerror(errno));
+               shutdown_client(sc, dss);
+               return;
        }
+       dfc = para_calloc(sizeof(*dfc));
+       sc->private_data = dfc;
+       dfc->fcp.data_slices_per_group  = conf.dccp_data_slices_per_group_arg;
+       dfc->fcp.slices_per_group       = conf.dccp_slices_per_group_arg;
+       dfc->fcp.init_fec               = dccp_init_fec;
+       dfc->fcp.send_fec               = dccp_send_fec;
+       dfc->fcp.need_periodic_header   = false;
+       dfc->fc = vss_add_fec_client(sc, &dfc->fcp);
 }
 
-static void dccp_shutdown_clients(void)
+static int dccp_com_on(__a_unused struct sender_command_data *scd)
 {
-       struct dccp_client *dc, *tmp;
+       return generic_com_on(dss, IPPROTO_DCCP);
+}
 
-       list_for_each_entry_safe(dc, tmp, &clients, node)
-               dccp_shutdown_client(dc);
+static int dccp_com_off(__a_unused struct sender_command_data *scd)
+{
+       dccp_shutdown_clients();
+       generic_com_off(dss);
+       return 1;
 }
 
-static char *dccp_info(void)
+
+static int dccp_com_deny(struct sender_command_data *scd)
 {
-       static char *buf;
-       int num_clients = 0;
-       struct dccp_client *dc, *tmp;
-
-       free(buf);
-       list_for_each_entry_safe(dc, tmp, &clients, node)
-               num_clients++;
-       buf = make_message("%d connected clients\n", num_clients);
-       return buf;
+       generic_com_deny(scd, dss);
+       return 1;
 }
 
-static char *dccp_help(void)
+static int dccp_com_allow(struct sender_command_data *scd)
 {
-       return make_message("no help available\n");
+       generic_com_allow(scd, dss);
+       return 1;
 }
 
+/**
+ * Return list of available CCIDs or warning, in static buffer.
+ */
+static const char *dccp_list_available_ccids(void)
+{
+       /* Worst case length: n * 3 digits + n-1 spaces + '\0' */
+       static char list[DCCP_MAX_HOST_CCIDS * 4];
+       uint8_t *ccids;
+       int i, len, nccids;
+
+       nccids = dccp_available_ccids(&ccids);
+       if (nccids < 0) {
+               snprintf(list, sizeof(list), "Unable to query available CCIDs");
+       } else {
+               for (i = len = 0; i < nccids; i++)
+                       len += snprintf(list + len, sizeof(list) - len,
+                                       "%s%d", i ? " " : "", ccids[i]);
+       }
+       return list;
+}
+
+static char *dccp_info(void)
+{
+       char *info = get_sender_info(dss, "dccp");
+       char *ret  = make_message("%s" "\tsupported ccids: %s\n",
+                                 info, dccp_list_available_ccids());
+       free(info);
+       return ret;
+}
+
+/**
+ * The init function of the dccp sender.
+ *
+ * \param s pointer to the dccp sender struct.
+ *
+ * It initializes all function pointers of \a s and starts
+ * listening on the given port.
+ */
 void dccp_send_init(struct sender *s)
 {
-       int ret;
+       int ret, k, n;
 
-       INIT_LIST_HEAD(&clients);
        s->info = dccp_info;
-       s->send = dccp_send;
+       s->send = NULL;
        s->pre_select = dccp_pre_select;
        s->post_select = dccp_post_select;
        s->shutdown_clients = dccp_shutdown_clients;
-       s->help = dccp_help;
-       s->client_cmds[SENDER_ON] = NULL;
-       s->client_cmds[SENDER_OFF] = NULL;
-       s->client_cmds[SENDER_DENY] = NULL;
-       s->client_cmds[SENDER_ALLOW] = NULL;
+       s->resolve_target = NULL;
+       s->help = generic_sender_help;
+       s->client_cmds[SENDER_ON] = dccp_com_on;
+       s->client_cmds[SENDER_OFF] = dccp_com_off;
+       s->client_cmds[SENDER_DENY] = dccp_com_deny;
+       s->client_cmds[SENDER_ALLOW] = dccp_com_allow;
        s->client_cmds[SENDER_ADD] = NULL;
        s->client_cmds[SENDER_DELETE] = NULL;
-       self = s;
-       ret = dccp_open();
+
+       k = conf.dccp_data_slices_per_group_arg;
+       n = conf.dccp_slices_per_group_arg;
+
+       if (k <= 0 || n <= 0 || k >= n) {
+               PARA_WARNING_LOG("invalid FEC parameters, using defaults\n");
+               conf.dccp_data_slices_per_group_arg = 3;
+               conf.dccp_slices_per_group_arg = 4;
+       }
+
+       init_sender_status(dss, conf.dccp_access_arg, conf.dccp_access_given,
+               conf.dccp_port_arg, conf.dccp_max_clients_arg,
+               conf.dccp_default_deny_given);
+       ret = generic_com_on(dss, IPPROTO_DCCP);
        if (ret < 0)
-               PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
-       else
-               s->status = SENDER_ON;
+               PARA_ERROR_LOG("%s\n", para_strerror(-ret));
 }