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 /** Track time (seconds) of last ICMP Port Unreachable error */
45 time_t last_unreachable
;
46 /** Common sender client data */
47 struct sender_client
*sc
;
48 /** The opaque structure returned by vss_add_fec_client(). */
49 struct fec_client
*fc
;
50 /** The FEC parameters for this target. */
51 struct fec_client_parms fcp
;
54 static struct list_head targets
;
55 static int sender_status
;
57 static void udp_close_target(struct sender_client
*sc
)
60 del_close_on_fork_list(sc
->fd
);
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
);
72 vss_del_fec_client(ut
->fc
);
80 * Perform AF-independent multicast sender setup.
82 * \param sc The connected sender client.
84 * \return Zero if okay, negative on error.
86 static int mcast_sender_setup(struct sender_client
*sc
)
88 struct sockaddr_storage ss
;
89 socklen_t sslen
= sizeof(ss
);
90 int ttl
= conf
.udp_ttl_arg
, id
= 0;
93 if (conf
.udp_mcast_iface_given
) {
94 char *iface
= conf
.udp_mcast_iface_arg
;
96 id
= if_nametoindex(iface
);
98 PARA_WARNING_LOG("could not resolve interface '%s', "
99 "using default", iface
);
102 if (getpeername(sc
->fd
, (struct sockaddr
*)&ss
, &sslen
) < 0)
104 assert(ss
.ss_family
== AF_INET
|| ss
.ss_family
== AF_INET6
);
106 /* RFC 3493, 5.2: -1 means 'use kernel default' */
107 if (ttl
< 0 || ttl
> 255)
110 switch (ss
.ss_family
) {
112 if (!IN_MULTICAST(htonl(((struct sockaddr_in
*)&ss
)->sin_addr
.s_addr
)))
118 memset(&mn
, 0, sizeof(mn
));
120 if (setsockopt(sc
->fd
, IPPROTO_IP
, IP_MULTICAST_IF
, &mn
, sizeof(mn
)) < 0)
123 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
127 * Enable receiving multicast messages generated on the local host
128 * At least on Linux, this is enabled by default.
130 if (setsockopt(sc
->fd
, IPPROTO_IP
, IP_MULTICAST_LOOP
, &on
, sizeof(on
)) < 0)
133 /* Default: use local subnet (do not flood out into the WAN) */
136 if (setsockopt(sc
->fd
, IPPROTO_IP
, IP_MULTICAST_TTL
, &ttl
, sizeof(ttl
)) < 0)
140 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6
*)&ss
)->sin6_addr
))
143 setsockopt(sc
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_IF
, &id
, sizeof(id
)) < 0)
145 if (setsockopt(sc
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
, &on
, sizeof(on
)) < 0)
147 if (setsockopt(sc
->fd
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
, &ttl
, sizeof(ttl
)) < 0)
152 return -ERRNO_TO_PARA_ERROR(errno
);
155 /** The maximal size of the per-target chunk queue. */
156 #define UDP_CQ_BYTES 40000
158 static void udp_init_session(struct sender_client
*sc
)
160 if (sc
->cq
== NULL
) {
161 sc
->cq
= cq_new(UDP_CQ_BYTES
);
162 add_close_on_fork_list(sc
->fd
);
163 PARA_NOTICE_LOG("sending to udp %s\n", sc
->name
);
167 static void udp_shutdown_targets(void)
169 struct sender_client
*sc
, *tmp
;
171 size_t len
= vss_get_fec_eof_packet(&buf
);
173 list_for_each_entry_safe(sc
, tmp
, &targets
, node
)
174 if (sc
->cq
!= NULL
) {
175 /* ignore return value, closing the target anyway. */
176 (void)write(sc
->fd
, buf
, len
);
177 udp_close_target(sc
);
181 static int udp_resolve_target(const char *url
, struct sender_command_data
*scd
)
186 ret
= parse_fec_url(url
, scd
);
189 port
= scd
->port
> 0 ? scd
->port
: conf
.udp_default_port_arg
;
191 ret
= para_connect_simple(IPPROTO_UDP
, scd
->host
, port
);
195 result
= remote_name(ret
);
198 if (!parse_url(result
, scd
->host
, sizeof(scd
->host
), &scd
->port
))
199 return -E_ADDRESS_LOOKUP
;
203 static int udp_com_on(__a_unused
struct sender_command_data
*scd
)
205 sender_status
= SENDER_ON
;
209 static int udp_com_off(__a_unused
struct sender_command_data
*scd
)
211 udp_shutdown_targets();
212 sender_status
= SENDER_OFF
;
216 static struct sender_client
*udp_lookup_target(struct sender_command_data
*scd
)
218 struct sender_client
*sc
, *tmp
;
219 char host
[MAX_HOSTLEN
];
222 list_for_each_entry_safe(sc
, tmp
, &targets
, node
) {
223 parse_url(sc
->name
, host
, sizeof(host
), &port
);
225 /* Unspecified port means wildcard port match */
226 if (scd
->port
> 0 && scd
->port
!= port
)
228 if (strcmp(host
, scd
->host
))
235 static int udp_com_delete(struct sender_command_data
*scd
)
237 struct sender_client
*sc
= udp_lookup_target(scd
);
240 udp_delete_target(sc
, "com_delete");
243 PARA_NOTICE_LOG("not deleting non-existing target '%s'\n", scd
->host
);
244 return -E_TARGET_NOT_FOUND
;
247 /** Initialize UDP session and set maximum payload size. */
248 static int udp_init_fec(struct sender_client
*sc
)
252 udp_init_session(sc
);
253 mps
= generic_max_transport_msg_size(sc
->fd
) - sizeof(struct udphdr
);
254 PARA_INFO_LOG("current MPS = %d bytes\n", mps
);
258 /** Check and clear socket error if any. */
259 static int udp_check_socket_state(struct sender_client
*sc
)
261 struct udp_target
*ut
= sc
->private_data
;
263 socklen_t errlen
= sizeof(ret
);
265 if (getsockopt(sc
->fd
, SOL_SOCKET
, SO_ERROR
, &ret
, &errlen
) < 0) {
266 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret
));
268 } else if (ret
== 0) {
270 } else if (ret
== ECONNREFUSED
) {
271 time_t dist
= now
->tv_sec
- ut
->last_unreachable
;
273 if (dist
<= UDP_MAX_UNREACHABLE_TIME
) {
275 } else if (dist
> 2 * UDP_MAX_UNREACHABLE_TIME
) {
276 ut
->last_unreachable
= now
->tv_sec
;
280 * unreachable_time < dist <= 2 * unreachable_time
281 * No errors are allowed during this time window.
283 PARA_NOTICE_LOG("Evicting %s after %d seconds "
284 "of connection errors.\n",
285 sc
->name
, (int)dist
);
288 return -ERRNO_TO_PARA_ERROR(ret
);
291 static int udp_send_fec(struct sender_client
*sc
, char *buf
, size_t len
)
295 if (sender_status
== SENDER_OFF
)
297 if (len
== 0 && !cq_peek(sc
->cq
))
299 ret
= udp_check_socket_state(sc
);
302 ret
= send_queued_chunks(sc
->fd
, sc
->cq
);
305 if (!ret
) { /* still data left in the queue */
306 ret
= cq_force_enqueue(sc
->cq
, buf
, len
);
310 ret
= write_nonblock(sc
->fd
, buf
, len
);
311 if (ret
== -ERRNO_TO_PARA_ERROR(ECONNREFUSED
)) {
313 * Happens if meanwhile an ICMP Destination / Port Unreachable
314 * has arrived. Ignore, persistent errors will be caught above.
321 ret
= cq_force_enqueue(sc
->cq
, buf
+ ret
, len
- ret
);
326 udp_delete_target(sc
, para_strerror(-ret
));
330 static int udp_com_add(struct sender_command_data
*scd
)
333 struct udp_target
*ut
;
334 struct sender_client
*sc
= udp_lookup_target(scd
);
337 PARA_NOTICE_LOG("target %s already exists - not adding it\n",
339 return -E_TARGET_EXISTS
;
341 ut
= para_calloc(sizeof(*ut
));
342 sc
= ut
->sc
= para_calloc(sizeof(*sc
));
343 ut
->fcp
.slices_per_group
= scd
->slices_per_group
;
344 ut
->fcp
.data_slices_per_group
= scd
->data_slices_per_group
;
345 ut
->fcp
.max_slice_bytes
= scd
->max_slice_bytes
;
346 ut
->fcp
.init_fec
= udp_init_fec
;
347 ut
->fcp
.send_fec
= udp_send_fec
;
349 sc
->private_data
= ut
;
351 ret
= para_connect_simple(IPPROTO_UDP
, scd
->host
, scd
->port
);
356 ret
= mcast_sender_setup(sc
);
359 ret
= mark_fd_nonblocking(sc
->fd
);
362 sc
->name
= para_strdup(remote_name(sc
->fd
));
363 PARA_INFO_LOG("adding to target list (%s)\n", sc
->name
);
364 ut
->fc
= vss_add_fec_client(sc
, &ut
->fcp
);
365 para_list_add(&sc
->node
, &targets
);
370 PARA_NOTICE_LOG("failed to set up %s:%d (%s)- not adding it\n",
371 scd
->host
, scd
->port
, para_strerror(-ret
));
377 static char *udp_info(void)
379 struct sender_client
*sc
;
380 char *ret
, *tgts
= NULL
;
382 list_for_each_entry(sc
, &targets
, node
) {
383 struct udp_target
*ut
= sc
->private_data
;
384 char *tmp
= make_message("%s%s/%u:%u:%u ",
385 tgts
? : "", sc
->name
,
386 ut
->fcp
.max_slice_bytes
,
387 ut
->fcp
.data_slices_per_group
,
388 ut
->fcp
.slices_per_group
398 (sender_status
== SENDER_ON
)? "on" : "off",
399 stringify_port(conf
.udp_default_port_arg
, "udp"),
400 tgts
? tgts
: "(none)"
406 static void udp_init_target_list(void)
408 struct sender_command_data scd
;
411 INIT_LIST_HEAD(&targets
);
412 for (i
= 0; i
< conf
.udp_target_given
; i
++) {
413 if (udp_resolve_target(conf
.udp_target_arg
[i
], &scd
) < 0)
414 PARA_CRIT_LOG("not adding requested target '%s'\n",
415 conf
.udp_target_arg
[i
]);
421 static char *udp_help(void)
425 "usage: {add|delete} ip_address[:port][/[packet_size:]k:n]\n"
426 " - k is the number of data slices per FEC group\n"
427 " - n is the total number of slices in a FEC group\n"
428 " - packet_size reduces the slice size below path MTU\n\n"
429 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
430 " add 224.0.1.38:8080/14:16 (explicit FEC)\n"
431 " add 10.10.1.42 (using default port)\n"
432 " add [FF05::42]:1500 (IPv6 multicast)\n"
433 " add [::1] (IPv6 localhost/default port)\n"
434 " delete myhost.net (host with port wildcard)\n"
435 " delete 10.1.2.3:456 (IPv4 with explicit port)\n"
436 " delete [badc0de::1] (IPv6 with port wildcard)\n"
441 * The init function of para_server's udp sender.
443 * \param s Pointer to the udp sender struct.
445 * It initializes all function pointers of \a s and the list of udp targets.
447 void udp_send_init(struct sender
*s
)
449 INIT_LIST_HEAD(&targets
);
453 s
->pre_select
= NULL
;
454 s
->post_select
= NULL
;
455 s
->shutdown_clients
= udp_shutdown_targets
;
456 s
->resolve_target
= udp_resolve_target
;
457 s
->client_cmds
[SENDER_ON
] = udp_com_on
;
458 s
->client_cmds
[SENDER_OFF
] = udp_com_off
;
459 s
->client_cmds
[SENDER_DENY
] = NULL
;
460 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
461 s
->client_cmds
[SENDER_ADD
] = udp_com_add
;
462 s
->client_cmds
[SENDER_DELETE
] = udp_com_delete
;
463 sender_status
= SENDER_OFF
;
464 udp_init_target_list();
465 if (!conf
.udp_no_autostart_given
)
466 sender_status
= SENDER_ON
;
467 PARA_DEBUG_LOG("udp sender init complete\n");