]> git.tuebingen.mpg.de Git - paraslash.git/blob - send_common.c
Sender code consolidation, on/off commands for the dccp sender.
[paraslash.git] / send_common.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file send_common.c Functions used by more than one paraslash sender. */
8
9 #include <dirent.h>
10 #include "para.h"
11 #include "error.h"
12 #include "string.h"
13 #include "fd.h"
14 #include "net.h"
15 #include "list.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "server.h"
19 #include "acl.h"
20 #include "send.h"
21 #include "close_on_fork.h"
22 #include "chunk_queue.h"
23 #include "vss.h"
24
25 /** Clients will be kicked if there are more than that many bytes pending. */
26 #define MAX_CQ_BYTES 40000
27
28 /**
29  * Open a passive socket of given layer4 type.
30  *
31  * Set the resultig file descriptor to nonblocking mode and add it to the list
32  * of fds that are being closed in the child process when the server calls
33  * fork().
34  *
35  * \param l4type The transport-layer protocol.
36  * \param port The port number.
37  *
38  * \return The listening fd on success, negative on errors.
39  */
40 static int open_sender(unsigned l4type, int port)
41 {
42         int fd, ret = para_listen(AF_UNSPEC, l4type, port);
43
44         if (ret < 0)
45                 return ret;
46         fd = ret;
47         ret = mark_fd_nonblocking(fd);
48         if (ret < 0) {
49                 close(fd);
50                 return ret;
51         }
52         add_close_on_fork_list(fd);
53         return fd;
54 }
55
56 /**
57  * Shut down a connected client.
58  *
59  * \param sc The client to be shut down.
60  *
61  * Close the file descriptor given by \a sc, remove it from the close-on-fork
62  * list, destroy the chunk queue of this client, delete the client from the
63  * list of connected clients and free the sender_client struct.
64  */
65 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
66 {
67         PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
68         free(sc->name);
69         close(sc->fd);
70         del_close_on_fork_list(sc->fd);
71         cq_destroy(sc->cq);
72         list_del(&sc->node);
73         free(sc->private_data);
74         free(sc);
75         ss->num_clients--;
76 }
77
78 void shutdown_clients(struct sender_status *ss)
79 {
80         struct sender_client *sc, *tmp;
81         list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
82                 shutdown_client(sc, ss);
83 }
84
85 /**
86  * Write a buffer to a non-blocking file descriptor.
87  *
88  * \param fd The file descriptor.
89  * \param buf the buffer to write.
90  * \param len the number of bytes of \a buf.
91  * \param max_bytes_per_write Do not write more than that many bytes at once.
92  *
93  * If \a max_bytes_per_write is non-zero, do not send more than that many bytes
94  * per write().
95  *
96  * EAGAIN is not considered an error condition.  For example CCID3 has a
97  * sending wait queue which fills up and is emptied asynchronously. The EAGAIN
98  * case means that there is currently no space in the wait queue, but this can
99  * change at any moment.
100  *
101  * \return Negative on errors, number of bytes written else.
102  */
103 static int write_nonblock(int fd, const char *buf, size_t len,
104                 size_t max_bytes_per_write)
105 {
106         size_t written = 0;
107         int ret = 0;
108
109         while (written < len) {
110                 size_t num = len - written;
111
112                 if (max_bytes_per_write && max_bytes_per_write < num)
113                         num = max_bytes_per_write;
114                 ret = write(fd, buf + written, num);
115                 if (ret < 0 && errno == EAGAIN)
116                         return written;
117                 if (ret < 0)
118                         return -ERRNO_TO_PARA_ERROR(errno);
119                 written += ret;
120         }
121         return written;
122 }
123
124 static int queue_chunk_or_shutdown(struct sender_client *sc,
125                 struct sender_status *ss, long unsigned chunk_num,
126                 size_t sent)
127 {
128         int ret = cq_enqueue(sc->cq, chunk_num, sent);
129         if (ret < 0)
130                 shutdown_client(sc, ss);
131         return ret;
132 }
133
134 /* return: negative on errors, zero if not everything was sent, one otherwise */
135 static int send_queued_chunks(struct sender_client *sc,
136                 size_t max_bytes_per_write)
137 {
138         struct queued_chunk *qc;
139         while ((qc = cq_peek(sc->cq))) {
140                 char *buf;
141                 size_t len;
142                 int ret;
143                 cq_get(qc, &buf, &len);
144                 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
145                 if (ret < 0)
146                         return ret;
147                 cq_update(sc->cq, ret);
148                 if (ret != len)
149                         return 0;
150                 cq_dequeue(sc->cq);
151         }
152         return 1;
153 }
154
155 /**
156  * Send one chunk of audio data to a connected client.
157  *
158  * \param sc The client.
159  * \param max_bytes_per_write Split writes to chunks of at most that many bytes.
160  * \param current_chunk The number of the chunk to write.
161  * \param buf The data to write.
162  * \param len The number of bytes of \a buf.
163  *
164  * On errors, the client is shut down. If only a part of the buffer could be
165  * written, the remainder is put into the chunk queue for that client.
166  */
167 void send_chunk(struct sender_client *sc, struct sender_status *ss,
168                 size_t max_bytes_per_write, long unsigned current_chunk,
169                 const char *buf, size_t len)
170 {
171         int ret;
172
173         if (!sc->header_sent && current_chunk) {
174                 size_t header_len;
175                 char *header_buf = vss_get_header(&header_len);
176                 if (header_buf && header_len > 0) {
177                         ret = queue_chunk_or_shutdown(sc, ss, -1U, 0);
178                         if (ret < 0)
179                                 goto out;
180                 }
181                 sc->header_sent = 1;
182         }
183         ret = send_queued_chunks(sc, max_bytes_per_write);
184         if (ret < 0) {
185                 shutdown_client(sc, ss);
186                 goto out;
187         }
188         if (!len)
189                 goto out;
190         if (!ret) { /* still data left in the queue */
191                 ret = queue_chunk_or_shutdown(sc, ss, current_chunk, 0);
192                 goto out;
193         }
194         ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
195         if (ret < 0) {
196                 shutdown_client(sc, ss);
197                 goto out;
198         }
199         if (ret != len)
200                 ret = queue_chunk_or_shutdown(sc, ss, current_chunk, ret);
201 out:
202         if (ret < 0)
203                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
204 }
205
206 void init_sender_status(struct sender_status *ss, char **access_arg,
207         int num_access_args, int port, int max_clients, int default_deny)
208 {
209         ss->listen_fd = -1;
210         INIT_LIST_HEAD(&ss->client_list);
211         ss->port = port;
212         acl_init(&ss->acl, access_arg, num_access_args);
213         ss->num_clients = 0;
214         ss->max_clients = max_clients;
215         ss->default_deny = default_deny;
216 }
217
218 char *get_sender_info(struct sender_status *ss, char *name)
219 {
220         char *clnts = NULL, *ret;
221         struct sender_client *sc, *tmp_sc;
222
223         char *acl_contents = acl_get_contents(&ss->acl);
224         list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
225                 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
226                 free(clnts);
227                 clnts = tmp;
228         }
229         ret = make_message(
230                 "%s sender:\n"
231                 "\tstatus: %s\n"
232                 "\tport: %d\n"
233                 "\tnumber of connected clients: %d\n"
234                 "\tmaximal number of clients: %d%s\n"
235                 "\tconnected clients: %s\n"
236                 "\taccess %s list: %s\n",
237                 name,
238                 (ss->listen_fd >= 0)? "on" : "off",
239                 ss->port,
240                 ss->num_clients,
241                 ss->max_clients,
242                 ss->max_clients > 0? "" : " (unlimited)",
243                 clnts? clnts : "(none)",
244                 ss->default_deny? "allow" : "deny",
245                 acl_contents? acl_contents : "(empty)"
246         );
247         free(acl_contents);
248         free(clnts);
249         return ret;
250 }
251
252 void generic_com_allow(struct sender_command_data *scd,
253                 struct sender_status *ss)
254 {
255         acl_allow(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
256 }
257
258 void generic_com_deny(struct sender_command_data *scd,
259                 struct sender_status *ss)
260 {
261         acl_deny(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
262 }
263
264 int generic_com_on(struct sender_status *ss, unsigned protocol)
265 {
266         int ret;
267
268         if (ss->listen_fd >= 0)
269                 return 1;
270         ret = open_sender(protocol, ss->port);
271         if (ret < 0)
272                 return ret;
273         ss->listen_fd = ret;
274         return 1;
275 }
276
277 void generic_com_off(struct sender_status *ss)
278 {
279         if (ss->listen_fd < 0)
280                 return;
281         PARA_NOTICE_LOG("closing port %d\n", ss->port);
282         close(ss->listen_fd);
283         del_close_on_fork_list(ss->listen_fd);
284         shutdown_clients(ss);
285         ss->listen_fd = -1;
286 }
287
288 struct sender_client *accept_sender_client(struct sender_status *ss)
289 {
290         struct sender_client *sc;
291         int fd, ret = para_accept(ss->listen_fd, NULL, 0);
292         if (ret < 0) {
293                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
294                 return NULL;
295         }
296         fd = ret;
297         ret = -E_MAX_CLIENTS;
298         if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
299                 goto err_out;
300         ret = mark_fd_nonblocking(fd);
301         if (ret < 0)
302                 goto err_out;
303         ret = acl_check_access(fd, &ss->acl, ss->default_deny);
304         if (ret < 0)
305                 goto err_out;
306         ss->num_clients++;
307         sc = para_calloc(sizeof(*sc));
308         sc->fd = fd;
309         sc->name = make_message("%s", remote_name(fd));
310         sc->cq = cq_new(MAX_CQ_BYTES);
311         para_list_add(&sc->node, &ss->client_list);
312         add_close_on_fork_list(fd);
313         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
314                 sc->name, fd);
315         return sc;
316 err_out:
317         PARA_WARNING_LOG("%s\n", para_strerror(-ret));
318         close(fd);
319         return NULL;
320 }
321
322 char *generic_sender_help(void)
323 {
324         return make_message(
325                 "usage: {on|off}\n"
326                 "usage: {allow|deny} IP mask\n"
327                 "example: allow 127.0.0.1 32\n"
328         );
329 }