e83239d346bdcad9c97c7defb8874a68d9dcf997
[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
140         udp_write_packet_type(buf, UDP_EOF_PACKET);
141         udp_write_magic(buf);
142         list_for_each_entry_safe(ut, tmp, &targets, node) {
143                 if (ut->fd < 0)
144                         continue;
145                 write(ut->fd, buf, UDP_AUDIO_HEADER_LEN);
146                 udp_close_target(ut);
147         }
148 }
149
150 static int need_extra_header(long unsigned current_chunk)
151 {
152         static struct timeval last_header;
153         struct timeval diff;
154
155         if (!current_chunk)
156                 return 0;
157         tv_diff(now, &last_header, &diff);
158         if (tv2ms(&diff) < conf.udp_header_interval_arg)
159                 return 0;
160         last_header = *now;
161         return 1;
162 }
163
164 static void udp_send(long unsigned current_chunk, __a_unused long unsigned chunks_sent,
165                 const char *buf, size_t len, const char *header_buf,
166                 size_t header_len)
167 {
168         size_t sendbuf_len;
169         uint8_t packet_type = UDP_DATA_PACKET;
170         char *sendbuf;
171         struct timeval *chunk_tv;
172         uint8_t stream_type = header_len? UDP_HEADER_STREAM : UDP_PLAIN_STREAM;
173
174 //      PARA_NOTICE_LOG("len: %zd, header_len: %zd\n", len, header_len);
175         if (sender_status != SENDER_ON)
176                 return;
177
178         /* we might not yet know the chunk time */
179         chunk_tv = vss_chunk_time();
180         if (!chunk_tv)
181                 return;
182         if (list_empty(&targets))
183                 return;
184         if (!need_extra_header(current_chunk))
185                 header_len = 0;
186         if (!current_chunk)
187                 packet_type = UDP_BOF_PACKET;
188         else if (header_len)
189                 packet_type = UDP_HEADER_PACKET;
190         sendbuf_len = UDP_AUDIO_HEADER_LEN + header_len + len;
191         sendbuf = para_malloc(sendbuf_len);
192         udp_write_magic(sendbuf);
193         udp_write_stream_type(sendbuf, stream_type);
194         udp_write_packet_type(sendbuf, packet_type);
195         udp_write_header_len(sendbuf, header_len);
196         if (header_len)
197                 memcpy(sendbuf + UDP_AUDIO_HEADER_LEN, header_buf,
198                         header_len);
199         memcpy(sendbuf + UDP_AUDIO_HEADER_LEN + header_len, buf, len);
200         udp_send_buf(sendbuf, sendbuf_len);
201         free(sendbuf);
202 }
203
204 static int udp_com_on(__a_unused struct sender_command_data *scd)
205 {
206         sender_status = SENDER_ON;
207         return 1;
208 }
209
210 static int udp_com_off(__a_unused struct sender_command_data *scd)
211 {
212         udp_shutdown_targets();
213         sender_status = SENDER_OFF;
214         return 1;
215 }
216
217 static int udp_com_delete(struct sender_command_data *scd)
218 {
219         char *a = para_strdup(inet_ntoa(scd->addr));
220         struct udp_target *ut, *tmp;
221         list_for_each_entry_safe(ut, tmp, &targets, node) {
222                 if (scd->port != ut->port)
223                         continue;
224                 if (strcmp(TARGET_ADDR(ut), a))
225                         continue;
226                 udp_delete_target(ut, "com_delete");
227         }
228         return 1;
229 }
230
231 static void udp_add_target(int port, struct in_addr *addr)
232 {
233         struct udp_target *ut = para_calloc(sizeof(struct udp_target));
234         ut->port = port;
235         ut->addr = *addr;
236         ut->fd = -1; /* not yet connected */
237         PARA_INFO_LOG("adding to target list (%s:%d)\n",
238                 TARGET_ADDR(ut), ut->port);
239         para_list_add(&ut->node, &targets);
240 }
241
242 static int udp_com_add(struct sender_command_data *scd)
243 {
244         int port = (scd->port > 0)? scd->port : conf.udp_default_port_arg;
245         udp_add_target(port, &scd->addr);
246         return 1;
247 }
248
249 static char *udp_info(void)
250 {
251         struct udp_target *ut;
252         char *ret, *tgts = NULL;
253
254         list_for_each_entry(ut, &targets, node) {
255                 char *tmp = make_message("%s%s:%d ", tgts? tgts : "",
256                         TARGET_ADDR(ut), ut->port);
257                 free(tgts);
258                 tgts = tmp;
259         }
260         ret = make_message(
261                 "udp sender:\n"
262                 "\tstatus: %s\n"
263                 "\tport: udp %d\n"
264                 "\ttargets: %s\n",
265                 (sender_status == SENDER_ON)? "on" : "off",
266                 conf.udp_default_port_arg,
267                 tgts? tgts : "(none)"
268         );
269         free(tgts);
270         return ret;
271 }
272
273 static void udp_init_target_list(void)
274 {
275         int i;
276
277         INIT_LIST_HEAD(&targets);
278         for (i = 0; i < conf.udp_target_given; i++) {
279                 char *arg = para_strdup(conf.udp_target_arg[i]);
280                 char *p = strchr(arg, ':');
281                 int port;
282                 struct in_addr addr;
283
284                 if (!p)
285                         goto err;
286                 *p = '\0';
287                 if (!inet_pton(AF_INET, arg, &addr))
288                         goto err;
289                 port = atoi(++p);
290                 if (port < 0 || port > 65535)
291                         port = conf.udp_default_port_arg;
292                 udp_add_target(port, &addr);
293                 goto success;
294 err:
295                 PARA_CRIT_LOG("syntax error for udp target option "
296                         "#%d, ignoring\n", i);
297 success:
298                 free(arg);
299                 continue;
300         }
301 }
302
303 static char *udp_help(void)
304 {
305         return make_message(
306                 "usage: {on|off}\n"
307                 "usage: {add|delete} IP port\n"
308                 "example: add 224.0.1.38 8000 (multicast streaming)\n"
309         );
310 }
311
312 /**
313  * The init function of para_server's udp sender.
314  *
315  * \param s Pointer to the http sender struct.
316  *
317  * It initializes all function pointers of \a s and the list of udp targets.
318  */
319 void udp_send_init(struct sender *s)
320 {
321         INIT_LIST_HEAD(&targets);
322         s->info = udp_info;
323         s->help = udp_help;
324         s->send = udp_send;
325         s->pre_select = NULL;
326         s->post_select = NULL;
327         s->shutdown_clients = udp_shutdown_targets;
328         s->client_cmds[SENDER_ON] = udp_com_on;
329         s->client_cmds[SENDER_OFF] = udp_com_off;
330         s->client_cmds[SENDER_DENY] = NULL;
331         s->client_cmds[SENDER_ALLOW] = NULL;
332         s->client_cmds[SENDER_ADD] = udp_com_add;
333         s->client_cmds[SENDER_DELETE] = udp_com_delete;
334         sender_status = SENDER_OFF;
335         udp_init_target_list();
336         if (!conf.udp_no_autostart_given)
337                 sender_status = SENDER_ON;
338         PARA_DEBUG_LOG("udp sender init complete\n");
339 }