audiod: Use lsu_merge_config_file_options().
[paraslash.git] / send_common.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file send_common.c Functions used by more than one paraslash sender. */
4
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <regex.h>
8 #include <osl.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "para.h"
15 #include "error.h"
16 #include "string.h"
17 #include "fd.h"
18 #include "net.h"
19 #include "list.h"
20 #include "afh.h"
21 #include "afs.h"
22 #include "server.h"
23 #include "acl.h"
24 #include "send.h"
25 #include "close_on_fork.h"
26 #include "chunk_queue.h"
27 #include "sched.h"
28 #include "vss.h"
29
30 /** Clients will be kicked if there are more than that many bytes pending. */
31 #define MAX_CQ_BYTES 40000
32
33 /**
34 * Shut down a client connected to a paraslash sender.
35 *
36 * \param sc The client to shut down.
37 * \param ss The sender whose clients are to be shut down.
38 *
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.
42 *
43 * \sa \ref shutdown_clients().
44 */
45 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
46 {
47 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
48 free(sc->name);
49 close(sc->fd);
50 del_close_on_fork_list(sc->fd);
51 cq_destroy(sc->cq);
52 list_del(&sc->node);
53 free(sc->private_data);
54 free(sc);
55 ss->num_clients--;
56 }
57
58 /**
59 * Shut down all clients connected to a paraslash sender.
60 *
61 * \param ss The sender whose clients are to be shut down.
62 *
63 * This just loops over all connected clients and calls shutdown_client()
64 * for each client.
65 */
66 void shutdown_clients(struct sender_status *ss)
67 {
68 struct sender_client *sc, *tmp;
69 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
70 shutdown_client(sc, ss);
71 }
72
73 /**
74 * Try to empty the chunk queue for this fd.
75 *
76 * \param fd The file descriptor.
77 * \param cq The list of queued chunks.
78 *
79 * \return Negative on errors, zero if not everything was sent, one otherwise.
80 */
81 int send_queued_chunks(int fd, struct chunk_queue *cq)
82 {
83 struct queued_chunk *qc;
84 while ((qc = cq_peek(cq))) {
85 const char *buf;
86 size_t len;
87 int ret;
88
89 cq_get(qc, &buf, &len);
90 ret = xwrite(fd, buf, len);
91 if (ret < 0)
92 return ret;
93 cq_update(cq, ret);
94 if (ret != len)
95 return 0;
96 cq_dequeue(cq);
97 }
98 return 1;
99 }
100
101 /**
102 * Initialize a struct sender status.
103 *
104 * \param ss The struct to initialize.
105 * \param acl_opt_result Contains array of --{http|dccp}-access arguments.
106 * \param port The tcp or dccp port to listen on.
107 * \param max_clients The maximal number of simultaneous connections.
108 * \param default_deny Whether a blacklist should be used for access control.
109 */
110 void init_sender_status(struct sender_status *ss,
111 const struct lls_opt_result *acl_opt_result, int port,
112 int max_clients, int default_deny)
113 {
114 int i;
115
116 ss->listen_fd = -1;
117 INIT_LIST_HEAD(&ss->client_list);
118 ss->port = port;
119
120 /* Initialize an access control list */
121 INIT_LIST_HEAD(&ss->acl);
122 for (i = 0; i < lls_opt_given(acl_opt_result); i++) {
123 const char *arg = lls_string_val(i, acl_opt_result);
124 char addr[16];
125 int mask;
126 if (!parse_cidr(arg, addr, sizeof(addr), &mask))
127 PARA_WARNING_LOG("ACL syntax error: %s, ignoring\n",
128 arg);
129 else
130 acl_add_entry(&ss->acl, addr, mask);
131 }
132 ss->num_clients = 0;
133 ss->max_clients = max_clients;
134 ss->default_deny = default_deny;
135 }
136
137 /**
138 * Return a string containing the current status of a sender.
139 *
140 * \param ss The sender.
141 * \param name Used for printing the header line.
142 *
143 * \return The string printed in the "si" command.
144 */
145 char *generic_sender_status(struct sender_status *ss, const char *name)
146 {
147 char *clnts = NULL, *ret;
148 struct sender_client *sc, *tmp_sc;
149
150 char *acl_contents = acl_get_contents(&ss->acl);
151 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
152 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
153 free(clnts);
154 clnts = tmp;
155 }
156 ret = make_message(
157 "status: %s\n"
158 "port: %s\n"
159 "number of connected clients: %d\n"
160 "maximal number of clients: %d%s\n"
161 "connected clients: %s\n"
162 "access %s list: %s\n",
163 (ss->listen_fd >= 0)? "on" : "off",
164 stringify_port(ss->port, strcmp(name, "http") ? "dccp" : "tcp"),
165 ss->num_clients,
166 ss->max_clients,
167 ss->max_clients > 0? "" : " (unlimited)",
168 clnts? clnts : "(none)",
169 ss->default_deny? "allow" : "deny",
170 acl_contents? acl_contents : "(empty)"
171 );
172 free(acl_contents);
173 free(clnts);
174 return ret;
175 }
176
177 /**
178 * Allow connections from the given range of IP addresses.
179 *
180 * \param scd Contains the IP and the netmask.
181 * \param ss The sender.
182 *
183 * \sa \ref generic_com_deny().
184 */
185 void generic_com_allow(struct sender_command_data *scd,
186 struct sender_status *ss)
187 {
188 acl_allow(scd->host, scd->netmask, &ss->acl, ss->default_deny);
189 }
190
191 /**
192 * Deny connections from the given range of IP addresses.
193 *
194 * \param scd see \ref generic_com_allow().
195 * \param ss see \ref generic_com_allow().
196 *
197 * \sa \ref generic_com_allow().
198 */
199 void generic_com_deny(struct sender_command_data *scd,
200 struct sender_status *ss)
201 {
202 acl_deny(scd->host, scd->netmask, &ss->acl, ss->default_deny);
203 }
204
205 /**
206 * Activate a paraslash sender.
207 *
208 * \param ss The sender to activate.
209 * \param protocol layer4 type (IPPROTO_TCP or IPPROTO_DCCP).
210 *
211 * This opens a passive socket of given layer4 type, sets the resulting file
212 * descriptor to nonblocking mode and adds it to the close on fork list.
213 *
214 * Errors are logged but otherwise ignored.
215 */
216 void generic_com_on(struct sender_status *ss, unsigned protocol)
217 {
218 int fd, ret;
219
220 if (ss->listen_fd >= 0)
221 return;
222 ret = para_listen_simple(protocol, ss->port);
223 if (ret < 0) {
224 PARA_ERROR_LOG("could not listen on port %d: %s\n", ss->port,
225 para_strerror(-ret));
226 return;
227 }
228 fd = ret;
229 ret = mark_fd_nonblocking(fd);
230 if (ret < 0) {
231 PARA_ERROR_LOG("could not set %s socket fd for port %d to "
232 "nonblocking mode: %s\n",
233 protocol == IPPROTO_TCP? "TCP" : "DCCP", ss->port,
234 para_strerror(-ret));
235 close(fd);
236 return;
237 }
238 add_close_on_fork_list(fd);
239 ss->listen_fd = ret;
240 return;
241 }
242
243 /**
244 * Deactivate a paraslash sender.
245 *
246 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
247 *
248 * \param ss The sender to deactivate.
249 *
250 * \sa \ref del_close_on_fork_list(), \ref shutdown_clients().
251 */
252 void generic_com_off(struct sender_status *ss)
253 {
254 if (ss->listen_fd < 0)
255 return;
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);
260 ss->listen_fd = -1;
261 }
262
263 /**
264 * Accept a connection on the socket this server is listening on.
265 *
266 * \param ss The sender whose listening fd is ready for reading.
267 * \param rfds Passed to para_accept(),
268 *
269 * This calls para_accept() and performs the following actions on the resulting
270 * file descriptor fd:
271 *
272 * - Checks whether the maximal number of connections are exceeded.
273 * - Sets \a fd to nonblocking mode.
274 * - Checks the acl of the sender to find out whether connections
275 * are allowed from the IP of the connecting peer.
276 * - Increases the number of connections for this sender.
277 * - Creates and initializes a new chunk queue for queuing network
278 * packets that can not be sent immediately.
279 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
280 * and \a name members.
281 * - Adds \a fd to the list of connected clients for this sender.
282 * - Adds \a fd to the list of file descriptors that should be closed
283 * in the child process when the server calls fork().
284 *
285 * \return A pointer to the allocated sender_client structure on success, \p
286 * NULL on errors.
287 *
288 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
289 * \ref cq_new(), \ref add_close_on_fork_list().
290 */
291 struct sender_client *accept_sender_client(struct sender_status *ss, fd_set *rfds)
292 {
293 struct sender_client *sc;
294 int fd, ret;
295
296 if (ss->listen_fd < 0)
297 return NULL;
298 ret = para_accept(ss->listen_fd, rfds, NULL, 0, &fd);
299 if (ret < 0)
300 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
301 if (ret <= 0)
302 return NULL;
303 ret = -E_MAX_CLIENTS;
304 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
305 goto err_out;
306 ret = mark_fd_nonblocking(fd);
307 if (ret < 0)
308 goto err_out;
309 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
310 if (ret < 0)
311 goto err_out;
312 ss->num_clients++;
313 sc = para_calloc(sizeof(*sc));
314 sc->fd = fd;
315 sc->name = para_strdup(remote_name(fd));
316 sc->cq = cq_new(MAX_CQ_BYTES);
317 para_list_add(&sc->node, &ss->client_list);
318 add_close_on_fork_list(fd);
319 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
320 sc->name, fd);
321 return sc;
322 err_out:
323 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
324 close(fd);
325 return NULL;
326 }
327
328 /**
329 * Get the generic help text.
330 *
331 * \return A dynamically allocated string containing the help text for
332 * a paraslash sender.
333 */
334 char *generic_sender_help(void)
335 {
336 return make_message(
337 "usage: {on|off}\n"
338 "usage: {allow|deny} IP[/netmask]\n"
339 " where mask defaults to 32\n"
340 "example: allow 192.168.0.1/24\n"
341 );
342 }
343
344 static int parse_fec_parms(const char *arg, struct sender_command_data *scd)
345 {
346 int32_t val;
347 char *a = para_strdup(arg),
348 *b = strchr(a, ':'),
349 *c = strrchr(a, ':');
350 int ret = -E_COMMAND_SYNTAX;
351
352 if (!b || !c)
353 goto out;
354 *b = *c = '\0';
355
356 ret = para_atoi32(a, &val);
357 if (ret < 0)
358 goto out;
359
360 /* optional max_slice_bytes (0 means "use MTU") */
361 if (b == c) {
362 scd->max_slice_bytes = 0;
363 } else {
364 if (val < 0 || val > 65535)
365 goto fec_einval;
366 scd->max_slice_bytes = val;
367
368 ret = para_atoi32(b + 1, &val);
369 if (ret < 0)
370 goto out;
371 }
372
373 /* k = data_slices_per_group */
374 if (val < 0 || val > 255)
375 goto fec_einval;
376 scd->data_slices_per_group = val;
377
378 /* n = slices_per_group */
379 ret = para_atoi32(c + 1, &val);
380 if (ret < 0)
381 goto out;
382 if (val < 0 || val < scd->data_slices_per_group)
383 goto fec_einval;
384 scd->slices_per_group = val;
385 ret = 0;
386 out:
387 free(a);
388 return ret;
389 fec_einval:
390 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
391 goto out;
392 }
393
394 /**
395 * Parse a FEC URL string.
396 *
397 * \param arg the URL string to parse.
398 * \param scd The structure containing host, port and the FEC parameters.
399 *
400 * \return Standard.
401 *
402 * A FEC URL consists of an ordinary URL string according to RFC 3986,
403 * optionally followed by a slash and the three FEC parameters slice_size,
404 * data_slices_per_group and slices_per_group. The three FEC parameters are
405 * separated by colons.
406 *
407 * \sa \ref parse_url().
408 */
409 int parse_fec_url(const char *arg, struct sender_command_data *scd)
410 {
411 char *a = para_strdup(arg), *p = strchr(a, '/');
412 int ret = 0;
413
414 /* default fec parameters */
415 scd->max_slice_bytes = 0;
416 scd->data_slices_per_group = 14;
417 scd->slices_per_group = 16;
418
419 if (p) {
420 *p = '\0';
421 ret = parse_fec_parms(p + 1, scd);
422 if (ret < 0)
423 goto out;
424 }
425 if (!parse_url(a, scd->host, sizeof(scd->host), &scd->port))
426 ret = -ERRNO_TO_PARA_ERROR(EINVAL);
427 out:
428 free(a);
429 return ret;
430 }