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