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