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 access_arg The array of access arguments given at the command line.
138 * \param num_access_args The number of elements in \a access_arg.
139 * \param port The tcp or dccp port to listen on.
140 * \param max_clients The maximal number of simultaneous connections.
141 * \param default_deny Whether a blacklist should be used for access control.
143 void init_sender_status(struct sender_status *ss, char **access_arg,
144 int num_access_args, int port, int max_clients, int default_deny)
147 INIT_LIST_HEAD(&ss->client_list);
149 acl_init(&ss->acl, access_arg, num_access_args);
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;
166 struct sender_client *sc, *tmp_sc;
168 char *acl_contents = acl_get_contents(&ss->acl);
169 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
170 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
177 "number of connected clients: %d\n"
178 "maximal number of clients: %d%s\n"
179 "connected clients: %s\n"
180 "access %s list: %s\n",
181 (ss->listen_fd >= 0)? "on" : "off",
182 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
185 ss->max_clients > 0? "" : " (unlimited)",
186 clnts? clnts : "(none)",
187 ss->default_deny? "allow" : "deny",
188 acl_contents? acl_contents : "(empty)"
196 * Allow connections from the given range of IP addresses.
198 * \param scd Contains the IP and the netmask.
199 * \param ss The sender.
201 * \sa generic_com_deny().
203 void generic_com_allow(struct sender_command_data *scd,
204 struct sender_status *ss)
206 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
210 * Deny connections from the given range of IP addresses.
212 * \param scd see \ref generic_com_allow().
213 * \param ss see \ref generic_com_allow().
215 * \sa generic_com_allow().
217 void generic_com_deny(struct sender_command_data *scd,
218 struct sender_status *ss)
220 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
224 * Activate a paraslash sender.
226 * \param ss The sender to activate.
227 * \param protocol The symbolic name of the transport-layer protocol.
231 int generic_com_on(struct sender_status *ss, unsigned protocol)
235 if (ss->listen_fd >= 0)
237 ret = open_sender(protocol, ss->port);
245 * Deactivate a paraslash sender.
247 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
249 * \param ss The sender to deactivate.
251 * \sa \ref del_close_on_fork_list(), shutdown_clients().
253 void generic_com_off(struct sender_status *ss)
255 if (ss->listen_fd < 0)
257 PARA_NOTICE_LOG("closing port %d\n", ss->port);
258 close(ss->listen_fd);
259 del_close_on_fork_list(ss->listen_fd);
260 shutdown_clients(ss);
265 * Accept a connection on the socket this server is listening on.
267 * \param ss The sender whose listening fd is ready for reading.
268 * \param rfds Passed to para_accept(),
270 * This must be called only if the socket fd of \a ss is ready for reading. It
271 * calls para_accept() to accept the connection and performs the following
272 * actions on the resulting file descriptor \a fd:
274 * - Checks whether the maximal number of connections are exceeded.
275 * - Sets \a fd to nonblocking mode.
276 * - Checks the acl of the sender to find out whether connections
277 * are allowed from the IP of the connecting peer.
278 * - Increases the number of connections for this sender.
279 * - Creates and initializes a new chunk queue for queuing network
280 * packets that can not be sent immediately.
281 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
282 * and \a name members.
283 * - Adds \a fd to the list of connected clients for this sender.
284 * - Adds \a fd to the list of file descriptors that should be closed
285 * in the child process when the server calls fork().
287 * \return A pointer to the allocated sender_client structure on success, \p
290 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
291 * \ref cq_new(), \ref add_close_on_fork_list().
293 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
295 struct sender_client *sc;
298 if (ss->listen_fd < 0)
300 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
302 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
305 ret = -E_MAX_CLIENTS;
306 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
308 ret = mark_fd_nonblocking(fd);
311 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
315 sc = para_calloc(sizeof(*sc));
317 sc->name = para_strdup(remote_name(fd));
318 sc->cq = cq_new(MAX_CQ_BYTES);
319 para_list_add(&sc->node, &ss->client_list);
320 add_close_on_fork_list(fd);
321 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
325 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
331 * Get the generic help text.
333 * \return A dynamically allocated string containing the help text for
334 * a paraslash sender.
336 char *generic_sender_help(void)
340 "usage: {allow|deny} IP[/netmask]\n"
341 " where mask defaults to 32\n"
342 "example: allow 192.168.0.1/24\n"
346 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
349 char *a = para_strdup(arg),
351 *c = strrchr(a, ':');
352 int ret = -E_COMMAND_SYNTAX;
358 ret = para_atoi32(a, &val);
362 /* optional max_slice_bytes (0 means "use MTU") */
364 scd->max_slice_bytes = 0;
366 if (val < 0 || val > 65535)
368 scd->max_slice_bytes = val;
370 ret = para_atoi32(b + 1, &val);
375 /* k = data_slices_per_group */
376 if (val < 0 || val > 255)
378 scd->data_slices_per_group = val;
380 /* n = slices_per_group */
381 ret = para_atoi32(c + 1, &val);
384 if (val < 0 || val < scd->data_slices_per_group)
386 scd->slices_per_group = val;
392 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
397 * Parse a FEC URL string.
399 * \param arg the URL string to parse.
400 * \param scd The structure containing host, port and the FEC parameters.
404 * A FEC URL consists of an ordinary URL string according to RFC 3986,
405 * optionally followed by a slash and the three FEC parameters slice_size,
406 * data_slices_per_group and slices_per_group. The three FEC parameters are
407 * separated by colons.
409 * \sa \ref parse_url().
411 int parse_fec_url(const char *arg, struct sender_command_data *scd)
413 char *a = para_strdup(arg), *p = strchr(a, '/');
416 /* default fec parameters */
417 scd->max_slice_bytes = 0;
418 scd->data_slices_per_group = 14;
419 scd->slices_per_group = 16;
423 ret = parse_fec_parms(p + 1, scd);
427 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
428 ret = -ERRNO_TO_PARA_ERROR(EINVAL);