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