btr support for the UDP receiver.
[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 "audiod.h"
22 #include "string.h"
23 #include "net.h"
24 #include "fd.h"
25 #include "buffer_tree.h"
26
27 /** The size of the receiver node buffer. */
28 #define UDP_RECV_CHUNK_SIZE (128 * 1024)
29 /**
30 * Data specific to the udp receiver.
31 *
32 * \sa \ref receiver, \ref receiver_node.
33 */
34 struct private_udp_recv_data {
35 /** The socket file descriptor. */
36 int fd;
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 #define UDP_RECV_READ_BUF_SIZE 1500
105
106 static void udp_recv_post_select_btr(__a_unused struct sched *s, struct task *t)
107 {
108 struct receiver_node *rn = container_of(t, struct receiver_node, task);
109 struct private_udp_recv_data *purd = rn->private_data;
110 struct btr_node *btrn = rn->btrn;
111 int ret;
112 char *buf = NULL;
113 size_t packet_size;
114
115 t->error = 0;
116 ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
117 if (ret < 0)
118 goto err;
119 if (ret == 0)
120 return;
121 if (!FD_ISSET(purd->fd, &s->rfds))
122 return;
123 buf = para_malloc(UDP_RECV_READ_BUF_SIZE);
124 ret = recv_bin_buffer(purd->fd, buf, UDP_RECV_READ_BUF_SIZE);
125 if (ret == 0)
126 ret = -E_RECV_EOF;
127 if (ret < 0)
128 goto err;
129 packet_size = ret;
130 if (packet_size >= FEC_EOF_PACKET_LEN) {
131 if (!memcmp(buf, FEC_EOF_PACKET, FEC_EOF_PACKET_LEN)) {
132 ret = -E_RECV_EOF;
133 goto err;
134 }
135 }
136 btr_add_output(buf, ret, btrn);
137 return;
138 err:
139 free(buf);
140 btr_remove_node(btrn);
141 t->error = ret;
142 }
143
144 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
145 {
146 struct receiver_node *rn = container_of(t, struct receiver_node, task);
147 if (rn->btrn)
148 return udp_recv_post_select_btr(s, t);
149 udp_recv_post_select_nobtr(s, t);
150 }
151
152 static void udp_shutdown(void)
153 {
154 return;
155 }
156
157 static void udp_recv_close(struct receiver_node *rn)
158 {
159 struct private_udp_recv_data *purd = rn->private_data;
160
161 if (purd->fd >= 0)
162 close(purd->fd);
163 free(rn->private_data);
164 free(rn->buf);
165 udp_recv_cmdline_parser_free(rn->conf);
166 }
167
168 static void *udp_recv_parse_config(int argc, char **argv)
169 {
170 int ret;
171 struct udp_recv_args_info *tmp =
172 para_calloc(sizeof(struct udp_recv_args_info));
173
174 ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
175 if (ret >= 0)
176 return tmp;
177 free(tmp);
178 return NULL;
179 }
180
181 /*
182 * Perform AF-independent joining of multicast receive addresses.
183 *
184 * \param fd Bound socket descriptor.
185 * \param iface The receiving multicast interface, or NULL for the default.
186 *
187 * \return Zero if okay, negative on error.
188 */
189 static int mcast_receiver_setup(int fd, const char *iface)
190 {
191 struct sockaddr_storage ss;
192 socklen_t sslen = sizeof(ss);
193 int id = iface == NULL ? 0 : if_nametoindex(iface);
194
195 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
196 goto err;
197
198 if (iface != NULL && id == 0)
199 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
200
201 switch (ss.ss_family) {
202 case AF_INET:
203 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
204 #ifdef HAVE_IP_MREQN
205 struct ip_mreqn m4;
206
207 m4.imr_address.s_addr = INADDR_ANY;
208 m4.imr_ifindex = id;
209 #else
210 struct ip_mreq m4;
211
212 m4.imr_interface.s_addr = INADDR_ANY;
213 if (id != 0)
214 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
215
216 #endif
217 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
218
219 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
220 break;
221 }
222 return 0;
223 case AF_INET6:
224 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
225 struct ipv6_mreq m6;
226
227 memset(&m6, 0, sizeof(m6));
228 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
229 m6.ipv6mr_interface = id;
230 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
231 break;
232 }
233 return 0;
234 default:
235 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
236 return -E_ADDRESS_LOOKUP;
237 }
238 err:
239 return -ERRNO_TO_PARA_ERROR(errno);
240 }
241
242 static int udp_recv_open(struct receiver_node *rn)
243 {
244 struct private_udp_recv_data *purd;
245 struct udp_recv_args_info *c = rn->conf;
246 char *iface = c->iface_given ? c->iface_arg : NULL;
247 int ret;
248
249 rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
250 rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
251 purd = rn->private_data;
252
253 ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
254 if (ret < 0)
255 goto err;
256 purd->fd = ret;
257
258 ret = mcast_receiver_setup(purd->fd, iface);
259 if (ret < 0) {
260 close(purd->fd);
261 return ret;
262 }
263
264 ret = mark_fd_nonblocking(purd->fd);
265 if (ret < 0)
266 goto err;
267 PARA_NOTICE_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
268 c->port_arg, purd->fd);
269 return purd->fd;
270 err:
271 free(rn->private_data);
272 free(rn->buf);
273 return ret;
274 }
275
276 /**
277 * The init function of the udp receiver.
278 *
279 * \param r Pointer to the receiver struct to initialize.
280 *
281 * Initialize all function pointers of \a r.
282 */
283 void udp_recv_init(struct receiver *r)
284 {
285 struct udp_recv_args_info dummy;
286
287 udp_recv_cmdline_parser_init(&dummy);
288 r->shutdown = udp_shutdown;
289 r->open = udp_recv_open;
290 r->close = udp_recv_close;
291 r->pre_select = udp_recv_pre_select;
292 r->post_select = udp_recv_post_select;
293 r->parse_config = udp_recv_parse_config;
294 r->help = (struct ggo_help) {
295 .short_help = udp_recv_args_info_help,
296 .detailed_help = udp_recv_args_info_detailed_help
297 };
298 udp_recv_cmdline_parser_free(&dummy);
299 }