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 * Open a passive socket of given layer4 type.
36 * Set the resulting file descriptor to nonblocking mode and add it to the list
37 * of fds that are being closed in the child process when the server calls
40 * \param l4type The transport-layer protocol.
41 * \param port The port number.
43 * \return The listening fd on success, negative on errors.
45 static int open_sender(unsigned l4type, int port)
47 int fd, ret = para_listen_simple(l4type, port);
52 ret = mark_fd_nonblocking(fd);
57 add_close_on_fork_list(fd);
62 * Shut down a client connected to a paraslash sender.
64 * \param sc The client to shut down.
65 * \param ss The sender whose clients are to be shut down.
67 * Close the file descriptor given by \a sc, remove it from the close-on-fork
68 * list, destroy the chunk queue of this client, delete the client from the
69 * list of connected clients and free the sender_client struct.
71 * \sa \ref shutdown_clients().
73 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
75 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
77 if (!process_is_command_handler()) {
79 del_close_on_fork_list(sc->fd);
83 free(sc->private_data);
89 * Shut down all clients connected to a paraslash sender.
91 * \param ss The sender whose clients are to be shut down.
93 * This just loops over all connected clients and calls shutdown_client()
96 void shutdown_clients(struct sender_status *ss)
98 struct sender_client *sc, *tmp;
99 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
100 shutdown_client(sc, ss);
104 * Try to empty the chunk queue for this fd.
106 * \param fd The file descriptor.
107 * \param cq The list of queued chunks.
109 * \return Negative on errors, zero if not everything was sent, one otherwise.
111 int send_queued_chunks(int fd, struct chunk_queue *cq)
113 struct queued_chunk *qc;
114 while ((qc = cq_peek(cq))) {
119 cq_get(qc, &buf, &len);
120 ret = xwrite(fd, buf, len);
132 * Initialize a struct sender status.
134 * \param ss The struct to initialize.
135 * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
136 * \param port The tcp or dccp port to listen on.
137 * \param max_clients The maximal number of simultaneous connections.
138 * \param default_deny Whether a blacklist should be used for access control.
140 void init_sender_status(struct sender_status *ss,
141 const struct lls_opt_result *acl_opt_result, int port,
142 int max_clients, int default_deny)
147 INIT_LIST_HEAD(&ss->client_list);
150 /* Initialize an access control list */
151 INIT_LIST_HEAD(&ss->acl);
152 for (i = 0; i < lls_opt_given(acl_opt_result); i++) {
153 const char *arg = lls_string_val(i, acl_opt_result);
156 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
157 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
160 acl_add_entry(&ss->acl, addr, mask);
163 ss->max_clients = max_clients;
164 ss->default_deny = default_deny;
168 * Return a string containing the current status of a sender.
170 * \param ss The sender.
171 * \param name Used for printing the header line.
173 * \return The string printed in the "si" command.
175 char *generic_sender_status(struct sender_status *ss, const char *name)
177 char *clnts = NULL, *ret;
178 struct sender_client *sc, *tmp_sc;
180 char *acl_contents = acl_get_contents(&ss->acl);
181 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
182 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
189 "number of connected clients: %d\n"
190 "maximal number of clients: %d%s\n"
191 "connected clients: %s\n"
192 "access %s list: %s\n",
193 (ss->listen_fd >= 0)? "on" : "off",
194 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
197 ss->max_clients > 0? "" : " (unlimited)",
198 clnts? clnts : "(none)",
199 ss->default_deny? "allow" : "deny",
200 acl_contents? acl_contents : "(empty)"
208 * Allow connections from the given range of IP addresses.
210 * \param scd Contains the IP and the netmask.
211 * \param ss The sender.
213 * \sa \ref generic_com_deny().
215 void generic_com_allow(struct sender_command_data *scd,
216 struct sender_status *ss)
218 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
222 * Deny connections from the given range of IP addresses.
224 * \param scd see \ref generic_com_allow().
225 * \param ss see \ref generic_com_allow().
227 * \sa \ref generic_com_allow().
229 void generic_com_deny(struct sender_command_data *scd,
230 struct sender_status *ss)
232 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
236 * Activate a paraslash sender.
238 * \param ss The sender to activate.
239 * \param protocol The symbolic name of the transport-layer protocol.
243 int generic_com_on(struct sender_status *ss, unsigned protocol)
247 if (ss->listen_fd >= 0)
249 ret = open_sender(protocol, ss->port);
257 * Deactivate a paraslash sender.
259 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
261 * \param ss The sender to deactivate.
263 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
265 void generic_com_off(struct sender_status *ss)
267 if (ss->listen_fd < 0)
269 PARA_NOTICE_LOG("closing port %d\n", ss->port);
270 close(ss->listen_fd);
271 del_close_on_fork_list(ss->listen_fd);
272 shutdown_clients(ss);
277 * Accept a connection on the socket this server is listening on.
279 * \param ss The sender whose listening fd is ready for reading.
280 * \param rfds Passed to para_accept(),
282 * This must be called only if the socket fd of \a ss is ready for reading. It
283 * calls para_accept() to accept the connection and performs the following
284 * actions on the resulting file descriptor \a fd:
286 * - Checks whether the maximal number of connections are exceeded.
287 * - Sets \a fd to nonblocking mode.
288 * - Checks the acl of the sender to find out whether connections
289 * are allowed from the IP of the connecting peer.
290 * - Increases the number of connections for this sender.
291 * - Creates and initializes a new chunk queue for queuing network
292 * packets that can not be sent immediately.
293 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
294 * and \a name members.
295 * - Adds \a fd to the list of connected clients for this sender.
296 * - Adds \a fd to the list of file descriptors that should be closed
297 * in the child process when the server calls fork().
299 * \return A pointer to the allocated sender_client structure on success, \p
302 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
303 * \ref cq_new(), \ref add_close_on_fork_list().
305 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
307 struct sender_client *sc;
310 if (ss->listen_fd < 0)
312 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
314 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
317 ret = -E_MAX_CLIENTS;
318 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
320 ret = mark_fd_nonblocking(fd);
323 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
327 sc = para_calloc(sizeof(*sc));
329 sc->name = para_strdup(remote_name(fd));
330 sc->cq = cq_new(MAX_CQ_BYTES);
331 para_list_add(&sc->node, &ss->client_list);
332 add_close_on_fork_list(fd);
333 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
337 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
343 * Get the generic help text.
345 * \return A dynamically allocated string containing the help text for
346 * a paraslash sender.
348 char *generic_sender_help(void)
352 "usage: {allow|deny} IP[/netmask]\n"
353 " where mask defaults to 32\n"
354 "example: allow 192.168.0.1/24\n"
358 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
361 char *a = para_strdup(arg),
363 *c = strrchr(a, ':');
364 int ret = -E_COMMAND_SYNTAX;
370 ret = para_atoi32(a, &val);
374 /* optional max_slice_bytes (0 means "use MTU") */
376 scd->max_slice_bytes = 0;
378 if (val < 0 || val > 65535)
380 scd->max_slice_bytes = val;
382 ret = para_atoi32(b + 1, &val);
387 /* k = data_slices_per_group */
388 if (val < 0 || val > 255)
390 scd->data_slices_per_group = val;
392 /* n = slices_per_group */
393 ret = para_atoi32(c + 1, &val);
396 if (val < 0 || val < scd->data_slices_per_group)
398 scd->slices_per_group = val;
404 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
409 * Parse a FEC URL string.
411 * \param arg the URL string to parse.
412 * \param scd The structure containing host, port and the FEC parameters.
416 * A FEC URL consists of an ordinary URL string according to RFC 3986,
417 * optionally followed by a slash and the three FEC parameters slice_size,
418 * data_slices_per_group and slices_per_group. The three FEC parameters are
419 * separated by colons.
421 * \sa \ref parse_url().
423 int parse_fec_url(const char *arg, struct sender_command_data *scd)
425 char *a = para_strdup(arg), *p = strchr(a, '/');
428 /* default fec parameters */
429 scd->max_slice_bytes = 0;
430 scd->data_slices_per_group = 14;
431 scd->slices_per_group = 16;
435 ret = parse_fec_parms(p + 1, scd);
439 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
440 ret = -ERRNO_TO_PARA_ERROR(EINVAL);