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