2 * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file udp_send.c Para_server's udp sender. */
11 #include <sys/socket.h>
12 #include <netinet/udp.h>
16 #include "server.cmdline.h"
27 #include "portable_io.h"
30 #include "close_on_fork.h"
33 * Time window during which ICMP Destination/Port Unreachable messages are
34 * ignored, covering transient receiver problems such as restarting the
35 * client, rebooting, reconfiguration, or handover.
37 #define UDP_MAX_UNREACHABLE_TIME 30
39 /** Describes one entry in the list of targets for the udp sender. */
41 /** Track time (seconds) of last ICMP Port Unreachable error */
42 time_t last_unreachable;
43 /** The opaque structure returned by vss_add_fec_client(). */
44 struct fec_client *fc;
45 /** The FEC parameters for this target. */
46 struct fec_client_parms fcp;
49 static struct list_head targets;
50 static int sender_status;
52 static void udp_close_target(struct sender_client *sc)
55 size_t len = vss_get_fec_eof_packet(&buf);
58 * Ignore the return value of wirte() since we are closing the target
59 * anyway. The sole purpose of the "do_nothing" statement is to silence
62 if (write(sc->fd, buf, len))
66 static void udp_delete_target(struct sender_client *sc, const char *msg)
68 struct udp_target *ut = sc->private_data;
70 PARA_NOTICE_LOG("deleting %s (%s) from list\n", sc->name, msg);
73 del_close_on_fork_list(sc->fd);
74 vss_del_fec_client(ut->fc);
82 * Perform AF-independent multicast sender setup.
84 * \param sc The connected sender client.
86 * \return Zero if okay, negative on error.
88 static int mcast_sender_setup(struct sender_client *sc)
90 struct sockaddr_storage ss;
91 socklen_t sslen = sizeof(ss);
92 int ttl = conf.udp_ttl_arg, id = 0;
95 if (conf.udp_mcast_iface_given) {
96 char *iface = conf.udp_mcast_iface_arg;
98 id = if_nametoindex(iface);
100 PARA_WARNING_LOG("could not resolve interface '%s', "
101 "using default", iface);
104 if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0)
106 assert(ss.ss_family == AF_INET || ss.ss_family == AF_INET6);
108 /* RFC 3493, 5.2: -1 means 'use kernel default' */
109 if (ttl < 0 || ttl > 255)
112 switch (ss.ss_family) {
114 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
120 memset(&mn, 0, sizeof(mn));
122 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
125 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
129 * Enable receiving multicast messages generated on the local host
130 * At least on Linux, this is enabled by default.
132 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
135 /* Default: use local subnet (do not flood out into the WAN) */
138 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
142 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
145 setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
147 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
149 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
154 return -ERRNO_TO_PARA_ERROR(errno);
157 static void udp_init_session(struct sender_client *sc)
159 PARA_NOTICE_LOG("sending to udp %s\n", sc->name);
162 static void udp_shutdown_targets(void)
164 struct sender_client *sc, *tmp;
165 list_for_each_entry_safe(sc, tmp, &targets, node)
166 udp_close_target(sc);
169 static int udp_resolve_target(const char *url, struct sender_command_data *scd)
174 ret = parse_fec_url(url, scd);
177 port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
179 ret = para_connect_simple(IPPROTO_UDP, scd->host, port);
183 result = remote_name(ret);
186 if (!parse_url(result, scd->host, sizeof(scd->host), &scd->port))
187 return -E_ADDRESS_LOOKUP;
191 static int udp_com_on(__a_unused struct sender_command_data *scd)
193 sender_status = SENDER_ON;
197 static int udp_com_off(__a_unused struct sender_command_data *scd)
199 udp_shutdown_targets();
200 sender_status = SENDER_OFF;
204 static struct sender_client *udp_lookup_target(struct sender_command_data *scd)
206 struct sender_client *sc, *tmp;
207 char host[MAX_HOSTLEN];
210 list_for_each_entry_safe(sc, tmp, &targets, node) {
211 parse_url(sc->name, host, sizeof(host), &port);
213 /* Unspecified port means wildcard port match */
214 if (scd->port > 0 && scd->port != port)
216 if (strcmp(host, scd->host))
223 static int udp_com_delete(struct sender_command_data *scd)
225 struct sender_client *sc = udp_lookup_target(scd);
228 udp_delete_target(sc, "com_delete");
231 PARA_NOTICE_LOG("not deleting non-existing target '%s'\n", scd->host);
232 return -E_TARGET_NOT_FOUND;
235 /** Initialize UDP session and set maximum payload size. */
236 static int udp_init_fec(struct sender_client *sc)
240 udp_init_session(sc);
241 mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
242 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
246 /** Check and clear socket error if any. */
247 static int udp_check_socket_state(struct sender_client *sc)
249 struct udp_target *ut = sc->private_data;
251 socklen_t errlen = sizeof(ret);
253 if (getsockopt(sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) {
254 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret));
256 } else if (ret == 0) {
258 } else if (ret == ECONNREFUSED) {
259 time_t dist = now->tv_sec - ut->last_unreachable;
261 if (dist <= UDP_MAX_UNREACHABLE_TIME) {
263 } else if (dist > 2 * UDP_MAX_UNREACHABLE_TIME) {
264 ut->last_unreachable = now->tv_sec;
268 * unreachable_time < dist <= 2 * unreachable_time
269 * No errors are allowed during this time window.
271 PARA_NOTICE_LOG("Evicting %s after %d seconds "
272 "of connection errors.\n",
273 sc->name, (int)dist);
276 return -ERRNO_TO_PARA_ERROR(ret);
279 static void udp_send_fec(struct sender_client *sc, char *buf, size_t len)
283 if (sender_status == SENDER_OFF)
287 ret = udp_check_socket_state(sc);
290 ret = xwrite(sc->fd, buf, len);
291 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED)) {
293 * Happens if meanwhile an ICMP Destination / Port Unreachable
294 * has arrived. Ignore, persistent errors will be caught above.
302 udp_delete_target(sc, para_strerror(-ret));
305 static int udp_com_add(struct sender_command_data *scd)
308 struct udp_target *ut;
309 struct sender_client *sc = udp_lookup_target(scd);
312 PARA_NOTICE_LOG("target %s already exists - not adding it\n",
314 return -E_TARGET_EXISTS;
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;
324 sc->private_data = ut;
326 ret = para_connect_simple(IPPROTO_UDP, scd->host, scd->port);
331 ret = mcast_sender_setup(sc);
334 ret = mark_fd_nonblocking(sc->fd);
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);
346 PARA_NOTICE_LOG("failed to set up %s:%d (%s)- not adding it\n",
347 scd->host, scd->port, para_strerror(-ret));
353 static char *udp_info(void)
355 struct sender_client *sc;
356 char *ret, *tgts = NULL;
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
373 (sender_status == SENDER_ON)? "on" : "off",
374 stringify_port(conf.udp_default_port_arg, "udp"),
375 tgts? tgts : "(none)"
381 static void udp_init_target_list(void)
383 struct sender_command_data scd;
386 INIT_LIST_HEAD(&targets);
387 for (i = 0; i < conf.udp_target_given; i++) {
388 if (udp_resolve_target(conf.udp_target_arg[i], &scd) < 0)
389 PARA_CRIT_LOG("not adding requested target '%s'\n",
390 conf.udp_target_arg[i]);
396 static char *udp_help(void)
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"
416 * The init function of para_server's udp sender.
418 * \param s Pointer to the udp sender struct.
420 * It initializes all function pointers of \a s and the list of udp targets.
422 void udp_send_init(struct sender *s)
424 INIT_LIST_HEAD(&targets);
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 (!conf.udp_no_autostart_given)
441 sender_status = SENDER_ON;
442 PARA_DEBUG_LOG("udp sender init complete\n");