2 * Copyright (C) 2005-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file send_common.c Functions used by more than one paraslash sender. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
13 #include <arpa/inet.h>
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
33 /** Clients will be kicked if there are more than that many bytes pending. */
34 #define MAX_CQ_BYTES 40000
37 * Open a passive socket of given layer4 type.
39 * Set the resulting file descriptor to nonblocking mode and add it to the list
40 * of fds that are being closed in the child process when the server calls
43 * \param l4type The transport-layer protocol.
44 * \param port The port number.
46 * \return The listening fd on success, negative on errors.
48 static int open_sender(unsigned l4type, int port)
50 int fd, ret = para_listen_simple(l4type, port);
55 ret = mark_fd_nonblocking(fd);
60 add_close_on_fork_list(fd);
65 * Shut down a client connected to a paraslash sender.
67 * \param sc The client to shut down.
68 * \param ss The sender whose clients are to be shut down.
70 * Close the file descriptor given by \a sc, remove it from the close-on-fork
71 * list, destroy the chunk queue of this client, delete the client from the
72 * list of connected clients and free the sender_client struct.
74 * \sa shutdown_clients().
76 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
78 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
81 del_close_on_fork_list(sc->fd);
84 free(sc->private_data);
90 * Shut down all clients connected to a paraslash sender.
92 * \param ss The sender whose clients are to be shut down.
94 * This just loops over all connected clients and calls shutdown_client()
97 void shutdown_clients(struct sender_status *ss)
99 struct sender_client *sc, *tmp;
100 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
101 shutdown_client(sc, ss);
105 * Try to empty the chunk queue for this fd.
107 * \param fd The file descriptor.
108 * \param cq The list of queued chunks.
110 * \return Negative on errors, zero if not everything was sent, one otherwise.
112 int send_queued_chunks(int fd, struct chunk_queue *cq)
114 struct queued_chunk *qc;
115 while ((qc = cq_peek(cq))) {
120 cq_get(qc, &buf, &len);
121 ret = xwrite(fd, buf, len);
133 * Initialize a struct sender status.
135 * \param ss The struct to initialize.
136 * \param access_arg The array of access arguments given at the command line.
137 * \param num_access_args The number of elements in \a access_arg.
138 * \param port The tcp or dccp port to listen on.
139 * \param max_clients The maximal number of simultaneous connections.
140 * \param default_deny Whether a blacklist should be used for access control.
142 void init_sender_status(struct sender_status *ss, char **access_arg,
143 int num_access_args, int port, int max_clients, int default_deny)
146 INIT_LIST_HEAD(&ss->client_list);
148 acl_init(&ss->acl, access_arg, num_access_args);
150 ss->max_clients = max_clients;
151 ss->default_deny = default_deny;
155 * Return a string containing the current status of a sender.
157 * \param ss The sender.
158 * \param name Used for printing the header line.
160 * \return The string printed in the "si" command.
162 char *get_sender_info(struct sender_status *ss, const char *name)
164 char *clnts = NULL, *ret;
165 struct sender_client *sc, *tmp_sc;
167 char *acl_contents = acl_get_contents(&ss->acl);
168 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
169 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
177 "\tnumber of connected clients: %d\n"
178 "\tmaximal number of clients: %d%s\n"
179 "\tconnected clients: %s\n"
180 "\taccess %s list: %s\n",
182 (ss->listen_fd >= 0)? "on" : "off",
183 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
186 ss->max_clients > 0? "" : " (unlimited)",
187 clnts? clnts : "(none)",
188 ss->default_deny? "allow" : "deny",
189 acl_contents? acl_contents : "(empty)"
197 * Allow connections from the given range of IP addresses.
199 * \param scd Contains the IP and the netmask.
200 * \param ss The sender.
202 * \sa generic_com_deny().
204 void generic_com_allow(struct sender_command_data *scd,
205 struct sender_status *ss)
207 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
211 * Deny connections from the given range of IP addresses.
213 * \param scd see \ref generic_com_allow().
214 * \param ss see \ref generic_com_allow().
216 * \sa generic_com_allow().
218 void generic_com_deny(struct sender_command_data *scd,
219 struct sender_status *ss)
221 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
225 * Activate a paraslash sender.
227 * \param ss The sender to activate.
228 * \param protocol The symbolic name of the transport-layer protocol.
232 int generic_com_on(struct sender_status *ss, unsigned protocol)
236 if (ss->listen_fd >= 0)
238 ret = open_sender(protocol, ss->port);
246 * Deactivate a paraslash sender.
248 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
250 * \param ss The sender to deactivate.
252 * \sa \ref del_close_on_fork_list(), shutdown_clients().
254 void generic_com_off(struct sender_status *ss)
256 if (ss->listen_fd < 0)
258 PARA_NOTICE_LOG("closing port %d\n", ss->port);
259 close(ss->listen_fd);
260 del_close_on_fork_list(ss->listen_fd);
261 shutdown_clients(ss);
266 * Accept a connection on the socket this server is listening on.
268 * \param ss The sender whose listening fd is ready for reading.
269 * \param rfds Passed to para_accept(),
271 * This must be called only if the socket fd of \a ss is ready for reading. It
272 * calls para_accept() to accept the connection and performs the following
273 * actions on the resulting file descriptor \a fd:
275 * - Checks whether the maximal number of connections are exceeded.
276 * - Sets \a fd to nonblocking mode.
277 * - Checks the acl of the sender to find out whether connections
278 * are allowed from the IP of the connecting peer.
279 * - Increases the number of connections for this sender.
280 * - Creates and initializes a new chunk queue for queuing network
281 * packets that can not be sent immediately.
282 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
283 * and \a name members.
284 * - Adds \a fd to the list of connected clients for this sender.
285 * - Adds \a fd to the list of file descriptors that should be closed
286 * in the child process when the server calls fork().
288 * \return A pointer to the allocated sender_client structure on success, \p
291 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
292 * \ref cq_new(), \ref add_close_on_fork_list().
294 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
296 struct sender_client *sc;
299 if (ss->listen_fd < 0)
301 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
303 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
306 ret = -E_MAX_CLIENTS;
307 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
309 ret = mark_fd_nonblocking(fd);
312 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
316 sc = para_calloc(sizeof(*sc));
318 sc->name = para_strdup(remote_name(fd));
319 sc->cq = cq_new(MAX_CQ_BYTES);
320 para_list_add(&sc->node, &ss->client_list);
321 add_close_on_fork_list(fd);
322 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
326 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
332 * Get the generic help text.
334 * \return A dynamically allocated string containing the help text for
335 * a paraslash sender.
337 char *generic_sender_help(void)
341 "usage: {allow|deny} IP[/netmask]\n"
342 " where mask defaults to 32\n"
343 "example: allow 192.168.0.1/24\n"
347 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
350 char *a = para_strdup(arg),
352 *c = strrchr(a, ':');
353 int ret = -E_COMMAND_SYNTAX;
359 ret = para_atoi32(a, &val);
363 /* optional max_slice_bytes (0 means "use MTU") */
365 scd->max_slice_bytes = 0;
367 if (val < 0 || val > 65535)
369 scd->max_slice_bytes = val;
371 ret = para_atoi32(b + 1, &val);
376 /* k = data_slices_per_group */
377 if (val < 0 || val > 255)
379 scd->data_slices_per_group = val;
381 /* n = slices_per_group */
382 ret = para_atoi32(c + 1, &val);
385 if (val < 0 || val < scd->data_slices_per_group)
387 scd->slices_per_group = val;
393 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
398 * Parse a FEC URL string.
400 * \param arg the URL string to parse.
401 * \param scd The structure containing host, port and the FEC parameters.
405 * A FEC URL consists of an ordinary URL string according to RFC 3986,
406 * optionally followed by a slash and the three FEC parameters slice_size,
407 * data_slices_per_group and slices_per_group. The three FEC parameters are
408 * separated by colons.
410 * \sa \ref parse_url().
412 int parse_fec_url(const char *arg, struct sender_command_data *scd)
414 char *a = para_strdup(arg), *p = strchr(a, '/');
417 /* default fec parameters */
418 scd->max_slice_bytes = 0;
419 scd->data_slices_per_group = 14;
420 scd->slices_per_group = 16;
424 ret = parse_fec_parms(p + 1, scd);
428 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
429 ret = -ERRNO_TO_PARA_ERROR(EINVAL);