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 listen_address_opt_result Where to listen on.
109 * \param default_port Used for addresses with no specified port.
110 * \param max_clients The maximal number of simultaneous connections.
111 * \param default_deny Whether a blacklist should be used for access control.
113 void init_sender_status(struct sender_status
*ss
,
114 const struct lls_opt_result
*acl_opt_result
,
115 const struct lls_opt_result
*listen_address_opt_result
,
116 int default_port
, int max_clients
, int default_deny
)
119 unsigned n
= lls_opt_given(listen_address_opt_result
);
122 ss
->num_listen_fds
= 1;
123 ss
->listen_addresses
= para_malloc(sizeof(char *));
124 ss
->listen_addresses
[0] = NULL
;
125 ss
->listen_fds
= para_malloc(sizeof(int));
126 ss
->listen_fds
[0] = -1;
128 ss
->num_listen_fds
= n
;
129 ss
->listen_addresses
= para_malloc(n
* sizeof(char *));
130 ss
->listen_fds
= para_malloc(n
* sizeof(int));
131 FOR_EACH_LISTEN_FD(i
, ss
) {
132 ss
->listen_addresses
[i
] = para_strdup(lls_string_val(i
,
133 listen_address_opt_result
));
134 ss
->listen_fds
[i
] = -1;
137 ss
->default_port
= default_port
;
139 INIT_LIST_HEAD(&ss
->client_list
);
140 /* Initialize an access control list */
141 INIT_LIST_HEAD(&ss
->acl
);
142 for (i
= 0; i
< lls_opt_given(acl_opt_result
); i
++) {
143 const char *arg
= lls_string_val(i
, acl_opt_result
);
146 if (!parse_cidr(arg
, addr
, sizeof(addr
), &mask
))
147 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
150 acl_add_entry(&ss
->acl
, addr
, mask
);
153 ss
->max_clients
= max_clients
;
154 ss
->default_deny
= default_deny
;
158 * Return a string containing the current status of a sender.
160 * \param ss The sender.
161 * \param name Used for printing the header line.
163 * \return The string printed in the "si" command.
165 char *generic_sender_status(struct sender_status
*ss
, const char *name
)
167 char *clnts
= NULL
, *ret
, *addr
= NULL
;
168 struct sender_client
*sc
, *tmp_sc
;
170 char *acl_contents
= acl_get_contents(&ss
->acl
);
172 list_for_each_entry_safe(sc
, tmp_sc
, &ss
->client_list
, node
) {
173 char *tmp
= make_message("%s%s ", clnts
? clnts
: "", sc
->name
);
177 FOR_EACH_LISTEN_FD(n
, ss
) {
178 char *url
= format_url(ss
->listen_addresses
[n
], ss
->default_port
);
179 char *tmp
= make_message("%s%s%s (fd %d)", addr
?
180 addr
: "", addr
? ", " : "", url
,
187 "listening address(es): %s\n"
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",
194 stringify_port(ss
->default_port
,
195 strcmp(name
, "http")? "dccp" : "tcp"),
198 ss
->max_clients
> 0? "" : " (unlimited)",
199 clnts
? clnts
: "(none)",
200 ss
->default_deny
? "allow" : "deny",
201 acl_contents
? acl_contents
: "(empty)"
209 * Allow connections from the given range of IP addresses.
211 * \param scd Contains the IP and the netmask.
212 * \param ss The sender.
214 * \sa \ref generic_com_deny().
216 void generic_com_allow(struct sender_command_data
*scd
,
217 struct sender_status
*ss
)
219 acl_allow(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
223 * Empty the access control list of a sender.
225 * \param acl The access control list of the sender.
227 * This is called from the ->shutdown methods of the http and the dccp sender.
229 void generic_acl_deplete(struct list_head
*acl
)
232 * Since default_deny is false, the ACL is considered a blacklist. A
233 * netmask of zero matches any IP address, so this call empties the ACL.
235 acl_allow("0.0.0.0", 0 /* netmask */, acl
, 0 /* default_deny */);
239 * Deny connections from the given range of IP addresses.
241 * \param scd see \ref generic_com_allow().
242 * \param ss see \ref generic_com_allow().
244 * \sa \ref generic_com_allow().
246 void generic_com_deny(struct sender_command_data
*scd
,
247 struct sender_status
*ss
)
249 acl_deny(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
253 * Activate a paraslash sender.
255 * \param ss The sender to activate.
256 * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
258 * This opens a passive socket of given layer4 type, sets the resulting file
259 * descriptor to nonblocking mode and adds it to the close on fork list.
261 * Errors are logged but otherwise ignored.
263 void generic_com_on(struct sender_status
*ss
, unsigned protocol
)
268 FOR_EACH_LISTEN_FD(n
, ss
) {
269 if (ss
->listen_fds
[n
] >= 0)
271 ret
= para_listen(protocol
, ss
->listen_addresses
[n
],
274 char *url
= format_url(ss
->listen_addresses
[n
],
276 PARA_ERROR_LOG("could not listen on %s %s: %s\n",
277 protocol
== IPPROTO_TCP
? "TCP" : "DCCP",
278 url
, para_strerror(-ret
));
282 ss
->listen_fds
[n
] = ret
;
283 ret
= mark_fd_nonblocking(ss
->listen_fds
[n
]);
285 char *url
= format_url(ss
->listen_addresses
[n
],
287 PARA_ERROR_LOG("could not set %s socket fd for %s to "
288 "nonblocking mode: %s\n",
289 protocol
== IPPROTO_TCP
? "TCP" : "DCCP", url
,
290 para_strerror(-ret
));
292 close(ss
->listen_fds
[n
]);
293 ss
->listen_fds
[n
] = -1;
296 add_close_on_fork_list(ss
->listen_fds
[n
]);
301 * Deactivate a paraslash sender.
303 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
305 * \param ss The sender to deactivate.
307 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
309 void generic_com_off(struct sender_status
*ss
)
313 FOR_EACH_LISTEN_FD(n
, ss
) {
314 if (ss
->listen_fds
[n
] < 0)
316 close(ss
->listen_fds
[n
]);
317 del_close_on_fork_list(ss
->listen_fds
[n
]);
318 shutdown_clients(ss
);
319 ss
->listen_fds
[n
] = -1;
324 * Accept a connection on the socket(s) this server is listening on.
326 * \param ss The sender whose listening fd is ready for reading.
327 * \param rfds Passed to para_accept(),
329 * This accepts incoming connections on any of the listening sockets of the
330 * server. If there is a connection pending, the function
332 * - Checks whether the maximal number of connections are exceeded.
333 * - Sets \a fd to nonblocking mode.
334 * - Checks the acl of the sender to find out whether connections
335 * are allowed from the IP of the connecting peer.
336 * - Increases the number of connections for this sender.
337 * - Creates and initializes a new chunk queue for queuing network
338 * packets that can not be sent immediately.
339 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
340 * and \a name members.
341 * - Adds \a fd to the list of connected clients for this sender.
342 * - Adds \a fd to the list of file descriptors that should be closed
343 * in the child process when the server calls fork().
345 * \return A pointer to the allocated sender_client structure on success, \p
348 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
349 * \ref cq_new(), \ref add_close_on_fork_list().
351 struct sender_client
*accept_sender_client(struct sender_status
*ss
, fd_set
*rfds
)
353 struct sender_client
*sc
;
357 FOR_EACH_LISTEN_FD(n
, ss
) {
358 if (ss
->listen_fds
[n
] < 0)
360 ret
= para_accept(ss
->listen_fds
[n
], rfds
, NULL
, 0, &fd
);
365 ret
= -E_MAX_CLIENTS
;
366 if (ss
->max_clients
> 0 && ss
->num_clients
>= ss
->max_clients
)
367 goto close_fd_and_warn
;
368 ret
= mark_fd_nonblocking(fd
);
370 goto close_fd_and_warn
;
371 ret
= acl_check_access(fd
, &ss
->acl
, ss
->default_deny
);
373 goto close_fd_and_warn
;
375 sc
= para_calloc(sizeof(*sc
));
377 sc
->name
= para_strdup(remote_name(fd
));
378 sc
->cq
= cq_new(MAX_CQ_BYTES
);
379 para_list_add(&sc
->node
, &ss
->client_list
);
380 add_close_on_fork_list(fd
);
381 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss
->num_clients
,
387 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
393 * Get the generic help text.
395 * \return A dynamically allocated string containing the help text for
396 * a paraslash sender.
398 char *generic_sender_help(void)
402 "usage: {allow|deny} IP[/netmask]\n"
403 " where mask defaults to 32\n"
404 "example: allow 192.168.0.1/24\n"
408 static int parse_fec_parms(const char *arg
, struct sender_command_data
*scd
)
411 char *a
= para_strdup(arg
),
413 *c
= strrchr(a
, ':');
414 int ret
= -E_COMMAND_SYNTAX
;
420 ret
= para_atoi32(a
, &val
);
424 /* optional max_slice_bytes (0 means "use MTU") */
426 scd
->max_slice_bytes
= 0;
428 if (val
< 0 || val
> 65535)
430 scd
->max_slice_bytes
= val
;
432 ret
= para_atoi32(b
+ 1, &val
);
437 /* k = data_slices_per_group */
438 if (val
< 0 || val
> 255)
440 scd
->data_slices_per_group
= val
;
442 /* n = slices_per_group */
443 ret
= para_atoi32(c
+ 1, &val
);
446 if (val
< 0 || val
< scd
->data_slices_per_group
)
448 scd
->slices_per_group
= val
;
454 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
459 * Parse a FEC URL string.
461 * \param arg the URL string to parse.
462 * \param scd The structure containing host, port and the FEC parameters.
466 * A FEC URL consists of an ordinary URL string according to RFC 3986,
467 * optionally followed by a slash and the three FEC parameters slice_size,
468 * data_slices_per_group and slices_per_group. The three FEC parameters are
469 * separated by colons.
471 * \sa \ref parse_url().
473 int parse_fec_url(const char *arg
, struct sender_command_data
*scd
)
475 char *a
= para_strdup(arg
), *p
= strchr(a
, '/');
478 /* default fec parameters */
479 scd
->max_slice_bytes
= 0;
480 scd
->data_slices_per_group
= 14;
481 scd
->slices_per_group
= 16;
485 ret
= parse_fec_parms(p
+ 1, scd
);
489 if (!parse_url(a
, scd
->host
, sizeof(scd
->host
), &scd
->port
))
490 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);