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"
35 /** Describes one entry in the list of targets for the udp sender. */
37 /** The hostname (DNS name or IPv4/v6 address string). */
38 char host[MAX_HOSTLEN];
41 /** Common sender client data */
42 struct sender_client *sc;
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)
57 del_close_on_fork_list(sc->fd);
63 static void udp_delete_target(struct udp_target *ut, const char *msg)
66 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
68 udp_close_target(ut->sc);
69 vss_del_fec_client(ut->fc);
70 list_del(&ut->sc->node);
76 * Perform AF-independent multicast sender setup.
78 * \param sc The connected sender client.
79 * \param ttl UDPv4 multicast TTL or UDPv6 multicast number of hops.
80 * Use -1 to mean default, 0..255 otherwise.
81 * \param iface The outgoing multicast interface, or NULL for the default.
83 * \return Zero if okay, negative on error.
85 static int mcast_sender_setup(struct sender_client *sc, int ttl, char *iface)
87 struct sockaddr_storage ss;
88 socklen_t sslen = sizeof(ss);
91 int id = iface == NULL ? 0 : if_nametoindex(iface);
93 if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0)
96 if (iface != NULL && id == 0)
97 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
99 /* RFC 3493, 5.2: -1 means 'use kernel default' */
100 if (ttl < 0 || ttl > 255)
103 switch (ss.ss_family) {
105 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
111 memset(&mn, 0, sizeof(mn));
113 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
116 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
120 * Enable receiving multicast messages generated on the local host
121 * At least on Linux, this is enabled by default.
123 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
126 /* Default: use local subnet (do not flood out into the WAN) */
129 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
133 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
136 setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
138 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
140 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
144 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
145 return -E_ADDRESS_LOOKUP;
148 return -ERRNO_TO_PARA_ERROR(errno);
151 /** The maximal size of the per-target chunk queue. */
152 #define UDP_CQ_BYTES 40000
154 static int udp_init_session(struct sender_client *sc)
156 struct udp_target *ut = sc->private_data;
160 if (sc->fd >= 0) /* nothing to do */
163 ret = para_connect_simple(IPPROTO_UDP, ut->host, ut->port);
168 if (conf.udp_mcast_iface_given)
169 iface = conf.udp_mcast_iface_arg;
171 ret = mcast_sender_setup(sc, conf.udp_ttl_arg, iface);
177 ret = mark_fd_nonblocking(sc->fd);
182 add_close_on_fork_list(sc->fd);
183 sc->cq = cq_new(UDP_CQ_BYTES);
184 PARA_NOTICE_LOG("sending to udp %s#%d\n", ut->host, ut->port);
188 static void udp_shutdown_targets(void)
190 struct sender_client *sc, *tmp;
191 const char *buf = NULL;
192 size_t len = 0; /* STFU, gcc */
194 list_for_each_entry_safe(sc, tmp, &targets, node) {
195 int ubuntu_glibc_headers_suck;
199 len = vss_get_fec_eof_packet(&buf);
200 /* ignore return value, we're closing the target anyway. */
201 ubuntu_glibc_headers_suck = write(sc->fd, buf, len); /* STFU */
202 udp_close_target(sc);
206 static int udp_com_on(__a_unused struct sender_command_data *scd)
208 sender_status = SENDER_ON;
212 static int udp_com_off(__a_unused struct sender_command_data *scd)
214 udp_shutdown_targets();
215 sender_status = SENDER_OFF;
219 static int udp_com_delete(struct sender_command_data *scd)
221 struct sender_client *sc, *tmp;
223 list_for_each_entry_safe(sc, tmp, &targets, node) {
224 struct udp_target *ut = sc->private_data;
226 /* Unspecified port means wildcard port match */
227 if (scd->port > 0 && scd->port != ut->port)
229 if (strcmp(ut->host, scd->host))
231 udp_delete_target(ut, "com_delete");
236 /** Initialize UDP session and set maximum payload size. */
237 static int udp_init_fec(struct sender_client *sc)
239 int mps, ret = udp_init_session(sc);
244 mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
245 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
249 static int udp_send_fec(struct sender_client *sc, char *buf, size_t len)
251 struct udp_target *ut = sc->private_data;
254 if (sender_status == SENDER_OFF)
256 ret = send_queued_chunks(sc->fd, sc->cq);
257 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
263 if (!ret) { /* still data left in the queue */
264 ret = cq_force_enqueue(sc->cq, buf, len);
267 ret = write_nonblock(sc->fd, buf, len);
268 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
273 ret = cq_force_enqueue(sc->cq, buf + ret, len - ret);
278 udp_delete_target(ut, para_strerror(-ret));
282 static void udp_add_target(struct sender_command_data *scd)
284 struct udp_target *ut = para_calloc(sizeof(*ut));
286 strncpy(ut->host, scd->host, sizeof(ut->host));
287 ut->port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
289 ut->fcp.slices_per_group = scd->slices_per_group;
290 ut->fcp.data_slices_per_group = scd->data_slices_per_group;
291 ut->fcp.max_slice_bytes = scd->max_slice_bytes;
292 ut->fcp.init_fec = udp_init_fec;
293 ut->fcp.send_fec = udp_send_fec;
295 ut->sc = para_calloc(sizeof(*ut->sc));
296 ut->sc->fd = -1; /* not yet connected */
297 ut->sc->private_data = ut;
298 ut->fc = vss_add_fec_client(ut->sc, &ut->fcp);
300 PARA_INFO_LOG("adding to target list (%s#%d)\n", ut->host, ut->port);
301 para_list_add(&ut->sc->node, &targets);
304 static int udp_com_add(struct sender_command_data *scd)
310 static char *udp_info(void)
312 struct sender_client *sc;
313 char *ret, *tgts = NULL;
315 list_for_each_entry(sc, &targets, node) {
316 struct udp_target *ut = sc->private_data;
317 bool is_v6 = strchr(ut->host, ':') != NULL;
318 char *tmp = make_message("%s%s%s%s:%d/%u:%u:%u ", tgts ? : "",
319 is_v6 ? "[" : "", ut->host,
320 is_v6 ? "]" : "", ut->port,
321 ut->fcp.max_slice_bytes,
322 ut->fcp.data_slices_per_group,
323 ut->fcp.slices_per_group
333 (sender_status == SENDER_ON)? "on" : "off",
334 stringify_port(conf.udp_default_port_arg, "udp"),
335 tgts? tgts : "(none)"
341 static void udp_init_target_list(void)
343 struct sender_command_data scd;
346 INIT_LIST_HEAD(&targets);
347 for (i = 0; i < conf.udp_target_given; i++) {
348 if (parse_fec_url(conf.udp_target_arg[i], &scd) < 0) {
349 PARA_CRIT_LOG("syntax error for udp target option "
350 "#%d, ignoring\n", i);
353 udp_add_target(&scd);
357 static char *udp_help(void)
361 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
362 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
363 " add 224.0.1.38:1500/1400:14:16\n"
364 " (likewise, using 1400 byte packets, with 14\n"
365 " data slices per 16 slice FEC group)\n"
366 " add 10.10.1.42 (using default port)\n"
367 " add [FF05::42]:1500 (IPv6 multicast)\n"
368 " add [::1] (IPv6 localhost/default port)\n"
369 " delete myhost.net (host with port wildcard)\n"
370 " delete [badc0de::1] (IPv6 with port wildcard)\n"
375 * The init function of para_server's udp sender.
377 * \param s Pointer to the udp sender struct.
379 * It initializes all function pointers of \a s and the list of udp targets.
381 void udp_send_init(struct sender *s)
383 INIT_LIST_HEAD(&targets);
387 s->pre_select = NULL;
388 s->post_select = NULL;
389 s->shutdown_clients = udp_shutdown_targets;
390 s->client_cmds[SENDER_ON] = udp_com_on;
391 s->client_cmds[SENDER_OFF] = udp_com_off;
392 s->client_cmds[SENDER_DENY] = NULL;
393 s->client_cmds[SENDER_ALLOW] = NULL;
394 s->client_cmds[SENDER_ADD] = udp_com_add;
395 s->client_cmds[SENDER_DELETE] = udp_com_delete;
396 sender_status = SENDER_OFF;
397 udp_init_target_list();
398 if (!conf.udp_no_autostart_given)
399 sender_status = SENDER_ON;
400 PARA_DEBUG_LOG("udp sender init complete\n");