3e8a7c0dc85049dcfc52a5982083a69d3e1babc6
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
);
77 if (!process_is_command_handler()) {
79 del_close_on_fork_list(sc
->fd
);
83 free(sc
->private_data
);
89 * Shut down all clients connected to a paraslash sender.
91 * \param ss The sender whose clients are to be shut down.
93 * This just loops over all connected clients and calls shutdown_client()
96 void shutdown_clients(struct sender_status
*ss
)
98 struct sender_client
*sc
, *tmp
;
99 list_for_each_entry_safe(sc
, tmp
, &ss
->client_list
, node
)
100 shutdown_client(sc
, ss
);
104 * Try to empty the chunk queue for this fd.
106 * \param fd The file descriptor.
107 * \param cq The list of queued chunks.
109 * \return Negative on errors, zero if not everything was sent, one otherwise.
111 int send_queued_chunks(int fd
, struct chunk_queue
*cq
)
113 struct queued_chunk
*qc
;
114 while ((qc
= cq_peek(cq
))) {
119 cq_get(qc
, &buf
, &len
);
120 ret
= xwrite(fd
, buf
, len
);
132 * Initialize a struct sender status.
134 * \param ss The struct to initialize.
135 * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
136 * \param port The tcp or dccp port to listen on.
137 * \param max_clients The maximal number of simultaneous connections.
138 * \param default_deny Whether a blacklist should be used for access control.
140 void init_sender_status(struct sender_status
*ss
,
141 const struct lls_opt_result
*acl_opt_result
, int port
,
142 int max_clients
, int default_deny
)
147 INIT_LIST_HEAD(&ss
->client_list
);
150 /* Initialize an access control list */
151 INIT_LIST_HEAD(&ss
->acl
);
152 for (i
= 0; i
< lls_opt_given(acl_opt_result
); i
++) {
153 const char *arg
= lls_string_val(i
, acl_opt_result
);
156 if (!parse_cidr(arg
, addr
, sizeof(addr
), &mask
))
157 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
160 acl_add_entry(&ss
->acl
, addr
, mask
);
163 ss
->max_clients
= max_clients
;
164 ss
->default_deny
= default_deny
;
168 * Return a string containing the current status of a sender.
170 * \param ss The sender.
171 * \param name Used for printing the header line.
173 * \return The string printed in the "si" command.
175 char *generic_sender_status(struct sender_status
*ss
, const char *name
)
177 char *clnts
= NULL
, *ret
;
178 struct sender_client
*sc
, *tmp_sc
;
180 char *acl_contents
= acl_get_contents(&ss
->acl
);
181 list_for_each_entry_safe(sc
, tmp_sc
, &ss
->client_list
, node
) {
182 char *tmp
= make_message("%s%s ", clnts
? clnts
: "", sc
->name
);
189 "number of connected clients: %d\n"
190 "maximal number of clients: %d%s\n"
191 "connected clients: %s\n"
192 "access %s list: %s\n",
193 (ss
->listen_fd
>= 0)? "on" : "off",
194 stringify_port(ss
->port
, strcmp(name
, "http") ? "dccp" : "tcp"),
197 ss
->max_clients
> 0? "" : " (unlimited)",
198 clnts
? clnts
: "(none)",
199 ss
->default_deny
? "allow" : "deny",
200 acl_contents
? acl_contents
: "(empty)"
208 * Allow connections from the given range of IP addresses.
210 * \param scd Contains the IP and the netmask.
211 * \param ss The sender.
213 * \sa \ref generic_com_deny().
215 void generic_com_allow(struct sender_command_data
*scd
,
216 struct sender_status
*ss
)
218 acl_allow(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
222 * Empty the access control list of a sender.
224 * \param acl The access control list of the sender.
226 * This is called from the ->shutdown methods of the http and the dccp sender.
228 void generic_acl_deplete(struct list_head
*acl
)
231 * Since default_deny is false, the ACL is considered a blacklist. A
232 * netmask of zero matches any IP address, so this call empties the ACL.
234 acl_allow("0.0.0.0", 0 /* netmask */, acl
, 0 /* default_deny */);
238 * Deny connections from the given range of IP addresses.
240 * \param scd see \ref generic_com_allow().
241 * \param ss see \ref generic_com_allow().
243 * \sa \ref generic_com_allow().
245 void generic_com_deny(struct sender_command_data
*scd
,
246 struct sender_status
*ss
)
248 acl_deny(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
252 * Activate a paraslash sender.
254 * \param ss The sender to activate.
255 * \param protocol The symbolic name of the transport-layer protocol.
259 int generic_com_on(struct sender_status
*ss
, unsigned protocol
)
263 if (ss
->listen_fd
>= 0)
265 ret
= open_sender(protocol
, ss
->port
);
273 * Deactivate a paraslash sender.
275 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
277 * \param ss The sender to deactivate.
279 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
281 void generic_com_off(struct sender_status
*ss
)
283 if (ss
->listen_fd
< 0)
285 PARA_NOTICE_LOG("closing port %d\n", ss
->port
);
286 close(ss
->listen_fd
);
287 del_close_on_fork_list(ss
->listen_fd
);
288 shutdown_clients(ss
);
293 * Accept a connection on the socket this server is listening on.
295 * \param ss The sender whose listening fd is ready for reading.
296 * \param rfds Passed to para_accept(),
298 * This must be called only if the socket fd of \a ss is ready for reading. It
299 * calls para_accept() to accept the connection and performs the following
300 * actions on the resulting file descriptor \a fd:
302 * - Checks whether the maximal number of connections are exceeded.
303 * - Sets \a fd to nonblocking mode.
304 * - Checks the acl of the sender to find out whether connections
305 * are allowed from the IP of the connecting peer.
306 * - Increases the number of connections for this sender.
307 * - Creates and initializes a new chunk queue for queuing network
308 * packets that can not be sent immediately.
309 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
310 * and \a name members.
311 * - Adds \a fd to the list of connected clients for this sender.
312 * - Adds \a fd to the list of file descriptors that should be closed
313 * in the child process when the server calls fork().
315 * \return A pointer to the allocated sender_client structure on success, \p
318 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
319 * \ref cq_new(), \ref add_close_on_fork_list().
321 struct sender_client
*accept_sender_client(struct sender_status
*ss
, fd_set
*rfds
)
323 struct sender_client
*sc
;
326 if (ss
->listen_fd
< 0)
328 ret
= para_accept(ss
->listen_fd
, rfds
, NULL
, 0, &fd
);
330 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
333 ret
= -E_MAX_CLIENTS
;
334 if (ss
->max_clients
> 0 && ss
->num_clients
>= ss
->max_clients
)
336 ret
= mark_fd_nonblocking(fd
);
339 ret
= acl_check_access(fd
, &ss
->acl
, ss
->default_deny
);
343 sc
= para_calloc(sizeof(*sc
));
345 sc
->name
= para_strdup(remote_name(fd
));
346 sc
->cq
= cq_new(MAX_CQ_BYTES
);
347 para_list_add(&sc
->node
, &ss
->client_list
);
348 add_close_on_fork_list(fd
);
349 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss
->num_clients
,
353 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
359 * Get the generic help text.
361 * \return A dynamically allocated string containing the help text for
362 * a paraslash sender.
364 char *generic_sender_help(void)
368 "usage: {allow|deny} IP[/netmask]\n"
369 " where mask defaults to 32\n"
370 "example: allow 192.168.0.1/24\n"
374 static int parse_fec_parms(const char *arg
, struct sender_command_data
*scd
)
377 char *a
= para_strdup(arg
),
379 *c
= strrchr(a
, ':');
380 int ret
= -E_COMMAND_SYNTAX
;
386 ret
= para_atoi32(a
, &val
);
390 /* optional max_slice_bytes (0 means "use MTU") */
392 scd
->max_slice_bytes
= 0;
394 if (val
< 0 || val
> 65535)
396 scd
->max_slice_bytes
= val
;
398 ret
= para_atoi32(b
+ 1, &val
);
403 /* k = data_slices_per_group */
404 if (val
< 0 || val
> 255)
406 scd
->data_slices_per_group
= val
;
408 /* n = slices_per_group */
409 ret
= para_atoi32(c
+ 1, &val
);
412 if (val
< 0 || val
< scd
->data_slices_per_group
)
414 scd
->slices_per_group
= val
;
420 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
425 * Parse a FEC URL string.
427 * \param arg the URL string to parse.
428 * \param scd The structure containing host, port and the FEC parameters.
432 * A FEC URL consists of an ordinary URL string according to RFC 3986,
433 * optionally followed by a slash and the three FEC parameters slice_size,
434 * data_slices_per_group and slices_per_group. The three FEC parameters are
435 * separated by colons.
437 * \sa \ref parse_url().
439 int parse_fec_url(const char *arg
, struct sender_command_data
*scd
)
441 char *a
= para_strdup(arg
), *p
= strchr(a
, '/');
444 /* default fec parameters */
445 scd
->max_slice_bytes
= 0;
446 scd
->data_slices_per_group
= 14;
447 scd
->slices_per_group
= 16;
451 ret
= parse_fec_parms(p
+ 1, scd
);
455 if (!parse_url(a
, scd
->host
, sizeof(scd
->host
), &scd
->port
))
456 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);