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