vss: Don't treat EAGAIN as an error when reading from the afs pipe.
[paraslash.git] / udp_send.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
7 /** \file udp_send.c Para_server's udp sender. */
8
9
10 #include <regex.h>
11 #include <sys/time.h>
12 #include <dirent.h>
13 #include <sys/socket.h>
14 #include <net/if.h>
15 #include <osl.h>
16
17 #include "server.cmdline.h"
18 #include "para.h"
19 #include "error.h"
20 #include "string.h"
21 #include "afh.h"
22 #include "afs.h"
23 #include "server.h"
24 #include "vss.h"
25 #include "list.h"
26 #include "send.h"
27 #include "portable_io.h"
28 #include "net.h"
29 #include "fd.h"
30 #include "sched.h"
31 #include "close_on_fork.h"
32 #include "chunk_queue.h"
33
34 /** Describes one entry in the list of targets for the udp sender. */
35 struct udp_target {
36         /** The position of this target in the list of targets. */
37         struct list_head node;
38         /** The hostname (DNS name or IPv4/v6 address string). */
39         char host[MAX_HOSTLEN];
40         /** The UDP port. */
41         int port;
42         /** The socket fd. */
43         int fd;
44         /** The list of queued chunks for this fd. */
45         struct chunk_queue *cq;
46         /** The opaque structure returned by vss_add_fec_client(). */
47         struct fec_client *fc;
48         /** The FEC parameters for this target. */
49         struct fec_client_parms fcp;
50 };
51
52 static struct list_head targets;
53 static int sender_status;
54
55 static void udp_close_target(struct udp_target *ut)
56 {
57         if (ut->fd < 0)
58                 return;
59         close(ut->fd);
60         del_close_on_fork_list(ut->fd);
61         cq_destroy(ut->cq);
62         ut->cq = NULL;
63         ut->fd = -1;
64 }
65
66 static void udp_delete_target(struct udp_target *ut, const char *msg)
67 {
68         PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
69                 ut->port, msg);
70         udp_close_target(ut);
71         vss_del_fec_client(ut->fc);
72         list_del(&ut->node);
73         free(ut);
74 }
75
76 /**
77  * Perform AF-independent multicast sender setup.
78  *
79  * \param fd    The connected socket descriptor.
80  * \param ttl   UDPv4 multicast TTL or UDPv6 multicast number of hops.
81  *              Use -1 to mean default, 0..255 otherwise.
82  * \param iface The outgoing multicast interface, or NULL for the default.
83  *
84  * \return Zero if okay, negative on error.
85  */
86 static int mcast_sender_setup(struct udp_target *ut, int ttl, char *iface)
87 {
88         struct sockaddr_storage ss;
89         socklen_t sslen = sizeof(ss);
90
91         const int on = 1;
92         int id = iface == NULL ? 0 : if_nametoindex(iface);
93
94         if (getpeername(ut->fd, (struct sockaddr *)&ss, &sslen) < 0)
95                 goto err;
96
97         if (iface != NULL && id == 0)
98                 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
99
100         /* RFC 3493, 5.2: -1 means 'use kernel default' */
101         if (ttl < 0 || ttl > 255)
102                 ttl = -1;
103
104         switch (ss.ss_family) {
105         case AF_INET:
106                 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
107                         return 0;
108                 if (id != 0) {
109 #ifdef HAVE_IP_MREQN
110                         struct ip_mreqn mn;
111
112                         memset(&mn, 0, sizeof(mn));
113                         mn.imr_ifindex = id;
114                         if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
115                                 goto err;
116 #else
117                         PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
118 #endif
119                 }
120                 /*
121                  * Enable receiving multicast messages generated on the local host
122                  * At least on Linux, this is enabled by default.
123                  */
124                 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
125                         break;
126
127                 /* Default: use local subnet (do not flood out into the WAN) */
128                 if (ttl == -1)
129                         ttl = 1;
130                 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
131                         break;
132                 return 0;
133         case AF_INET6:
134                 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
135                         return 0;
136                 if (id != 0 &&
137                     setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
138                         break;
139                 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
140                         break;
141                 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
142                         break;
143                 return 0;
144         default:
145                 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
146                 return -E_ADDRESS_LOOKUP;
147         }
148 err:
149         return -ERRNO_TO_PARA_ERROR(errno);
150 }
151
152 /** The maximal size of the per-target chunk queue. */
153 #define UDP_CQ_BYTES 40000
154
155 static int udp_init_session(struct udp_target *ut)
156 {
157         int ret;
158         char *iface = NULL;
159
160         if (ut->fd >= 0) /* nothing to do */
161                 return 0;
162
163         ret = para_connect_simple(IPPROTO_UDP, ut->host, ut->port);
164         if (ret < 0)
165                 return ret;
166         ut->fd = ret;
167
168         if (conf.udp_mcast_iface_given)
169                 iface = conf.udp_mcast_iface_arg;
170
171         ret = mcast_sender_setup(ut, conf.udp_ttl_arg, iface);
172         if (ret < 0) {
173                 close(ut->fd);
174                 return ret;
175         }
176
177         ret = mark_fd_nonblocking(ut->fd);
178         if (ret < 0) {
179                 close(ut->fd);
180                 return ret;
181         }
182         add_close_on_fork_list(ut->fd);
183         ut->cq = cq_new(UDP_CQ_BYTES);
184         PARA_NOTICE_LOG("sending to udp %s#%d\n", ut->host, ut->port);
185         return 1;
186 }
187
188 static void udp_shutdown_targets(void)
189 {
190         struct udp_target *ut, *tmp;
191         const char *buf = NULL;
192         size_t len = 0; /* STFU, gcc */
193
194         list_for_each_entry_safe(ut, tmp, &targets, node) {
195                 int ubuntu_glibc_headers_suck;
196                 if (ut->fd < 0)
197                         continue;
198                 if (!buf)
199                         len = vss_get_fec_eof_packet(&buf);
200                 /* ignore return value, we're closing the target anyway. */
201                 ubuntu_glibc_headers_suck = write(ut->fd, buf, len); /* STFU */
202                 udp_close_target(ut);
203         }
204 }
205
206 static int udp_com_on(__a_unused struct sender_command_data *scd)
207 {
208         sender_status = SENDER_ON;
209         return 1;
210 }
211
212 static int udp_com_off(__a_unused struct sender_command_data *scd)
213 {
214         udp_shutdown_targets();
215         sender_status = SENDER_OFF;
216         return 1;
217 }
218
219 static int udp_com_delete(struct sender_command_data *scd)
220 {
221         struct udp_target *ut, *tmp;
222
223         list_for_each_entry_safe(ut, tmp, &targets, node) {
224                 /* Unspecified port means wildcard port match */
225                 if (scd->port > 0 && scd->port != ut->port)
226                         continue;
227                 if (strcmp(ut->host, scd->host))
228                         continue;
229                 udp_delete_target(ut, "com_delete");
230         }
231         return 1;
232 }
233
234 static int udp_send_fec(char *buf, size_t len, void *private_data)
235 {
236         struct udp_target *ut = private_data;
237         int ret;
238
239         if (sender_status == SENDER_OFF)
240                 return 0;
241         ret = udp_init_session(ut);
242         if (ret < 0)
243                 goto fail;
244         ret = send_queued_chunks(ut->fd, ut->cq, 0);
245         if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
246                 ret = 0;
247         if (ret < 0)
248                 goto fail;
249         if (!len)
250                 return 0;
251         if (!ret) { /* still data left in the queue */
252                 ret = cq_force_enqueue(ut->cq, buf, len);
253                 assert(ret >= 0);
254         }
255         ret = write_nonblock(ut->fd, buf, len, 0);
256         if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
257                 ret = 0;
258         if (ret < 0)
259                 goto fail;
260         if (ret != len) {
261                 ret = cq_force_enqueue(ut->cq, buf + ret, len - ret);
262                 assert(ret >= 0);
263         }
264         return 1;
265 fail:
266         udp_delete_target(ut, para_strerror(-ret));
267         return ret;
268 }
269
270 static void udp_add_target(struct sender_command_data *scd)
271 {
272         struct udp_target *ut = para_calloc(sizeof(struct udp_target));
273
274         strncpy(ut->host, scd->host, sizeof(ut->host));
275         ut->port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
276         ut->fd = -1; /* not yet connected */
277         PARA_INFO_LOG("adding to target list (%s#%d)\n", ut->host, ut->port);
278         para_list_add(&ut->node, &targets);
279         ut->fcp.slices_per_group = scd->slices_per_group;
280         ut->fcp.data_slices_per_group = scd->data_slices_per_group;
281         ut->fcp.max_slice_bytes = scd->max_slice_bytes;
282         ut->fcp.send = udp_send_fec;
283         ut->fcp.private_data = ut;
284         vss_add_fec_client(&ut->fcp, &ut->fc);
285 }
286
287 static int udp_com_add(struct sender_command_data *scd)
288 {
289         udp_add_target(scd);
290         return 1;
291 }
292
293 static char *udp_info(void)
294 {
295         struct udp_target *ut;
296         char *ret, *tgts = NULL;
297
298         list_for_each_entry(ut, &targets, node) {
299                 bool is_v6 = strchr(ut->host, ':') != NULL;
300                 char *tmp = make_message("%s%s%s%s:%d/%u:%u:%u ", tgts ? : "",
301                         is_v6 ? "[" : "", ut->host,
302                         is_v6 ? "]" : "", ut->port,
303                         ut->fcp.max_slice_bytes,
304                         ut->fcp.data_slices_per_group,
305                         ut->fcp.slices_per_group
306                 );
307                 free(tgts);
308                 tgts = tmp;
309         }
310         ret = make_message(
311                 "udp sender:\n"
312                 "\tstatus: %s\n"
313                 "\tport: %s\n"
314                 "\ttargets: %s\n",
315                 (sender_status == SENDER_ON)? "on" : "off",
316                 stringify_port(conf.udp_default_port_arg, "udp"),
317                 tgts? tgts : "(none)"
318         );
319         free(tgts);
320         return ret;
321 }
322
323 static void udp_init_target_list(void)
324 {
325         struct sender_command_data scd;
326         int i;
327
328         INIT_LIST_HEAD(&targets);
329         for (i = 0; i < conf.udp_target_given; i++) {
330                 if (parse_fec_url(conf.udp_target_arg[i], &scd) < 0) {
331                         PARA_CRIT_LOG("syntax error for udp target option "
332                                 "#%d, ignoring\n", i);
333                         continue;
334                 }
335                 udp_add_target(&scd);
336         }
337 }
338
339 static char *udp_help(void)
340 {
341         return make_message(
342                 "usage: {on|off}\n"
343                 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
344                 "examples: add 224.0.1.38:1500  (IPv4 multicast)\n"
345                 "          add 224.0.1.38:1500/1400:14:16\n"
346                 "              (likewise, using 1400 byte packets, with 14\n"
347                 "               data slices per 16 slice FEC group)\n"
348                 "          add 10.10.1.42       (using default port)\n"
349                 "          add [FF05::42]:1500  (IPv6 multicast)\n"
350                 "          add [::1]            (IPv6 localhost/default port)\n"
351                 "          delete myhost.net    (host with port wildcard)\n"
352                 "          delete [badc0de::1]  (IPv6 with port wildcard)\n"
353         );
354 }
355
356 /**
357  * The init function of para_server's udp sender.
358  *
359  * \param s Pointer to the udp sender struct.
360  *
361  * It initializes all function pointers of \a s and the list of udp targets.
362  */
363 void udp_send_init(struct sender *s)
364 {
365         INIT_LIST_HEAD(&targets);
366         s->info = udp_info;
367         s->help = udp_help;
368         s->send = NULL;
369         s->pre_select = NULL;
370         s->post_select = NULL;
371         s->shutdown_clients = udp_shutdown_targets;
372         s->client_cmds[SENDER_ON] = udp_com_on;
373         s->client_cmds[SENDER_OFF] = udp_com_off;
374         s->client_cmds[SENDER_DENY] = NULL;
375         s->client_cmds[SENDER_ALLOW] = NULL;
376         s->client_cmds[SENDER_ADD] = udp_com_add;
377         s->client_cmds[SENDER_DELETE] = udp_com_delete;
378         sender_status = SENDER_OFF;
379         udp_init_target_list();
380         if (!conf.udp_no_autostart_given)
381                 sender_status = SENDER_ON;
382         PARA_DEBUG_LOG("udp sender init complete\n");
383 }