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