2 * Copyright (C) 2005-2014 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file send_common.c Functions used by more than one paraslash sender. */
9 #include <netinet/in.h>
10 #include <sys/socket.h>
13 #include <arpa/inet.h>
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
33 /** Clients will be kicked if there are more than that many bytes pending. */
34 #define MAX_CQ_BYTES 40000
37 * Open a passive socket of given layer4 type.
39 * Set the resulting file descriptor to nonblocking mode and add it to the list
40 * of fds that are being closed in the child process when the server calls
43 * \param l4type The transport-layer protocol.
44 * \param port The port number.
46 * \return The listening fd on success, negative on errors.
48 static int open_sender(unsigned l4type
, int port
)
50 int fd
, ret
= para_listen_simple(l4type
, port
);
55 ret
= mark_fd_nonblocking(fd
);
60 add_close_on_fork_list(fd
);
65 * Shut down a client connected to a paraslash sender.
67 * \param sc The client to shut down.
68 * \param ss The sender whose clients are to be shut down.
70 * Close the file descriptor given by \a sc, remove it from the close-on-fork
71 * list, destroy the chunk queue of this client, delete the client from the
72 * list of connected clients and free the sender_client struct.
74 * \sa shutdown_clients().
76 void shutdown_client(struct sender_client
*sc
, struct sender_status
*ss
)
78 PARA_INFO_LOG("shutting down %s on fd %d\n", sc
->name
, sc
->fd
);
81 del_close_on_fork_list(sc
->fd
);
84 free(sc
->private_data
);
90 * Shut down all clients connected to a paraslash sender.
92 * \param ss The sender whose clients are to be shut down.
94 * This just loops over all connected clients and calls shutdown_client()
97 void shutdown_clients(struct sender_status
*ss
)
99 struct sender_client
*sc
, *tmp
;
100 list_for_each_entry_safe(sc
, tmp
, &ss
->client_list
, node
)
101 shutdown_client(sc
, ss
);
105 * Try to empty the chunk queue for this fd.
107 * \param fd The file descriptor.
108 * \param cq The list of queued chunks.
110 * \return Negative on errors, zero if not everything was sent, one otherwise.
112 int send_queued_chunks(int fd
, struct chunk_queue
*cq
)
114 struct queued_chunk
*qc
;
115 while ((qc
= cq_peek(cq
))) {
120 cq_get(qc
, &buf
, &len
);
121 ret
= xwrite(fd
, buf
, len
);
133 * Initialize a struct sender status.
135 * \param ss The struct to initialize.
136 * \param access_arg The array of access arguments given at the command line.
137 * \param num_access_args The number of elements in \a access_arg.
138 * \param port The tcp or dccp port to listen on.
139 * \param max_clients The maximal number of simultaneous connections.
140 * \param default_deny Whether a blacklist should be used for access control.
142 void init_sender_status(struct sender_status
*ss
, char **access_arg
,
143 int num_access_args
, int port
, int max_clients
, int default_deny
)
146 INIT_LIST_HEAD(&ss
->client_list
);
148 acl_init(&ss
->acl
, access_arg
, num_access_args
);
150 ss
->max_clients
= max_clients
;
151 ss
->default_deny
= default_deny
;
155 * Return a string containing the current status of a sender.
157 * \param ss The sender.
158 * \param name Used for printing the header line.
160 * \return The string printed in the "si" command.
162 char *generic_sender_status(struct sender_status
*ss
, const char *name
)
164 char *clnts
= NULL
, *ret
;
165 struct sender_client
*sc
, *tmp_sc
;
167 char *acl_contents
= acl_get_contents(&ss
->acl
);
168 list_for_each_entry_safe(sc
, tmp_sc
, &ss
->client_list
, node
) {
169 char *tmp
= make_message("%s%s ", clnts
? clnts
: "", sc
->name
);
176 "number of connected clients: %d\n"
177 "maximal number of clients: %d%s\n"
178 "connected clients: %s\n"
179 "access %s list: %s\n",
180 (ss
->listen_fd
>= 0)? "on" : "off",
181 stringify_port(ss
->port
, strcmp(name
, "http") ? "dccp" : "tcp"),
184 ss
->max_clients
> 0? "" : " (unlimited)",
185 clnts
? clnts
: "(none)",
186 ss
->default_deny
? "allow" : "deny",
187 acl_contents
? acl_contents
: "(empty)"
195 * Allow connections from the given range of IP addresses.
197 * \param scd Contains the IP and the netmask.
198 * \param ss The sender.
200 * \sa generic_com_deny().
202 void generic_com_allow(struct sender_command_data
*scd
,
203 struct sender_status
*ss
)
205 acl_allow(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
209 * Deny connections from the given range of IP addresses.
211 * \param scd see \ref generic_com_allow().
212 * \param ss see \ref generic_com_allow().
214 * \sa generic_com_allow().
216 void generic_com_deny(struct sender_command_data
*scd
,
217 struct sender_status
*ss
)
219 acl_deny(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
223 * Activate a paraslash sender.
225 * \param ss The sender to activate.
226 * \param protocol The symbolic name of the transport-layer protocol.
230 int generic_com_on(struct sender_status
*ss
, unsigned protocol
)
234 if (ss
->listen_fd
>= 0)
236 ret
= open_sender(protocol
, ss
->port
);
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(), 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 must be called only if the socket fd of \a ss is ready for reading. It
270 * calls para_accept() to accept the connection and performs the following
271 * actions on the resulting file descriptor \a fd:
273 * - Checks whether the maximal number of connections are exceeded.
274 * - Sets \a fd to nonblocking mode.
275 * - Checks the acl of the sender to find out whether connections
276 * are allowed from the IP of the connecting peer.
277 * - Increases the number of connections for this sender.
278 * - Creates and initializes a new chunk queue for queuing network
279 * packets that can not be sent immediately.
280 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
281 * and \a name members.
282 * - Adds \a fd to the list of connected clients for this sender.
283 * - Adds \a fd to the list of file descriptors that should be closed
284 * in the child process when the server calls fork().
286 * \return A pointer to the allocated sender_client structure on success, \p
289 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
290 * \ref cq_new(), \ref add_close_on_fork_list().
292 struct sender_client
*accept_sender_client(struct sender_status
*ss
, fd_set
*rfds
)
294 struct sender_client
*sc
;
297 if (ss
->listen_fd
< 0)
299 ret
= para_accept(ss
->listen_fd
, rfds
, NULL
, 0, &fd
);
301 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
304 ret
= -E_MAX_CLIENTS
;
305 if (ss
->max_clients
> 0 && ss
->num_clients
>= ss
->max_clients
)
307 ret
= mark_fd_nonblocking(fd
);
310 ret
= acl_check_access(fd
, &ss
->acl
, ss
->default_deny
);
314 sc
= para_calloc(sizeof(*sc
));
316 sc
->name
= para_strdup(remote_name(fd
));
317 sc
->cq
= cq_new(MAX_CQ_BYTES
);
318 para_list_add(&sc
->node
, &ss
->client_list
);
319 add_close_on_fork_list(fd
);
320 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss
->num_clients
,
324 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
330 * Get the generic help text.
332 * \return A dynamically allocated string containing the help text for
333 * a paraslash sender.
335 char *generic_sender_help(void)
339 "usage: {allow|deny} IP[/netmask]\n"
340 " where mask defaults to 32\n"
341 "example: allow 192.168.0.1/24\n"
345 static int parse_fec_parms(const char *arg
, struct sender_command_data
*scd
)
348 char *a
= para_strdup(arg
),
350 *c
= strrchr(a
, ':');
351 int ret
= -E_COMMAND_SYNTAX
;
357 ret
= para_atoi32(a
, &val
);
361 /* optional max_slice_bytes (0 means "use MTU") */
363 scd
->max_slice_bytes
= 0;
365 if (val
< 0 || val
> 65535)
367 scd
->max_slice_bytes
= val
;
369 ret
= para_atoi32(b
+ 1, &val
);
374 /* k = data_slices_per_group */
375 if (val
< 0 || val
> 255)
377 scd
->data_slices_per_group
= val
;
379 /* n = slices_per_group */
380 ret
= para_atoi32(c
+ 1, &val
);
383 if (val
< 0 || val
< scd
->data_slices_per_group
)
385 scd
->slices_per_group
= val
;
391 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
396 * Parse a FEC URL string.
398 * \param arg the URL string to parse.
399 * \param scd The structure containing host, port and the FEC parameters.
403 * A FEC URL consists of an ordinary URL string according to RFC 3986,
404 * optionally followed by a slash and the three FEC parameters slice_size,
405 * data_slices_per_group and slices_per_group. The three FEC parameters are
406 * separated by colons.
408 * \sa \ref parse_url().
410 int parse_fec_url(const char *arg
, struct sender_command_data
*scd
)
412 char *a
= para_strdup(arg
), *p
= strchr(a
, '/');
415 /* default fec parameters */
416 scd
->max_slice_bytes
= 0;
417 scd
->data_slices_per_group
= 14;
418 scd
->slices_per_group
= 16;
422 ret
= parse_fec_parms(p
+ 1, scd
);
426 if (!parse_url(a
, scd
->host
, sizeof(scd
->host
), &scd
->port
))
427 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);