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