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