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