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>
25 #include "close_on_fork.h"
26 #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 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
50 del_close_on_fork_list(sc->fd);
53 free(sc->private_data);
59 * Shut down all clients connected to a paraslash sender.
61 * \param ss The sender whose clients are to be shut down.
63 * This just loops over all connected clients and calls shutdown_client()
66 void shutdown_clients(struct sender_status *ss)
68 struct sender_client *sc, *tmp;
69 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
70 shutdown_client(sc, ss);
74 * Try to empty the chunk queue for this fd.
76 * \param fd The file descriptor.
77 * \param cq The list of queued chunks.
79 * \return Negative on errors, zero if not everything was sent, one otherwise.
81 int send_queued_chunks(int fd, struct chunk_queue *cq)
83 struct queued_chunk *qc;
84 while ((qc = cq_peek(cq))) {
89 cq_get(qc, &buf, &len);
90 ret = xwrite(fd, buf, len);
102 * Initialize a struct sender status.
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.
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)
117 unsigned n = lls_opt_given(listen_address_opt_result);
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;
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;
135 ss->default_port = default_port;
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);
144 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
145 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
148 acl_add_entry(&ss->acl, addr, mask);
151 ss->max_clients = max_clients;
152 ss->default_deny = default_deny;
156 * Return a string containing the current status of a sender.
158 * \param ss The sender.
159 * \param name Used for printing the header line.
161 * \return The string printed in the "si" command.
163 char *generic_sender_status(struct sender_status *ss, const char *name)
165 char *clnts = NULL, *ret, *addr = NULL;
166 struct sender_client *sc, *tmp_sc;
168 char *acl_contents = acl_get_contents(&ss->acl);
170 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
171 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
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,
185 "listening address(es): %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",
192 stringify_port(ss->default_port,
193 strcmp(name, "http")? "dccp" : "tcp"),
196 ss->max_clients > 0? "" : " (unlimited)",
197 clnts? clnts : "(none)",
198 ss->default_deny? "allow" : "deny",
199 acl_contents? acl_contents : "(empty)"
207 * Allow connections from the given range of IP addresses.
209 * \param scd Contains the IP and the netmask.
210 * \param ss The sender.
212 * \sa \ref generic_com_deny().
214 void generic_com_allow(struct sender_command_data *scd,
215 struct sender_status *ss)
217 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
221 * Deny connections from the given range of IP addresses.
223 * \param scd see \ref generic_com_allow().
224 * \param ss see \ref generic_com_allow().
226 * \sa \ref generic_com_allow().
228 void generic_com_deny(struct sender_command_data *scd,
229 struct sender_status *ss)
231 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
235 * Activate a paraslash sender.
237 * \param ss The sender to activate.
238 * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
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.
243 * Errors are logged but otherwise ignored.
245 void generic_com_on(struct sender_status *ss, unsigned protocol)
250 FOR_EACH_LISTEN_FD(n, ss) {
251 if (ss->listen_fds[n] >= 0)
253 ret = para_listen(protocol, ss->listen_addresses[n],
256 char *url = format_url(ss->listen_addresses[n],
258 PARA_ERROR_LOG("could not listen on %s %s: %s\n",
259 protocol == IPPROTO_TCP? "TCP" : "DCCP",
260 url, para_strerror(-ret));
264 ss->listen_fds[n] = ret;
265 ret = mark_fd_nonblocking(ss->listen_fds[n]);
267 char *url = format_url(ss->listen_addresses[n],
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));
274 close(ss->listen_fds[n]);
275 ss->listen_fds[n] = -1;
278 add_close_on_fork_list(ss->listen_fds[n]);
283 * Deactivate a paraslash sender.
285 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
287 * \param ss The sender to deactivate.
289 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
291 void generic_com_off(struct sender_status *ss)
295 FOR_EACH_LISTEN_FD(n, ss) {
296 if (ss->listen_fds[n] < 0)
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;
306 * Accept a connection on the socket(s) this server is listening on.
308 * \param ss The sender whose listening fd is ready for reading.
309 * \param rfds Passed to para_accept(),
311 * This accepts incoming connections on any of the listening sockets of the
312 * server. If there is a connection pending, the function
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().
327 * \return A pointer to the allocated sender_client structure on success, \p
330 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
331 * \ref cq_new(), \ref add_close_on_fork_list().
333 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
335 struct sender_client *sc;
339 FOR_EACH_LISTEN_FD(n, ss) {
340 if (ss->listen_fds[n] < 0)
342 ret = para_accept(ss->listen_fds[n], rfds, NULL, 0, &fd);
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);
352 goto close_fd_and_warn;
353 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
355 goto close_fd_and_warn;
357 sc = para_calloc(sizeof(*sc));
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,
369 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
375 * Get the generic help text.
377 * \return A dynamically allocated string containing the help text for
378 * a paraslash sender.
380 char *generic_sender_help(void)
384 "usage: {allow|deny} IP[/netmask]\n"
385 " where mask defaults to 32\n"
386 "example: allow 192.168.0.1/24\n"
390 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
393 char *a = para_strdup(arg),
395 *c = strrchr(a, ':');
396 int ret = -E_COMMAND_SYNTAX;
402 ret = para_atoi32(a, &val);
406 /* optional max_slice_bytes (0 means "use MTU") */
408 scd->max_slice_bytes = 0;
410 if (val < 0 || val > 65535)
412 scd->max_slice_bytes = val;
414 ret = para_atoi32(b + 1, &val);
419 /* k = data_slices_per_group */
420 if (val < 0 || val > 255)
422 scd->data_slices_per_group = val;
424 /* n = slices_per_group */
425 ret = para_atoi32(c + 1, &val);
428 if (val < 0 || val < scd->data_slices_per_group)
430 scd->slices_per_group = val;
436 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
441 * Parse a FEC URL string.
443 * \param arg the URL string to parse.
444 * \param scd The structure containing host, port and the FEC parameters.
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.
453 * \sa \ref parse_url().
455 int parse_fec_url(const char *arg, struct sender_command_data *scd)
457 char *a = para_strdup(arg), *p = strchr(a, '/');
460 /* default fec parameters */
461 scd->max_slice_bytes = 0;
462 scd->data_slices_per_group = 14;
463 scd->slices_per_group = 16;
467 ret = parse_fec_parms(p + 1, scd);
471 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
472 ret = -ERRNO_TO_PARA_ERROR(EINVAL);