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 * Shut down a client connected to a paraslash sender.
36 * \param sc The client to shut down.
37 * \param ss The sender whose clients are to be shut down.
39 * Close the file descriptor given by \a sc, remove it from the close-on-fork
40 * list, destroy the chunk queue of this client, delete the client from the
41 * list of connected clients and free the sender_client struct.
43 * \sa \ref shutdown_clients().
45 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
47 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
50 del_close_on_fork_list(sc->fd);
53 free(sc->private_data);
59 * Shut down all clients connected to a paraslash sender.
61 * \param ss The sender whose clients are to be shut down.
63 * This just loops over all connected clients and calls shutdown_client()
66 void shutdown_clients(struct sender_status *ss)
68 struct sender_client *sc, *tmp;
69 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
70 shutdown_client(sc, ss);
74 * Try to empty the chunk queue for this fd.
76 * \param fd The file descriptor.
77 * \param cq The list of queued chunks.
79 * \return Negative on errors, zero if not everything was sent, one otherwise.
81 int send_queued_chunks(int fd, struct chunk_queue *cq)
83 struct queued_chunk *qc;
84 while ((qc = cq_peek(cq))) {
89 cq_get(qc, &buf, &len);
90 ret = xwrite(fd, buf, len);
102 * Initialize a struct sender status.
104 * \param ss The struct to initialize.
105 * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
106 * \param port The tcp or dccp port to listen on.
107 * \param max_clients The maximal number of simultaneous connections.
108 * \param default_deny Whether a blacklist should be used for access control.
110 void init_sender_status(struct sender_status *ss,
111 const struct lls_opt_result *acl_opt_result, int port,
112 int max_clients, int default_deny)
117 INIT_LIST_HEAD(&ss->client_list);
120 /* Initialize an access control list */
121 INIT_LIST_HEAD(&ss->acl);
122 for (i = 0; i < lls_opt_given(acl_opt_result); i++) {
123 const char *arg = lls_string_val(i, acl_opt_result);
126 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
127 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
130 acl_add_entry(&ss->acl, addr, mask);
133 ss->max_clients = max_clients;
134 ss->default_deny = default_deny;
138 * Return a string containing the current status of a sender.
140 * \param ss The sender.
141 * \param name Used for printing the header line.
143 * \return The string printed in the "si" command.
145 char *generic_sender_status(struct sender_status *ss, const char *name)
147 char *clnts = NULL, *ret;
148 struct sender_client *sc, *tmp_sc;
150 char *acl_contents = acl_get_contents(&ss->acl);
151 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
152 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
159 "number of connected clients: %d\n"
160 "maximal number of clients: %d%s\n"
161 "connected clients: %s\n"
162 "access %s list: %s\n",
163 (ss->listen_fd >= 0)? "on" : "off",
164 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
167 ss->max_clients > 0? "" : " (unlimited)",
168 clnts? clnts : "(none)",
169 ss->default_deny? "allow" : "deny",
170 acl_contents? acl_contents : "(empty)"
178 * Allow connections from the given range of IP addresses.
180 * \param scd Contains the IP and the netmask.
181 * \param ss The sender.
183 * \sa \ref generic_com_deny().
185 void generic_com_allow(struct sender_command_data *scd,
186 struct sender_status *ss)
188 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
192 * Deny connections from the given range of IP addresses.
194 * \param scd see \ref generic_com_allow().
195 * \param ss see \ref generic_com_allow().
197 * \sa \ref generic_com_allow().
199 void generic_com_deny(struct sender_command_data *scd,
200 struct sender_status *ss)
202 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
206 * Activate a paraslash sender.
208 * \param ss The sender to activate.
209 * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
211 * This opens a passive socket of given layer4 type, sets the resulting file
212 * descriptor to nonblocking mode and adds it to the close on fork list.
214 * Errors are logged but otherwise ignored.
216 void generic_com_on(struct sender_status *ss, unsigned protocol)
220 if (ss->listen_fd >= 0)
222 ret = para_listen_simple(protocol, ss->port);
224 PARA_ERROR_LOG("could not listen on port %d: %s\n", ss->port,
225 para_strerror(-ret));
229 ret = mark_fd_nonblocking(fd);
231 PARA_ERROR_LOG("could not set %s socket fd for port %d to "
232 "nonblocking mode: %s\n",
233 protocol == IPPROTO_TCP? "TCP" : "DCCP", ss->port,
234 para_strerror(-ret));
238 add_close_on_fork_list(fd);
244 * Deactivate a paraslash sender.
246 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
248 * \param ss The sender to deactivate.
250 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
252 void generic_com_off(struct sender_status *ss)
254 if (ss->listen_fd < 0)
256 PARA_NOTICE_LOG("closing port %d\n", ss->port);
257 close(ss->listen_fd);
258 del_close_on_fork_list(ss->listen_fd);
259 shutdown_clients(ss);
264 * Accept a connection on the socket this server is listening on.
266 * \param ss The sender whose listening fd is ready for reading.
267 * \param rfds Passed to para_accept(),
269 * This calls para_accept() and performs the following actions on the resulting
270 * file descriptor fd:
272 * - Checks whether the maximal number of connections are exceeded.
273 * - Sets \a fd to nonblocking mode.
274 * - Checks the acl of the sender to find out whether connections
275 * are allowed from the IP of the connecting peer.
276 * - Increases the number of connections for this sender.
277 * - Creates and initializes a new chunk queue for queuing network
278 * packets that can not be sent immediately.
279 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
280 * and \a name members.
281 * - Adds \a fd to the list of connected clients for this sender.
282 * - Adds \a fd to the list of file descriptors that should be closed
283 * in the child process when the server calls fork().
285 * \return A pointer to the allocated sender_client structure on success, \p
288 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
289 * \ref cq_new(), \ref add_close_on_fork_list().
291 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
293 struct sender_client *sc;
296 if (ss->listen_fd < 0)
298 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
300 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
303 ret = -E_MAX_CLIENTS;
304 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
306 ret = mark_fd_nonblocking(fd);
309 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
313 sc = para_calloc(sizeof(*sc));
315 sc->name = para_strdup(remote_name(fd));
316 sc->cq = cq_new(MAX_CQ_BYTES);
317 para_list_add(&sc->node, &ss->client_list);
318 add_close_on_fork_list(fd);
319 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
323 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
329 * Get the generic help text.
331 * \return A dynamically allocated string containing the help text for
332 * a paraslash sender.
334 char *generic_sender_help(void)
338 "usage: {allow|deny} IP[/netmask]\n"
339 " where mask defaults to 32\n"
340 "example: allow 192.168.0.1/24\n"
344 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
347 char *a = para_strdup(arg),
349 *c = strrchr(a, ':');
350 int ret = -E_COMMAND_SYNTAX;
356 ret = para_atoi32(a, &val);
360 /* optional max_slice_bytes (0 means "use MTU") */
362 scd->max_slice_bytes = 0;
364 if (val < 0 || val > 65535)
366 scd->max_slice_bytes = val;
368 ret = para_atoi32(b + 1, &val);
373 /* k = data_slices_per_group */
374 if (val < 0 || val > 255)
376 scd->data_slices_per_group = val;
378 /* n = slices_per_group */
379 ret = para_atoi32(c + 1, &val);
382 if (val < 0 || val < scd->data_slices_per_group)
384 scd->slices_per_group = val;
390 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
395 * Parse a FEC URL string.
397 * \param arg the URL string to parse.
398 * \param scd The structure containing host, port and the FEC parameters.
402 * A FEC URL consists of an ordinary URL string according to RFC 3986,
403 * optionally followed by a slash and the three FEC parameters slice_size,
404 * data_slices_per_group and slices_per_group. The three FEC parameters are
405 * separated by colons.
407 * \sa \ref parse_url().
409 int parse_fec_url(const char *arg, struct sender_command_data *scd)
411 char *a = para_strdup(arg), *p = strchr(a, '/');
414 /* default fec parameters */
415 scd->max_slice_bytes = 0;
416 scd->data_slices_per_group = 14;
417 scd->slices_per_group = 16;
421 ret = parse_fec_parms(p + 1, scd);
425 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
426 ret = -ERRNO_TO_PARA_ERROR(EINVAL);