stdin.h: Add missing doxygen comment for stdin_task->btrp.
[paraslash.git] / dccp_recv.c
1 /*
2 * Copyright (C) 2006-2011 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;
109 int i, j, nccids;
110
111 nccids = dccp_available_ccids(&ccids);
112 if (nccids <= 0)
113 return false;
114
115 for (i = 0; i < conf->ccid_given; i++) {
116 for (j = 0; j < nccids && ccids[j] != conf->ccid_arg[i]; j++)
117 ;
118 if (j == nccids) {
119 PARA_ERROR_LOG("'CCID-%d' not supported on this host.\n",
120 conf->ccid_arg[i]);
121 return false;
122 }
123 }
124 return true;
125 }
126
127 static void *dccp_recv_parse_config(int argc, char **argv)
128 {
129 struct dccp_recv_args_info *tmp = para_calloc(sizeof(*tmp));
130
131 if (!dccp_recv_cmdline_parser(argc, argv, tmp) &&
132 dccp_recv_ccid_support_check(tmp))
133 return tmp;
134 free(tmp);
135 return NULL;
136 }
137
138 static void dccp_recv_pre_select(struct sched *s, struct task *t)
139 {
140 struct receiver_node *rn = container_of(t, struct receiver_node, task);
141 struct private_dccp_recv_data *pdd = rn->private_data;
142
143 t->error = 0;
144 if (generic_recv_pre_select(s, t) <= 0)
145 return;
146 para_fd_set(pdd->fd, &s->rfds, &s->max_fileno);
147 }
148
149 static void dccp_recv_post_select(struct sched *s, struct task *t)
150 {
151 struct receiver_node *rn = container_of(t, struct receiver_node, task);
152 struct private_dccp_recv_data *pdd = rn->private_data;
153 struct btr_node *btrn = rn->btrn;
154 struct iovec iov[2];
155 int ret, iovcnt;
156 size_t num_bytes;
157
158 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
159 if (ret <= 0)
160 goto out;
161 iovcnt = btr_pool_get_buffers(pdd->btrp, iov);
162 ret = -E_DCCP_OVERRUN;
163 if (iovcnt == 0)
164 goto out;
165 ret = readv_nonblock(pdd->fd, iov, iovcnt, &s->rfds, &num_bytes);
166 if (num_bytes == 0)
167 goto out;
168 if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */
169 btr_add_output_pool(pdd->btrp, num_bytes, btrn);
170 else { /* both buffers contain data */
171 btr_add_output_pool(pdd->btrp, iov[0].iov_len, btrn);
172 btr_add_output_pool(pdd->btrp, num_bytes - iov[0].iov_len, btrn);
173 }
174 out:
175 if (ret >= 0)
176 return;
177 btr_remove_node(rn->btrn);
178 t->error = ret;
179 }
180
181 static void dccp_recv_free_config(void *conf)
182 {
183 dccp_recv_cmdline_parser_free(conf);
184 free(conf);
185 }
186
187 /**
188 * The init function of the dccp receiver.
189 *
190 * \param r Pointer to the receiver struct to initialize.
191 *
192 * Initialize all function pointers of \a r.
193 */
194 void dccp_recv_init(struct receiver *r)
195 {
196 struct dccp_recv_args_info dummy;
197
198 dccp_recv_cmdline_parser_init(&dummy);
199 r->open = dccp_recv_open;
200 r->close = dccp_recv_close;
201 r->pre_select = dccp_recv_pre_select;
202 r->post_select = dccp_recv_post_select;
203 r->parse_config = dccp_recv_parse_config;
204 r->free_config = dccp_recv_free_config;
205 r->help = (struct ggo_help) {
206 .short_help = dccp_recv_args_info_help,
207 .detailed_help = dccp_recv_args_info_detailed_help
208 };
209 dccp_recv_cmdline_parser_free(&dummy);
210 }