Major udp sender/receiver improvements.
[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 #define UDP_CQ_BYTES 40000
72
73 static int udp_init_session(struct udp_target *ut)
74 {
75         int ret;
76
77         if (ut->fd >= 0) /* nothing to do */
78                 return 0;
79         ret = create_udp_send_socket(TARGET_ADDR(ut), ut->port,
80                 conf.udp_ttl_arg);
81         if (ret < 0)
82                 return ret;
83         ut->fd = ret;
84         ret = mark_fd_nonblocking(ut->fd);
85         if (ret < 0) {
86                 close(ut->fd);
87                 return ret;
88         }
89         add_close_on_fork_list(ut->fd);
90         ut->cq = cq_new(UDP_CQ_BYTES);
91         PARA_NOTICE_LOG("sending to udp %s:%d\n", TARGET_ADDR(ut), ut->port);
92         return 1;
93 }
94
95 static void udp_send_buf(char *buf, size_t len)
96 {
97         struct udp_target *ut, *tmp;
98         int ret;
99
100         list_for_each_entry_safe(ut, tmp, &targets, node) {
101                 ret = udp_init_session(ut);
102                 if (ret < 0) {
103                         udp_delete_target(ut, para_strerror(-ret));
104                         continue;
105                 }
106                 ret = send_queued_chunks(ut->fd, ut->cq, 0);
107                 if (ret < 0) {
108                         udp_delete_target(ut, para_strerror(-ret));
109                         continue;
110                 }
111                 if (!len)
112                         continue;
113                 if (!ret) { /* still data left in the queue */
114                         ret = cq_enqueue(ut->cq, buf, len);
115                         if (ret < 0) {
116                                 udp_delete_target(ut, para_strerror(-ret));
117                                 continue;
118                         }
119                 }
120                 ret = write_nonblock(ut->fd, buf, len, 0);
121                 if (ret < 0) {
122                         udp_delete_target(ut, para_strerror(-ret));
123                         continue;
124                 }
125                 if (ret != len) {
126                         ret = cq_enqueue(ut->cq, buf + ret, len - ret);
127                         if (ret < 0) {
128                                 udp_delete_target(ut, para_strerror(-ret));
129                                 continue;
130                         }
131                 }
132         }
133 }
134
135 static void udp_shutdown_targets(void)
136 {
137         char buf[UDP_AUDIO_HEADER_LEN];
138         struct udp_target *ut, *tmp;
139         struct udp_audio_header uah = {
140                 .stream_type = UDP_UNKNOWN_STREAM,
141                 .packet_type = UDP_EOF_PACKET,
142         };
143
144         write_udp_audio_header(buf, &uah);
145         list_for_each_entry_safe(ut, tmp, &targets, node) {
146                 if (ut->fd < 0)
147                         continue;
148                 write(ut->fd, buf, UDP_AUDIO_HEADER_LEN);
149                 udp_close_target(ut);
150         }
151 }
152
153 static int need_extra_header(long unsigned current_chunk)
154 {
155         static struct timeval last_header;
156         struct timeval diff;
157
158         if (!current_chunk)
159                 return 0;
160         tv_diff(now, &last_header, &diff);
161         if (tv2ms(&diff) < conf.udp_header_interval_arg)
162                 return 0;
163         last_header = *now;
164         return 1;
165 }
166
167 static void udp_send(long unsigned current_chunk, __a_unused long unsigned chunks_sent,
168                 const char *buf, size_t len, const char *header_buf,
169                 size_t header_len)
170 {
171         char *sendbuf;
172         size_t sendbuf_len;
173         struct timeval *chunk_tv;
174         struct udp_audio_header uah;
175
176 //      PARA_NOTICE_LOG("len: %zd, header_len: %zd\n", len, header_len);
177         if (sender_status != SENDER_ON)
178                 return;
179
180         /* we might not yet know the chunk time */
181         chunk_tv = vss_chunk_time();
182         if (!chunk_tv)
183                 return;
184         if (list_empty(&targets))
185                 return;
186         uah.stream_type = header_len? UDP_HEADER_STREAM : UDP_PLAIN_STREAM;
187         uah.header_len = need_extra_header(current_chunk)? header_len : 0;
188         if (!current_chunk)
189                 uah.packet_type = UDP_BOF_PACKET;
190         else if (uah.header_len)
191                 uah.packet_type = UDP_HEADER_PACKET;
192         else
193                 uah.packet_type = UDP_DATA_PACKET;
194         uah.payload_len = uah.header_len + len;
195         sendbuf_len = UDP_AUDIO_HEADER_LEN + uah.payload_len;
196         sendbuf = para_malloc(sendbuf_len);
197         write_udp_audio_header(sendbuf, &uah);
198         if (uah.header_len)
199                 memcpy(sendbuf + UDP_AUDIO_HEADER_LEN, header_buf,
200                         uah.header_len);
201         memcpy(sendbuf + UDP_AUDIO_HEADER_LEN + uah.header_len, buf, len);
202         udp_send_buf(sendbuf, sendbuf_len);
203         free(sendbuf);
204 }
205
206 static int udp_com_on(__a_unused struct sender_command_data *scd)
207 {
208         sender_status = SENDER_ON;
209         return 1;
210 }
211
212 static int udp_com_off(__a_unused struct sender_command_data *scd)
213 {
214         udp_shutdown_targets();
215         sender_status = SENDER_OFF;
216         return 1;
217 }
218
219 static int udp_com_delete(struct sender_command_data *scd)
220 {
221         char *a = para_strdup(inet_ntoa(scd->addr));
222         struct udp_target *ut, *tmp;
223         list_for_each_entry_safe(ut, tmp, &targets, node) {
224                 if (scd->port != ut->port)
225                         continue;
226                 if (strcmp(TARGET_ADDR(ut), a))
227                         continue;
228                 udp_delete_target(ut, "com_delete");
229         }
230         return 1;
231 }
232
233 static void udp_add_target(int port, struct in_addr *addr)
234 {
235         struct udp_target *ut = para_calloc(sizeof(struct udp_target));
236         ut->port = port;
237         ut->addr = *addr;
238         ut->fd = -1; /* not yet connected */
239         PARA_INFO_LOG("adding to target list (%s:%d)\n",
240                 TARGET_ADDR(ut), ut->port);
241         para_list_add(&ut->node, &targets);
242 }
243
244 static int udp_com_add(struct sender_command_data *scd)
245 {
246         int port = (scd->port > 0)? scd->port : conf.udp_default_port_arg;
247         udp_add_target(port, &scd->addr);
248         return 1;
249 }
250
251 static char *udp_info(void)
252 {
253         struct udp_target *ut;
254         char *ret, *tgts = NULL;
255
256         list_for_each_entry(ut, &targets, node) {
257                 char *tmp = make_message("%s%s:%d ", tgts? tgts : "",
258                         TARGET_ADDR(ut), ut->port);
259                 free(tgts);
260                 tgts = tmp;
261         }
262         ret = make_message(
263                 "udp sender:\n"
264                 "\tstatus: %s\n"
265                 "\tport: udp %d\n"
266                 "\ttargets: %s\n",
267                 (sender_status == SENDER_ON)? "on" : "off",
268                 conf.udp_default_port_arg,
269                 tgts? tgts : "(none)"
270         );
271         free(tgts);
272         return ret;
273 }
274
275 static void udp_init_target_list(void)
276 {
277         int i;
278
279         INIT_LIST_HEAD(&targets);
280         for (i = 0; i < conf.udp_target_given; i++) {
281                 char *arg = para_strdup(conf.udp_target_arg[i]);
282                 char *p = strchr(arg, ':');
283                 int port;
284                 struct in_addr addr;
285
286                 if (!p)
287                         goto err;
288                 *p = '\0';
289                 if (!inet_pton(AF_INET, arg, &addr))
290                         goto err;
291                 port = atoi(++p);
292                 if (port < 0 || port > 65535)
293                         port = conf.udp_default_port_arg;
294                 udp_add_target(port, &addr);
295                 goto success;
296 err:
297                 PARA_CRIT_LOG("syntax error for udp target option "
298                         "#%d, ignoring\n", i);
299 success:
300                 free(arg);
301                 continue;
302         }
303 }
304
305 static char *udp_help(void)
306 {
307         return make_message(
308                 "usage: {on|off}\n"
309                 "usage: {add|delete} IP port\n"
310                 "example: add 224.0.1.38 8000 (multicast streaming)\n"
311         );
312 }
313
314 /**
315  * The init function of para_server's udp sender.
316  *
317  * \param s Pointer to the http sender struct.
318  *
319  * It initializes all function pointers of \a s and the list of udp targets.
320  */
321 void udp_send_init(struct sender *s)
322 {
323         INIT_LIST_HEAD(&targets);
324         s->info = udp_info;
325         s->help = udp_help;
326         s->send = udp_send;
327         s->pre_select = NULL;
328         s->post_select = NULL;
329         s->shutdown_clients = udp_shutdown_targets;
330         s->client_cmds[SENDER_ON] = udp_com_on;
331         s->client_cmds[SENDER_OFF] = udp_com_off;
332         s->client_cmds[SENDER_DENY] = NULL;
333         s->client_cmds[SENDER_ALLOW] = NULL;
334         s->client_cmds[SENDER_ADD] = udp_com_add;
335         s->client_cmds[SENDER_DELETE] = udp_com_delete;
336         sender_status = SENDER_OFF;
337         udp_init_target_list();
338         if (!conf.udp_no_autostart_given)
339                 sender_status = SENDER_ON;
340         PARA_DEBUG_LOG("udp sender init complete\n");
341 }