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