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