string.c: Fix typos in comment.
[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 * Return a string containing the current status of a sender.
159 *
160 * \param ss The sender.
161 * \param name Used for printing the header line.
162 *
163 * \return The string printed in the "si" command.
164 */
165 char *generic_sender_status(struct sender_status *ss, const char *name)
166 {
167 char *clnts = NULL, *ret, *addr = NULL;
168 struct sender_client *sc, *tmp_sc;
169 unsigned n;
170 char *acl_contents = acl_get_contents(&ss->acl);
171
172 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
173 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
174 free(clnts);
175 clnts = tmp;
176 }
177 FOR_EACH_LISTEN_FD(n, ss) {
178 char *url = format_url(ss->listen_addresses[n], ss->default_port);
179 char *tmp = make_message("%s%s%s (fd %d)", addr?
180 addr : "", addr? ", " : "", url,
181 ss->listen_fds[n]);
182 free(url);
183 free(addr);
184 addr = tmp;
185 }
186 ret = make_message(
187 "listening address(es): %s\n"
188 "default 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 addr,
194 stringify_port(ss->default_port,
195 strcmp(name, "http")? "dccp" : "tcp"),
196 ss->num_clients,
197 ss->max_clients,
198 ss->max_clients > 0? "" : " (unlimited)",
199 clnts? clnts : "(none)",
200 ss->default_deny? "allow" : "deny",
201 acl_contents? acl_contents : "(empty)"
202 );
203 free(acl_contents);
204 free(clnts);
205 return ret;
206 }
207
208 /**
209 * Allow connections from the given range of IP addresses.
210 *
211 * \param scd Contains the IP and the netmask.
212 * \param ss The sender.
213 *
214 * \sa \ref generic_com_deny().
215 */
216 void generic_com_allow(struct sender_command_data *scd,
217 struct sender_status *ss)
218 {
219 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
220 }
221
222 /**
223 * Empty the access control list of a sender.
224 *
225 * \param acl The access control list of the sender.
226 *
227 * This is called from the ->shutdown methods of the http and the dccp sender.
228 */
229 void generic_acl_deplete(struct list_head *acl)
230 {
231 /*
232 * Since default_deny is false, the ACL is considered a blacklist. A
233 * netmask of zero matches any IP address, so this call empties the ACL.
234 */
235 acl_allow("0.0.0.0", 0 /* netmask */, acl, 0 /* default_deny */);
236 }
237
238 /**
239 * Deny connections from the given range of IP addresses.
240 *
241 * \param scd see \ref generic_com_allow().
242 * \param ss see \ref generic_com_allow().
243 *
244 * \sa \ref generic_com_allow().
245 */
246 void generic_com_deny(struct sender_command_data *scd,
247 struct sender_status *ss)
248 {
249 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
250 }
251
252 /**
253 * Activate a paraslash sender.
254 *
255 * \param ss The sender to activate.
256 * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
257 *
258 * This opens a passive socket of given layer4 type, sets the resulting file
259 * descriptor to nonblocking mode and adds it to the close on fork list.
260 *
261 * Errors are logged but otherwise ignored.
262 */
263 void generic_com_on(struct sender_status *ss, unsigned protocol)
264 {
265 int ret;
266 unsigned n;
267
268 FOR_EACH_LISTEN_FD(n, ss) {
269 if (ss->listen_fds[n] >= 0)
270 continue;
271 ret = para_listen(protocol, ss->listen_addresses[n],
272 ss->default_port);
273 if (ret < 0) {
274 char *url = format_url(ss->listen_addresses[n],
275 ss->default_port);
276 PARA_ERROR_LOG("could not listen on %s %s: %s\n",
277 protocol == IPPROTO_TCP? "TCP" : "DCCP",
278 url, para_strerror(-ret));
279 free(url);
280 continue;
281 }
282 ss->listen_fds[n] = ret;
283 ret = mark_fd_nonblocking(ss->listen_fds[n]);
284 if (ret < 0) {
285 char *url = format_url(ss->listen_addresses[n],
286 ss->default_port);
287 PARA_ERROR_LOG("could not set %s socket fd for %s to "
288 "nonblocking mode: %s\n",
289 protocol == IPPROTO_TCP? "TCP" : "DCCP", url,
290 para_strerror(-ret));
291 free(url);
292 close(ss->listen_fds[n]);
293 ss->listen_fds[n] = -1;
294 continue;
295 }
296 add_close_on_fork_list(ss->listen_fds[n]);
297 }
298 }
299
300 /**
301 * Deactivate a paraslash sender.
302 *
303 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
304 *
305 * \param ss The sender to deactivate.
306 *
307 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
308 */
309 void generic_com_off(struct sender_status *ss)
310 {
311 unsigned n;
312
313 FOR_EACH_LISTEN_FD(n, ss) {
314 if (ss->listen_fds[n] < 0)
315 return;
316 close(ss->listen_fds[n]);
317 del_close_on_fork_list(ss->listen_fds[n]);
318 shutdown_clients(ss);
319 ss->listen_fds[n] = -1;
320 }
321 }
322
323 /**
324 * Accept a connection on the socket(s) this server is listening on.
325 *
326 * \param ss The sender whose listening fd is ready for reading.
327 * \param rfds Passed to para_accept(),
328 *
329 * This accepts incoming connections on any of the listening sockets of the
330 * server. If there is a connection pending, the function
331 *
332 * - Checks whether the maximal number of connections are exceeded.
333 * - Sets \a fd to nonblocking mode.
334 * - Checks the acl of the sender to find out whether connections
335 * are allowed from the IP of the connecting peer.
336 * - Increases the number of connections for this sender.
337 * - Creates and initializes a new chunk queue for queuing network
338 * packets that can not be sent immediately.
339 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
340 * and \a name members.
341 * - Adds \a fd to the list of connected clients for this sender.
342 * - Adds \a fd to the list of file descriptors that should be closed
343 * in the child process when the server calls fork().
344 *
345 * \return A pointer to the allocated sender_client structure on success, \p
346 * NULL on errors.
347 *
348 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
349 * \ref cq_new(), \ref add_close_on_fork_list().
350 */
351 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
352 {
353 struct sender_client *sc;
354 int fd, ret;
355 unsigned n;
356
357 FOR_EACH_LISTEN_FD(n, ss) {
358 if (ss->listen_fds[n] < 0)
359 continue;
360 ret = para_accept(ss->listen_fds[n], rfds, NULL, 0, &fd);
361 if (ret < 0)
362 goto warn;
363 if (ret == 0)
364 continue;
365 ret = -E_MAX_CLIENTS;
366 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
367 goto close_fd_and_warn;
368 ret = mark_fd_nonblocking(fd);
369 if (ret < 0)
370 goto close_fd_and_warn;
371 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
372 if (ret < 0)
373 goto close_fd_and_warn;
374 ss->num_clients++;
375 sc = para_calloc(sizeof(*sc));
376 sc->fd = fd;
377 sc->name = para_strdup(remote_name(fd));
378 sc->cq = cq_new(MAX_CQ_BYTES);
379 para_list_add(&sc->node, &ss->client_list);
380 add_close_on_fork_list(fd);
381 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
382 sc->name, fd);
383 return sc;
384 close_fd_and_warn:
385 close(fd);
386 warn:
387 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
388 }
389 return NULL;
390 }
391
392 /**
393 * Get the generic help text.
394 *
395 * \return A dynamically allocated string containing the help text for
396 * a paraslash sender.
397 */
398 char *generic_sender_help(void)
399 {
400 return make_message(
401 "usage: {on|off}\n"
402 "usage: {allow|deny} IP[/netmask]\n"
403 " where mask defaults to 32\n"
404 "example: allow 192.168.0.1/24\n"
405 );
406 }
407
408 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
409 {
410 int32_t val;
411 char *a = para_strdup(arg),
412 *b = strchr(a, ':'),
413 *c = strrchr(a, ':');
414 int ret = -E_COMMAND_SYNTAX;
415
416 if (!b || !c)
417 goto out;
418 *b = *c = '\0';
419
420 ret = para_atoi32(a, &val);
421 if (ret < 0)
422 goto out;
423
424 /* optional max_slice_bytes (0 means "use MTU") */
425 if (b == c) {
426 scd->max_slice_bytes = 0;
427 } else {
428 if (val < 0 || val > 65535)
429 goto fec_einval;
430 scd->max_slice_bytes = val;
431
432 ret = para_atoi32(b + 1, &val);
433 if (ret < 0)
434 goto out;
435 }
436
437 /* k = data_slices_per_group */
438 if (val < 0 || val > 255)
439 goto fec_einval;
440 scd->data_slices_per_group = val;
441
442 /* n = slices_per_group */
443 ret = para_atoi32(c + 1, &val);
444 if (ret < 0)
445 goto out;
446 if (val < 0 || val < scd->data_slices_per_group)
447 goto fec_einval;
448 scd->slices_per_group = val;
449 ret = 0;
450 out:
451 free(a);
452 return ret;
453 fec_einval:
454 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
455 goto out;
456 }
457
458 /**
459 * Parse a FEC URL string.
460 *
461 * \param arg the URL string to parse.
462 * \param scd The structure containing host, port and the FEC parameters.
463 *
464 * \return Standard.
465 *
466 * A FEC URL consists of an ordinary URL string according to RFC 3986,
467 * optionally followed by a slash and the three FEC parameters slice_size,
468 * data_slices_per_group and slices_per_group. The three FEC parameters are
469 * separated by colons.
470 *
471 * \sa \ref parse_url().
472 */
473 int parse_fec_url(const char *arg, struct sender_command_data *scd)
474 {
475 char *a = para_strdup(arg), *p = strchr(a, '/');
476 int ret = 0;
477
478 /* default fec parameters */
479 scd->max_slice_bytes = 0;
480 scd->data_slices_per_group = 14;
481 scd->slices_per_group = 16;
482
483 if (p) {
484 *p = '\0';
485 ret = parse_fec_parms(p + 1, scd);
486 if (ret < 0)
487 goto out;
488 }
489 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
490 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
491 out:
492 free(a);
493 return ret;
494 }