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)
55 del_close_on_fork_list(sc->fd);
64 static void udp_delete_target(struct udp_target *ut, const char *msg)
67 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
69 udp_close_target(ut->sc);
70 vss_del_fec_client(ut->fc);
71 list_del(&ut->sc->node);
77 * Perform AF-independent multicast sender setup.
79 * \param sc The connected sender client.
81 * \return Zero if okay, negative on error.
83 static int mcast_sender_setup(struct sender_client *sc)
85 struct sockaddr_storage ss;
86 socklen_t sslen = sizeof(ss);
87 int ttl = conf.udp_ttl_arg, id = 0;
90 if (conf.udp_mcast_iface_given) {
91 char *iface = conf.udp_mcast_iface_arg;
93 id = if_nametoindex(iface);
95 PARA_WARNING_LOG("could not resolve interface '%s', "
96 "using default", iface);
99 if (getpeername(sc->fd, (struct sockaddr *)&ss, &sslen) < 0)
102 /* RFC 3493, 5.2: -1 means 'use kernel default' */
103 if (ttl < 0 || ttl > 255)
106 switch (ss.ss_family) {
108 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
114 memset(&mn, 0, sizeof(mn));
116 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
119 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
123 * Enable receiving multicast messages generated on the local host
124 * At least on Linux, this is enabled by default.
126 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
129 /* Default: use local subnet (do not flood out into the WAN) */
132 if (setsockopt(sc->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
136 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
139 setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
141 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
143 if (setsockopt(sc->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
147 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
148 return -E_ADDRESS_LOOKUP;
151 return -ERRNO_TO_PARA_ERROR(errno);
154 /** The maximal size of the per-target chunk queue. */
155 #define UDP_CQ_BYTES 40000
157 static void udp_init_session(struct sender_client *sc)
159 if (sc->cq == NULL) {
160 add_close_on_fork_list(sc->fd);
161 sc->cq = cq_new(UDP_CQ_BYTES);
162 sc->name = para_strdup(remote_name(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_com_on(__a_unused struct sender_command_data *scd)
183 sender_status = SENDER_ON;
187 static int udp_com_off(__a_unused struct sender_command_data *scd)
189 udp_shutdown_targets();
190 sender_status = SENDER_OFF;
194 static int udp_com_delete(struct sender_command_data *scd)
196 struct sender_client *sc, *tmp;
198 list_for_each_entry_safe(sc, tmp, &targets, node) {
199 struct udp_target *ut = sc->private_data;
201 /* Unspecified port means wildcard port match */
202 if (scd->port > 0 && scd->port != ut->port)
204 if (strcmp(ut->host, scd->host))
206 udp_delete_target(ut, "com_delete");
211 /** Initialize UDP session and set maximum payload size. */
212 static int udp_init_fec(struct sender_client *sc)
216 udp_init_session(sc);
217 mps = generic_max_transport_msg_size(sc->fd) - sizeof(struct udphdr);
218 PARA_INFO_LOG("current MPS = %d bytes\n", mps);
222 static int udp_send_fec(struct sender_client *sc, char *buf, size_t len)
224 struct udp_target *ut = sc->private_data;
227 if (sender_status == SENDER_OFF)
229 ret = send_queued_chunks(sc->fd, sc->cq);
230 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
236 if (!ret) { /* still data left in the queue */
237 ret = cq_force_enqueue(sc->cq, buf, len);
241 ret = write_nonblock(sc->fd, buf, len);
242 if (ret == -ERRNO_TO_PARA_ERROR(ECONNREFUSED))
247 ret = cq_force_enqueue(sc->cq, buf + ret, len - ret);
252 udp_delete_target(ut, para_strerror(-ret));
256 static void udp_add_target(struct sender_command_data *scd)
258 int ret, port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
259 struct udp_target *ut = para_calloc(sizeof(*ut));
261 strncpy(ut->host, scd->host, sizeof(ut->host));
262 ut->port = scd->port > 0 ? scd->port : conf.udp_default_port_arg;
264 ut->fcp.slices_per_group = scd->slices_per_group;
265 ut->fcp.data_slices_per_group = scd->data_slices_per_group;
266 ut->fcp.max_slice_bytes = scd->max_slice_bytes;
267 ut->fcp.init_fec = udp_init_fec;
268 ut->fcp.send_fec = udp_send_fec;
270 ut->sc = para_calloc(sizeof(*ut->sc));
271 ut->sc->private_data = ut;
273 ret = para_connect_simple(IPPROTO_UDP, scd->host, port);
278 ret = mcast_sender_setup(ut->sc);
281 ret = mark_fd_nonblocking(ut->sc->fd);
284 PARA_INFO_LOG("adding to target list (%s#%d)\n", ut->host, ut->port);
285 ut->fc = vss_add_fec_client(ut->sc, &ut->fcp);
286 para_list_add(&ut->sc->node, &targets);
291 PARA_NOTICE_LOG("failed to set up %s#%d (%s)- not adding it\n",
292 scd->host, port, para_strerror(-ret));
297 static int udp_com_add(struct sender_command_data *scd)
303 static char *udp_info(void)
305 struct sender_client *sc;
306 char *ret, *tgts = NULL;
308 list_for_each_entry(sc, &targets, node) {
309 struct udp_target *ut = sc->private_data;
310 bool is_v6 = strchr(ut->host, ':') != NULL;
311 char *tmp = make_message("%s%s%s%s:%d/%u:%u:%u ", tgts ? : "",
312 is_v6 ? "[" : "", ut->host,
313 is_v6 ? "]" : "", ut->port,
314 ut->fcp.max_slice_bytes,
315 ut->fcp.data_slices_per_group,
316 ut->fcp.slices_per_group
326 (sender_status == SENDER_ON)? "on" : "off",
327 stringify_port(conf.udp_default_port_arg, "udp"),
328 tgts? tgts : "(none)"
334 static void udp_init_target_list(void)
336 struct sender_command_data scd;
339 INIT_LIST_HEAD(&targets);
340 for (i = 0; i < conf.udp_target_given; i++) {
341 if (parse_fec_url(conf.udp_target_arg[i], &scd) < 0) {
342 PARA_CRIT_LOG("syntax error for udp target option "
343 "#%d, ignoring\n", i);
346 udp_add_target(&scd);
350 static char *udp_help(void)
354 "usage: {add|delete} host[:port][/packet_size:k:n]\n"
355 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
356 " add 224.0.1.38:1500/1400:14:16\n"
357 " (likewise, using 1400 byte packets, with 14\n"
358 " data slices per 16 slice FEC group)\n"
359 " add 10.10.1.42 (using default port)\n"
360 " add [FF05::42]:1500 (IPv6 multicast)\n"
361 " add [::1] (IPv6 localhost/default port)\n"
362 " delete myhost.net (host with port wildcard)\n"
363 " delete [badc0de::1] (IPv6 with port wildcard)\n"
368 * The init function of para_server's udp sender.
370 * \param s Pointer to the udp sender struct.
372 * It initializes all function pointers of \a s and the list of udp targets.
374 void udp_send_init(struct sender *s)
376 INIT_LIST_HEAD(&targets);
380 s->pre_select = NULL;
381 s->post_select = NULL;
382 s->shutdown_clients = udp_shutdown_targets;
383 s->client_cmds[SENDER_ON] = udp_com_on;
384 s->client_cmds[SENDER_OFF] = udp_com_off;
385 s->client_cmds[SENDER_DENY] = NULL;
386 s->client_cmds[SENDER_ALLOW] = NULL;
387 s->client_cmds[SENDER_ADD] = udp_com_add;
388 s->client_cmds[SENDER_DELETE] = udp_com_delete;
389 sender_status = SENDER_OFF;
390 udp_init_target_list();
391 if (!conf.udp_no_autostart_given)
392 sender_status = SENDER_ON;
393 PARA_DEBUG_LOG("udp sender init complete\n");