1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file send_common.c Functions used by more than one paraslash sender. */
5 #include <netinet/in.h>
6 #include <sys/socket.h>
26 #include "close_on_fork.h"
27 #include "chunk_queue.h"
30 /** Clients will be kicked if there are more than that many bytes pending. */
31 #define MAX_CQ_BYTES 40000
34 * Shut down a client connected to a paraslash sender.
36 * \param sc The client to shut down.
37 * \param ss The sender whose clients are to be shut down.
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.
43 * \sa \ref shutdown_clients().
45 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
47 if (!process_is_command_handler()) {
48 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
50 del_close_on_fork_list(sc->fd);
55 free(sc->private_data);
61 * Shut down all clients connected to a paraslash sender.
63 * \param ss The sender whose clients are to be shut down.
65 * This just loops over all connected clients and calls shutdown_client()
68 void shutdown_clients(struct sender_status *ss)
70 struct sender_client *sc, *tmp;
71 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
72 shutdown_client(sc, ss);
76 * Try to empty the chunk queue for this fd.
78 * \param fd The file descriptor.
79 * \param cq The list of queued chunks.
81 * \return Negative on errors, zero if not everything was sent, one otherwise.
83 int send_queued_chunks(int fd, struct chunk_queue *cq)
85 struct queued_chunk *qc;
86 while ((qc = cq_peek(cq))) {
91 cq_get(qc, &buf, &len);
92 ret = xwrite(fd, buf, len);
104 * Initialize a struct sender status.
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.
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)
119 unsigned n = lls_opt_given(listen_address_opt_result);
122 ss->num_listen_fds = 1;
123 ss->listen_addresses = alloc(sizeof(char *));
124 ss->listen_addresses[0] = NULL;
125 ss->listen_fds = alloc(sizeof(int));
126 ss->listen_fds[0] = -1;
128 ss->num_listen_fds = n;
129 ss->listen_addresses = alloc(n * sizeof(char *));
130 ss->listen_fds = alloc(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;
137 ss->default_port = default_port;
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);
146 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
147 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
150 acl_add_entry(&ss->acl, addr, mask);
153 ss->max_clients = max_clients;
154 ss->default_deny = default_deny;
158 * Deallocate all resources allocated in \ref init_sender_status().
160 * \param ss The structure whose components should be freed.
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.
166 void free_sender_status(const struct sender_status *ss)
170 free(ss->listen_fds);
171 FOR_EACH_LISTEN_FD(i, ss)
172 free(ss->listen_addresses[i]);
173 free(ss->listen_addresses);
177 * Return a string containing the current status of a sender.
179 * \param ss The sender.
180 * \param name Used for printing the header line.
182 * \return The string printed in the "si" command.
184 __malloc char *generic_sender_status(struct sender_status *ss, const char *name)
186 char *clnts = NULL, *ret, *addr = NULL;
187 struct sender_client *sc, *tmp_sc;
189 char *acl_contents = acl_get_contents(&ss->acl);
191 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
192 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
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,
206 "listening address(es): %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",
213 stringify_port(ss->default_port,
214 strcmp(name, "http")? "dccp" : "tcp"),
217 ss->max_clients > 0? "" : " (unlimited)",
218 clnts? clnts : "(none)",
219 ss->default_deny? "allow" : "deny",
220 acl_contents? acl_contents : "(empty)"
228 * Allow connections from the given range of IP addresses.
230 * \param scd Contains the IP and the netmask.
231 * \param ss The sender.
233 * \sa \ref generic_com_deny().
235 void generic_com_allow(struct sender_command_data *scd,
236 struct sender_status *ss)
238 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
242 * Empty the access control list of a sender.
244 * \param acl The access control list of the sender.
246 * This is called from the ->shutdown methods of the http and the dccp sender.
248 void generic_acl_deplete(struct list_head *acl)
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.
254 acl_allow("0.0.0.0", 0 /* netmask */, acl, 0 /* default_deny */);
258 * Deny connections from the given range of IP addresses.
260 * \param scd see \ref generic_com_allow().
261 * \param ss see \ref generic_com_allow().
263 * \sa \ref generic_com_allow().
265 void generic_com_deny(struct sender_command_data *scd,
266 struct sender_status *ss)
268 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
272 * Activate a paraslash sender.
274 * \param ss The sender to activate.
275 * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
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.
280 * Errors are logged but otherwise ignored.
282 void generic_com_on(struct sender_status *ss, unsigned protocol)
287 FOR_EACH_LISTEN_FD(n, ss) {
288 if (ss->listen_fds[n] >= 0)
290 ret = para_listen(protocol, ss->listen_addresses[n],
293 char *url = format_url(ss->listen_addresses[n],
295 PARA_ERROR_LOG("could not listen on %s %s: %s\n",
296 protocol == IPPROTO_TCP? "TCP" : "DCCP",
297 url, para_strerror(-ret));
301 ss->listen_fds[n] = ret;
302 ret = mark_fd_nonblocking(ss->listen_fds[n]);
304 char *url = format_url(ss->listen_addresses[n],
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));
311 close(ss->listen_fds[n]);
312 ss->listen_fds[n] = -1;
315 add_close_on_fork_list(ss->listen_fds[n]);
320 * Deactivate a paraslash sender.
322 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
324 * \param ss The sender to deactivate.
326 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
328 void generic_com_off(struct sender_status *ss)
332 FOR_EACH_LISTEN_FD(n, ss) {
333 if (ss->listen_fds[n] < 0)
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;
343 * Accept a connection on the socket(s) this server is listening on.
345 * \param ss The sender whose listening fd is ready for reading.
347 * This accepts incoming connections on any of the listening sockets of the
348 * server. If there is a connection pending, the function
350 * - Checks whether the maximal number of connections are exceeded.
351 * - Sets \a fd to nonblocking mode.
352 * - Checks the acl of the sender to find out whether connections
353 * are allowed from the IP of the connecting peer.
354 * - Increases the number of connections for this sender.
355 * - Creates and initializes a new chunk queue for queuing network
356 * packets that can not be sent immediately.
357 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
358 * and \a name members.
359 * - Adds \a fd to the list of connected clients for this sender.
360 * - Adds \a fd to the list of file descriptors that should be closed
361 * in the child process when the server calls fork().
363 * \return A pointer to the allocated sender_client structure on success, \p
366 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
367 * \ref cq_new(), \ref add_close_on_fork_list().
369 struct sender_client *accept_sender_client(struct sender_status *ss)
371 struct sender_client *sc;
375 FOR_EACH_LISTEN_FD(n, ss) {
376 if (ss->listen_fds[n] < 0)
378 ret = para_accept(ss->listen_fds[n], NULL, 0, &fd);
383 ret = -E_MAX_CLIENTS;
384 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
385 goto close_fd_and_warn;
386 ret = mark_fd_nonblocking(fd);
388 goto close_fd_and_warn;
389 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
391 goto close_fd_and_warn;
393 sc = zalloc(sizeof(*sc));
395 sc->name = para_strdup(remote_name(fd));
396 sc->cq = cq_new(MAX_CQ_BYTES);
397 para_list_add(&sc->node, &ss->client_list);
398 add_close_on_fork_list(fd);
399 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
405 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
411 * Get the generic help text.
413 * \return A dynamically allocated string containing the help text for
414 * a paraslash sender.
416 __malloc char *generic_sender_help(void)
420 "usage: {allow|deny} IP[/netmask]\n"
421 " where mask defaults to 32\n"
422 "example: allow 192.168.0.1/24\n"
426 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
429 char *a = para_strdup(arg),
431 *c = strrchr(a, ':');
432 int ret = -E_COMMAND_SYNTAX;
438 ret = para_atoi32(a, &val);
442 /* optional max_slice_bytes (0 means "use MTU") */
444 scd->max_slice_bytes = 0;
446 if (val < 0 || val > 65535)
448 scd->max_slice_bytes = val;
450 ret = para_atoi32(b + 1, &val);
455 /* k = data_slices_per_group */
456 if (val < 0 || val > 255)
458 scd->data_slices_per_group = val;
460 /* n = slices_per_group */
461 ret = para_atoi32(c + 1, &val);
464 if (val < 0 || val < scd->data_slices_per_group)
466 scd->slices_per_group = val;
472 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
477 * Parse a FEC URL string.
479 * \param arg the URL string to parse.
480 * \param scd The structure containing host, port and the FEC parameters.
484 * A FEC URL consists of an ordinary URL string according to RFC 3986,
485 * optionally followed by a slash and the three FEC parameters slice_size,
486 * data_slices_per_group and slices_per_group. The three FEC parameters are
487 * separated by colons.
489 * \sa \ref parse_url().
491 int parse_fec_url(const char *arg, struct sender_command_data *scd)
493 char *a = para_strdup(arg), *p = strchr(a, '/');
496 /* default fec parameters */
497 scd->max_slice_bytes = 0;
498 scd->data_slices_per_group = 14;
499 scd->slices_per_group = 16;
503 ret = parse_fec_parms(p + 1, scd);
507 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
508 ret = -ERRNO_TO_PARA_ERROR(EINVAL);