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