2 * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file dccp_recv.c paraslash's dccp receiver */
10 * based on client.c of dccp-cs-0.01.tar.bz2,
11 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
14 #include <netinet/in.h>
15 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <arpa/inet.h>
27 #include "buffer_tree.h"
33 #include "dccp_recv.cmdline.h"
35 static void dccp_recv_close(struct receiver_node *rn)
39 btr_pool_free(rn->btrp);
42 static int dccp_recv_open(struct receiver_node *rn)
44 struct dccp_recv_args_info *conf = rn->conf;
45 struct flowopts *fo = NULL;
46 uint8_t *ccids = NULL;
49 /* Copy CCID preference list (u8 array required) */
50 if (conf->ccid_given) {
51 ccids = para_malloc(conf->ccid_given);
54 for (i = 0; i < conf->ccid_given; i++)
55 ccids[i] = conf->ccid_arg[i];
57 OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i);
60 fd = makesock(IPPROTO_DCCP, 0, conf->host_arg, conf->port_arg, fo);
66 * Disable unused CCIDs: the receiver does not send any application
67 * data to the server. By shutting down this unused path we reduce
68 * internal processing costs, as the unused CCIDs (in the kernel) are
71 if (shutdown(fd, SHUT_WR) < 0) {
72 ret = -ERRNO_TO_PARA_ERROR(errno);
75 ret = mark_fd_nonblocking(fd);
78 rn->btrp = btr_pool_new("dccp_recv", 320 * 1024);
87 * Check whether the host supports the requested 'ccid' arguments.
88 * \param conf DCCP receiver arguments.
89 * \return True if all CCIDs requested in \a conf are supported.
91 static bool dccp_recv_ccid_support_check(struct dccp_recv_args_info *conf)
96 nccids = dccp_available_ccids(&ccids);
100 for (i = 0; i < conf->ccid_given; i++) {
101 for (j = 0; j < nccids && ccids[j] != conf->ccid_arg[i]; j++)
104 PARA_ERROR_LOG("'CCID-%d' not supported on this host.\n",
112 static void *dccp_recv_parse_config(int argc, char **argv)
114 struct dccp_recv_args_info *tmp = para_calloc(sizeof(*tmp));
116 dccp_recv_cmdline_parser(argc, argv, tmp);
117 if (!dccp_recv_ccid_support_check(tmp))
122 static void dccp_recv_pre_select(struct sched *s, struct task *t)
124 struct receiver_node *rn = task_context(t);
126 if (generic_recv_pre_select(s, t) <= 0)
128 para_fd_set(rn->fd, &s->rfds, &s->max_fileno);
131 static int dccp_recv_post_select(struct sched *s, struct task *t)
133 struct receiver_node *rn = task_context(t);
134 struct btr_node *btrn = rn->btrn;
139 ret = task_get_notification(t);
142 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
145 iovcnt = btr_pool_get_buffers(rn->btrp, iov);
146 ret = -E_DCCP_OVERRUN;
149 ret = readv_nonblock(rn->fd, iov, iovcnt, &s->rfds, &num_bytes);
152 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
153 btr_add_output_pool(rn->btrp, num_bytes, btrn);
154 else { /* both buffers contain data */
155 btr_add_output_pool(rn->btrp, iov[0].iov_len, btrn);
156 btr_add_output_pool(rn->btrp, num_bytes - iov[0].iov_len, btrn);
160 btr_remove_node(&rn->btrn);
164 static void dccp_recv_free_config(void *conf)
166 dccp_recv_cmdline_parser_free(conf);
171 * The init function of the dccp receiver.
173 * \param r Pointer to the receiver struct to initialize.
175 * Initialize all function pointers of \a r.
177 void dccp_recv_init(struct receiver *r)
179 struct dccp_recv_args_info dummy;
181 dccp_recv_cmdline_parser_init(&dummy);
182 r->open = dccp_recv_open;
183 r->close = dccp_recv_close;
184 r->pre_select = dccp_recv_pre_select;
185 r->post_select = dccp_recv_post_select;
186 r->parse_config = dccp_recv_parse_config;
187 r->free_config = dccp_recv_free_config;
188 r->help = (struct ggo_help)DEFINE_GGO_HELP(dccp_recv);
189 dccp_recv_cmdline_parser_free(&dummy);