oss: Avoid sound artefacts on some setups.
[paraslash.git] / send_common.c
1 /*
2 * Copyright (C) 2005-2014 Andre Noll <maan@tuebingen.mpg.de>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file send_common.c Functions used by more than one paraslash sender. */
8
9 #include <netinet/in.h>
10 #include <sys/socket.h>
11 #include <regex.h>
12 #include <osl.h>
13 #include <arpa/inet.h>
14 #include <sys/un.h>
15 #include <netdb.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "string.h"
20 #include "fd.h"
21 #include "net.h"
22 #include "list.h"
23 #include "afh.h"
24 #include "afs.h"
25 #include "server.h"
26 #include "acl.h"
27 #include "send.h"
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
30 #include "sched.h"
31 #include "vss.h"
32
33 /** Clients will be kicked if there are more than that many bytes pending. */
34 #define MAX_CQ_BYTES 40000
35
36 /**
37 * Open a passive socket of given layer4 type.
38 *
39 * Set the resulting file descriptor to nonblocking mode and add it to the list
40 * of fds that are being closed in the child process when the server calls
41 * fork().
42 *
43 * \param l4type The transport-layer protocol.
44 * \param port The port number.
45 *
46 * \return The listening fd on success, negative on errors.
47 */
48 static int open_sender(unsigned l4type, int port)
49 {
50 int fd, ret = para_listen_simple(l4type, port);
51
52 if (ret < 0)
53 return ret;
54 fd = ret;
55 ret = mark_fd_nonblocking(fd);
56 if (ret < 0) {
57 close(fd);
58 return ret;
59 }
60 add_close_on_fork_list(fd);
61 return fd;
62 }
63
64 /**
65 * Shut down a client connected to a paraslash sender.
66 *
67 * \param sc The client to shut down.
68 * \param ss The sender whose clients are to be shut down.
69 *
70 * Close the file descriptor given by \a sc, remove it from the close-on-fork
71 * list, destroy the chunk queue of this client, delete the client from the
72 * list of connected clients and free the sender_client struct.
73 *
74 * \sa shutdown_clients().
75 */
76 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
77 {
78 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
79 free(sc->name);
80 close(sc->fd);
81 del_close_on_fork_list(sc->fd);
82 cq_destroy(sc->cq);
83 list_del(&sc->node);
84 free(sc->private_data);
85 free(sc);
86 ss->num_clients--;
87 }
88
89 /**
90 * Shut down all clients connected to a paraslash sender.
91 *
92 * \param ss The sender whose clients are to be shut down.
93 *
94 * This just loops over all connected clients and calls shutdown_client()
95 * for each client.
96 */
97 void shutdown_clients(struct sender_status *ss)
98 {
99 struct sender_client *sc, *tmp;
100 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
101 shutdown_client(sc, ss);
102 }
103
104 /**
105 * Try to empty the chunk queue for this fd.
106 *
107 * \param fd The file descriptor.
108 * \param cq The list of queued chunks.
109 *
110 * \return Negative on errors, zero if not everything was sent, one otherwise.
111 */
112 int send_queued_chunks(int fd, struct chunk_queue *cq)
113 {
114 struct queued_chunk *qc;
115 while ((qc = cq_peek(cq))) {
116 const char *buf;
117 size_t len;
118 int ret;
119
120 cq_get(qc, &buf, &len);
121 ret = xwrite(fd, buf, len);
122 if (ret < 0)
123 return ret;
124 cq_update(cq, ret);
125 if (ret != len)
126 return 0;
127 cq_dequeue(cq);
128 }
129 return 1;
130 }
131
132 /**
133 * Initialize a struct sender status.
134 *
135 * \param ss The struct to initialize.
136 * \param access_arg The array of access arguments given at the command line.
137 * \param num_access_args The number of elements in \a access_arg.
138 * \param port The tcp or dccp port to listen on.
139 * \param max_clients The maximal number of simultaneous connections.
140 * \param default_deny Whether a blacklist should be used for access control.
141 */
142 void init_sender_status(struct sender_status *ss, char **access_arg,
143 int num_access_args, int port, int max_clients, int default_deny)
144 {
145 ss->listen_fd = -1;
146 INIT_LIST_HEAD(&ss->client_list);
147 ss->port = port;
148 acl_init(&ss->acl, access_arg, num_access_args);
149 ss->num_clients = 0;
150 ss->max_clients = max_clients;
151 ss->default_deny = default_deny;
152 }
153
154 /**
155 * Return a string containing the current status of a sender.
156 *
157 * \param ss The sender.
158 * \param name Used for printing the header line.
159 *
160 * \return The string printed in the "si" command.
161 */
162 char *get_sender_info(struct sender_status *ss, const char *name)
163 {
164 char *clnts = NULL, *ret;
165 struct sender_client *sc, *tmp_sc;
166
167 char *acl_contents = acl_get_contents(&ss->acl);
168 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
169 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
170 free(clnts);
171 clnts = tmp;
172 }
173 ret = make_message(
174 "%s sender:\n"
175 "\tstatus: %s\n"
176 "\tport: %s\n"
177 "\tnumber of connected clients: %d\n"
178 "\tmaximal number of clients: %d%s\n"
179 "\tconnected clients: %s\n"
180 "\taccess %s list: %s\n",
181 name,
182 (ss->listen_fd >= 0)? "on" : "off",
183 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
184 ss->num_clients,
185 ss->max_clients,
186 ss->max_clients > 0? "" : " (unlimited)",
187 clnts? clnts : "(none)",
188 ss->default_deny? "allow" : "deny",
189 acl_contents? acl_contents : "(empty)"
190 );
191 free(acl_contents);
192 free(clnts);
193 return ret;
194 }
195
196 /**
197 * Allow connections from the given range of IP addresses.
198 *
199 * \param scd Contains the IP and the netmask.
200 * \param ss The sender.
201 *
202 * \sa generic_com_deny().
203 */
204 void generic_com_allow(struct sender_command_data *scd,
205 struct sender_status *ss)
206 {
207 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
208 }
209
210 /**
211 * Deny connections from the given range of IP addresses.
212 *
213 * \param scd see \ref generic_com_allow().
214 * \param ss see \ref generic_com_allow().
215 *
216 * \sa generic_com_allow().
217 */
218 void generic_com_deny(struct sender_command_data *scd,
219 struct sender_status *ss)
220 {
221 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
222 }
223
224 /**
225 * Activate a paraslash sender.
226 *
227 * \param ss The sender to activate.
228 * \param protocol The symbolic name of the transport-layer protocol.
229 *
230 * \return Standard.
231 */
232 int generic_com_on(struct sender_status *ss, unsigned protocol)
233 {
234 int ret;
235
236 if (ss->listen_fd >= 0)
237 return 1;
238 ret = open_sender(protocol, ss->port);
239 if (ret < 0)
240 return ret;
241 ss->listen_fd = ret;
242 return 1;
243 }
244
245 /**
246 * Deactivate a paraslash sender.
247 *
248 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
249 *
250 * \param ss The sender to deactivate.
251 *
252 * \sa \ref del_close_on_fork_list(), shutdown_clients().
253 */
254 void generic_com_off(struct sender_status *ss)
255 {
256 if (ss->listen_fd < 0)
257 return;
258 PARA_NOTICE_LOG("closing port %d\n", ss->port);
259 close(ss->listen_fd);
260 del_close_on_fork_list(ss->listen_fd);
261 shutdown_clients(ss);
262 ss->listen_fd = -1;
263 }
264
265 /**
266 * Accept a connection on the socket this server is listening on.
267 *
268 * \param ss The sender whose listening fd is ready for reading.
269 * \param rfds Passed to para_accept(),
270 *
271 * This must be called only if the socket fd of \a ss is ready for reading. It
272 * calls para_accept() to accept the connection and performs the following
273 * actions on the resulting file descriptor \a fd:
274 *
275 * - Checks whether the maximal number of connections are exceeded.
276 * - Sets \a fd to nonblocking mode.
277 * - Checks the acl of the sender to find out whether connections
278 * are allowed from the IP of the connecting peer.
279 * - Increases the number of connections for this sender.
280 * - Creates and initializes a new chunk queue for queuing network
281 * packets that can not be sent immediately.
282 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
283 * and \a name members.
284 * - Adds \a fd to the list of connected clients for this sender.
285 * - Adds \a fd to the list of file descriptors that should be closed
286 * in the child process when the server calls fork().
287 *
288 * \return A pointer to the allocated sender_client structure on success, \p
289 * NULL on errors.
290 *
291 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
292 * \ref cq_new(), \ref add_close_on_fork_list().
293 */
294 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
295 {
296 struct sender_client *sc;
297 int fd, ret;
298
299 if (ss->listen_fd < 0)
300 return NULL;
301 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
302 if (ret < 0)
303 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
304 if (ret <= 0)
305 return NULL;
306 ret = -E_MAX_CLIENTS;
307 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
308 goto err_out;
309 ret = mark_fd_nonblocking(fd);
310 if (ret < 0)
311 goto err_out;
312 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
313 if (ret < 0)
314 goto err_out;
315 ss->num_clients++;
316 sc = para_calloc(sizeof(*sc));
317 sc->fd = fd;
318 sc->name = para_strdup(remote_name(fd));
319 sc->cq = cq_new(MAX_CQ_BYTES);
320 para_list_add(&sc->node, &ss->client_list);
321 add_close_on_fork_list(fd);
322 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
323 sc->name, fd);
324 return sc;
325 err_out:
326 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
327 close(fd);
328 return NULL;
329 }
330
331 /**
332 * Get the generic help text.
333 *
334 * \return A dynamically allocated string containing the help text for
335 * a paraslash sender.
336 */
337 char *generic_sender_help(void)
338 {
339 return make_message(
340 "usage: {on|off}\n"
341 "usage: {allow|deny} IP[/netmask]\n"
342 " where mask defaults to 32\n"
343 "example: allow 192.168.0.1/24\n"
344 );
345 }
346
347 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
348 {
349 int32_t val;
350 char *a = para_strdup(arg),
351 *b = strchr(a, ':'),
352 *c = strrchr(a, ':');
353 int ret = -E_COMMAND_SYNTAX;
354
355 if (!b || !c)
356 goto out;
357 *b = *c = '\0';
358
359 ret = para_atoi32(a, &val);
360 if (ret < 0)
361 goto out;
362
363 /* optional max_slice_bytes (0 means "use MTU") */
364 if (b == c) {
365 scd->max_slice_bytes = 0;
366 } else {
367 if (val < 0 || val > 65535)
368 goto fec_einval;
369 scd->max_slice_bytes = val;
370
371 ret = para_atoi32(b + 1, &val);
372 if (ret < 0)
373 goto out;
374 }
375
376 /* k = data_slices_per_group */
377 if (val < 0 || val > 255)
378 goto fec_einval;
379 scd->data_slices_per_group = val;
380
381 /* n = slices_per_group */
382 ret = para_atoi32(c + 1, &val);
383 if (ret < 0)
384 goto out;
385 if (val < 0 || val < scd->data_slices_per_group)
386 goto fec_einval;
387 scd->slices_per_group = val;
388 ret = 0;
389 out:
390 free(a);
391 return ret;
392 fec_einval:
393 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
394 goto out;
395 }
396
397 /**
398 * Parse a FEC URL string.
399 *
400 * \param arg the URL string to parse.
401 * \param scd The structure containing host, port and the FEC parameters.
402 *
403 * \return Standard.
404 *
405 * A FEC URL consists of an ordinary URL string according to RFC 3986,
406 * optionally followed by a slash and the three FEC parameters slice_size,
407 * data_slices_per_group and slices_per_group. The three FEC parameters are
408 * separated by colons.
409 *
410 * \sa \ref parse_url().
411 */
412 int parse_fec_url(const char *arg, struct sender_command_data *scd)
413 {
414 char *a = para_strdup(arg), *p = strchr(a, '/');
415 int ret = 0;
416
417 /* default fec parameters */
418 scd->max_slice_bytes = 0;
419 scd->data_slices_per_group = 14;
420 scd->slices_per_group = 16;
421
422 if (p) {
423 *p = '\0';
424 ret = parse_fec_parms(p + 1, scd);
425 if (ret < 0)
426 goto out;
427 }
428 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
429 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
430 out:
431 free(a);
432 return ret;
433 }