61a12c827342758fa97949db669872e8b2d504b8
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
);
49 if (!process_is_command_handler()) {
51 del_close_on_fork_list(sc
->fd
);
55 free(sc
->private_data
);
61 * Shut down all clients connected to a paraslash sender.
63 * \param ss The sender whose clients are to be shut down.
65 * This just loops over all connected clients and calls shutdown_client()
68 void shutdown_clients(struct sender_status
*ss
)
70 struct sender_client
*sc
, *tmp
;
71 list_for_each_entry_safe(sc
, tmp
, &ss
->client_list
, node
)
72 shutdown_client(sc
, ss
);
76 * Try to empty the chunk queue for this fd.
78 * \param fd The file descriptor.
79 * \param cq The list of queued chunks.
81 * \return Negative on errors, zero if not everything was sent, one otherwise.
83 int send_queued_chunks(int fd
, struct chunk_queue
*cq
)
85 struct queued_chunk
*qc
;
86 while ((qc
= cq_peek(cq
))) {
91 cq_get(qc
, &buf
, &len
);
92 ret
= xwrite(fd
, buf
, len
);
104 * Initialize a struct sender status.
106 * \param ss The struct to initialize.
107 * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
108 * \param port The tcp or dccp port to listen on.
109 * \param max_clients The maximal number of simultaneous connections.
110 * \param default_deny Whether a blacklist should be used for access control.
112 void init_sender_status(struct sender_status
*ss
,
113 const struct lls_opt_result
*acl_opt_result
, int port
,
114 int max_clients
, int default_deny
)
119 INIT_LIST_HEAD(&ss
->client_list
);
122 /* Initialize an access control list */
123 INIT_LIST_HEAD(&ss
->acl
);
124 for (i
= 0; i
< lls_opt_given(acl_opt_result
); i
++) {
125 const char *arg
= lls_string_val(i
, acl_opt_result
);
128 if (!parse_cidr(arg
, addr
, sizeof(addr
), &mask
))
129 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
132 acl_add_entry(&ss
->acl
, addr
, mask
);
135 ss
->max_clients
= max_clients
;
136 ss
->default_deny
= default_deny
;
140 * Return a string containing the current status of a sender.
142 * \param ss The sender.
143 * \param name Used for printing the header line.
145 * \return The string printed in the "si" command.
147 char *generic_sender_status(struct sender_status
*ss
, const char *name
)
149 char *clnts
= NULL
, *ret
;
150 struct sender_client
*sc
, *tmp_sc
;
152 char *acl_contents
= acl_get_contents(&ss
->acl
);
153 list_for_each_entry_safe(sc
, tmp_sc
, &ss
->client_list
, node
) {
154 char *tmp
= make_message("%s%s ", clnts
? clnts
: "", sc
->name
);
161 "number of connected clients: %d\n"
162 "maximal number of clients: %d%s\n"
163 "connected clients: %s\n"
164 "access %s list: %s\n",
165 (ss
->listen_fd
>= 0)? "on" : "off",
166 stringify_port(ss
->port
, strcmp(name
, "http") ? "dccp" : "tcp"),
169 ss
->max_clients
> 0? "" : " (unlimited)",
170 clnts
? clnts
: "(none)",
171 ss
->default_deny
? "allow" : "deny",
172 acl_contents
? acl_contents
: "(empty)"
180 * Allow connections from the given range of IP addresses.
182 * \param scd Contains the IP and the netmask.
183 * \param ss The sender.
185 * \sa \ref generic_com_deny().
187 void generic_com_allow(struct sender_command_data
*scd
,
188 struct sender_status
*ss
)
190 acl_allow(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
194 * Empty the access control list of a sender.
196 * \param acl The access control list of the sender.
198 * This is called from the ->shutdown methods of the http and the dccp sender.
200 void generic_acl_deplete(struct list_head
*acl
)
203 * Since default_deny is false, the ACL is considered a blacklist. A
204 * netmask of zero matches any IP address, so this call empties the ACL.
206 acl_allow("0.0.0.0", 0 /* netmask */, acl
, 0 /* 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 \ref 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 layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
229 * This opens a passive socket of given layer4 type, sets the resulting file
230 * descriptor to nonblocking mode and adds it to the close on fork list.
232 * Errors are logged but otherwise ignored.
234 void generic_com_on(struct sender_status
*ss
, unsigned protocol
)
238 if (ss
->listen_fd
>= 0)
240 ret
= para_listen_simple(protocol
, ss
->port
);
242 PARA_ERROR_LOG("could not listen on port %d: %s\n", ss
->port
,
243 para_strerror(-ret
));
247 ret
= mark_fd_nonblocking(fd
);
249 PARA_ERROR_LOG("could not set %s socket fd for port %d to "
250 "nonblocking mode: %s\n",
251 protocol
== IPPROTO_TCP
? "TCP" : "DCCP", ss
->port
,
252 para_strerror(-ret
));
256 add_close_on_fork_list(fd
);
262 * Deactivate a paraslash sender.
264 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
266 * \param ss The sender to deactivate.
268 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
270 void generic_com_off(struct sender_status
*ss
)
272 if (ss
->listen_fd
< 0)
274 PARA_NOTICE_LOG("closing port %d\n", ss
->port
);
275 close(ss
->listen_fd
);
276 del_close_on_fork_list(ss
->listen_fd
);
277 shutdown_clients(ss
);
282 * Accept a connection on the socket this server is listening on.
284 * \param ss The sender whose listening fd is ready for reading.
285 * \param rfds Passed to para_accept(),
287 * This calls para_accept() and performs the following actions on the resulting
288 * file descriptor fd:
290 * - Checks whether the maximal number of connections are exceeded.
291 * - Sets \a fd to nonblocking mode.
292 * - Checks the acl of the sender to find out whether connections
293 * are allowed from the IP of the connecting peer.
294 * - Increases the number of connections for this sender.
295 * - Creates and initializes a new chunk queue for queuing network
296 * packets that can not be sent immediately.
297 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
298 * and \a name members.
299 * - Adds \a fd to the list of connected clients for this sender.
300 * - Adds \a fd to the list of file descriptors that should be closed
301 * in the child process when the server calls fork().
303 * \return A pointer to the allocated sender_client structure on success, \p
306 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
307 * \ref cq_new(), \ref add_close_on_fork_list().
309 struct sender_client
*accept_sender_client(struct sender_status
*ss
, fd_set
*rfds
)
311 struct sender_client
*sc
;
314 if (ss
->listen_fd
< 0)
316 ret
= para_accept(ss
->listen_fd
, rfds
, NULL
, 0, &fd
);
318 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
321 ret
= -E_MAX_CLIENTS
;
322 if (ss
->max_clients
> 0 && ss
->num_clients
>= ss
->max_clients
)
324 ret
= mark_fd_nonblocking(fd
);
327 ret
= acl_check_access(fd
, &ss
->acl
, ss
->default_deny
);
331 sc
= para_calloc(sizeof(*sc
));
333 sc
->name
= para_strdup(remote_name(fd
));
334 sc
->cq
= cq_new(MAX_CQ_BYTES
);
335 para_list_add(&sc
->node
, &ss
->client_list
);
336 add_close_on_fork_list(fd
);
337 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss
->num_clients
,
341 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
347 * Get the generic help text.
349 * \return A dynamically allocated string containing the help text for
350 * a paraslash sender.
352 char *generic_sender_help(void)
356 "usage: {allow|deny} IP[/netmask]\n"
357 " where mask defaults to 32\n"
358 "example: allow 192.168.0.1/24\n"
362 static int parse_fec_parms(const char *arg
, struct sender_command_data
*scd
)
365 char *a
= para_strdup(arg
),
367 *c
= strrchr(a
, ':');
368 int ret
= -E_COMMAND_SYNTAX
;
374 ret
= para_atoi32(a
, &val
);
378 /* optional max_slice_bytes (0 means "use MTU") */
380 scd
->max_slice_bytes
= 0;
382 if (val
< 0 || val
> 65535)
384 scd
->max_slice_bytes
= val
;
386 ret
= para_atoi32(b
+ 1, &val
);
391 /* k = data_slices_per_group */
392 if (val
< 0 || val
> 255)
394 scd
->data_slices_per_group
= val
;
396 /* n = slices_per_group */
397 ret
= para_atoi32(c
+ 1, &val
);
400 if (val
< 0 || val
< scd
->data_slices_per_group
)
402 scd
->slices_per_group
= val
;
408 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
413 * Parse a FEC URL string.
415 * \param arg the URL string to parse.
416 * \param scd The structure containing host, port and the FEC parameters.
420 * A FEC URL consists of an ordinary URL string according to RFC 3986,
421 * optionally followed by a slash and the three FEC parameters slice_size,
422 * data_slices_per_group and slices_per_group. The three FEC parameters are
423 * separated by colons.
425 * \sa \ref parse_url().
427 int parse_fec_url(const char *arg
, struct sender_command_data
*scd
)
429 char *a
= para_strdup(arg
), *p
= strchr(a
, '/');
432 /* default fec parameters */
433 scd
->max_slice_bytes
= 0;
434 scd
->data_slices_per_group
= 14;
435 scd
->slices_per_group
= 16;
439 ret
= parse_fec_parms(p
+ 1, scd
);
443 if (!parse_url(a
, scd
->host
, sizeof(scd
->host
), &scd
->port
))
444 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);