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