Merge branch 'maint'
[paraslash.git] / udp_send.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <sys/types.h>
13 #include <netinet/udp.h>
14 #include <net/if.h>
15 #include <arpa/inet.h>
16 #include <sys/un.h>
17 #include <netdb.h>
18 #include <lopsub.h>
19
20 #include "server.lsg.h"
21 #include "para.h"
22 #include "error.h"
23 #include "string.h"
24 #include "afh.h"
25 #include "server.h"
26 #include "list.h"
27 #include "send.h"
28 #include "sched.h"
29 #include "vss.h"
30 #include "portable_io.h"
31 #include "net.h"
32 #include "fd.h"
33 #include "close_on_fork.h"
34
35 /**
36  * Time window during which ICMP Destination/Port Unreachable messages are
37  * ignored, covering transient receiver problems such as restarting the
38  * client, rebooting, reconfiguration, or handover.
39  */
40 #define UDP_MAX_UNREACHABLE_TIME 30
41
42 /** Describes one entry in the list of targets for the udp sender. */
43 struct udp_target {
44         /** Track time (seconds) of last ICMP Port Unreachable error */
45         time_t last_unreachable;
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         /** Whether we already sent the FEC eof packet to this target. */
51         bool sent_fec_eof;
52 };
53
54 static struct list_head targets;
55 static int sender_status;
56
57 static void udp_close_target(struct sender_client *sc)
58 {
59         const char *buf;
60         size_t len;
61         struct udp_target *ut = sc->private_data;
62
63         if (ut->sent_fec_eof)
64                 return;
65         PARA_NOTICE_LOG("sending FEC EOF\n");
66         len = vss_get_fec_eof_packet(&buf);
67         /* Ignore write() errors since we are closing the target anyway. */
68         if (write(sc->fd, buf, len))
69                 do_nothing; /* avoid "ignoring return value" warning */
70         ut->sent_fec_eof = true;
71 }
72
73 static void udp_delete_target(struct sender_client *sc, const char *msg)
74 {
75         struct udp_target *ut = sc->private_data;
76
77         PARA_NOTICE_LOG("deleting %s (%s) from list\n", sc->name, msg);
78         udp_close_target(sc);
79         close(sc->fd);
80         del_close_on_fork_list(sc->fd);
81         vss_del_fec_client(ut->fc);
82         list_del(&sc->node);
83         free(sc->name);
84         free(sc);
85         free(ut);
86 }
87
88 /**
89  * Perform AF-independent multicast sender setup.
90  *
91  * \param sc    The connected sender client.
92  *
93  * \return Zero if okay, negative on error.
94  */
95 static int mcast_sender_setup(struct sender_client *sc)
96 {
97         struct sockaddr_storage ss;
98         socklen_t sslen = sizeof(ss);
99         int ttl = OPT_INT32_VAL(UDP_TTL), id = 0;
100         const int on = 1;
101
102         if (OPT_GIVEN(UDP_MCAST_IFACE)) {
103                 const char *iface = OPT_STRING_VAL(UDP_MCAST_IFACE);
104
105                 id = if_nametoindex(iface);
106                 if (id == 0)
107                         PARA_WARNING_LOG("could not resolve interface '%s', "
108                                         "using default", iface);
109         }
110
111         if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0)
112                 goto err;
113         assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
114
115         /* RFC 3493, 5.2: -1 means 'use kernel default' */
116         if (ttl < 0 || ttl > 255)
117                 ttl = -1;
118
119         switch (ss.ss_family) {
120         case AF_INET:
121                 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
122                         return 0;
123                 if (id != 0) {
124 #ifdef HAVE_IP_MREQN
125                         struct ip_mreqn mn;
126
127                         memset(&mn, 0, sizeof(mn));
128                         mn.imr_ifindex = id;
129                         if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
130                                 goto err;
131 #else
132                         PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
133 #endif
134                 }
135                 /*
136                  * Enable receiving multicast messages generated on the local host
137                  * At least on Linux, this is enabled by default.
138                  */
139                 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
140                         break;
141
142                 /* Default: use local subnet (do not flood out into the WAN) */
143                 if (ttl == -1)
144                         ttl = 1;
145                 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
146                         break;
147                 return 0;
148         case AF_INET6:
149                 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
150                         return 0;
151                 if (id != 0 &&
152                     setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
153                         break;
154                 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
155                         break;
156                 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
157                         break;
158                 return 0;
159         }
160 err:
161         return -ERRNO_TO_PARA_ERROR(errno);
162 }
163
164 static void udp_shutdown_targets(void)
165 {
166         struct sender_client *sc, *tmp;
167         list_for_each_entry_safe(sc, tmp, &targets, node)
168                 udp_close_target(sc);
169 }
170
171 static int udp_resolve_target(const char *url, struct sender_command_data *scd)
172 {
173         const char *result;
174         int ret, port;
175
176         ret = parse_fec_url(url, scd);
177         if (ret)
178                 return ret;
179         port = scd->port > 0 ? scd->port : OPT_UINT32_VAL(UDP_DEFAULT_PORT);
180
181         ret = para_connect_simple(IPPROTO_UDP, scd->host, port);
182         if (ret < 0)
183                 return ret;
184
185         result = remote_name(ret);
186         close(ret);
187
188         if (!parse_url(result, scd->host, sizeof(scd->host), &scd->port))
189                 return -E_ADDRESS_LOOKUP;
190         return 1;
191 }
192
193 static int udp_com_on(__a_unused struct sender_command_data *scd)
194 {
195         sender_status = SENDER_on;
196         return 1;
197 }
198
199 static int udp_com_off(__a_unused struct sender_command_data *scd)
200 {
201         udp_shutdown_targets();
202         sender_status = SENDER_off;
203         return 1;
204 }
205
206 static struct sender_client *udp_lookup_target(struct sender_command_data *scd)
207 {
208         struct sender_client *sc, *tmp;
209         char host[MAX_HOSTLEN];
210         int32_t port;
211
212         list_for_each_entry_safe(sc, tmp, &targets, node) {
213                 parse_url(sc->name, host, sizeof(host), &port);
214
215                 /* Unspecified port means wildcard port match */
216                 if (scd->port > 0 && scd->port != port)
217                         continue;
218                 if (strcmp(host, scd->host))
219                         continue;
220                 return sc;
221         }
222         return NULL;
223 }
224
225 static int udp_com_delete(struct sender_command_data *scd)
226 {
227         struct sender_client *sc = udp_lookup_target(scd);
228
229         if (sc) {
230                 udp_delete_target(sc, "com_delete");
231                 return 1;
232         }
233         PARA_NOTICE_LOG("not deleting non-existing target '%s'\n", scd->host);
234         return -E_TARGET_NOT_FOUND;
235 }
236
237 /** Initialize UDP session and set maximum payload size. */
238 static int udp_init_fec(struct sender_client *sc)
239 {
240         struct udp_target *ut = sc->private_data;
241         int mps;
242
243         PARA_NOTICE_LOG("sending to udp %s\n", sc->name);
244         ut->sent_fec_eof = false;
245         mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
246         PARA_INFO_LOG("current MPS = %d bytes\n", mps);
247         return mps;
248 }
249
250 /** Check and clear socket error if any. */
251 static int udp_check_socket_state(struct sender_client *sc)
252 {
253         struct udp_target *ut = sc->private_data;
254         int ret;
255         socklen_t errlen = sizeof(ret);
256
257         if (getsockopt(sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) {
258                 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret));
259                 return 0;
260         } else if (ret == 0) {
261                 return 0;
262         } else if (ret == ECONNREFUSED) {
263                 time_t dist = now->tv_sec - ut->last_unreachable;
264
265                 if (dist <= UDP_MAX_UNREACHABLE_TIME) {
266                         return 0;
267                 } else if (dist > 2 * UDP_MAX_UNREACHABLE_TIME) {
268                         ut->last_unreachable = now->tv_sec;
269                         return 0;
270                 } else {
271                         /*
272                          * unreachable_time < dist <= 2 * unreachable_time
273                          * No errors are allowed during this time window.
274                          */
275                         PARA_NOTICE_LOG("Evicting %s after %d seconds "
276                                         "of connection errors.\n",
277                                         sc->name, (int)dist);
278                 }
279         }
280         return -ERRNO_TO_PARA_ERROR(ret);
281 }
282
283 static void udp_send_fec(struct sender_client *sc, char *buf, size_t len)
284 {
285         int ret;
286
287         if (sender_status == SENDER_off)
288                 return;
289         if (len == 0)
290                 return;
291         ret = udp_check_socket_state(sc);
292         if (ret < 0)
293                 goto fail;
294         ret = xwrite(sc->fd, buf, len);
295         if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED)) {
296                 /*
297                  * Happens if meanwhile an ICMP Destination / Port Unreachable
298                  * has arrived. Ignore, persistent errors will be caught above.
299                  */
300                 ret = 0;
301         }
302         if (ret < 0)
303                 goto fail;
304         return;
305 fail:
306         udp_delete_target(sc, para_strerror(-ret));
307 }
308
309 static int udp_com_add(struct sender_command_data *scd)
310 {
311         int ret;
312         struct udp_target *ut;
313         struct sender_client *sc = udp_lookup_target(scd);
314
315         if (sc) {
316                 PARA_NOTICE_LOG("target %s already exists - not adding it\n",
317                                 sc->name);
318                 return -E_TARGET_EXISTS;
319         }
320         ut = para_calloc(sizeof(*ut));
321         sc = para_calloc(sizeof(*sc));
322         ut->fcp.slices_per_group      = scd->slices_per_group;
323         ut->fcp.data_slices_per_group = scd->data_slices_per_group;
324         ut->fcp.init_fec              = udp_init_fec;
325         ut->fcp.send_fec              = udp_send_fec;
326         ut->fcp.need_periodic_header  = true;
327
328         sc->private_data = ut;
329         sc->fd = -1;
330         ret = para_connect_simple(IPPROTO_UDP, scd->host, scd->port);
331         if (ret < 0)
332                 goto err;
333         sc->fd = ret;
334
335         ret = mcast_sender_setup(sc);
336         if (ret < 0)
337                 goto err;
338         ret = mark_fd_nonblocking(sc->fd);
339         if (ret < 0)
340                 goto err;
341         sc->name = para_strdup(remote_name(sc->fd));
342         PARA_INFO_LOG("adding to target list (%s)\n", sc->name);
343         ut->fc = vss_add_fec_client(sc, &ut->fcp);
344         para_list_add(&sc->node, &targets);
345         add_close_on_fork_list(sc->fd);
346         return 1;
347 err:
348         if (sc->fd >= 0)
349                 close(sc->fd);
350         PARA_NOTICE_LOG("failed to set up %s:%d (%s)- not adding it\n",
351                         scd->host, scd->port, para_strerror(-ret));
352         free(sc);
353         free(ut);
354         return ret;
355 }
356
357 static char *udp_status(void)
358 {
359         struct sender_client *sc;
360         char *ret, *tgts = NULL;
361
362         list_for_each_entry(sc, &targets, node) {
363                 struct udp_target *ut = sc->private_data;
364                 char *tmp = make_message("%s%s/%u:%u ",
365                         tgts ? : "", sc->name,
366                         ut->fcp.data_slices_per_group,
367                         ut->fcp.slices_per_group
368                 );
369                 free(tgts);
370                 tgts = tmp;
371         }
372         ret = make_message(
373                 "status: %s\n"
374                 "port: %s\n"
375                 "targets: %s\n",
376                 (sender_status == SENDER_on)? "on" : "off",
377                 stringify_port(OPT_UINT32_VAL(UDP_DEFAULT_PORT), "udp"),
378                 tgts? tgts : "(none)"
379         );
380         free(tgts);
381         return ret;
382 }
383
384 static void udp_init_target_list(void)
385 {
386         struct sender_command_data scd;
387         int i;
388
389         INIT_LIST_HEAD(&targets);
390         for (i = 0; i < OPT_GIVEN(UDP_TARGET); i++) {
391                 const char *arg = lls_string_val(i, OPT_RESULT(UDP_TARGET));
392                 if (udp_resolve_target(arg, &scd) < 0)
393                         PARA_CRIT_LOG("not adding requested target '%s'\n",
394                                         arg);
395                 else
396                         udp_com_add(&scd);
397         }
398 }
399
400 static char *udp_help(void)
401 {
402         return make_message(
403                 "usage: {on|off}\n"
404                 "usage: {add|delete} ip_address[:port][/[packet_size:]k:n]\n"
405                 "       - k is the number of data slices per FEC group\n"
406                 "       - n is the total number of slices in a FEC group\n"
407                 "       - packet_size reduces the slice size below path MTU\n\n"
408                 "examples: add 224.0.1.38:1500  (IPv4 multicast)\n"
409                 "          add 224.0.1.38:8080/14:16 (explicit FEC)\n"
410                 "          add 10.10.1.42       (using default port)\n"
411                 "          add [FF05::42]:1500  (IPv6 multicast)\n"
412                 "          add [::1]            (IPv6 localhost/default port)\n"
413                 "          delete myhost.net    (host with port wildcard)\n"
414                 "          delete 10.1.2.3:456  (IPv4 with explicit port)\n"
415                 "          delete [badc0de::1]  (IPv6 with port wildcard)\n"
416         );
417 }
418
419 /**
420  * The init function of para_server's udp sender.
421  *
422  * \param s Pointer to the udp sender struct.
423  *
424  * It initializes all function pointers of \a s and the list of udp targets.
425  */
426 void udp_send_init(struct sender *s)
427 {
428         INIT_LIST_HEAD(&targets);
429         s->status = udp_status;
430         s->help = udp_help;
431         s->send = NULL;
432         s->pre_select = NULL;
433         s->post_select = NULL;
434         s->shutdown_clients = udp_shutdown_targets;
435         s->resolve_target = udp_resolve_target;
436         s->client_cmds[SENDER_on] = udp_com_on;
437         s->client_cmds[SENDER_off] = udp_com_off;
438         s->client_cmds[SENDER_deny] = NULL;
439         s->client_cmds[SENDER_allow] = NULL;
440         s->client_cmds[SENDER_add] = udp_com_add;
441         s->client_cmds[SENDER_delete] = udp_com_delete;
442         sender_status = SENDER_off;
443         udp_init_target_list();
444         if (!OPT_GIVEN(UDP_NO_AUTOSTART))
445                 sender_status = SENDER_on;
446         PARA_DEBUG_LOG("udp sender init complete\n");
447 }