0c5a3abf3fd58546ea187fee33516bd761973731
[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_CRIT_LOG("%p: eof\n", rn);
134 ret = -E_RECV_EOF;
135 goto err;
136 }
137 }
138 btr_pool_allocate(purd->btrp, packet_size);
139 btr_add_output_pool(purd->btrp, buf, packet_size, btrn);
140 return;
141 err:
142 btr_remove_node(btrn);
143 t->error = ret;
144 close(purd->fd);
145 purd->fd = -1;
146 }
147
148 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
149 {
150 struct receiver_node *rn = container_of(t, struct receiver_node, task);
151 if (rn->btrn)
152 return udp_recv_post_select_btr(s, t);
153 udp_recv_post_select_nobtr(s, t);
154 }
155
156 static void udp_shutdown(void)
157 {
158 return;
159 }
160
161 static void udp_recv_close(struct receiver_node *rn)
162 {
163 struct private_udp_recv_data *purd = rn->private_data;
164
165 if (purd->fd >= 0)
166 close(purd->fd);
167 PARA_CRIT_LOG("%p: close\n", rn);
168 btr_pool_free(purd->btrp);
169 free(rn->private_data);
170 free(rn->buf);
171 }
172
173 static void *udp_recv_parse_config(int argc, char **argv)
174 {
175 int ret;
176 struct udp_recv_args_info *tmp =
177 para_calloc(sizeof(struct udp_recv_args_info));
178
179 ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
180 if (ret >= 0)
181 return tmp;
182 free(tmp);
183 return NULL;
184 }
185
186 /*
187 * Perform AF-independent joining of multicast receive addresses.
188 *
189 * \param fd Bound socket descriptor.
190 * \param iface The receiving multicast interface, or NULL for the default.
191 *
192 * \return Zero if okay, negative on error.
193 */
194 static int mcast_receiver_setup(int fd, const char *iface)
195 {
196 struct sockaddr_storage ss;
197 socklen_t sslen = sizeof(ss);
198 int id = iface == NULL ? 0 : if_nametoindex(iface);
199
200 if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
201 goto err;
202
203 if (iface != NULL && id == 0)
204 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
205
206 switch (ss.ss_family) {
207 case AF_INET:
208 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
209 #ifdef HAVE_IP_MREQN
210 struct ip_mreqn m4;
211
212 m4.imr_address.s_addr = INADDR_ANY;
213 m4.imr_ifindex = id;
214 #else
215 struct ip_mreq m4;
216
217 m4.imr_interface.s_addr = INADDR_ANY;
218 if (id != 0)
219 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
220
221 #endif
222 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
223
224 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
225 break;
226 }
227 return 0;
228 case AF_INET6:
229 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
230 struct ipv6_mreq m6;
231
232 memset(&m6, 0, sizeof(m6));
233 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
234 m6.ipv6mr_interface = id;
235 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
236 break;
237 }
238 return 0;
239 default:
240 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
241 return -E_ADDRESS_LOOKUP;
242 }
243 err:
244 return -ERRNO_TO_PARA_ERROR(errno);
245 }
246
247 static int udp_recv_open(struct receiver_node *rn)
248 {
249 struct private_udp_recv_data *purd;
250 struct udp_recv_args_info *c = rn->conf;
251 char *iface = c->iface_given ? c->iface_arg : NULL;
252 int ret;
253
254 rn->buf = para_calloc(UDP_RECV_CHUNK_SIZE);
255 rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
256 purd = rn->private_data;
257
258 ret = makesock(AF_UNSPEC, IPPROTO_UDP, 1, c->host_arg, c->port_arg);
259 if (ret < 0)
260 goto err;
261 purd->fd = ret;
262
263 ret = mcast_receiver_setup(purd->fd, iface);
264 if (ret < 0) {
265 close(purd->fd);
266 return ret;
267 }
268
269 ret = mark_fd_nonblocking(purd->fd);
270 if (ret < 0) {
271 close(purd->fd);
272 goto err;
273 }
274 PARA_CRIT_LOG("rn %p: receiving from %s:%d, fd=%d\n", rn, c->host_arg,
275 c->port_arg, purd->fd);
276 purd->btrp = btr_pool_new(320 * 1024);
277 return purd->fd;
278 err:
279 free(rn->private_data);
280 free(rn->buf);
281 return ret;
282 }
283
284 static void udp_recv_free_config(void *conf)
285 {
286 udp_recv_cmdline_parser_free(conf);
287 }
288
289 /**
290 * The init function of the udp receiver.
291 *
292 * \param r Pointer to the receiver struct to initialize.
293 *
294 * Initialize all function pointers of \a r.
295 */
296 void udp_recv_init(struct receiver *r)
297 {
298 struct udp_recv_args_info dummy;
299
300 udp_recv_cmdline_parser_init(&dummy);
301 r->shutdown = udp_shutdown;
302 r->open = udp_recv_open;
303 r->close = udp_recv_close;
304 r->pre_select = udp_recv_pre_select;
305 r->post_select = udp_recv_post_select;
306 r->parse_config = udp_recv_parse_config;
307 r->free_config = udp_recv_free_config;
308 r->help = (struct ggo_help) {
309 .short_help = udp_recv_args_info_help,
310 .detailed_help = udp_recv_args_info_detailed_help
311 };
312 udp_recv_cmdline_parser_free(&dummy);
313 }