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 The symbolic name of the transport-layer protocol.
213 int generic_com_on(struct sender_status *ss, unsigned protocol)
217 if (ss->listen_fd >= 0)
219 ret = para_listen_simple(protocol, ss->port);
223 ret = mark_fd_nonblocking(fd);
228 add_close_on_fork_list(fd);
234 * Deactivate a paraslash sender.
236 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
238 * \param ss The sender to deactivate.
240 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
242 void generic_com_off(struct sender_status *ss)
244 if (ss->listen_fd < 0)
246 PARA_NOTICE_LOG("closing port %d\n", ss->port);
247 close(ss->listen_fd);
248 del_close_on_fork_list(ss->listen_fd);
249 shutdown_clients(ss);
254 * Accept a connection on the socket this server is listening on.
256 * \param ss The sender whose listening fd is ready for reading.
257 * \param rfds Passed to para_accept(),
259 * This calls para_accept() and performs the following actions on the resulting
260 * file descriptor fd:
262 * - Checks whether the maximal number of connections are exceeded.
263 * - Sets \a fd to nonblocking mode.
264 * - Checks the acl of the sender to find out whether connections
265 * are allowed from the IP of the connecting peer.
266 * - Increases the number of connections for this sender.
267 * - Creates and initializes a new chunk queue for queuing network
268 * packets that can not be sent immediately.
269 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
270 * and \a name members.
271 * - Adds \a fd to the list of connected clients for this sender.
272 * - Adds \a fd to the list of file descriptors that should be closed
273 * in the child process when the server calls fork().
275 * \return A pointer to the allocated sender_client structure on success, \p
278 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
279 * \ref cq_new(), \ref add_close_on_fork_list().
281 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
283 struct sender_client *sc;
286 if (ss->listen_fd < 0)
288 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
290 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
293 ret = -E_MAX_CLIENTS;
294 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
296 ret = mark_fd_nonblocking(fd);
299 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
303 sc = para_calloc(sizeof(*sc));
305 sc->name = para_strdup(remote_name(fd));
306 sc->cq = cq_new(MAX_CQ_BYTES);
307 para_list_add(&sc->node, &ss->client_list);
308 add_close_on_fork_list(fd);
309 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
313 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
319 * Get the generic help text.
321 * \return A dynamically allocated string containing the help text for
322 * a paraslash sender.
324 char *generic_sender_help(void)
328 "usage: {allow|deny} IP[/netmask]\n"
329 " where mask defaults to 32\n"
330 "example: allow 192.168.0.1/24\n"
334 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
337 char *a = para_strdup(arg),
339 *c = strrchr(a, ':');
340 int ret = -E_COMMAND_SYNTAX;
346 ret = para_atoi32(a, &val);
350 /* optional max_slice_bytes (0 means "use MTU") */
352 scd->max_slice_bytes = 0;
354 if (val < 0 || val > 65535)
356 scd->max_slice_bytes = val;
358 ret = para_atoi32(b + 1, &val);
363 /* k = data_slices_per_group */
364 if (val < 0 || val > 255)
366 scd->data_slices_per_group = val;
368 /* n = slices_per_group */
369 ret = para_atoi32(c + 1, &val);
372 if (val < 0 || val < scd->data_slices_per_group)
374 scd->slices_per_group = val;
380 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
385 * Parse a FEC URL string.
387 * \param arg the URL string to parse.
388 * \param scd The structure containing host, port and the FEC parameters.
392 * A FEC URL consists of an ordinary URL string according to RFC 3986,
393 * optionally followed by a slash and the three FEC parameters slice_size,
394 * data_slices_per_group and slices_per_group. The three FEC parameters are
395 * separated by colons.
397 * \sa \ref parse_url().
399 int parse_fec_url(const char *arg, struct sender_command_data *scd)
401 char *a = para_strdup(arg), *p = strchr(a, '/');
404 /* default fec parameters */
405 scd->max_slice_bytes = 0;
406 scd->data_slices_per_group = 14;
407 scd->slices_per_group = 16;
411 ret = parse_fec_parms(p + 1, scd);
415 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
416 ret = -ERRNO_TO_PARA_ERROR(EINVAL);