2 * Copyright (C) 2005-2009 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. */
14 #include "server.cmdline.h"
24 #include "portable_io.h"
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
31 /** Describes one entry in the list of targets for the udp sender. */
33 /** The position of this target in the list of targets. */
34 struct list_head node;
35 /** The hostname (DNS name or IPv4/v6 address string). */
36 char host[MAX_HOSTLEN];
41 /** The list of queued chunks for this fd. */
42 struct chunk_queue *cq;
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 udp_target *ut)
57 del_close_on_fork_list(ut->fd);
63 static void udp_delete_target(struct udp_target *ut, const char *msg)
65 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
68 vss_del_fec_client(ut->fc);
74 * Perform AF-independent multicast sender setup.
76 * \param fd The connected socket descriptor.
77 * \param ttl UDPv4 multicast TTL or UDPv6 multicast number of hops.
78 * Use -1 to mean default, 0..255 otherwise.
79 * \param iface The outgoing multicast interface, or NULL for the default.
81 * \return Zero if okay, negative on error.
83 static int mcast_sender_setup(struct udp_target *ut, int ttl, char *iface)
85 struct sockaddr_storage ss;
86 socklen_t sslen = sizeof(ss);
89 int id = iface == NULL ? 0 : if_nametoindex(iface);
91 if (getpeername(ut->fd, (struct sockaddr *)&ss, &sslen) < 0)
94 if (iface != NULL && id == 0)
95 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
97 /* RFC 3493, 5.2: -1 means 'use kernel default' */
98 if (ttl < 0 || ttl > 255)
101 switch (ss.ss_family) {
103 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
109 memset(&mn, 0, sizeof(mn));
111 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
114 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
118 * Enable receiving multicast messages generated on the local host
119 * At least on Linux, this is enabled by default.
121 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
124 /* Default: use local subnet (do not flood out into the WAN) */
127 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
131 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
134 setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
136 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
138 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
142 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
143 return -E_ADDRESS_LOOKUP;
146 return -ERRNO_TO_PARA_ERROR(errno);
149 /** The maximal size of the per-target chunk queue. */
150 #define UDP_CQ_BYTES 40000
152 static int udp_init_session(struct udp_target *ut)
157 if (ut->fd >= 0) /* nothing to do */
160 ret = makesock(AF_UNSPEC, IPPROTO_UDP, 0, ut->host, ut->port);
165 if (conf.udp_mcast_iface_given)
166 iface = conf.udp_mcast_iface_arg;
168 ret = mcast_sender_setup(ut, conf.udp_ttl_arg, iface);
174 ret = mark_fd_nonblocking(ut->fd);
179 add_close_on_fork_list(ut->fd);
180 ut->cq = cq_new(UDP_CQ_BYTES);
181 PARA_NOTICE_LOG("sending to udp %s#%d\n", ut->host, ut->port);
185 static void udp_shutdown_targets(void)
187 struct udp_target *ut, *tmp;
188 const char *buf = NULL;
189 size_t len = 0; /* STFU, gcc */
191 list_for_each_entry_safe(ut, tmp, &targets, node) {
195 len = vss_get_fec_eof_packet(&buf);
196 write(ut->fd, buf, len);
197 udp_close_target(ut);
201 static int udp_com_on(__a_unused struct sender_command_data *scd)
203 sender_status = SENDER_ON;
207 static int udp_com_off(__a_unused struct sender_command_data *scd)
209 udp_shutdown_targets();
210 sender_status = SENDER_OFF;
214 static int udp_com_delete(struct sender_command_data *scd)
216 struct udp_target *ut, *tmp;
218 list_for_each_entry_safe(ut, tmp, &targets, node) {
219 /* Unspecified port means wildcard port match */
220 if (scd->port > 0 && scd->port != ut->port)
222 if (strcmp(ut->host, scd->host))
224 udp_delete_target(ut, "com_delete");
229 static int udp_send_fec(char *buf, size_t len, void *private_data)
231 struct udp_target *ut = private_data;
232 int ret = udp_init_session(ut);
236 ret = send_queued_chunks(ut->fd, ut->cq, 0);
241 if (!ret) { /* still data left in the queue */
242 ret = cq_enqueue(ut->cq, buf, len);
246 ret = write_nonblock(ut->fd, buf, len, 0);
250 ret = cq_enqueue(ut->cq, buf + ret, len - ret);
256 udp_delete_target(ut, para_strerror(-ret));
260 static void udp_add_target(const char *host, int port)
262 struct udp_target *ut = para_calloc(sizeof(struct udp_target));
264 strncpy(ut->host, host, sizeof(ut->host));
265 ut->port = port > 0 ? port : conf.udp_default_port_arg;
266 ut->fd = -1; /* not yet connected */
267 PARA_INFO_LOG("adding to target list (%s#%d)\n",
269 para_list_add(&ut->node, &targets);
270 ut->fcp.slices_per_group = 16;
271 ut->fcp.data_slices_per_group = 14;
272 ut->fcp.max_slice_bytes = 1400;
273 ut->fcp.send = udp_send_fec;
274 ut->fcp.private_data = ut;
275 vss_add_fec_client(&ut->fcp, &ut->fc);
278 static int udp_com_add(struct sender_command_data *scd)
280 udp_add_target(scd->host, scd->port);
284 static char *udp_info(void)
286 struct udp_target *ut;
287 char *ret, *tgts = NULL;
289 list_for_each_entry(ut, &targets, node) {
290 bool is_v6 = strchr(ut->host, ':') != NULL;
291 char *tmp = make_message("%s%s%s%s:%d ", tgts ? : "",
292 is_v6 ? "[" : "", ut->host,
293 is_v6 ? "]" : "", ut->port);
302 (sender_status == SENDER_ON)? "on" : "off",
303 conf.udp_default_port_arg,
304 tgts? tgts : "(none)"
310 static void udp_init_target_list(void)
312 char host[MAX_HOSTLEN];
315 INIT_LIST_HEAD(&targets);
316 for (i = 0; i < conf.udp_target_given; i++)
317 if (parse_url(conf.udp_target_arg[i], host,
318 sizeof(host), &port) == NULL)
319 PARA_CRIT_LOG("syntax error for udp target option "
320 "#%d, ignoring\n", i);
322 udp_add_target(host, port);
325 static char *udp_help(void)
329 "usage: {add|delete} host[:port]\n"
330 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
331 " add 10.10.1.42 (using default port)\n"
332 " add [FF05::42]:1500 (IPv6 multicast)\n"
333 " add [::1] (IPv6 localhost/default port)\n"
334 " delete myhost.net (host with port wildcard)\n"
335 " delete [badc0de::1] (IPv6 with port wildcard)\n"
340 * The init function of para_server's udp sender.
342 * \param s Pointer to the http sender struct.
344 * It initializes all function pointers of \a s and the list of udp targets.
346 void udp_send_init(struct sender *s)
348 INIT_LIST_HEAD(&targets);
352 s->pre_select = NULL;
353 s->post_select = NULL;
354 s->shutdown_clients = udp_shutdown_targets;
355 s->client_cmds[SENDER_ON] = udp_com_on;
356 s->client_cmds[SENDER_OFF] = udp_com_off;
357 s->client_cmds[SENDER_DENY] = NULL;
358 s->client_cmds[SENDER_ALLOW] = NULL;
359 s->client_cmds[SENDER_ADD] = udp_com_add;
360 s->client_cmds[SENDER_DELETE] = udp_com_delete;
361 sender_status = SENDER_OFF;
362 udp_init_target_list();
363 if (!conf.udp_no_autostart_given)
364 sender_status = SENDER_ON;
365 PARA_DEBUG_LOG("udp sender init complete\n");