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