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