2 * Copyright (C) 2005-2011 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. */
23 #include "close_on_fork.h"
24 #include "chunk_queue.h"
27 /** Clients will be kicked if there are more than that many bytes pending. */
28 #define MAX_CQ_BYTES 40000
31 * Open a passive socket of given layer4 type.
33 * Set the resulting file descriptor to nonblocking mode and add it to the list
34 * of fds that are being closed in the child process when the server calls
37 * \param l4type The transport-layer protocol.
38 * \param port The port number.
40 * \return The listening fd on success, negative on errors.
42 static int open_sender(unsigned l4type, int port)
44 int fd, ret = para_listen_simple(l4type, port);
49 ret = mark_fd_nonblocking(fd);
54 add_close_on_fork_list(fd);
59 * Shut down a client connected to a paraslash sender.
61 * \param sc The client to shut down.
62 * \param ss The sender whose clients are to be shut down.
64 * Close the file descriptor given by \a sc, remove it from the close-on-fork
65 * list, destroy the chunk queue of this client, delete the client from the
66 * list of connected clients and free the sender_client struct.
68 * \sa shutdown_clients().
70 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
72 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
75 del_close_on_fork_list(sc->fd);
78 free(sc->private_data);
84 * Shut down all clients connected to a paraslash sender.
86 * \param ss The sender whose clients are to be shut down.
88 * This just loops over all connected clients and calls shutdown_client()
91 void shutdown_clients(struct sender_status *ss)
93 struct sender_client *sc, *tmp;
94 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
95 shutdown_client(sc, ss);
99 * Try to empty the chunk queue for this fd.
101 * \param fd The file descriptor.
102 * \param cq The list of queued chunks.
104 * \return Negative on errors, zero if not everything was sent, one otherwise.
106 int send_queued_chunks(int fd, struct chunk_queue *cq)
108 struct queued_chunk *qc;
109 while ((qc = cq_peek(cq))) {
114 cq_get(qc, &buf, &len);
115 ret = write_nonblock(fd, buf, len);
127 * Initialize a struct sender status.
129 * \param ss The struct to initialize.
130 * \param access_arg The array of access arguments given at the command line.
131 * \param num_access_args The number of elements in \a access_arg.
132 * \param port The tcp or dccp port to listen on.
133 * \param max_clients The maximal number of simultaneous connections.
134 * \param default_deny Whether a blacklist should be used for access control.
136 void init_sender_status(struct sender_status *ss, char **access_arg,
137 int num_access_args, int port, int max_clients, int default_deny)
140 INIT_LIST_HEAD(&ss->client_list);
142 acl_init(&ss->acl, access_arg, num_access_args);
144 ss->max_clients = max_clients;
145 ss->default_deny = default_deny;
149 * Return a string containing the current status of a sender.
151 * \param ss The sender.
152 * \param name Used for printing the header line.
154 * \return The string printed in the "si" command.
156 char *get_sender_info(struct sender_status *ss, const char *name)
158 char *clnts = NULL, *ret;
159 struct sender_client *sc, *tmp_sc;
161 char *acl_contents = acl_get_contents(&ss->acl);
162 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
163 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
171 "\tnumber of connected clients: %d\n"
172 "\tmaximal number of clients: %d%s\n"
173 "\tconnected clients: %s\n"
174 "\taccess %s list: %s\n",
176 (ss->listen_fd >= 0)? "on" : "off",
177 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
180 ss->max_clients > 0? "" : " (unlimited)",
181 clnts? clnts : "(none)",
182 ss->default_deny? "allow" : "deny",
183 acl_contents? acl_contents : "(empty)"
191 * Allow connections from the given range of IP addresses.
193 * \param scd Contains the IP and the netmask.
194 * \param ss The sender.
196 * \sa generic_com_deny().
198 void generic_com_allow(struct sender_command_data *scd,
199 struct sender_status *ss)
201 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
205 * Deny connections from the given range of IP addresses.
207 * \param scd see \ref generic_com_allow().
208 * \param ss see \ref generic_com_allow().
210 * \sa generic_com_allow().
212 void generic_com_deny(struct sender_command_data *scd,
213 struct sender_status *ss)
215 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
219 * Activate a paraslash sender.
221 * \param ss The sender to activate.
222 * \param protocol The symbolic name of the transport-layer protocol.
226 int generic_com_on(struct sender_status *ss, unsigned protocol)
230 if (ss->listen_fd >= 0)
232 ret = open_sender(protocol, ss->port);
240 * Deactivate a paraslash sender.
242 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
244 * \param ss The sender to deactivate.
246 * \sa \ref del_close_on_fork_list(), shutdown_clients().
248 void generic_com_off(struct sender_status *ss)
250 if (ss->listen_fd < 0)
252 PARA_NOTICE_LOG("closing port %d\n", ss->port);
253 close(ss->listen_fd);
254 del_close_on_fork_list(ss->listen_fd);
255 shutdown_clients(ss);
260 * Accept a connection on the socket this server is listening on.
262 * \param ss The sender whose listening fd is ready for reading.
263 * \param rfds Passed to para_accept(),
265 * This must be called only if the socket fd of \a ss is ready for reading. It
266 * calls para_accept() to accept the connection and performs the following
267 * actions on the resulting file descriptor \a fd:
269 * - Checks whether the maximal number of connections are exceeded.
270 * - Sets \a fd to nonblocking mode.
271 * - Checks the acl of the sender to find out whether connections
272 * are allowed from the IP of the connecting peer.
273 * - Increases the number of connections for this sender.
274 * - Creates and initializes a new chunk queue for queuing network
275 * packets that can not be sent immediately.
276 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
277 * and \a name members.
278 * - Adds \a fd to the list of connected clients for this sender.
279 * - Adds \a fd to the list of file descriptors that should be closed
280 * in the child process when the server calls fork().
282 * \return A pointer to the allocated sender_client structure on success, \p
285 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
286 * \ref cq_new(), \ref add_close_on_fork_list().
288 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
290 struct sender_client *sc;
293 if (ss->listen_fd < 0)
295 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
297 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
300 ret = -E_MAX_CLIENTS;
301 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
303 ret = mark_fd_nonblocking(fd);
306 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
310 sc = para_calloc(sizeof(*sc));
312 sc->name = para_strdup(remote_name(fd));
313 sc->cq = cq_new(MAX_CQ_BYTES);
314 para_list_add(&sc->node, &ss->client_list);
315 add_close_on_fork_list(fd);
316 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
320 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
326 * Get the generic help text.
328 * \return A dynamically allocated string containing the help text for
329 * a paraslash sender.
331 char *generic_sender_help(void)
335 "usage: {allow|deny} IP[/netmask]\n"
336 " where mask defaults to 32\n"
337 "example: allow 192.168.0.1/24\n"
341 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
344 char *a = para_strdup(arg),
346 *c = strrchr(a, ':');
347 int ret = -E_COMMAND_SYNTAX;
353 ret = para_atoi32(a, &val);
357 /* optional max_slice_bytes (0 means "use MTU") */
359 scd->max_slice_bytes = 0;
361 if (val < 0 || val > 65535)
363 scd->max_slice_bytes = val;
365 ret = para_atoi32(b + 1, &val);
370 /* k = data_slices_per_group */
371 if (val < 0 || val > 255)
373 scd->data_slices_per_group = val;
375 /* n = slices_per_group */
376 ret = para_atoi32(c + 1, &val);
379 if (val < 0 || val < scd->data_slices_per_group)
381 scd->slices_per_group = val;
387 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
392 * Parse a FEC URL string.
394 * \param arg the URL string to parse.
395 * \param scd The structure containing host, port and the FEC parameters.
399 * A FEC URL consists of an ordinary URL string according to RFC 3986,
400 * optionally followed by a slash and the three FEC parameters slice_size,
401 * data_slices_per_group and slices_per_group. The three FEC parameters are
402 * separated by colons.
404 * \sa \ref parse_url().
406 int parse_fec_url(const char *arg, struct sender_command_data *scd)
408 char *a = para_strdup(arg), *p = strchr(a, '/');
411 /* default fec parameters */
412 scd->max_slice_bytes = 0;
413 scd->data_slices_per_group = 14;
414 scd->slices_per_group = 16;
418 ret = parse_fec_parms(p + 1, scd);
422 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
423 ret = -ERRNO_TO_PARA_ERROR(EINVAL);