2 * Copyright (C) 2005-2010 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. */
13 #include <sys/socket.h>
14 #include <netinet/udp.h>
18 #include "server.cmdline.h"
28 #include "portable_io.h"
32 #include "close_on_fork.h"
33 #include "chunk_queue.h"
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.
40 #define UDP_MAX_UNREACHABLE_TIME 30
42 /** Describes one entry in the list of targets for the udp sender. */
44 /** The hostname (DNS name or IPv4/v6 address string). */
45 char host[MAX_HOSTLEN];
48 /** Track time (seconds) of last ICMP Port Unreachable error */
49 time_t last_unreachable;
50 /** Common sender client data */
51 struct sender_client *sc;
52 /** The opaque structure returned by vss_add_fec_client(). */
53 struct fec_client *fc;
54 /** The FEC parameters for this target. */
55 struct fec_client_parms fcp;
58 static struct list_head targets;
59 static int sender_status;
61 static void udp_close_target(struct sender_client *sc)
64 del_close_on_fork_list(sc->fd);
73 static void udp_delete_target(struct udp_target *ut, const char *msg)
76 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
78 udp_close_target(ut->sc);
79 vss_del_fec_client(ut->fc);
80 list_del(&ut->sc->node);
86 * Perform AF-independent multicast sender setup.
88 * \param sc The connected sender client.
90 * \return Zero if okay, negative on error.
92 static int mcast_sender_setup(struct sender_client *sc)
94 struct sockaddr_storage ss;
95 socklen_t sslen = sizeof(ss);
96 int ttl = conf.udp_ttl_arg, id = 0;
99 if (conf.udp_mcast_iface_given) {
100 char *iface = conf.udp_mcast_iface_arg;
102 id = if_nametoindex(iface);
104 PARA_WARNING_LOG("could not resolve interface '%s', "
105 "using default", iface);
108 if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0)
111 /* RFC 3493, 5.2: -1 means 'use kernel default' */
112 if (ttl < 0 || ttl > 255)
115 switch (ss.ss_family) {
117 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
123 memset(&mn, 0, sizeof(mn));
125 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
128 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
132 * Enable receiving multicast messages generated on the local host
133 * At least on Linux, this is enabled by default.
135 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
138 /* Default: use local subnet (do not flood out into the WAN) */
141 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
145 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
148 setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
150 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
152 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
156 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
157 return -E_ADDRESS_LOOKUP;
160 return -ERRNO_TO_PARA_ERROR(errno);
163 /** The maximal size of the per-target chunk queue. */
164 #define UDP_CQ_BYTES 40000
166 static void udp_init_session(struct sender_client *sc)
168 if (sc->cq == NULL) {
169 add_close_on_fork_list(sc->fd);
170 sc->cq = cq_new(UDP_CQ_BYTES);
171 sc->name = para_strdup(remote_name(sc->fd));
172 PARA_NOTICE_LOG("sending to udp %s\n", sc->name);
176 static void udp_shutdown_targets(void)
178 struct sender_client *sc, *tmp;
180 size_t len = vss_get_fec_eof_packet(&buf);
182 list_for_each_entry_safe(sc, tmp, &targets, node)
183 if (sc->cq != NULL) {
184 /* ignore return value, closing the target anyway. */
185 (void)write(sc->fd, buf, len);
186 udp_close_target(sc);
190 static int udp_resolve_target(const char *url, struct sender_command_data *scd)
195 ret = parse_fec_url(url, scd);
198 port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
200 ret = para_connect_simple(IPPROTO_UDP, scd->host, port);
204 result = remote_name(ret);
207 if (!parse_url(result, scd->host, sizeof(scd->host), &scd->port))
208 return -E_ADDRESS_LOOKUP;
212 static int udp_com_on(__a_unused struct sender_command_data *scd)
214 sender_status = SENDER_ON;
218 static int udp_com_off(__a_unused struct sender_command_data *scd)
220 udp_shutdown_targets();
221 sender_status = SENDER_OFF;
225 static int udp_com_delete(struct sender_command_data *scd)
227 struct sender_client *sc, *tmp;
229 list_for_each_entry_safe(sc, tmp, &targets, node) {
230 struct udp_target *ut = sc->private_data;
232 /* Unspecified port means wildcard port match */
233 if (scd->port > 0 && scd->port != ut->port)
235 if (strcmp(ut->host, scd->host))
237 udp_delete_target(ut, "com_delete");
242 /** Initialize UDP session and set maximum payload size. */
243 static int udp_init_fec(struct sender_client *sc)
247 udp_init_session(sc);
248 mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
249 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
253 /** Check and clear socket error if any. */
254 static int udp_check_socket_state(struct udp_target *ut)
257 socklen_t errlen = sizeof(ret);
259 if (getsockopt(ut->sc->fd, SOL_SOCKET, SO_ERROR, &ret, &errlen) < 0) {
260 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret));
262 } else if (ret == 0) {
264 } else if (ret == ECONNREFUSED) {
265 time_t dist = now->tv_sec - ut->last_unreachable;
267 if (dist <= UDP_MAX_UNREACHABLE_TIME) {
269 } else if (dist > 2 * UDP_MAX_UNREACHABLE_TIME) {
270 ut->last_unreachable = now->tv_sec;
274 * unreachable_time < dist <= 2 * unreachable_time
275 * No errors are allowed during this time window.
277 PARA_NOTICE_LOG("Evicting %s#%d after %d seconds "
278 "of connection errors.\n",
279 ut->host, ut->port, (int)dist);
282 return -ERRNO_TO_PARA_ERROR(ret);
285 static int udp_send_fec(struct sender_client *sc, char *buf, size_t len)
287 struct udp_target *ut = sc->private_data;
290 if (sender_status == SENDER_OFF)
292 if (len == 0 && !cq_peek(ut->sc->cq))
294 ret = udp_check_socket_state(ut);
297 ret = send_queued_chunks(sc->fd, sc->cq);
300 if (!ret) { /* still data left in the queue */
301 ret = cq_force_enqueue(sc->cq, buf, len);
305 ret = write_nonblock(sc->fd, buf, len);
306 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED)) {
308 * Happens if meanwhile an ICMP Destination / Port Unreachable
309 * has arrived. Ignore, persistent errors will be caught above.
316 ret = cq_force_enqueue(sc->cq, buf + ret, len - ret);
321 udp_delete_target(ut, para_strerror(-ret));
325 static void udp_add_target(struct sender_command_data *scd)
327 int ret, port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
328 struct udp_target *ut = para_calloc(sizeof(*ut));
330 strncpy(ut->host, scd->host, sizeof(ut->host));
331 ut->port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
333 ut->fcp.slices_per_group = scd->slices_per_group;
334 ut->fcp.data_slices_per_group = scd->data_slices_per_group;
335 ut->fcp.max_slice_bytes = scd->max_slice_bytes;
336 ut->fcp.init_fec = udp_init_fec;
337 ut->fcp.send_fec = udp_send_fec;
339 ut->sc = para_calloc(sizeof(*ut->sc));
340 ut->sc->private_data = ut;
342 ret = para_connect_simple(IPPROTO_UDP, scd->host, port);
347 ret = mcast_sender_setup(ut->sc);
350 ret = mark_fd_nonblocking(ut->sc->fd);
353 PARA_INFO_LOG("adding to target list (%s#%d)\n", ut->host, ut->port);
354 ut->fc = vss_add_fec_client(ut->sc, &ut->fcp);
355 para_list_add(&ut->sc->node, &targets);
360 PARA_NOTICE_LOG("failed to set up %s#%d (%s)- not adding it\n",
361 scd->host, port, para_strerror(-ret));
366 static int udp_com_add(struct sender_command_data *scd)
372 static char *udp_info(void)
374 struct sender_client *sc;
375 char *ret, *tgts = NULL;
377 list_for_each_entry(sc, &targets, node) {
378 struct udp_target *ut = sc->private_data;
379 bool is_v6 = strchr(ut->host, ':') != NULL;
380 char *tmp = make_message("%s%s%s%s:%d/%u:%u:%u ", tgts ? : "",
381 is_v6 ? "[" : "", ut->host,
382 is_v6 ? "]" : "", ut->port,
383 ut->fcp.max_slice_bytes,
384 ut->fcp.data_slices_per_group,
385 ut->fcp.slices_per_group
395 (sender_status == SENDER_ON)? "on" : "off",
396 stringify_port(conf.udp_default_port_arg, "udp"),
397 tgts? tgts : "(none)"
403 static void udp_init_target_list(void)
405 struct sender_command_data scd;
408 INIT_LIST_HEAD(&targets);
409 for (i = 0; i < conf.udp_target_given; i++) {
410 if (parse_fec_url(conf.udp_target_arg[i], &scd) < 0) {
411 PARA_CRIT_LOG("syntax error for udp target option "
412 "#%d, ignoring\n", i);
415 udp_add_target(&scd);
419 static char *udp_help(void)
423 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
424 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
425 " add 224.0.1.38:1500/1400:14:16\n"
426 " (likewise, using 1400 byte packets, with 14\n"
427 " data slices per 16 slice FEC group)\n"
428 " add 10.10.1.42 (using default port)\n"
429 " add [FF05::42]:1500 (IPv6 multicast)\n"
430 " add [::1] (IPv6 localhost/default port)\n"
431 " delete myhost.net (host with port wildcard)\n"
432 " delete [badc0de::1] (IPv6 with port wildcard)\n"
437 * The init function of para_server's udp sender.
439 * \param s Pointer to the udp sender struct.
441 * It initializes all function pointers of \a s and the list of udp targets.
443 void udp_send_init(struct sender *s)
445 INIT_LIST_HEAD(&targets);
449 s->pre_select = NULL;
450 s->post_select = NULL;
451 s->shutdown_clients = udp_shutdown_targets;
452 s->resolve_target = udp_resolve_target;
453 s->client_cmds[SENDER_ON] = udp_com_on;
454 s->client_cmds[SENDER_OFF] = udp_com_off;
455 s->client_cmds[SENDER_DENY] = NULL;
456 s->client_cmds[SENDER_ALLOW] = NULL;
457 s->client_cmds[SENDER_ADD] = udp_com_add;
458 s->client_cmds[SENDER_DELETE] = udp_com_delete;
459 sender_status = SENDER_OFF;
460 udp_init_target_list();
461 if (!conf.udp_no_autostart_given)
462 sender_status = SENDER_ON;
463 PARA_DEBUG_LOG("udp sender init complete\n");