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