URL/UDPv6 support for the UDP sender.
[paraslash.git] / udp_send.c
1 /*
2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file udp_send.c Para_server's udp sender. */
8
9
10 #include <sys/time.h>
11 #include <dirent.h>
12
13 #include "server.cmdline.h"
14 #include "para.h"
15 #include "error.h"
16 #include "string.h"
17 #include "afh.h"
18 #include "afs.h"
19 #include "server.h"
20 #include "vss.h"
21 #include "list.h"
22 #include "send.h"
23 #include "portable_io.h"
24 #include "udp_header.h"
25 #include "net.h"
26 #include "fd.h"
27 #include "sched.h"
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
30
31 /** Describes one entry in the list of targets for the udp sender. */
32 struct udp_target {
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];
37 /** The UDP port. */
38 int port;
39 /** The socket fd. */
40 int fd;
41 /** The list of queued chunks for this fd. */
42 struct chunk_queue *cq;
43 };
44
45 static struct list_head targets;
46 static int sender_status;
47
48 static void udp_close_target(struct udp_target *ut)
49 {
50 if (ut->fd < 0)
51 return;
52 close(ut->fd);
53 del_close_on_fork_list(ut->fd);
54 cq_destroy(ut->cq);
55 ut->cq = NULL;
56 ut->fd = -1;
57 }
58
59 static void udp_delete_target(struct udp_target *ut, const char *msg)
60 {
61 PARA_NOTICE_LOG("deleting %s#%d (%s) from list\n", ut->host,
62 ut->port, msg);
63 udp_close_target(ut);
64 list_del(&ut->node);
65 free(ut);
66 }
67
68 /**
69 * Perform AF-independent multicast sender setup.
70 *
71 * \param fd The connected socket descriptor.
72 * \param ttl UDPv4 multicast TTL or UDPv6 multicast number of hops.
73 * Use -1 to mean default, 0..255 otherwise.
74 * \param iface The outgoing multicast interface, or NULL for the default.
75 *
76 * \return Zero if okay, negative on error.
77 */
78 static int mcast_sender_setup(struct udp_target *ut, int ttl, char *iface)
79 {
80 struct sockaddr_storage ss;
81 socklen_t sslen = sizeof(ss);
82
83 const int on = 1;
84 int id = iface == NULL ? 0 : if_nametoindex(iface);
85
86 if (getpeername(ut->fd, (struct sockaddr *)&ss, &sslen) < 0)
87 goto err;
88
89 if (iface != NULL && id == 0)
90 PARA_WARNING_LOG("could not resolve interface %s, using default", iface);
91
92 /* RFC 3493, 5.2: -1 means 'use kernel default' */
93 if (ttl < 0 || ttl > 255)
94 ttl = -1;
95
96 switch (ss.ss_family) {
97 case AF_INET:
98 if (!IN_MULTICAST(htonl(((struct sockaddr_in *)&ss)->sin_addr.s_addr)))
99 return 0;
100 if (id != 0) {
101 #ifdef HAVE_IP_MREQN
102 struct ip_mreqn mn;
103
104 memset(&mn, 0, sizeof(mn));
105 mn.imr_ifindex = id;
106 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_IF, &mn, sizeof(mn)) < 0)
107 goto err;
108 #else
109 PARA_ERROR_LOG("No support for setting outgoing IPv4 mcast interface.");
110 #endif
111 }
112 /*
113 * Enable receiving multicast messages generated on the local host
114 * At least on Linux, this is enabled by default.
115 */
116 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_LOOP, &on, sizeof(on)) < 0)
117 break;
118
119 /* Default: use local subnet (do not flood out into the WAN) */
120 if (ttl == -1)
121 ttl = 1;
122 if (setsockopt(ut->fd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
123 break;
124 return 0;
125 case AF_INET6:
126 if (!IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)&ss)->sin6_addr))
127 return 0;
128 if (id != 0 &&
129 setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &id, sizeof(id)) < 0)
130 break;
131 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_LOOP, &on, sizeof(on)) < 0)
132 break;
133 if (setsockopt(ut->fd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &ttl, sizeof(ttl)) < 0)
134 break;
135 return 0;
136 default:
137 PARA_ERROR_LOG("address family %d not supported", ss.ss_family);
138 return -E_ADDRESS_LOOKUP;
139 }
140 err:
141 return -ERRNO_TO_PARA_ERROR(errno);
142 }
143
144 /** The maximal size of the per-target chunk queue. */
145 #define UDP_CQ_BYTES 40000
146
147 static int udp_init_session(struct udp_target *ut)
148 {
149 int ret;
150 char *iface = NULL;
151
152 if (ut->fd >= 0) /* nothing to do */
153 return 0;
154
155 ret = makesock(AF_UNSPEC, IPPROTO_UDP, 0, ut->host, ut->port);
156 if (ret < 0)
157 return ret;
158 ut->fd = ret;
159
160 if (conf.udp_mcast_iface_given)
161 iface = conf.udp_mcast_iface_arg;
162
163 ret = mcast_sender_setup(ut, conf.udp_ttl_arg, iface);
164 if (ret < 0) {
165 close(ut->fd);
166 return ret;
167 }
168
169 ret = mark_fd_nonblocking(ut->fd);
170 if (ret < 0) {
171 close(ut->fd);
172 return ret;
173 }
174 add_close_on_fork_list(ut->fd);
175 ut->cq = cq_new(UDP_CQ_BYTES);
176 PARA_NOTICE_LOG("sending to udp %s#%d\n", ut->host, ut->port);
177 return 1;
178 }
179
180 static void udp_send_buf(char *buf, size_t len)
181 {
182 struct udp_target *ut, *tmp;
183 int ret;
184
185 list_for_each_entry_safe(ut, tmp, &targets, node) {
186 ret = udp_init_session(ut);
187 if (ret < 0) {
188 udp_delete_target(ut, para_strerror(-ret));
189 continue;
190 }
191 ret = send_queued_chunks(ut->fd, ut->cq, 0);
192 if (ret < 0) {
193 udp_delete_target(ut, para_strerror(-ret));
194 continue;
195 }
196 if (!len)
197 continue;
198 if (!ret) { /* still data left in the queue */
199 ret = cq_enqueue(ut->cq, buf, len);
200 if (ret < 0) {
201 udp_delete_target(ut, para_strerror(-ret));
202 continue;
203 }
204 }
205 ret = write_nonblock(ut->fd, buf, len, 0);
206 if (ret < 0) {
207 udp_delete_target(ut, para_strerror(-ret));
208 continue;
209 }
210 if (ret != len) {
211 ret = cq_enqueue(ut->cq, buf + ret, len - ret);
212 if (ret < 0) {
213 udp_delete_target(ut, para_strerror(-ret));
214 continue;
215 }
216 }
217 }
218 }
219
220 static void udp_shutdown_targets(void)
221 {
222 char buf[UDP_AUDIO_HEADER_LEN];
223 struct udp_target *ut, *tmp;
224 struct udp_audio_header uah = {
225 .stream_type = UDP_UNKNOWN_STREAM,
226 .packet_type = UDP_EOF_PACKET,
227 };
228
229 write_udp_audio_header(buf, &uah);
230 list_for_each_entry_safe(ut, tmp, &targets, node) {
231 if (ut->fd < 0)
232 continue;
233 write(ut->fd, buf, UDP_AUDIO_HEADER_LEN);
234 udp_close_target(ut);
235 }
236 }
237
238 static int need_extra_header(long unsigned current_chunk)
239 {
240 static struct timeval last_header;
241 struct timeval diff;
242
243 if (!current_chunk)
244 return 0;
245 tv_diff(now, &last_header, &diff);
246 if (tv2ms(&diff) < conf.udp_header_interval_arg)
247 return 0;
248 last_header = *now;
249 return 1;
250 }
251
252 static void udp_send(long unsigned current_chunk, __a_unused long unsigned chunks_sent,
253 const char *buf, size_t len, const char *header_buf,
254 size_t header_len)
255 {
256 char *sendbuf;
257 size_t sendbuf_len;
258 struct timeval *chunk_tv;
259 struct udp_audio_header uah;
260
261 // PARA_NOTICE_LOG("len: %zd, header_len: %zd\n", len, header_len);
262 if (sender_status != SENDER_ON)
263 return;
264
265 /* we might not yet know the chunk time */
266 chunk_tv = vss_chunk_time();
267 if (!chunk_tv)
268 return;
269 if (list_empty(&targets))
270 return;
271 uah.stream_type = header_len? UDP_HEADER_STREAM : UDP_PLAIN_STREAM;
272 uah.header_len = need_extra_header(current_chunk)? header_len : 0;
273 if (!current_chunk)
274 uah.packet_type = UDP_BOF_PACKET;
275 else if (uah.header_len)
276 uah.packet_type = UDP_HEADER_PACKET;
277 else
278 uah.packet_type = UDP_DATA_PACKET;
279 uah.payload_len = uah.header_len + len;
280 sendbuf_len = UDP_AUDIO_HEADER_LEN + uah.payload_len;
281 sendbuf = para_malloc(sendbuf_len);
282 write_udp_audio_header(sendbuf, &uah);
283 if (uah.header_len)
284 memcpy(sendbuf + UDP_AUDIO_HEADER_LEN, header_buf,
285 uah.header_len);
286 memcpy(sendbuf + UDP_AUDIO_HEADER_LEN + uah.header_len, buf, len);
287 udp_send_buf(sendbuf, sendbuf_len);
288 free(sendbuf);
289 }
290
291 static int udp_com_on(__a_unused struct sender_command_data *scd)
292 {
293 sender_status = SENDER_ON;
294 return 1;
295 }
296
297 static int udp_com_off(__a_unused struct sender_command_data *scd)
298 {
299 udp_shutdown_targets();
300 sender_status = SENDER_OFF;
301 return 1;
302 }
303
304 static int udp_com_delete(struct sender_command_data *scd)
305 {
306 struct udp_target *ut, *tmp;
307
308 list_for_each_entry_safe(ut, tmp, &targets, node) {
309 /* Unspecified port means wildcard port match */
310 if (scd->port > 0 && scd->port != ut->port)
311 continue;
312 if (strcmp(ut->host, scd->host))
313 continue;
314 udp_delete_target(ut, "com_delete");
315 }
316 return 1;
317 }
318
319 static void udp_add_target(const char *host, int port)
320 {
321 struct udp_target *ut = para_calloc(sizeof(struct udp_target));
322
323 strncpy(ut->host, host, sizeof(ut->host));
324 ut->port = port > 0 ? port : conf.udp_default_port_arg;
325 ut->fd = -1; /* not yet connected */
326 PARA_INFO_LOG("adding to target list (%s#%d)\n",
327 ut->host, ut->port);
328 para_list_add(&ut->node, &targets);
329 }
330
331 static int udp_com_add(struct sender_command_data *scd)
332 {
333 udp_add_target(scd->host, scd->port);
334 return 1;
335 }
336
337 static char *udp_info(void)
338 {
339 struct udp_target *ut;
340 char *ret, *tgts = NULL;
341
342 list_for_each_entry(ut, &targets, node) {
343 bool is_v6 = strchr(ut->host, ':') != NULL;
344 char *tmp = make_message("%s%s%s%s:%d ", tgts ? : "",
345 is_v6 ? "[" : "", ut->host,
346 is_v6 ? "]" : "", ut->port);
347 free(tgts);
348 tgts = tmp;
349 }
350 ret = make_message(
351 "udp sender:\n"
352 "\tstatus: %s\n"
353 "\tport: udp %d\n"
354 "\ttargets: %s\n",
355 (sender_status == SENDER_ON)? "on" : "off",
356 conf.udp_default_port_arg,
357 tgts? tgts : "(none)"
358 );
359 free(tgts);
360 return ret;
361 }
362
363 static void udp_init_target_list(void)
364 {
365 char host[MAX_HOSTLEN];
366 int port, i;
367
368 INIT_LIST_HEAD(&targets);
369 for (i = 0; i < conf.udp_target_given; i++)
370 if (parse_url(conf.udp_target_arg[i], host,
371 sizeof(host), &port) == NULL)
372 PARA_CRIT_LOG("syntax error for udp target option "
373 "#%d, ignoring\n", i);
374 else
375 udp_add_target(host, port);
376 }
377
378 static char *udp_help(void)
379 {
380 return make_message(
381 "usage: {on|off}\n"
382 "usage: {add|delete} host[:port]\n"
383 "examples: add 224.0.1.38:1500 (IPv4 multicast)\n"
384 " add 10.10.1.42 (using default port)\n"
385 " add [FF05::42]:1500 (IPv6 multicast)\n"
386 " add [::1] (IPv6 localhost/default port)\n"
387 " delete myhost.net (host with port wildcard)\n"
388 " delete [badc0de::1] (IPv6 with port wildcard)\n"
389 );
390 }
391
392 /**
393 * The init function of para_server's udp sender.
394 *
395 * \param s Pointer to the http sender struct.
396 *
397 * It initializes all function pointers of \a s and the list of udp targets.
398 */
399 void udp_send_init(struct sender *s)
400 {
401 INIT_LIST_HEAD(&targets);
402 s->info = udp_info;
403 s->help = udp_help;
404 s->send = udp_send;
405 s->pre_select = NULL;
406 s->post_select = NULL;
407 s->shutdown_clients = udp_shutdown_targets;
408 s->client_cmds[SENDER_ON] = udp_com_on;
409 s->client_cmds[SENDER_OFF] = udp_com_off;
410 s->client_cmds[SENDER_DENY] = NULL;
411 s->client_cmds[SENDER_ALLOW] = NULL;
412 s->client_cmds[SENDER_ADD] = udp_com_add;
413 s->client_cmds[SENDER_DELETE] = udp_com_delete;
414 sender_status = SENDER_OFF;
415 udp_init_target_list();
416 if (!conf.udp_no_autostart_given)
417 sender_status = SENDER_ON;
418 PARA_DEBUG_LOG("udp sender init complete\n");
419 }