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