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