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_com_on(__a_unused
struct sender_command_data
*scd
)
192 sender_status
= SENDER_ON
;
196 static int udp_com_off(__a_unused
struct sender_command_data
*scd
)
198 udp_shutdown_targets();
199 sender_status
= SENDER_OFF
;
203 static int udp_com_delete(struct sender_command_data
*scd
)
205 struct sender_client
*sc
, *tmp
;
207 list_for_each_entry_safe(sc
, tmp
, &targets
, node
) {
208 struct udp_target
*ut
= sc
->private_data
;
210 /* Unspecified port means wildcard port match */
211 if (scd
->port
> 0 && scd
->port
!= ut
->port
)
213 if (strcmp(ut
->host
, scd
->host
))
215 udp_delete_target(ut
, "com_delete");
220 /** Initialize UDP session and set maximum payload size. */
221 static int udp_init_fec(struct sender_client
*sc
)
225 udp_init_session(sc
);
226 mps
= generic_max_transport_msg_size(sc
->fd
) - sizeof(struct udphdr
);
227 PARA_INFO_LOG("current MPS = %d bytes\n", mps
);
231 /** Check and clear socket error if any. */
232 static int udp_check_socket_state(struct udp_target
*ut
)
235 socklen_t errlen
= sizeof(ret
);
237 if (getsockopt(ut
->sc
->fd
, SOL_SOCKET
, SO_ERROR
, &ret
, &errlen
) < 0) {
238 PARA_ERROR_LOG("SO_ERROR failed: %s\n", strerror(ret
));
240 } else if (ret
== 0) {
242 } else if (ret
== ECONNREFUSED
) {
243 time_t dist
= now
->tv_sec
- ut
->last_unreachable
;
245 if (dist
<= UDP_MAX_UNREACHABLE_TIME
) {
247 } else if (dist
> 2 * UDP_MAX_UNREACHABLE_TIME
) {
248 ut
->last_unreachable
= now
->tv_sec
;
252 * unreachable_time < dist <= 2 * unreachable_time
253 * No errors are allowed during this time window.
255 PARA_NOTICE_LOG("Evicting %s#%d after %d seconds "
256 "of connection errors.\n",
257 ut
->host
, ut
->port
, (int)dist
);
260 return -ERRNO_TO_PARA_ERROR(ret
);
263 static int udp_send_fec(struct sender_client
*sc
, char *buf
, size_t len
)
265 struct udp_target
*ut
= sc
->private_data
;
268 if (sender_status
== SENDER_OFF
)
270 if (len
== 0 && !cq_peek(ut
->sc
->cq
))
272 ret
= udp_check_socket_state(ut
);
275 ret
= send_queued_chunks(sc
->fd
, sc
->cq
);
278 if (!ret
) { /* still data left in the queue */
279 ret
= cq_force_enqueue(sc
->cq
, buf
, len
);
283 ret
= write_nonblock(sc
->fd
, buf
, len
);
284 if (ret
== -ERRNO_TO_PARA_ERROR(ECONNREFUSED
)) {
286 * Happens if meanwhile an ICMP Destination / Port Unreachable
287 * has arrived. Ignore, persistent errors will be caught above.
294 ret
= cq_force_enqueue(sc
->cq
, buf
+ ret
, len
- ret
);
299 udp_delete_target(ut
, para_strerror(-ret
));
303 static void udp_add_target(struct sender_command_data
*scd
)
305 int ret
, port
= scd
->port
> 0 ? scd
->port
: conf
.udp_default_port_arg
;
306 struct udp_target
*ut
= para_calloc(sizeof(*ut
));
308 strncpy(ut
->host
, scd
->host
, sizeof(ut
->host
));
309 ut
->port
= scd
->port
> 0 ? scd
->port
: conf
.udp_default_port_arg
;
311 ut
->fcp
.slices_per_group
= scd
->slices_per_group
;
312 ut
->fcp
.data_slices_per_group
= scd
->data_slices_per_group
;
313 ut
->fcp
.max_slice_bytes
= scd
->max_slice_bytes
;
314 ut
->fcp
.init_fec
= udp_init_fec
;
315 ut
->fcp
.send_fec
= udp_send_fec
;
317 ut
->sc
= para_calloc(sizeof(*ut
->sc
));
318 ut
->sc
->private_data
= ut
;
320 ret
= para_connect_simple(IPPROTO_UDP
, scd
->host
, port
);
325 ret
= mcast_sender_setup(ut
->sc
);
328 ret
= mark_fd_nonblocking(ut
->sc
->fd
);
331 PARA_INFO_LOG("adding to target list (%s#%d)\n", ut
->host
, ut
->port
);
332 ut
->fc
= vss_add_fec_client(ut
->sc
, &ut
->fcp
);
333 para_list_add(&ut
->sc
->node
, &targets
);
338 PARA_NOTICE_LOG("failed to set up %s#%d (%s)- not adding it\n",
339 scd
->host
, port
, para_strerror(-ret
));
344 static int udp_com_add(struct sender_command_data
*scd
)
350 static char *udp_info(void)
352 struct sender_client
*sc
;
353 char *ret
, *tgts
= NULL
;
355 list_for_each_entry(sc
, &targets
, node
) {
356 struct udp_target
*ut
= sc
->private_data
;
357 bool is_v6
= strchr(ut
->host
, ':') != NULL
;
358 char *tmp
= make_message("%s%s%s%s:%d/%u:%u:%u ", tgts
? : "",
359 is_v6
? "[" : "", ut
->host
,
360 is_v6
? "]" : "", ut
->port
,
361 ut
->fcp
.max_slice_bytes
,
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 (parse_fec_url(conf
.udp_target_arg
[i
], &scd
) < 0) {
389 PARA_CRIT_LOG("syntax error for udp target option "
390 "#%d, ignoring\n", i
);
393 udp_add_target(&scd
);
397 static char *udp_help(void)
401 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
402 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
403 " add 224.0.1.38:1500/1400:14:16\n"
404 " (likewise, using 1400 byte packets, with 14\n"
405 " data slices per 16 slice FEC group)\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 [badc0de::1] (IPv6 with port wildcard)\n"
415 * The init function of para_server's udp sender.
417 * \param s Pointer to the udp sender struct.
419 * It initializes all function pointers of \a s and the list of udp targets.
421 void udp_send_init(struct sender
*s
)
423 INIT_LIST_HEAD(&targets
);
427 s
->pre_select
= NULL
;
428 s
->post_select
= NULL
;
429 s
->shutdown_clients
= udp_shutdown_targets
;
430 s
->client_cmds
[SENDER_ON
] = udp_com_on
;
431 s
->client_cmds
[SENDER_OFF
] = udp_com_off
;
432 s
->client_cmds
[SENDER_DENY
] = NULL
;
433 s
->client_cmds
[SENDER_ALLOW
] = NULL
;
434 s
->client_cmds
[SENDER_ADD
] = udp_com_add
;
435 s
->client_cmds
[SENDER_DELETE
] = udp_com_delete
;
436 sender_status
= SENDER_OFF
;
437 udp_init_target_list();
438 if (!conf
.udp_no_autostart_given
)
439 sender_status
= SENDER_ON
;
440 PARA_DEBUG_LOG("udp sender init complete\n");