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