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 * Deallocate all resources allocated in \ref init_sender_status().
160 * \param ss The structure whose components should be freed.
162 * This frees the dynamically allocated parts of the structure which was
163 * initialized by an earlier call to \ref init_sender_status(). It does *not*
164 * call free(ss), though.
166 void free_sender_status(const struct sender_status
*ss
)
170 free(ss
->listen_fds
);
171 FOR_EACH_LISTEN_FD(i
, ss
)
172 free(ss
->listen_addresses
[i
]);
173 free(ss
->listen_addresses
);
177 * Return a string containing the current status of a sender.
179 * \param ss The sender.
180 * \param name Used for printing the header line.
182 * \return The string printed in the "si" command.
184 char *generic_sender_status(struct sender_status
*ss
, const char *name
)
186 char *clnts
= NULL
, *ret
, *addr
= NULL
;
187 struct sender_client
*sc
, *tmp_sc
;
189 char *acl_contents
= acl_get_contents(&ss
->acl
);
191 list_for_each_entry_safe(sc
, tmp_sc
, &ss
->client_list
, node
) {
192 char *tmp
= make_message("%s%s ", clnts
? clnts
: "", sc
->name
);
196 FOR_EACH_LISTEN_FD(n
, ss
) {
197 char *url
= format_url(ss
->listen_addresses
[n
], ss
->default_port
);
198 char *tmp
= make_message("%s%s%s (fd %d)", addr
?
199 addr
: "", addr
? ", " : "", url
,
206 "listening address(es): %s\n"
208 "number of connected clients: %d\n"
209 "maximal number of clients: %d%s\n"
210 "connected clients: %s\n"
211 "access %s list: %s\n",
213 stringify_port(ss
->default_port
,
214 strcmp(name
, "http")? "dccp" : "tcp"),
217 ss
->max_clients
> 0? "" : " (unlimited)",
218 clnts
? clnts
: "(none)",
219 ss
->default_deny
? "allow" : "deny",
220 acl_contents
? acl_contents
: "(empty)"
228 * Allow connections from the given range of IP addresses.
230 * \param scd Contains the IP and the netmask.
231 * \param ss The sender.
233 * \sa \ref generic_com_deny().
235 void generic_com_allow(struct sender_command_data
*scd
,
236 struct sender_status
*ss
)
238 acl_allow(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
242 * Empty the access control list of a sender.
244 * \param acl The access control list of the sender.
246 * This is called from the ->shutdown methods of the http and the dccp sender.
248 void generic_acl_deplete(struct list_head
*acl
)
251 * Since default_deny is false, the ACL is considered a blacklist. A
252 * netmask of zero matches any IP address, so this call empties the ACL.
254 acl_allow("0.0.0.0", 0 /* netmask */, acl
, 0 /* default_deny */);
258 * Deny connections from the given range of IP addresses.
260 * \param scd see \ref generic_com_allow().
261 * \param ss see \ref generic_com_allow().
263 * \sa \ref generic_com_allow().
265 void generic_com_deny(struct sender_command_data
*scd
,
266 struct sender_status
*ss
)
268 acl_deny(scd
->host
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
272 * Activate a paraslash sender.
274 * \param ss The sender to activate.
275 * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
277 * This opens a passive socket of given layer4 type, sets the resulting file
278 * descriptor to nonblocking mode and adds it to the close on fork list.
280 * Errors are logged but otherwise ignored.
282 void generic_com_on(struct sender_status
*ss
, unsigned protocol
)
287 FOR_EACH_LISTEN_FD(n
, ss
) {
288 if (ss
->listen_fds
[n
] >= 0)
290 ret
= para_listen(protocol
, ss
->listen_addresses
[n
],
293 char *url
= format_url(ss
->listen_addresses
[n
],
295 PARA_ERROR_LOG("could not listen on %s %s: %s\n",
296 protocol
== IPPROTO_TCP
? "TCP" : "DCCP",
297 url
, para_strerror(-ret
));
301 ss
->listen_fds
[n
] = ret
;
302 ret
= mark_fd_nonblocking(ss
->listen_fds
[n
]);
304 char *url
= format_url(ss
->listen_addresses
[n
],
306 PARA_ERROR_LOG("could not set %s socket fd for %s to "
307 "nonblocking mode: %s\n",
308 protocol
== IPPROTO_TCP
? "TCP" : "DCCP", url
,
309 para_strerror(-ret
));
311 close(ss
->listen_fds
[n
]);
312 ss
->listen_fds
[n
] = -1;
315 add_close_on_fork_list(ss
->listen_fds
[n
]);
320 * Deactivate a paraslash sender.
322 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
324 * \param ss The sender to deactivate.
326 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
328 void generic_com_off(struct sender_status
*ss
)
332 FOR_EACH_LISTEN_FD(n
, ss
) {
333 if (ss
->listen_fds
[n
] < 0)
335 close(ss
->listen_fds
[n
]);
336 del_close_on_fork_list(ss
->listen_fds
[n
]);
337 shutdown_clients(ss
);
338 ss
->listen_fds
[n
] = -1;
343 * Accept a connection on the socket(s) this server is listening on.
345 * \param ss The sender whose listening fd is ready for reading.
346 * \param rfds Passed to para_accept(),
348 * This accepts incoming connections on any of the listening sockets of the
349 * server. If there is a connection pending, the function
351 * - Checks whether the maximal number of connections are exceeded.
352 * - Sets \a fd to nonblocking mode.
353 * - Checks the acl of the sender to find out whether connections
354 * are allowed from the IP of the connecting peer.
355 * - Increases the number of connections for this sender.
356 * - Creates and initializes a new chunk queue for queuing network
357 * packets that can not be sent immediately.
358 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
359 * and \a name members.
360 * - Adds \a fd to the list of connected clients for this sender.
361 * - Adds \a fd to the list of file descriptors that should be closed
362 * in the child process when the server calls fork().
364 * \return A pointer to the allocated sender_client structure on success, \p
367 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
368 * \ref cq_new(), \ref add_close_on_fork_list().
370 struct sender_client
*accept_sender_client(struct sender_status
*ss
, fd_set
*rfds
)
372 struct sender_client
*sc
;
376 FOR_EACH_LISTEN_FD(n
, ss
) {
377 if (ss
->listen_fds
[n
] < 0)
379 ret
= para_accept(ss
->listen_fds
[n
], rfds
, NULL
, 0, &fd
);
384 ret
= -E_MAX_CLIENTS
;
385 if (ss
->max_clients
> 0 && ss
->num_clients
>= ss
->max_clients
)
386 goto close_fd_and_warn
;
387 ret
= mark_fd_nonblocking(fd
);
389 goto close_fd_and_warn
;
390 ret
= acl_check_access(fd
, &ss
->acl
, ss
->default_deny
);
392 goto close_fd_and_warn
;
394 sc
= para_calloc(sizeof(*sc
));
396 sc
->name
= para_strdup(remote_name(fd
));
397 sc
->cq
= cq_new(MAX_CQ_BYTES
);
398 para_list_add(&sc
->node
, &ss
->client_list
);
399 add_close_on_fork_list(fd
);
400 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss
->num_clients
,
406 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
412 * Get the generic help text.
414 * \return A dynamically allocated string containing the help text for
415 * a paraslash sender.
417 char *generic_sender_help(void)
421 "usage: {allow|deny} IP[/netmask]\n"
422 " where mask defaults to 32\n"
423 "example: allow 192.168.0.1/24\n"
427 static int parse_fec_parms(const char *arg
, struct sender_command_data
*scd
)
430 char *a
= para_strdup(arg
),
432 *c
= strrchr(a
, ':');
433 int ret
= -E_COMMAND_SYNTAX
;
439 ret
= para_atoi32(a
, &val
);
443 /* optional max_slice_bytes (0 means "use MTU") */
445 scd
->max_slice_bytes
= 0;
447 if (val
< 0 || val
> 65535)
449 scd
->max_slice_bytes
= val
;
451 ret
= para_atoi32(b
+ 1, &val
);
456 /* k = data_slices_per_group */
457 if (val
< 0 || val
> 255)
459 scd
->data_slices_per_group
= val
;
461 /* n = slices_per_group */
462 ret
= para_atoi32(c
+ 1, &val
);
465 if (val
< 0 || val
< scd
->data_slices_per_group
)
467 scd
->slices_per_group
= val
;
473 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);
478 * Parse a FEC URL string.
480 * \param arg the URL string to parse.
481 * \param scd The structure containing host, port and the FEC parameters.
485 * A FEC URL consists of an ordinary URL string according to RFC 3986,
486 * optionally followed by a slash and the three FEC parameters slice_size,
487 * data_slices_per_group and slices_per_group. The three FEC parameters are
488 * separated by colons.
490 * \sa \ref parse_url().
492 int parse_fec_url(const char *arg
, struct sender_command_data
*scd
)
494 char *a
= para_strdup(arg
), *p
= strchr(a
, '/');
497 /* default fec parameters */
498 scd
->max_slice_bytes
= 0;
499 scd
->data_slices_per_group
= 14;
500 scd
->slices_per_group
= 16;
504 ret
= parse_fec_parms(p
+ 1, scd
);
508 if (!parse_url(a
, scd
->host
, sizeof(scd
->host
), &scd
->port
))
509 ret
= -ERRNO_TO_PARA_ERROR(EINVAL
);