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);
78 del_close_on_fork_list(sc->fd);
81 free(sc->private_data);
87 * Shut down all clients connected to a paraslash sender.
89 * \param ss The sender whose clients are to be shut down.
91 * This just loops over all connected clients and calls shutdown_client()
94 void shutdown_clients(struct sender_status *ss)
96 struct sender_client *sc, *tmp;
97 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
98 shutdown_client(sc, ss);
102 * Try to empty the chunk queue for this fd.
104 * \param fd The file descriptor.
105 * \param cq The list of queued chunks.
107 * \return Negative on errors, zero if not everything was sent, one otherwise.
109 int send_queued_chunks(int fd, struct chunk_queue *cq)
111 struct queued_chunk *qc;
112 while ((qc = cq_peek(cq))) {
117 cq_get(qc, &buf, &len);
118 ret = xwrite(fd, buf, len);
130 * Initialize a struct sender status.
132 * \param ss The struct to initialize.
133 * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
134 * \param port The tcp or dccp port to listen on.
135 * \param max_clients The maximal number of simultaneous connections.
136 * \param default_deny Whether a blacklist should be used for access control.
138 void init_sender_status(struct sender_status *ss,
139 const struct lls_opt_result *acl_opt_result, int port,
140 int max_clients, int default_deny)
145 INIT_LIST_HEAD(&ss->client_list);
148 /* Initialize an access control list */
149 INIT_LIST_HEAD(&ss->acl);
150 for (i = 0; i < lls_opt_given(acl_opt_result); i++) {
151 const char *arg = lls_string_val(i, acl_opt_result);
154 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
155 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
158 acl_add_entry(&ss->acl, addr, mask);
161 ss->max_clients = max_clients;
162 ss->default_deny = default_deny;
166 * Return a string containing the current status of a sender.
168 * \param ss The sender.
169 * \param name Used for printing the header line.
171 * \return The string printed in the "si" command.
173 char *generic_sender_status(struct sender_status *ss, const char *name)
175 char *clnts = NULL, *ret;
176 struct sender_client *sc, *tmp_sc;
178 char *acl_contents = acl_get_contents(&ss->acl);
179 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
180 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
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",
191 (ss->listen_fd >= 0)? "on" : "off",
192 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
195 ss->max_clients > 0? "" : " (unlimited)",
196 clnts? clnts : "(none)",
197 ss->default_deny? "allow" : "deny",
198 acl_contents? acl_contents : "(empty)"
206 * Allow connections from the given range of IP addresses.
208 * \param scd Contains the IP and the netmask.
209 * \param ss The sender.
211 * \sa \ref generic_com_deny().
213 void generic_com_allow(struct sender_command_data *scd,
214 struct sender_status *ss)
216 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
220 * Deny connections from the given range of IP addresses.
222 * \param scd see \ref generic_com_allow().
223 * \param ss see \ref generic_com_allow().
225 * \sa \ref generic_com_allow().
227 void generic_com_deny(struct sender_command_data *scd,
228 struct sender_status *ss)
230 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
234 * Activate a paraslash sender.
236 * \param ss The sender to activate.
237 * \param protocol The symbolic name of the transport-layer protocol.
241 int generic_com_on(struct sender_status *ss, unsigned protocol)
245 if (ss->listen_fd >= 0)
247 ret = open_sender(protocol, ss->port);
255 * Deactivate a paraslash sender.
257 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
259 * \param ss The sender to deactivate.
261 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
263 void generic_com_off(struct sender_status *ss)
265 if (ss->listen_fd < 0)
267 PARA_NOTICE_LOG("closing port %d\n", ss->port);
268 close(ss->listen_fd);
269 del_close_on_fork_list(ss->listen_fd);
270 shutdown_clients(ss);
275 * Accept a connection on the socket this server is listening on.
277 * \param ss The sender whose listening fd is ready for reading.
278 * \param rfds Passed to para_accept(),
280 * This must be called only if the socket fd of \a ss is ready for reading. It
281 * calls para_accept() to accept the connection and performs the following
282 * actions on the resulting file descriptor \a fd:
284 * - Checks whether the maximal number of connections are exceeded.
285 * - Sets \a fd to nonblocking mode.
286 * - Checks the acl of the sender to find out whether connections
287 * are allowed from the IP of the connecting peer.
288 * - Increases the number of connections for this sender.
289 * - Creates and initializes a new chunk queue for queuing network
290 * packets that can not be sent immediately.
291 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
292 * and \a name members.
293 * - Adds \a fd to the list of connected clients for this sender.
294 * - Adds \a fd to the list of file descriptors that should be closed
295 * in the child process when the server calls fork().
297 * \return A pointer to the allocated sender_client structure on success, \p
300 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
301 * \ref cq_new(), \ref add_close_on_fork_list().
303 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
305 struct sender_client *sc;
308 if (ss->listen_fd < 0)
310 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
312 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
315 ret = -E_MAX_CLIENTS;
316 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
318 ret = mark_fd_nonblocking(fd);
321 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
325 sc = para_calloc(sizeof(*sc));
327 sc->name = para_strdup(remote_name(fd));
328 sc->cq = cq_new(MAX_CQ_BYTES);
329 para_list_add(&sc->node, &ss->client_list);
330 add_close_on_fork_list(fd);
331 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
335 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
341 * Get the generic help text.
343 * \return A dynamically allocated string containing the help text for
344 * a paraslash sender.
346 char *generic_sender_help(void)
350 "usage: {allow|deny} IP[/netmask]\n"
351 " where mask defaults to 32\n"
352 "example: allow 192.168.0.1/24\n"
356 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
359 char *a = para_strdup(arg),
361 *c = strrchr(a, ':');
362 int ret = -E_COMMAND_SYNTAX;
368 ret = para_atoi32(a, &val);
372 /* optional max_slice_bytes (0 means "use MTU") */
374 scd->max_slice_bytes = 0;
376 if (val < 0 || val > 65535)
378 scd->max_slice_bytes = val;
380 ret = para_atoi32(b + 1, &val);
385 /* k = data_slices_per_group */
386 if (val < 0 || val > 255)
388 scd->data_slices_per_group = val;
390 /* n = slices_per_group */
391 ret = para_atoi32(c + 1, &val);
394 if (val < 0 || val < scd->data_slices_per_group)
396 scd->slices_per_group = val;
402 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
407 * Parse a FEC URL string.
409 * \param arg the URL string to parse.
410 * \param scd The structure containing host, port and the FEC parameters.
414 * A FEC URL consists of an ordinary URL string according to RFC 3986,
415 * optionally followed by a slash and the three FEC parameters slice_size,
416 * data_slices_per_group and slices_per_group. The three FEC parameters are
417 * separated by colons.
419 * \sa \ref parse_url().
421 int parse_fec_url(const char *arg, struct sender_command_data *scd)
423 char *a = para_strdup(arg), *p = strchr(a, '/');
426 /* default fec parameters */
427 scd->max_slice_bytes = 0;
428 scd->data_slices_per_group = 14;
429 scd->slices_per_group = 16;
433 ret = parse_fec_parms(p + 1, scd);
437 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
438 ret = -ERRNO_TO_PARA_ERROR(EINVAL);