mp: Always set mp_context to NULL on errors.
[paraslash.git] / send_common.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file send_common.c Functions used by more than one paraslash sender. */
4
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <regex.h>
8 #include <osl.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "para.h"
15 #include "error.h"
16 #include "string.h"
17 #include "fd.h"
18 #include "net.h"
19 #include "list.h"
20 #include "afh.h"
21 #include "afs.h"
22 #include "server.h"
23 #include "acl.h"
24 #include "send.h"
25 #include "close_on_fork.h"
26 #include "chunk_queue.h"
27 #include "sched.h"
28 #include "vss.h"
29
30 /** Clients will be kicked if there are more than that many bytes pending. */
31 #define MAX_CQ_BYTES 40000
32
33 /**
34  * Shut down a client connected to a paraslash sender.
35  *
36  * \param sc The client to shut down.
37  * \param ss The sender whose clients are to be shut down.
38  *
39  * Close the file descriptor given by \a sc, remove it from the close-on-fork
40  * list, destroy the chunk queue of this client, delete the client from the
41  * list of connected clients and free the sender_client struct.
42  *
43  * \sa \ref shutdown_clients().
44  */
45 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
46 {
47         PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
48         free(sc->name);
49         if (!process_is_command_handler()) {
50                 close(sc->fd);
51                 del_close_on_fork_list(sc->fd);
52         }
53         cq_destroy(sc->cq);
54         list_del(&sc->node);
55         free(sc->private_data);
56         free(sc);
57         ss->num_clients--;
58 }
59
60 /**
61  * Shut down all clients connected to a paraslash sender.
62  *
63  * \param ss The sender whose clients are to be shut down.
64  *
65  * This just loops over all connected clients and calls shutdown_client()
66  * for each client.
67  */
68 void shutdown_clients(struct sender_status *ss)
69 {
70         struct sender_client *sc, *tmp;
71         list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
72                 shutdown_client(sc, ss);
73 }
74
75 /**
76  * Try to empty the chunk queue for this fd.
77  *
78  * \param fd The file descriptor.
79  * \param cq The list of queued chunks.
80  *
81  * \return Negative on errors, zero if not everything was sent, one otherwise.
82  */
83 int send_queued_chunks(int fd, struct chunk_queue *cq)
84 {
85         struct queued_chunk *qc;
86         while ((qc = cq_peek(cq))) {
87                 const char *buf;
88                 size_t len;
89                 int ret;
90
91                 cq_get(qc, &buf, &len);
92                 ret = xwrite(fd, buf, len);
93                 if (ret < 0)
94                         return ret;
95                 cq_update(cq, ret);
96                 if (ret != len)
97                         return 0;
98                 cq_dequeue(cq);
99         }
100         return 1;
101 }
102
103 /**
104  * Initialize a struct sender status.
105  *
106  * \param ss The struct to initialize.
107  * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
108  * \param listen_address_opt_result Where to listen on.
109  * \param default_port Used for addresses with no specified port.
110  * \param max_clients The maximal number of simultaneous connections.
111  * \param default_deny Whether a blacklist should be used for access control.
112  */
113 void init_sender_status(struct sender_status *ss,
114                 const struct lls_opt_result *acl_opt_result,
115                 const struct lls_opt_result *listen_address_opt_result,
116                 int default_port, int max_clients, int default_deny)
117 {
118         int i;
119         unsigned n = lls_opt_given(listen_address_opt_result);
120
121         if (n == 0) {
122                 ss->num_listen_fds = 1;
123                 ss->listen_addresses = para_malloc(sizeof(char *));
124                 ss->listen_addresses[0] = NULL;
125                 ss->listen_fds = para_malloc(sizeof(int));
126                 ss->listen_fds[0] = -1;
127         } else {
128                 ss->num_listen_fds = n;
129                 ss->listen_addresses = para_malloc(n * sizeof(char *));
130                 ss->listen_fds = para_malloc(n * sizeof(int));
131                 FOR_EACH_LISTEN_FD(i, ss) {
132                         ss->listen_addresses[i] = para_strdup(lls_string_val(i,
133                                 listen_address_opt_result));
134                         ss->listen_fds[i] = -1;
135                 }
136         }
137         ss->default_port = default_port;
138
139         INIT_LIST_HEAD(&ss->client_list);
140         /* Initialize an access control list */
141         INIT_LIST_HEAD(&ss->acl);
142         for (i = 0; i < lls_opt_given(acl_opt_result); i++) {
143                 const char *arg = lls_string_val(i, acl_opt_result);
144                 char addr[16];
145                 int mask;
146                 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
147                         PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
148                                 arg);
149                 else
150                         acl_add_entry(&ss->acl, addr, mask);
151         }
152         ss->num_clients = 0;
153         ss->max_clients = max_clients;
154         ss->default_deny = default_deny;
155 }
156
157 /**
158  * Deallocate all resources allocated in \ref init_sender_status().
159  *
160  * \param ss The structure whose components should be freed.
161  *
162  * This frees the dynamically allocated parts of the structure which was
163  * initialized by an earlier call to \ref init_sender_status(). It does *not*
164  * call free(ss), though.
165  */
166 void free_sender_status(const struct sender_status *ss)
167 {
168         int i;
169
170         free(ss->listen_fds);
171         FOR_EACH_LISTEN_FD(i, ss)
172                 free(ss->listen_addresses[i]);
173         free(ss->listen_addresses);
174 }
175
176 /**
177  * Return a string containing the current status of a sender.
178  *
179  * \param ss The sender.
180  * \param name Used for printing the header line.
181  *
182  * \return The string printed in the "si" command.
183  */
184 char *generic_sender_status(struct sender_status *ss, const char *name)
185 {
186         char *clnts = NULL, *ret, *addr = NULL;
187         struct sender_client *sc, *tmp_sc;
188         unsigned n;
189         char *acl_contents = acl_get_contents(&ss->acl);
190
191         list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
192                 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
193                 free(clnts);
194                 clnts = tmp;
195         }
196         FOR_EACH_LISTEN_FD(n, ss) {
197                 char *url = format_url(ss->listen_addresses[n], ss->default_port);
198                 char *tmp = make_message("%s%s%s (fd %d)", addr?
199                         addr : "", addr? ", " : "", url,
200                         ss->listen_fds[n]);
201                 free(url);
202                 free(addr);
203                 addr = tmp;
204         }
205         ret = make_message(
206                 "listening address(es): %s\n"
207                 "default port: %s\n"
208                 "number of connected clients: %d\n"
209                 "maximal number of clients: %d%s\n"
210                 "connected clients: %s\n"
211                 "access %s list: %s\n",
212                 addr,
213                 stringify_port(ss->default_port,
214                         strcmp(name, "http")? "dccp" : "tcp"),
215                 ss->num_clients,
216                 ss->max_clients,
217                 ss->max_clients > 0? "" : " (unlimited)",
218                 clnts? clnts : "(none)",
219                 ss->default_deny? "allow" : "deny",
220                 acl_contents? acl_contents : "(empty)"
221         );
222         free(acl_contents);
223         free(clnts);
224         return ret;
225 }
226
227 /**
228  * Allow connections from the given range of IP addresses.
229  *
230  * \param scd Contains the IP and the netmask.
231  * \param ss The sender.
232  *
233  * \sa \ref generic_com_deny().
234  */
235 void generic_com_allow(struct sender_command_data *scd,
236                 struct sender_status *ss)
237 {
238         acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
239 }
240
241 /**
242  * Empty the access control list of a sender.
243  *
244  * \param acl The access control list of the sender.
245  *
246  * This is called from the ->shutdown methods of the http and the dccp sender.
247  */
248 void generic_acl_deplete(struct list_head *acl)
249 {
250         /*
251          * Since default_deny is false, the ACL is considered a blacklist. A
252          * netmask of zero matches any IP address, so this call empties the ACL.
253          */
254         acl_allow("0.0.0.0", 0 /* netmask */, acl, 0 /* default_deny */);
255 }
256
257 /**
258  * Deny connections from the given range of IP addresses.
259  *
260  * \param scd see \ref generic_com_allow().
261  * \param ss see \ref generic_com_allow().
262  *
263  * \sa \ref generic_com_allow().
264  */
265 void generic_com_deny(struct sender_command_data *scd,
266                 struct sender_status *ss)
267 {
268         acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
269 }
270
271 /**
272  * Activate a paraslash sender.
273  *
274  * \param ss The sender to activate.
275  * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
276  *
277  * This opens a passive socket of given layer4 type, sets the resulting file
278  * descriptor to nonblocking mode and adds it to the close on fork list.
279  *
280  * Errors are logged but otherwise ignored.
281  */
282 void generic_com_on(struct sender_status *ss, unsigned protocol)
283 {
284         int ret;
285         unsigned n;
286
287         FOR_EACH_LISTEN_FD(n, ss) {
288                 if (ss->listen_fds[n] >= 0)
289                         continue;
290                 ret = para_listen(protocol, ss->listen_addresses[n],
291                         ss->default_port);
292                 if (ret < 0) {
293                         char *url = format_url(ss->listen_addresses[n],
294                                 ss->default_port);
295                         PARA_ERROR_LOG("could not listen on %s %s: %s\n",
296                                 protocol == IPPROTO_TCP? "TCP" : "DCCP",
297                                 url, para_strerror(-ret));
298                         free(url);
299                         continue;
300                 }
301                 ss->listen_fds[n] = ret;
302                 ret = mark_fd_nonblocking(ss->listen_fds[n]);
303                 if (ret < 0) {
304                         char *url = format_url(ss->listen_addresses[n],
305                                 ss->default_port);
306                         PARA_ERROR_LOG("could not set %s socket fd for %s to "
307                                 "nonblocking mode: %s\n",
308                                 protocol == IPPROTO_TCP? "TCP" : "DCCP", url,
309                                 para_strerror(-ret));
310                         free(url);
311                         close(ss->listen_fds[n]);
312                         ss->listen_fds[n] = -1;
313                         continue;
314                 }
315                 add_close_on_fork_list(ss->listen_fds[n]);
316         }
317 }
318
319 /**
320  * Deactivate a paraslash sender.
321  *
322  * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
323  *
324  * \param ss The sender to deactivate.
325  *
326  * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
327  */
328 void generic_com_off(struct sender_status *ss)
329 {
330         unsigned n;
331
332         FOR_EACH_LISTEN_FD(n, ss) {
333                 if (ss->listen_fds[n] < 0)
334                         return;
335                 close(ss->listen_fds[n]);
336                 del_close_on_fork_list(ss->listen_fds[n]);
337                 shutdown_clients(ss);
338                 ss->listen_fds[n] = -1;
339         }
340 }
341
342 /**
343  * Accept a connection on the socket(s) this server is listening on.
344  *
345  * \param ss The sender whose listening fd is ready for reading.
346  * \param rfds Passed to para_accept(),
347  *
348  * This accepts incoming connections on any of the listening sockets of the
349  * server. If there is a connection pending, the function
350  *
351  *      - Checks whether the maximal number of connections are exceeded.
352  *      - Sets \a fd to nonblocking mode.
353  *      - Checks the acl of the sender to find out whether connections
354  *        are allowed from the IP of the connecting peer.
355  *      - Increases the number of connections for this sender.
356  *      - Creates and initializes a new chunk queue for queuing network
357  *        packets that can not be sent immediately.
358  *      - Allocates a new struct sender_client and fills in its \a fd, \a cq
359  *        and \a name members.
360  *      - Adds \a fd to the list of connected clients for this sender.
361  *      - Adds \a fd to the list of file descriptors that should be closed
362  *        in the child process when the server calls fork().
363  *
364  * \return A pointer to the allocated sender_client structure on success, \p
365  * NULL on errors.
366  *
367  * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
368  * \ref cq_new(), \ref add_close_on_fork_list().
369  */
370 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
371 {
372         struct sender_client *sc;
373         int fd, ret;
374         unsigned n;
375
376         FOR_EACH_LISTEN_FD(n, ss) {
377                 if (ss->listen_fds[n] < 0)
378                         continue;
379                 ret = para_accept(ss->listen_fds[n], rfds, NULL, 0, &fd);
380                 if (ret < 0)
381                         goto warn;
382                 if (ret == 0)
383                         continue;
384                 ret = -E_MAX_CLIENTS;
385                 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
386                         goto close_fd_and_warn;
387                 ret = mark_fd_nonblocking(fd);
388                 if (ret < 0)
389                         goto close_fd_and_warn;
390                 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
391                 if (ret < 0)
392                         goto close_fd_and_warn;
393                 ss->num_clients++;
394                 sc = para_calloc(sizeof(*sc));
395                 sc->fd = fd;
396                 sc->name = para_strdup(remote_name(fd));
397                 sc->cq = cq_new(MAX_CQ_BYTES);
398                 para_list_add(&sc->node, &ss->client_list);
399                 add_close_on_fork_list(fd);
400                 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
401                         sc->name, fd);
402                 return sc;
403 close_fd_and_warn:
404                 close(fd);
405 warn:
406                 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
407         }
408         return NULL;
409 }
410
411 /**
412  * Get the generic help text.
413  *
414  * \return A dynamically allocated string containing the help text for
415  * a paraslash sender.
416  */
417 char *generic_sender_help(void)
418 {
419         return make_message(
420                 "usage: {on|off}\n"
421                 "usage: {allow|deny} IP[/netmask]\n"
422                 "       where mask defaults to 32\n"
423                 "example: allow 192.168.0.1/24\n"
424         );
425 }
426
427 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
428 {
429         int32_t val;
430         char *a = para_strdup(arg),
431              *b = strchr(a, ':'),
432              *c = strrchr(a, ':');
433         int ret = -E_COMMAND_SYNTAX;
434
435         if (!b || !c)
436                 goto out;
437         *b = *c = '\0';
438
439         ret = para_atoi32(a, &val);
440         if (ret < 0)
441                 goto out;
442
443         /* optional max_slice_bytes (0 means "use MTU") */
444         if (b == c) {
445                 scd->max_slice_bytes = 0;
446         } else {
447                 if (val < 0 || val > 65535)
448                         goto fec_einval;
449                 scd->max_slice_bytes = val;
450
451                 ret = para_atoi32(b + 1, &val);
452                 if (ret < 0)
453                         goto out;
454         }
455
456         /* k = data_slices_per_group */
457         if (val < 0 || val > 255)
458                 goto fec_einval;
459         scd->data_slices_per_group = val;
460
461         /* n = slices_per_group */
462         ret = para_atoi32(c + 1, &val);
463         if (ret < 0)
464                 goto out;
465         if (val < 0 || val < scd->data_slices_per_group)
466                 goto fec_einval;
467         scd->slices_per_group = val;
468         ret = 0;
469 out:
470         free(a);
471         return ret;
472 fec_einval:
473         ret = -ERRNO_TO_PARA_ERROR(EINVAL);
474         goto out;
475 }
476
477 /**
478  * Parse a FEC URL string.
479  *
480  * \param arg the URL string to parse.
481  * \param scd The structure containing host, port and the FEC parameters.
482  *
483  * \return Standard.
484  *
485  * A FEC URL consists of an ordinary URL string according to RFC 3986,
486  * optionally followed by a slash and the three FEC parameters slice_size,
487  * data_slices_per_group and slices_per_group. The three FEC parameters are
488  * separated by colons.
489  *
490  * \sa \ref parse_url().
491  */
492 int parse_fec_url(const char *arg, struct sender_command_data *scd)
493 {
494         char *a = para_strdup(arg), *p = strchr(a, '/');
495         int ret = 0;
496
497         /* default fec parameters */
498         scd->max_slice_bytes       = 0;
499         scd->data_slices_per_group = 14;
500         scd->slices_per_group      = 16;
501
502         if (p) {
503                 *p = '\0';
504                 ret = parse_fec_parms(p + 1, scd);
505                 if (ret < 0)
506                         goto out;
507         }
508         if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
509                 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
510 out:
511         free(a);
512         return ret;
513 }