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