Introduce per group slice sizes.
[paraslash.git] / udp_recv.c
1 /*
2  * Copyright (C) 2005-2010 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 /**
27  * Data specific to the udp receiver.
28  *
29  * \sa \ref receiver, \ref receiver_node.
30  */
31 struct private_udp_recv_data {
32         /** The socket file descriptor. */
33         int fd;
34         struct btr_pool *btrp;
35 };
36
37 static void udp_recv_pre_select(struct sched *s, struct task *t)
38 {
39         struct receiver_node *rn = container_of(t, struct receiver_node, task);
40         struct private_udp_recv_data *purd = rn->private_data;
41
42         if (generic_recv_pre_select(s, t) <= 0)
43                 return;
44         para_fd_set(purd->fd, &s->rfds, &s->max_fileno);
45 }
46
47 static int udp_check_eof(size_t sz, struct iovec iov[2])
48 {
49         if (sz < FEC_EOF_PACKET_LEN)
50                 return 0;
51         if (iov[0].iov_len >= FEC_EOF_PACKET_LEN) {
52                 if (memcmp(iov[0].iov_base, FEC_EOF_PACKET,
53                                 FEC_EOF_PACKET_LEN) != 0)
54                         return 0;
55                 return -E_RECV_EOF;
56         }
57         if (memcmp(iov[0].iov_base, FEC_EOF_PACKET, iov[0].iov_len) != 0)
58                 return 0;
59         if (memcmp(iov[1].iov_base, FEC_EOF_PACKET + iov[0].iov_len,
60                         FEC_EOF_PACKET_LEN - iov[0].iov_len) != 0)
61                 return 0;
62         return -E_RECV_EOF;
63 }
64
65 static void udp_recv_post_select(__a_unused struct sched *s, struct task *t)
66 {
67         struct receiver_node *rn = container_of(t, struct receiver_node, task);
68         struct private_udp_recv_data *purd = rn->private_data;
69         struct btr_node *btrn = rn->btrn;
70         size_t num_bytes;
71         struct iovec iov[2];
72         int ret, readv_ret, iovcnt;
73
74         t->error = 0;
75         ret = btr_node_status(btrn, 0, BTR_NT_ROOT);
76         if (ret <= 0)
77                 goto out;
78         iovcnt = btr_pool_get_buffers(purd->btrp, iov);
79         ret = -E_UDP_OVERRUN;
80         if (iovcnt == 0)
81                 goto out;
82         ret = readv_nonblock(purd->fd, iov, iovcnt, &s->rfds, &num_bytes);
83         if (num_bytes == 0)
84                 goto out;
85         readv_ret = ret;
86         ret = udp_check_eof(num_bytes, iov);
87         if (ret < 0)
88                 goto out;
89         if (iov[0].iov_len >= num_bytes)
90                 btr_add_output_pool(purd->btrp, num_bytes, btrn);
91         else { /* both buffers contain data */
92                 btr_add_output_pool(purd->btrp, iov[0].iov_len, btrn);
93                 btr_add_output_pool(purd->btrp, num_bytes - iov[0].iov_len,
94                         btrn);
95         }
96         ret = readv_ret;
97 out:
98         if (ret >= 0)
99                 return;
100         btr_remove_node(btrn);
101         t->error = ret;
102         close(purd->fd);
103         purd->fd = -1;
104 }
105
106 static void udp_recv_close(struct receiver_node *rn)
107 {
108         struct private_udp_recv_data *purd = rn->private_data;
109
110         if (purd->fd >= 0)
111                 close(purd->fd);
112         btr_pool_free(purd->btrp);
113         free(rn->private_data);
114 }
115
116 static void *udp_recv_parse_config(int argc, char **argv)
117 {
118         int ret;
119         struct udp_recv_args_info *tmp =
120                 para_calloc(sizeof(struct udp_recv_args_info));
121
122         ret = udp_recv_cmdline_parser(argc, argv, tmp)? -E_UDP_SYNTAX : 1;
123         if (ret >= 0)
124                 return tmp;
125         free(tmp);
126         return NULL;
127 }
128
129 /*
130  * Perform AF-independent joining of multicast receive addresses.
131  *
132  * \param fd    Bound socket descriptor.
133  * \param iface The receiving multicast interface, or NULL for the default.
134  *
135  * \return Zero if okay, negative on error.
136  */
137 static int mcast_receiver_setup(int fd, const char *iface)
138 {
139         struct sockaddr_storage ss;
140         socklen_t sslen = sizeof(ss);
141         int id = iface == NULL ? 0 : if_nametoindex(iface);
142
143         if (getsockname(fd, (struct sockaddr *)&ss, &sslen) < 0)
144                 goto err;
145         assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
146
147         if (iface != NULL && id == 0)
148                 PARA_WARNING_LOG("could not resolve interface %s, using default\n",
149                         iface);
150
151         switch (ss.ss_family) {
152         case AF_INET:
153                 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
154 #ifdef HAVE_IP_MREQN
155                         struct ip_mreqn m4;
156
157                         m4.imr_address.s_addr   = INADDR_ANY;
158                         m4.imr_ifindex          = id;
159 #else
160                         struct ip_mreq m4;
161
162                         m4.imr_interface.s_addr = INADDR_ANY;
163                         if (id != 0)
164                                 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported\n");
165
166 #endif
167                         m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
168
169                         if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
170                                         &m4, sizeof(m4)) < 0)
171                                 break;
172                 }
173                 return 0;
174         case AF_INET6:
175                 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
176                         struct ipv6_mreq m6;
177
178                         memset(&m6, 0, sizeof(m6));
179                         memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
180                         m6.ipv6mr_interface = id;
181                         if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
182                                 break;
183                 }
184                 return 0;
185         }
186 err:
187         return -ERRNO_TO_PARA_ERROR(errno);
188 }
189
190 static int udp_recv_open(struct receiver_node *rn)
191 {
192         struct private_udp_recv_data *purd;
193         struct udp_recv_args_info *c = rn->conf;
194         char  *iface = c->iface_given ? c->iface_arg : NULL;
195         int ret;
196
197         rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
198         purd = rn->private_data;
199
200         ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
201         if (ret < 0)
202                 goto err;
203         purd->fd = ret;
204
205         ret = mcast_receiver_setup(purd->fd, iface);
206         if (ret < 0) {
207                 close(purd->fd);
208                 goto err;
209         }
210
211         ret = mark_fd_nonblocking(purd->fd);
212         if (ret < 0) {
213                 close(purd->fd);
214                 goto err;
215         }
216         PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
217                 c->port_arg, purd->fd);
218         purd->btrp = btr_pool_new("udp_recv", 320 * 1024);
219         return purd->fd;
220 err:
221         free(rn->private_data);
222         return ret;
223 }
224
225 static void udp_recv_free_config(void *conf)
226 {
227         udp_recv_cmdline_parser_free(conf);
228         free(conf);
229 }
230
231 /**
232  * The init function of the udp receiver.
233  *
234  * \param r Pointer to the receiver struct to initialize.
235  *
236  * Initialize all function pointers of \a r.
237  */
238 void udp_recv_init(struct receiver *r)
239 {
240         struct udp_recv_args_info dummy;
241
242         udp_recv_cmdline_parser_init(&dummy);
243         r->open = udp_recv_open;
244         r->close = udp_recv_close;
245         r->pre_select = udp_recv_pre_select;
246         r->post_select = udp_recv_post_select;
247         r->parse_config = udp_recv_parse_config;
248         r->free_config = udp_recv_free_config;
249         r->help = (struct ggo_help) {
250                 .short_help = udp_recv_args_info_help,
251                 .detailed_help = udp_recv_args_info_detailed_help
252         };
253         udp_recv_cmdline_parser_free(&dummy);
254 }