2 * Copyright (C) 2005-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file udp_send.c Para_server's udp sender. */
13 #include "server.cmdline.h"
23 #include "portable_io.h"
24 #include "udp_header.h"
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
31 /** Convert in_addr to ascii. */
32 #define TARGET_ADDR(oc) inet_ntoa((oc)->addr)
34 /** Describes one entry in the list of targets for the udp sender. */
38 /** The position of this target in the list of targets. */
39 struct list_head node;
44 /** The list of queued chunks for this fd. */
45 struct chunk_queue *cq;
48 static struct list_head targets;
49 static int sender_status;
51 static void udp_close_target(struct udp_target *ut)
56 del_close_on_fork_list(ut->fd);
62 static void udp_delete_target(struct udp_target *ut, const char *msg)
64 PARA_NOTICE_LOG("deleting %s:%d (%s) from list\n", TARGET_ADDR(ut),
71 #define UDP_CQ_BYTES 40000
73 static int udp_init_session(struct udp_target *ut)
77 if (ut->fd >= 0) /* nothing to do */
79 ret = create_udp_send_socket(TARGET_ADDR(ut), ut->port,
84 ret = mark_fd_nonblocking(ut->fd);
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);
95 static void udp_send_buf(char *buf, size_t len)
97 struct udp_target *ut, *tmp;
100 list_for_each_entry_safe(ut, tmp, &targets, node) {
101 ret = udp_init_session(ut);
103 udp_delete_target(ut, para_strerror(-ret));
106 ret = send_queued_chunks(ut->fd, ut->cq, 0);
108 udp_delete_target(ut, para_strerror(-ret));
113 if (!ret) { /* still data left in the queue */
114 ret = cq_enqueue(ut->cq, buf, len);
116 udp_delete_target(ut, para_strerror(-ret));
120 ret = write_nonblock(ut->fd, buf, len, 0);
122 udp_delete_target(ut, para_strerror(-ret));
126 ret = cq_enqueue(ut->cq, buf + ret, len - ret);
128 udp_delete_target(ut, para_strerror(-ret));
135 static void udp_shutdown_targets(void)
137 char buf[UDP_AUDIO_HEADER_LEN];
138 struct udp_target *ut, *tmp;
140 udp_write_packet_type(buf, UDP_EOF_PACKET);
141 udp_write_magic(buf);
142 list_for_each_entry_safe(ut, tmp, &targets, node) {
145 write(ut->fd, buf, UDP_AUDIO_HEADER_LEN);
146 udp_close_target(ut);
150 static int need_extra_header(long unsigned current_chunk)
152 static struct timeval last_header;
157 tv_diff(now, &last_header, &diff);
158 if (tv2ms(&diff) < conf.udp_header_interval_arg)
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,
169 uint8_t packet_type = UDP_DATA_PACKET;
171 struct timeval *chunk_tv;
172 uint8_t stream_type = header_len? UDP_HEADER_STREAM : UDP_PLAIN_STREAM;
174 // PARA_NOTICE_LOG("len: %zd, header_len: %zd\n", len, header_len);
175 if (sender_status != SENDER_ON)
178 /* we might not yet know the chunk time */
179 chunk_tv = vss_chunk_time();
182 if (list_empty(&targets))
184 if (!need_extra_header(current_chunk))
187 packet_type = UDP_BOF_PACKET;
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);
197 memcpy(sendbuf + UDP_AUDIO_HEADER_LEN, header_buf,
199 memcpy(sendbuf + UDP_AUDIO_HEADER_LEN + header_len, buf, len);
200 udp_send_buf(sendbuf, sendbuf_len);
204 static int udp_com_on(__a_unused struct sender_command_data *scd)
206 sender_status = SENDER_ON;
210 static int udp_com_off(__a_unused struct sender_command_data *scd)
212 udp_shutdown_targets();
213 sender_status = SENDER_OFF;
217 static int udp_com_delete(struct sender_command_data *scd)
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)
224 if (strcmp(TARGET_ADDR(ut), a))
226 udp_delete_target(ut, "com_delete");
231 static void udp_add_target(int port, struct in_addr *addr)
233 struct udp_target *ut = para_calloc(sizeof(struct udp_target));
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);
242 static int udp_com_add(struct sender_command_data *scd)
244 int port = (scd->port > 0)? scd->port : conf.udp_default_port_arg;
245 udp_add_target(port, &scd->addr);
249 static char *udp_info(void)
251 struct udp_target *ut;
252 char *ret, *tgts = NULL;
254 list_for_each_entry(ut, &targets, node) {
255 char *tmp = make_message("%s%s:%d ", tgts? tgts : "",
256 TARGET_ADDR(ut), ut->port);
265 (sender_status == SENDER_ON)? "on" : "off",
266 conf.udp_default_port_arg,
267 tgts? tgts : "(none)"
273 static void udp_init_target_list(void)
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, ':');
287 if (!inet_pton(AF_INET, arg, &addr))
290 if (port < 0 || port > 65535)
291 port = conf.udp_default_port_arg;
292 udp_add_target(port, &addr);
295 PARA_CRIT_LOG("syntax error for udp target option "
296 "#%d, ignoring\n", i);
303 static char *udp_help(void)
307 "usage: {add|delete} IP port\n"
308 "example: add 224.0.1.38 8000 (multicast streaming)\n"
313 * The init function of para_server's udp sender.
315 * \param s Pointer to the http sender struct.
317 * It initializes all function pointers of \a s and the list of udp targets.
319 void udp_send_init(struct sender *s)
321 INIT_LIST_HEAD(&targets);
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");