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