Add btr pool support to the fecdec filter.
[paraslash.git] / udp_recv.c
1 /*
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 /** \file udp_recv.c Paraslash's udp receiver */
7
8 #include <regex.h>
9 #include <dirent.h>
10 #include <sys/socket.h>
11 #include <net/if.h>
12
13 #include "para.h"
14 #include "error.h"
15 #include "portable_io.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "ggo.h"
19 #include "recv.h"
20 #include "udp_recv.cmdline.h"
21 #include "string.h"
22 #include "net.h"
23 #include "fd.h"
24 #include "buffer_tree.h"
25
26 /** The size of the receiver node buffer. */
27 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
28 /**
29 * Data specific to the udp receiver.
30 *
31 * \sa \ref receiver, \ref receiver_node.
32 */
33 struct private_udp_recv_data {
34 /** The socket file descriptor. */
35 int fd;
36 struct btr_pool *btrp;
37 };
38
39 static void udp_recv_pre_select(struct sched *s, struct task *t)
40 {
41 struct receiver_node *rn = container_of(t, struct receiver_node, task);
42 struct private_udp_recv_data *purd = rn->private_data;
43 int ret;
44
45 if (rn->btrn) {
46 ret = generic_recv_pre_select(s, t);
47 if (ret <= 0)
48 return;
49 }
50 para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
51 }
52
53 static int enough_space(size_t nbytes, size_t loaded)
54 {
55 return nbytes + loaded < UDP_RECV_CHUNK_SIZE;
56 }
57
58 static int add_rn_output(struct receiver_node *rn, char *buf, size_t len)
59 {
60 if (!len)
61 return 1;
62 if (!enough_space(len, rn->loaded))
63 return -E_UDP_OVERRUN;
64 memcpy(rn->buf + rn->loaded, buf, len);
65 rn->loaded += len;
66 return 1;
67 }
68
69 static void udp_recv_post_select_nobtr(__a_unused struct sched *s, struct task *t)
70 {
71 struct receiver_node *rn = container_of(t, struct receiver_node, task);
72 struct private_udp_recv_data *purd = rn->private_data;
73 int ret;
74 char tmpbuf[UDP_RECV_CHUNK_SIZE];
75 size_t packet_size;
76
77 if (rn->output_error && *rn->output_error < 0) {
78 t->error = *rn->output_error;
79 return;
80 }
81 if (!FD_ISSET(purd->fd, &s->rfds))
82 return;
83 ret = recv_bin_buffer(purd->fd, tmpbuf, UDP_RECV_CHUNK_SIZE);
84 if (ret < 0) {
85 if (is_errno(ret, EINTR) || is_errno(ret, EAGAIN))
86 goto success;
87 t->error = ret;
88 return;
89 }
90 t->error = -E_RECV_EOF;
91 if (!ret)
92 return;
93 packet_size = ret;
94 if (packet_size >= FEC_EOF_PACKET_LEN)
95 if (!memcmp(tmpbuf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN))
96 return;
97 t->error = add_rn_output(rn, tmpbuf, packet_size);
98 if (t->error < 0)
99 return;
100 success:
101 t->error = 1;
102 }
103
104 static void udp_recv_post_select_btr(__a_unused struct sched *s, struct task *t)
105 {
106 struct receiver_node *rn = container_of(t, struct receiver_node, task);
107 struct private_udp_recv_data *purd = rn->private_data;
108 struct btr_node *btrn = rn->btrn;
109 int ret;
110 char *buf = NULL;
111 size_t packet_size, bufsize;
112
113 t->error = 0;
114 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
115 if (ret < 0)
116 goto err;
117 if (ret == 0)
118 return;
119 if (!FD_ISSET(purd->fd, &s->rfds))
120 return;
121 bufsize = btr_pool_get_buffer(purd->btrp, &buf);
122 ret = -E_UDP_OVERRUN;
123 if (bufsize == 0)
124 goto err;
125 ret = recv_bin_buffer(purd->fd, buf, bufsize);
126 if (ret == 0)
127 ret = -E_RECV_EOF;
128 if (ret < 0)
129 goto err;
130 packet_size = ret;
131 if (packet_size >= FEC_EOF_PACKET_LEN) {
132 if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) {
133 PARA_INFO_LOG("received eof packet\n");
134 ret = -E_RECV_EOF;
135 goto err;
136 }
137 }
138 btr_add_output_pool(purd->btrp, packet_size, btrn);
139 return;
140 err:
141 btr_remove_node(btrn);
142 t->error = ret;
143 close(purd->fd);
144 purd->fd = -1;
145 }
146
147 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
148 {
149 struct receiver_node *rn = container_of(t, struct receiver_node, task);
150 if (rn->btrn)
151 return udp_recv_post_select_btr(s, t);
152 udp_recv_post_select_nobtr(s, t);
153 }
154
155 static void udp_shutdown(void)
156 {
157 return;
158 }
159
160 static void udp_recv_close(struct receiver_node *rn)
161 {
162 struct private_udp_recv_data *purd = rn->private_data;
163
164 if (purd->fd >= 0)
165 close(purd->fd);
166 PARA_CRIT_LOG("%p: close\n", rn);
167 btr_pool_free(purd->btrp);
168 free(rn->private_data);
169 free(rn->buf);
170 }
171
172 static void *udp_recv_parse_config(int argc, char **argv)
173 {
174 int ret;
175 struct udp_recv_args_info *tmp =
176 para_calloc(sizeof(struct udp_recv_args_info));
177
178 ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
179 if (ret >= 0)
180 return tmp;
181 free(tmp);
182 return NULL;
183 }
184
185 /*
186 * Perform AF-independent joining of multicast receive addresses.
187 *
188 * \param fd Bound socket descriptor.
189 * \param iface The receiving multicast interface, or NULL for the default.
190 *
191 * \return Zero if okay, negative on error.
192 */
193 static int mcast_receiver_setup(int fd, const char *iface)
194 {
195 struct sockaddr_storage ss;
196 socklen_t sslen = sizeof(ss);
197 int id = iface == NULL ? 0 : if_nametoindex(iface);
198
199 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
200 goto err;
201
202 if (iface != NULL && id == 0)
203 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
204
205 switch (ss.ss_family) {
206 case AF_INET:
207 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
208 #ifdef HAVE_IP_MREQN
209 struct ip_mreqn m4;
210
211 m4.imr_address.s_addr = INADDR_ANY;
212 m4.imr_ifindex = id;
213 #else
214 struct ip_mreq m4;
215
216 m4.imr_interface.s_addr = INADDR_ANY;
217 if (id != 0)
218 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
219
220 #endif
221 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
222
223 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
224 break;
225 }
226 return 0;
227 case AF_INET6:
228 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
229 struct ipv6_mreq m6;
230
231 memset(&m6, 0, sizeof(m6));
232 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
233 m6.ipv6mr_interface = id;
234 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
235 break;
236 }
237 return 0;
238 default:
239 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
240 return -E_ADDRESS_LOOKUP;
241 }
242 err:
243 return -ERRNO_TO_PARA_ERROR(errno);
244 }
245
246 static int udp_recv_open(struct receiver_node *rn)
247 {
248 struct private_udp_recv_data *purd;
249 struct udp_recv_args_info *c = rn->conf;
250 char *iface = c->iface_given ? c->iface_arg : NULL;
251 int ret;
252
253 rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
254 rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
255 purd = rn->private_data;
256
257 ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
258 if (ret < 0)
259 goto err;
260 purd->fd = ret;
261
262 ret = mcast_receiver_setup(purd->fd, iface);
263 if (ret < 0) {
264 close(purd->fd);
265 return ret;
266 }
267
268 ret = mark_fd_nonblocking(purd->fd);
269 if (ret < 0) {
270 close(purd->fd);
271 goto err;
272 }
273 PARA_CRIT_LOG("rn %p: receiving from %s:%d, fd=%d\n", rn, c->host_arg,
274 c->port_arg, purd->fd);
275 purd->btrp = btr_pool_new("udp_recv", 320 * 1024);
276 return purd->fd;
277 err:
278 free(rn->private_data);
279 free(rn->buf);
280 return ret;
281 }
282
283 static void udp_recv_free_config(void *conf)
284 {
285 udp_recv_cmdline_parser_free(conf);
286 }
287
288 /**
289 * The init function of the udp receiver.
290 *
291 * \param r Pointer to the receiver struct to initialize.
292 *
293 * Initialize all function pointers of \a r.
294 */
295 void udp_recv_init(struct receiver *r)
296 {
297 struct udp_recv_args_info dummy;
298
299 udp_recv_cmdline_parser_init(&dummy);
300 r->shutdown = udp_shutdown;
301 r->open = udp_recv_open;
302 r->close = udp_recv_close;
303 r->pre_select = udp_recv_pre_select;
304 r->post_select = udp_recv_post_select;
305 r->parse_config = udp_recv_parse_config;
306 r->free_config = udp_recv_free_config;
307 r->help = (struct ggo_help) {
308 .short_help = udp_recv_args_info_help,
309 .detailed_help = udp_recv_args_info_detailed_help
310 };
311 udp_recv_cmdline_parser_free(&dummy);
312 }