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