2 * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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>
29 #include "close_on_fork.h"
30 #include "chunk_queue.h"
34 /** Clients will be kicked if there are more than that many bytes pending. */
35 #define MAX_CQ_BYTES 40000
38 * Open a passive socket of given layer4 type.
40 * Set the resulting file descriptor to nonblocking mode and add it to the list
41 * of fds that are being closed in the child process when the server calls
44 * \param l4type The transport-layer protocol.
45 * \param port The port number.
47 * \return The listening fd on success, negative on errors.
49 static int open_sender(unsigned l4type, int port)
51 int fd, ret = para_listen_simple(l4type, port);
56 ret = mark_fd_nonblocking(fd);
61 add_close_on_fork_list(fd);
66 * Shut down a client connected to a paraslash sender.
68 * \param sc The client to shut down.
69 * \param ss The sender whose clients are to be shut down.
71 * Close the file descriptor given by \a sc, remove it from the close-on-fork
72 * list, destroy the chunk queue of this client, delete the client from the
73 * list of connected clients and free the sender_client struct.
75 * \sa shutdown_clients().
77 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
79 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
82 del_close_on_fork_list(sc->fd);
85 free(sc->private_data);
91 * Shut down all clients connected to a paraslash sender.
93 * \param ss The sender whose clients are to be shut down.
95 * This just loops over all connected clients and calls shutdown_client()
98 void shutdown_clients(struct sender_status *ss)
100 struct sender_client *sc, *tmp;
101 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
102 shutdown_client(sc, ss);
106 * Try to empty the chunk queue for this fd.
108 * \param fd The file descriptor.
109 * \param cq The list of queued chunks.
111 * \return Negative on errors, zero if not everything was sent, one otherwise.
113 int send_queued_chunks(int fd, struct chunk_queue *cq)
115 struct queued_chunk *qc;
116 while ((qc = cq_peek(cq))) {
121 cq_get(qc, &buf, &len);
122 ret = xwrite(fd, buf, len);
134 * Initialize a struct sender status.
136 * \param ss The struct to initialize.
137 * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
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,
143 const struct lls_opt_result *acl_opt_result, int port,
144 int max_clients, int default_deny)
149 INIT_LIST_HEAD(&ss->client_list);
152 /* Initialize an access control list */
153 INIT_LIST_HEAD(&ss->acl);
154 for (i = 0; i < lls_opt_given(acl_opt_result); i++) {
155 const char *arg = lls_string_val(i, acl_opt_result);
158 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
159 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
162 acl_add_entry(&ss->acl, addr, mask);
165 ss->max_clients = max_clients;
166 ss->default_deny = default_deny;
170 * Return a string containing the current status of a sender.
172 * \param ss The sender.
173 * \param name Used for printing the header line.
175 * \return The string printed in the "si" command.
177 char *generic_sender_status(struct sender_status *ss, const char *name)
179 char *clnts = NULL, *ret;
180 struct sender_client *sc, *tmp_sc;
182 char *acl_contents = acl_get_contents(&ss->acl);
183 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
184 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
191 "number of connected clients: %d\n"
192 "maximal number of clients: %d%s\n"
193 "connected clients: %s\n"
194 "access %s list: %s\n",
195 (ss->listen_fd >= 0)? "on" : "off",
196 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
199 ss->max_clients > 0? "" : " (unlimited)",
200 clnts? clnts : "(none)",
201 ss->default_deny? "allow" : "deny",
202 acl_contents? acl_contents : "(empty)"
210 * Allow connections from the given range of IP addresses.
212 * \param scd Contains the IP and the netmask.
213 * \param ss The sender.
215 * \sa generic_com_deny().
217 void generic_com_allow(struct sender_command_data *scd,
218 struct sender_status *ss)
220 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
224 * Deny connections from the given range of IP addresses.
226 * \param scd see \ref generic_com_allow().
227 * \param ss see \ref generic_com_allow().
229 * \sa generic_com_allow().
231 void generic_com_deny(struct sender_command_data *scd,
232 struct sender_status *ss)
234 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
238 * Activate a paraslash sender.
240 * \param ss The sender to activate.
241 * \param protocol The symbolic name of the transport-layer protocol.
245 int generic_com_on(struct sender_status *ss, unsigned protocol)
249 if (ss->listen_fd >= 0)
251 ret = open_sender(protocol, ss->port);
259 * Deactivate a paraslash sender.
261 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
263 * \param ss The sender to deactivate.
265 * \sa \ref del_close_on_fork_list(), shutdown_clients().
267 void generic_com_off(struct sender_status *ss)
269 if (ss->listen_fd < 0)
271 PARA_NOTICE_LOG("closing port %d\n", ss->port);
272 close(ss->listen_fd);
273 del_close_on_fork_list(ss->listen_fd);
274 shutdown_clients(ss);
279 * Accept a connection on the socket this server is listening on.
281 * \param ss The sender whose listening fd is ready for reading.
282 * \param rfds Passed to para_accept(),
284 * This must be called only if the socket fd of \a ss is ready for reading. It
285 * calls para_accept() to accept the connection and performs the following
286 * actions on the resulting file descriptor \a fd:
288 * - Checks whether the maximal number of connections are exceeded.
289 * - Sets \a fd to nonblocking mode.
290 * - Checks the acl of the sender to find out whether connections
291 * are allowed from the IP of the connecting peer.
292 * - Increases the number of connections for this sender.
293 * - Creates and initializes a new chunk queue for queuing network
294 * packets that can not be sent immediately.
295 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
296 * and \a name members.
297 * - Adds \a fd to the list of connected clients for this sender.
298 * - Adds \a fd to the list of file descriptors that should be closed
299 * in the child process when the server calls fork().
301 * \return A pointer to the allocated sender_client structure on success, \p
304 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
305 * \ref cq_new(), \ref add_close_on_fork_list().
307 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
309 struct sender_client *sc;
312 if (ss->listen_fd < 0)
314 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
316 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
319 ret = -E_MAX_CLIENTS;
320 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
322 ret = mark_fd_nonblocking(fd);
325 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
329 sc = para_calloc(sizeof(*sc));
331 sc->name = para_strdup(remote_name(fd));
332 sc->cq = cq_new(MAX_CQ_BYTES);
333 para_list_add(&sc->node, &ss->client_list);
334 add_close_on_fork_list(fd);
335 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
339 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
345 * Get the generic help text.
347 * \return A dynamically allocated string containing the help text for
348 * a paraslash sender.
350 char *generic_sender_help(void)
354 "usage: {allow|deny} IP[/netmask]\n"
355 " where mask defaults to 32\n"
356 "example: allow 192.168.0.1/24\n"
360 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
363 char *a = para_strdup(arg),
365 *c = strrchr(a, ':');
366 int ret = -E_COMMAND_SYNTAX;
372 ret = para_atoi32(a, &val);
376 /* optional max_slice_bytes (0 means "use MTU") */
378 scd->max_slice_bytes = 0;
380 if (val < 0 || val > 65535)
382 scd->max_slice_bytes = val;
384 ret = para_atoi32(b + 1, &val);
389 /* k = data_slices_per_group */
390 if (val < 0 || val > 255)
392 scd->data_slices_per_group = val;
394 /* n = slices_per_group */
395 ret = para_atoi32(c + 1, &val);
398 if (val < 0 || val < scd->data_slices_per_group)
400 scd->slices_per_group = val;
406 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
411 * Parse a FEC URL string.
413 * \param arg the URL string to parse.
414 * \param scd The structure containing host, port and the FEC parameters.
418 * A FEC URL consists of an ordinary URL string according to RFC 3986,
419 * optionally followed by a slash and the three FEC parameters slice_size,
420 * data_slices_per_group and slices_per_group. The three FEC parameters are
421 * separated by colons.
423 * \sa \ref parse_url().
425 int parse_fec_url(const char *arg, struct sender_command_data *scd)
427 char *a = para_strdup(arg), *p = strchr(a, '/');
430 /* default fec parameters */
431 scd->max_slice_bytes = 0;
432 scd->data_slices_per_group = 14;
433 scd->slices_per_group = 16;
437 ret = parse_fec_parms(p + 1, scd);
441 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
442 ret = -ERRNO_TO_PARA_ERROR(EINVAL);