new codename, reset version to git
[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", iface);
149
150 switch (ss.ss_family) {
151 case AF_INET:
152 if (IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr))) {
153 #ifdef HAVE_IP_MREQN
154 struct ip_mreqn m4;
155
156 m4.imr_address.s_addr = INADDR_ANY;
157 m4.imr_ifindex = id;
158 #else
159 struct ip_mreq m4;
160
161 m4.imr_interface.s_addr = INADDR_ANY;
162 if (id != 0)
163 PARA_ERROR_LOG("Setting IPv4 receiver mcast interface not supported.");
164
165 #endif
166 m4.imr_multiaddr = ((struct sockaddr_in *)&ss)->sin_addr;
167
168 if (setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m4, sizeof(m4)) < 0)
169 break;
170 }
171 return 0;
172 case AF_INET6:
173 if (IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr)) {
174 struct ipv6_mreq m6;
175
176 memset(&m6, 0, sizeof(m6));
177 memcpy(&m6.ipv6mr_multiaddr, &((struct sockaddr_in6 *)&ss)->sin6_addr, 16);
178 m6.ipv6mr_interface = id;
179 if (setsockopt(fd, IPPROTO_IPV6, IPV6_JOIN_GROUP, &m6, sizeof(m6)) < 0)
180 break;
181 }
182 return 0;
183 }
184 err:
185 return -ERRNO_TO_PARA_ERROR(errno);
186 }
187
188 static int udp_recv_open(struct receiver_node *rn)
189 {
190 struct private_udp_recv_data *purd;
191 struct udp_recv_args_info *c = rn->conf;
192 char *iface = c->iface_given ? c->iface_arg : NULL;
193 int ret;
194
195 rn->private_data = para_calloc(sizeof(struct private_udp_recv_data));
196 purd = rn->private_data;
197
198 ret = makesock(IPPROTO_UDP, 1, c->host_arg, c->port_arg, NULL);
199 if (ret < 0)
200 goto err;
201 purd->fd = ret;
202
203 ret = mcast_receiver_setup(purd->fd, iface);
204 if (ret < 0) {
205 close(purd->fd);
206 goto err;
207 }
208
209 ret = mark_fd_nonblocking(purd->fd);
210 if (ret < 0) {
211 close(purd->fd);
212 goto err;
213 }
214 PARA_INFO_LOG("receiving from %s:%d, fd=%d\n", c->host_arg,
215 c->port_arg, purd->fd);
216 purd->btrp = btr_pool_new("udp_recv", 320 * 1024);
217 return purd->fd;
218 err:
219 free(rn->private_data);
220 return ret;
221 }
222
223 static void udp_recv_free_config(void *conf)
224 {
225 udp_recv_cmdline_parser_free(conf);
226 free(conf);
227 }
228
229 /**
230 * The init function of the udp receiver.
231 *
232 * \param r Pointer to the receiver struct to initialize.
233 *
234 * Initialize all function pointers of \a r.
235 */
236 void udp_recv_init(struct receiver *r)
237 {
238 struct udp_recv_args_info dummy;
239
240 udp_recv_cmdline_parser_init(&dummy);
241 r->open = udp_recv_open;
242 r->close = udp_recv_close;
243 r->pre_select = udp_recv_pre_select;
244 r->post_select = udp_recv_post_select;
245 r->parse_config = udp_recv_parse_config;
246 r->free_config = udp_recv_free_config;
247 r->help = (struct ggo_help) {
248 .short_help = udp_recv_args_info_help,
249 .detailed_help = udp_recv_args_info_detailed_help
250 };
251 udp_recv_cmdline_parser_free(&dummy);
252 }