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