btr: Kill an unused variable.
[paraslash.git] / dccp_recv.c
1 /*
2  * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file dccp_recv.c paraslash's dccp receiver */
8
9 /*
10  * based on client.c of dccp-cs-0.01.tar.bz2,
11  * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
12  */
13
14 #include <regex.h>
15 #include <sys/types.h>
16 #include <dirent.h>
17
18 #include "para.h"
19 #include "error.h"
20 #include "list.h"
21 #include "sched.h"
22 #include "ggo.h"
23 #include "recv.h"
24 #include "string.h"
25 #include "net.h"
26 #include "fd.h"
27 #include "buffer_tree.h"
28
29 #include "dccp_recv.cmdline.h"
30
31 /**
32  * data specific to the dccp receiver
33  *
34  * \sa receiver receiver_node
35  */
36 struct private_dccp_recv_data {
37         /** the file descriptor for the dccp socket */
38         int fd;
39         struct btr_pool *btrp;
40 };
41
42
43 static void dccp_recv_close(struct receiver_node *rn)
44 {
45
46         struct private_dccp_recv_data *pdd = rn->private_data;
47
48         if (pdd && pdd->fd > 0)
49                 close(pdd->fd);
50         btr_pool_free(pdd->btrp);
51         free(rn->private_data);
52         rn->private_data = NULL;
53 }
54
55
56 static int dccp_recv_open(struct receiver_node *rn)
57 {
58         struct private_dccp_recv_data *pdd;
59         struct dccp_recv_args_info *conf = rn->conf;
60         struct flowopts *fo = NULL;
61         uint8_t *ccids = NULL;
62         int fd, ret, i;
63
64         /* Copy CCID preference list (u8 array required) */
65         if (conf->ccid_given) {
66                 ccids = para_malloc(conf->ccid_given);
67                 fo    = flowopt_new();
68
69                 for (i = 0; i < conf->ccid_given; i++)
70                         ccids[i] = conf->ccid_arg[i];
71
72                 OPT_ADD(fo, SOL_DCCP, DCCP_SOCKOPT_CCID, ccids, i);
73         }
74
75         fd = makesock(IPPROTO_DCCP, 0, conf->host_arg, conf->port_arg, fo);
76         free(ccids);
77         if (fd < 0)
78                 return fd;
79         /*
80          * Disable unused CCIDs: the receiver does not send any application
81          * data to the server. By shutting down this unused path we reduce
82          * internal processing costs, as the unused CCIDs (in the kernel) are
83          * then bypassed.
84          */
85         if (shutdown(fd, SHUT_WR) < 0) {
86                 ret = -ERRNO_TO_PARA_ERROR(errno);
87                 goto err;
88         }
89         ret = mark_fd_nonblocking(fd);
90         if (ret < 0)
91                 goto err;
92         rn->private_data = pdd = para_calloc(sizeof(struct private_dccp_recv_data));
93         pdd->btrp = btr_pool_new("dccp_recv", 320 * 1024);
94         pdd->fd = fd;
95         return 1;
96 err:
97         close(fd);
98         return ret;
99 }
100
101 /**
102  * Check whether the host supports the requested 'ccid' arguments.
103  * \param conf DCCP receiver arguments.
104  * \return True if all CCIDs requested in \a conf are supported.
105  */
106 static bool dccp_recv_ccid_support_check(struct dccp_recv_args_info *conf)
107 {
108         uint8_t ccids[DCCP_MAX_HOST_CCIDS];
109         uint8_t nccids = sizeof(ccids), i, j;
110
111         if (dccp_available_ccids(ccids, &nccids) == NULL)
112                 return false;
113
114         for (i = 0; i < conf->ccid_given; i++) {
115                 for (j = 0; j < nccids && ccids[j] != conf->ccid_arg[i]; j++)
116                         ;
117                 if (j == nccids) {
118                         PARA_ERROR_LOG("'CCID-%d' not supported on this host.\n",
119                                         conf->ccid_arg[i]);
120                         return false;
121                 }
122         }
123         return true;
124 }
125
126 static void *dccp_recv_parse_config(int argc, char **argv)
127 {
128         struct dccp_recv_args_info *tmp = para_calloc(sizeof(*tmp));
129
130         if (!dccp_recv_cmdline_parser(argc, argv, tmp) &&
131             dccp_recv_ccid_support_check(tmp))
132                 return tmp;
133         free(tmp);
134         return NULL;
135 }
136
137 static void dccp_recv_pre_select(struct sched *s, struct task *t)
138 {
139         struct receiver_node *rn = container_of(t, struct receiver_node, task);
140         struct private_dccp_recv_data *pdd = rn->private_data;
141
142         t->error = 0;
143         if (generic_recv_pre_select(s, t) <= 0)
144                 return;
145         para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
146 }
147
148 static void dccp_recv_post_select(struct sched *s, struct task *t)
149 {
150         struct receiver_node *rn = container_of(t, struct receiver_node, task);
151         struct private_dccp_recv_data *pdd = rn->private_data;
152         struct btr_node *btrn = rn->btrn;
153         struct iovec iov[2];
154         int ret, iovcnt;
155
156         ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
157         if (ret < 0)
158                 goto err;
159         if (ret == 0)
160                 return;
161         if (!FD_ISSET(pdd->fd, &s->rfds))
162                 return; /* nothing to do */
163         iovcnt = btr_pool_get_buffers(pdd->btrp, iov);
164         ret = -E_DCCP_OVERRUN;
165         if (iovcnt == 0)
166                 goto err;
167         ret = para_readv(pdd->fd, iov, iovcnt);
168         if (ret == 0)
169                 ret = -E_RECV_EOF;
170         if (ret < 0)
171                 goto err;
172         if (ret <= iov[0].iov_len) /* only the first buffer was filled */
173                 btr_add_output_pool(pdd->btrp, ret, btrn);
174         else { /* both buffers contain data */
175                 btr_add_output_pool(pdd->btrp, iov[0].iov_len, btrn);
176                 btr_add_output_pool(pdd->btrp, ret - iov[0].iov_len, btrn);
177         }
178         return;
179 err:
180         btr_remove_node(rn->btrn);
181         t->error = ret;
182 }
183
184 static void dccp_recv_free_config(void *conf)
185 {
186         dccp_recv_cmdline_parser_free(conf);
187         free(conf);
188 }
189
190 /**
191  * The init function of the dccp receiver.
192  *
193  * \param r Pointer to the receiver struct to initialize.
194  *
195  * Initialize all function pointers of \a r.
196  */
197 void dccp_recv_init(struct receiver *r)
198 {
199         struct dccp_recv_args_info dummy;
200
201         dccp_recv_cmdline_parser_init(&dummy);
202         r->open = dccp_recv_open;
203         r->close = dccp_recv_close;
204         r->pre_select = dccp_recv_pre_select;
205         r->post_select = dccp_recv_post_select;
206         r->parse_config = dccp_recv_parse_config;
207         r->free_config = dccp_recv_free_config;
208         r->help = (struct ggo_help) {
209                 .short_help = dccp_recv_args_info_help,
210                 .detailed_help = dccp_recv_args_info_detailed_help
211         };
212         dccp_recv_cmdline_parser_free(&dummy);
213 }