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