upd_send.c: Use write_nonblock() rather than write_all().
[paraslash.git] / send_common.c
1 /*
2 * Copyright (C) 2005-2009 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 resulting 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 client connected to a paraslash sender.
58 *
59 * \param sc The client to shut down.
60 * \param ss The sender whose clients are to be shut down.
61 *
62 * Close the file descriptor given by \a sc, remove it from the close-on-fork
63 * list, destroy the chunk queue of this client, delete the client from the
64 * list of connected clients and free the sender_client struct.
65 *
66 * \sa shutdown_clients().
67 */
68 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
69 {
70 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
71 free(sc->name);
72 close(sc->fd);
73 del_close_on_fork_list(sc->fd);
74 cq_destroy(sc->cq);
75 list_del(&sc->node);
76 free(sc->private_data);
77 free(sc);
78 ss->num_clients--;
79 }
80
81 /**
82 * Shut down all clients connected to a paraslash sender.
83 *
84 * \param ss The sender whose clients are to be shut down.
85 *
86 * This just loops over all connected clients and calls shutdown_client()
87 * for each client.
88 */
89 void shutdown_clients(struct sender_status *ss)
90 {
91 struct sender_client *sc, *tmp;
92 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
93 shutdown_client(sc, ss);
94 }
95
96 static int queue_chunk_or_shutdown(struct sender_client *sc,
97 struct sender_status *ss, const char *buf, size_t num_bytes)
98 {
99 int ret = cq_enqueue(sc->cq, buf, num_bytes);
100 if (ret < 0)
101 shutdown_client(sc, ss);
102 return ret;
103 }
104
105 /* return: negative on errors, zero if not everything was sent, one otherwise */
106 static int send_queued_chunks(struct sender_client *sc,
107 size_t max_bytes_per_write)
108 {
109 struct queued_chunk *qc;
110 while ((qc = cq_peek(sc->cq))) {
111 const char *buf;
112 size_t len;
113 int ret;
114 cq_get(qc, &buf, &len);
115 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
116 if (ret < 0)
117 return ret;
118 cq_update(sc->cq, ret);
119 if (ret != len)
120 return 0;
121 cq_dequeue(sc->cq);
122 }
123 return 1;
124 }
125
126 /**
127 * Send one chunk of audio data to a connected client.
128 *
129 * \param sc The client.
130 * \param ss The sender.
131 * \param max_bytes_per_write Split writes to chunks of at most that many bytes.
132 * \param current_chunk The number of the chunk to write.
133 * \param buf The data to write.
134 * \param len The number of bytes of \a buf.
135 * \param header_buf The audio file header.
136 * \param header_len The number of bytes of \a header_buf.
137 *
138 * On errors, the client is shut down. If only a part of the buffer could be
139 * written, the remainder is put into the chunk queue for that client.
140 */
141 void send_chunk(struct sender_client *sc, struct sender_status *ss,
142 size_t max_bytes_per_write, long unsigned current_chunk,
143 const char *buf, size_t len, const char *header_buf,
144 size_t header_len)
145 {
146 int ret;
147
148 if (!sc->header_sent && current_chunk) {
149 if (header_buf && header_len > 0) {
150 ret = queue_chunk_or_shutdown(sc, ss, header_buf, header_len);
151 if (ret < 0)
152 goto out;
153 }
154 sc->header_sent = 1;
155 }
156 ret = send_queued_chunks(sc, max_bytes_per_write);
157 if (ret < 0) {
158 shutdown_client(sc, ss);
159 goto out;
160 }
161 if (!len)
162 goto out;
163 if (!ret) { /* still data left in the queue */
164 ret = queue_chunk_or_shutdown(sc, ss, buf, len);
165 goto out;
166 }
167 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
168 if (ret < 0) {
169 shutdown_client(sc, ss);
170 goto out;
171 }
172 if (ret != len)
173 ret = queue_chunk_or_shutdown(sc, ss, buf + ret, len - ret);
174 out:
175 if (ret < 0)
176 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
177 }
178
179 /**
180 * Initialize a struct sender status.
181 *
182 * \param ss The struct to initialize.
183 * \param access_arg The array of access arguments given at the command line.
184 * \param num_access_args The number of elements in \a access_arg.
185 * \param port The tcp or dccp port to listen on.
186 * \param max_clients The maximal number of simultaneous connections.
187 * \param default_deny Whether a blacklist should be used for access control.
188 */
189 void init_sender_status(struct sender_status *ss, char **access_arg,
190 int num_access_args, int port, int max_clients, int default_deny)
191 {
192 ss->listen_fd = -1;
193 INIT_LIST_HEAD(&ss->client_list);
194 ss->port = port;
195 acl_init(&ss->acl, access_arg, num_access_args);
196 ss->num_clients = 0;
197 ss->max_clients = max_clients;
198 ss->default_deny = default_deny;
199 }
200
201 /**
202 * Return a string containing the current status of a sender.
203 *
204 * \param ss The sender.
205 * \param name Used for printing the header line.
206 *
207 * \return The string printed in the "si" command.
208 */
209 char *get_sender_info(struct sender_status *ss, char *name)
210 {
211 char *clnts = NULL, *ret;
212 struct sender_client *sc, *tmp_sc;
213
214 char *acl_contents = acl_get_contents(&ss->acl);
215 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
216 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
217 free(clnts);
218 clnts = tmp;
219 }
220 ret = make_message(
221 "%s sender:\n"
222 "\tstatus: %s\n"
223 "\tport: %d\n"
224 "\tnumber of connected clients: %d\n"
225 "\tmaximal number of clients: %d%s\n"
226 "\tconnected clients: %s\n"
227 "\taccess %s list: %s\n",
228 name,
229 (ss->listen_fd >= 0)? "on" : "off",
230 ss->port,
231 ss->num_clients,
232 ss->max_clients,
233 ss->max_clients > 0? "" : " (unlimited)",
234 clnts? clnts : "(none)",
235 ss->default_deny? "allow" : "deny",
236 acl_contents? acl_contents : "(empty)"
237 );
238 free(acl_contents);
239 free(clnts);
240 return ret;
241 }
242
243 /**
244 * Allow connections from the given range of IP addresses.
245 *
246 * \param scd Contains the IP and the netmask.
247 * \param ss The sender.
248 *
249 * \sa generic_com_deny().
250 */
251 void generic_com_allow(struct sender_command_data *scd,
252 struct sender_status *ss)
253 {
254 acl_allow(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
255 }
256
257 /**
258 * Deny connections from the given range of IP addresses.
259 *
260 * \param scd see \ref generic_com_allow().
261 * \param ss see \ref generic_com_allow().
262 *
263 * \sa generic_com_allow().
264 */
265 void generic_com_deny(struct sender_command_data *scd,
266 struct sender_status *ss)
267 {
268 acl_deny(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
269 }
270
271 /**
272 * Activate a paraslash sender.
273 *
274 * \param ss The sender to activate.
275 * \param protocol The symbolic name of the transport-layer protocol.
276 *
277 * \return Standard.
278 */
279 int generic_com_on(struct sender_status *ss, unsigned protocol)
280 {
281 int ret;
282
283 if (ss->listen_fd >= 0)
284 return 1;
285 ret = open_sender(protocol, ss->port);
286 if (ret < 0)
287 return ret;
288 ss->listen_fd = ret;
289 return 1;
290 }
291
292 /**
293 * Deactivate a paraslash sender.
294 *
295 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
296 *
297 * \param ss The sender to deactivate.
298 *
299 * \sa \ref del_close_on_fork_list(), shutdown_clients().
300 */
301 void generic_com_off(struct sender_status *ss)
302 {
303 if (ss->listen_fd < 0)
304 return;
305 PARA_NOTICE_LOG("closing port %d\n", ss->port);
306 close(ss->listen_fd);
307 del_close_on_fork_list(ss->listen_fd);
308 shutdown_clients(ss);
309 ss->listen_fd = -1;
310 }
311
312 /**
313 * Accept a connection on the socket this server is listening on.
314 *
315 * \param ss The sender whose listening fd is ready for reading.
316 *
317 * This must be called only if the socket fd of \a ss is ready for reading. It
318 * calls para_accept() to accept the connection and performs the following
319 * actions on the resulting file descriptor \a fd:
320 *
321 * - Checks whether the maximal number of connections are exceeded.
322 * - Sets \a fd to nonblocking mode.
323 * - Checks the acl of the sender to find out whether connections
324 * are allowed from the IP of the connecting peer.
325 * - Increases the number of connections for this sender.
326 * - Creates and initializes a new chunk queue for queuing network
327 * packets that can not be sent immediately.
328 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
329 * and \a name members.
330 * - Adds \a fd to the list of connected clients for this sender.
331 * - Adds \a fd to the list of file descriptors that should be closed
332 * in the child process when the server calls fork().
333 *
334 * \return A pointer to the allocated sender_client structure on success, \p
335 * NULL on errors.
336 *
337 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
338 * \ref cq_new(), \ref add_close_on_fork_list().
339 */
340 struct sender_client *accept_sender_client(struct sender_status *ss)
341 {
342 struct sender_client *sc;
343 int fd, ret = para_accept(ss->listen_fd, NULL, 0);
344 if (ret < 0) {
345 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
346 return NULL;
347 }
348 fd = ret;
349 ret = -E_MAX_CLIENTS;
350 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
351 goto err_out;
352 ret = mark_fd_nonblocking(fd);
353 if (ret < 0)
354 goto err_out;
355 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
356 if (ret < 0)
357 goto err_out;
358 ss->num_clients++;
359 sc = para_calloc(sizeof(*sc));
360 sc->fd = fd;
361 sc->name = make_message("%s", remote_name(fd));
362 sc->cq = cq_new(MAX_CQ_BYTES);
363 para_list_add(&sc->node, &ss->client_list);
364 add_close_on_fork_list(fd);
365 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
366 sc->name, fd);
367 return sc;
368 err_out:
369 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
370 close(fd);
371 return NULL;
372 }
373
374 /**
375 * Get the generic help text.
376 *
377 * \return A dynamically allocated string containing the help text for
378 * a paraslash sender.
379 */
380 char *generic_sender_help(void)
381 {
382 return make_message(
383 "usage: {on|off}\n"
384 "usage: {allow|deny} IP mask\n"
385 "example: allow 127.0.0.1 32\n"
386 );
387 }