Remove the .purpose and .help args from struct ggo_help.
[paraslash.git] / send_common.c
1 /*
2  * Copyright (C) 2005-2008 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 /**
97  * Write a buffer to a non-blocking file descriptor.
98  *
99  * \param fd The file descriptor.
100  * \param buf the buffer to write.
101  * \param len the number of bytes of \a buf.
102  * \param max_bytes_per_write Do not write more than that many bytes at once.
103  *
104  * If \a max_bytes_per_write is non-zero, do not send more than that many bytes
105  * per write().
106  *
107  * EAGAIN is not considered an error condition.  For example CCID3 has a
108  * sending wait queue which fills up and is emptied asynchronously. The EAGAIN
109  * case means that there is currently no space in the wait queue, but this can
110  * change at any moment.
111  *
112  * \return Negative on errors, number of bytes written else.
113  */
114 static int write_nonblock(int fd, const char *buf, size_t len,
115                 size_t max_bytes_per_write)
116 {
117         size_t written = 0;
118         int ret = 0;
119
120         while (written < len) {
121                 size_t num = len - written;
122
123                 if (max_bytes_per_write && max_bytes_per_write < num)
124                         num = max_bytes_per_write;
125                 ret = write(fd, buf + written, num);
126                 if (ret < 0 && errno == EAGAIN)
127                         return written;
128                 if (ret < 0)
129                         return -ERRNO_TO_PARA_ERROR(errno);
130                 written += ret;
131         }
132         return written;
133 }
134
135 static int queue_chunk_or_shutdown(struct sender_client *sc,
136                 struct sender_status *ss, const char *buf, size_t num_bytes)
137 {
138         int ret = cq_enqueue(sc->cq, buf, num_bytes);
139         if (ret < 0)
140                 shutdown_client(sc, ss);
141         return ret;
142 }
143
144 /* return: negative on errors, zero if not everything was sent, one otherwise */
145 static int send_queued_chunks(struct sender_client *sc,
146                 size_t max_bytes_per_write)
147 {
148         struct queued_chunk *qc;
149         while ((qc = cq_peek(sc->cq))) {
150                 const char *buf;
151                 size_t len;
152                 int ret;
153                 cq_get(qc, &buf, &len);
154                 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
155                 if (ret < 0)
156                         return ret;
157                 cq_update(sc->cq, ret);
158                 if (ret != len)
159                         return 0;
160                 cq_dequeue(sc->cq);
161         }
162         return 1;
163 }
164
165 /**
166  * Send one chunk of audio data to a connected client.
167  *
168  * \param sc The client.
169  * \param ss The sender.
170  * \param max_bytes_per_write Split writes to chunks of at most that many bytes.
171  * \param current_chunk The number of the chunk to write.
172  * \param buf The data to write.
173  * \param len The number of bytes of \a buf.
174  * \param header_buf The audio file header.
175  * \param header_len The number of bytes of \a header_buf.
176  *
177  * On errors, the client is shut down. If only a part of the buffer could be
178  * written, the remainder is put into the chunk queue for that client.
179  */
180 void send_chunk(struct sender_client *sc, struct sender_status *ss,
181                 size_t max_bytes_per_write, long unsigned current_chunk,
182                 const char *buf, size_t len, const char *header_buf,
183                 size_t header_len)
184 {
185         int ret;
186
187         if (!sc->header_sent && current_chunk) {
188                 if (header_buf && header_len > 0) {
189                         ret = queue_chunk_or_shutdown(sc, ss, header_buf, header_len);
190                         if (ret < 0)
191                                 goto out;
192                 }
193                 sc->header_sent = 1;
194         }
195         ret = send_queued_chunks(sc, max_bytes_per_write);
196         if (ret < 0) {
197                 shutdown_client(sc, ss);
198                 goto out;
199         }
200         if (!len)
201                 goto out;
202         if (!ret) { /* still data left in the queue */
203                 ret = queue_chunk_or_shutdown(sc, ss, buf, len);
204                 goto out;
205         }
206         ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
207         if (ret < 0) {
208                 shutdown_client(sc, ss);
209                 goto out;
210         }
211         if (ret != len)
212                 ret = queue_chunk_or_shutdown(sc, ss, buf + ret, len - ret);
213 out:
214         if (ret < 0)
215                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
216 }
217
218 /**
219  * Initialize a struct sender status.
220  *
221  * \param ss The struct to initialize.
222  * \param access_arg The array of access arguments given at the command line.
223  * \param num_access_args The number of elements in \a access_arg.
224  * \param port The tcp or dccp port to listen on.
225  * \param max_clients The maximal number of simultaneous connections.
226  * \param default_deny Whether a blacklist should be used for access control.
227  */
228 void init_sender_status(struct sender_status *ss, char **access_arg,
229         int num_access_args, int port, int max_clients, int default_deny)
230 {
231         ss->listen_fd = -1;
232         INIT_LIST_HEAD(&ss->client_list);
233         ss->port = port;
234         acl_init(&ss->acl, access_arg, num_access_args);
235         ss->num_clients = 0;
236         ss->max_clients = max_clients;
237         ss->default_deny = default_deny;
238 }
239
240 /**
241  * Return a string containing the current status of a sender.
242  *
243  * \param ss The sender.
244  * \param name Used for printing the header line.
245  *
246  * \return The string printed in the "si" command.
247  */
248 char *get_sender_info(struct sender_status *ss, char *name)
249 {
250         char *clnts = NULL, *ret;
251         struct sender_client *sc, *tmp_sc;
252
253         char *acl_contents = acl_get_contents(&ss->acl);
254         list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
255                 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
256                 free(clnts);
257                 clnts = tmp;
258         }
259         ret = make_message(
260                 "%s sender:\n"
261                 "\tstatus: %s\n"
262                 "\tport: %d\n"
263                 "\tnumber of connected clients: %d\n"
264                 "\tmaximal number of clients: %d%s\n"
265                 "\tconnected clients: %s\n"
266                 "\taccess %s list: %s\n",
267                 name,
268                 (ss->listen_fd >= 0)? "on" : "off",
269                 ss->port,
270                 ss->num_clients,
271                 ss->max_clients,
272                 ss->max_clients > 0? "" : " (unlimited)",
273                 clnts? clnts : "(none)",
274                 ss->default_deny? "allow" : "deny",
275                 acl_contents? acl_contents : "(empty)"
276         );
277         free(acl_contents);
278         free(clnts);
279         return ret;
280 }
281
282 /**
283  * Allow connections from the given range of IP addresses.
284  *
285  * \param scd Contains the IP and the netmask.
286  * \param ss The sender.
287  *
288  * \sa generic_com_deny().
289  */
290 void generic_com_allow(struct sender_command_data *scd,
291                 struct sender_status *ss)
292 {
293         acl_allow(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
294 }
295
296 /**
297  * Deny connections from the given range of IP addresses.
298  *
299  * \param scd see \ref generic_com_allow().
300  * \param ss see \ref generic_com_allow().
301  *
302  * \sa generic_com_allow().
303  */
304 void generic_com_deny(struct sender_command_data *scd,
305                 struct sender_status *ss)
306 {
307         acl_deny(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
308 }
309
310 /**
311  * Activate a paraslash sender.
312  *
313  * \param ss The sender to activate.
314  * \param protocol The symbolic name of the transport-layer protocol.
315  *
316  * \return Standard.
317  */
318 int generic_com_on(struct sender_status *ss, unsigned protocol)
319 {
320         int ret;
321
322         if (ss->listen_fd >= 0)
323                 return 1;
324         ret = open_sender(protocol, ss->port);
325         if (ret < 0)
326                 return ret;
327         ss->listen_fd = ret;
328         return 1;
329 }
330
331 /**
332  * Deactivate a paraslash sender.
333  *
334  * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
335  *
336  * \param ss The sender to deactivate.
337  *
338  * \sa \ref del_close_on_fork_list(), shutdown_clients().
339  */
340 void generic_com_off(struct sender_status *ss)
341 {
342         if (ss->listen_fd < 0)
343                 return;
344         PARA_NOTICE_LOG("closing port %d\n", ss->port);
345         close(ss->listen_fd);
346         del_close_on_fork_list(ss->listen_fd);
347         shutdown_clients(ss);
348         ss->listen_fd = -1;
349 }
350
351 /**
352  * Accept a connection on the socket this server is listening on.
353  *
354  * \param ss The sender whose listening fd is ready for reading.
355  *
356  * This must be called only if the socket fd of \a ss is ready for reading.  It
357  * calls para_accept() to accept the connection and performs the following
358  * actions on the resulting file descriptor \a fd:
359  *
360  *      - Checks whether the maximal number of connections are exceeded.
361  *      - Sets \a fd to nonblocking mode.
362  *      - Checks the acl of the sender to find out whether connections
363  *        are allowed from the IP of the connecting peer.
364  *      - Increases the number of connections for this sender.
365  *      - Creates and initializes a new chunk queue for queuing network
366  *        packets that can not be sent immediately.
367  *      - Allocates a new struct sender_client and fills in its \a fd, \a cq
368  *        and \a name members.
369  *      - Adds \a fd to the list of connected clients for this sender.
370  *      - Adds \a fd to the list of file descriptors that should be closed
371  *        in the child process when the server calls fork().
372  *
373  * \return A pointer to the allocated sender_client structure on success, \p
374  * NULL on errors.
375  *
376  * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
377  * \ref cq_new(), \ref add_close_on_fork_list().
378  */
379 struct sender_client *accept_sender_client(struct sender_status *ss)
380 {
381         struct sender_client *sc;
382         int fd, ret = para_accept(ss->listen_fd, NULL, 0);
383         if (ret < 0) {
384                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
385                 return NULL;
386         }
387         fd = ret;
388         ret = -E_MAX_CLIENTS;
389         if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
390                 goto err_out;
391         ret = mark_fd_nonblocking(fd);
392         if (ret < 0)
393                 goto err_out;
394         ret = acl_check_access(fd, &ss->acl, ss->default_deny);
395         if (ret < 0)
396                 goto err_out;
397         ss->num_clients++;
398         sc = para_calloc(sizeof(*sc));
399         sc->fd = fd;
400         sc->name = make_message("%s", remote_name(fd));
401         sc->cq = cq_new(MAX_CQ_BYTES);
402         para_list_add(&sc->node, &ss->client_list);
403         add_close_on_fork_list(fd);
404         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
405                 sc->name, fd);
406         return sc;
407 err_out:
408         PARA_WARNING_LOG("%s\n", para_strerror(-ret));
409         close(fd);
410         return NULL;
411 }
412
413 /**
414  * Get the generic help text.
415  *
416  * \return A dynamically allocated string containing the help text for
417  * a paraslash sender.
418  */
419 char *generic_sender_help(void)
420 {
421         return make_message(
422                 "usage: {on|off}\n"
423                 "usage: {allow|deny} IP mask\n"
424                 "example: allow 127.0.0.1 32\n"
425         );
426 }