2 * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file send_common.c Functions used by more than one paraslash sender. */
21 #include "close_on_fork.h"
22 #include "chunk_queue.h"
25 /** Clients will be kicked if there are more than that many bytes pending. */
26 #define MAX_CQ_BYTES 40000
29 * Open a passive socket of given layer4 type.
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
35 * \param l4type The transport-layer protocol.
36 * \param port The port number.
38 * \return The listening fd on success, negative on errors.
40 static int open_sender(unsigned l4type, int port)
42 int fd, ret = para_listen(AF_UNSPEC, l4type, port);
47 ret = mark_fd_nonblocking(fd);
52 add_close_on_fork_list(fd);
57 * Shut down a client connected to a paraslash sender.
59 * \param sc The client to shut down.
60 * \param ss The sender whose clients are to be shut down.
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.
66 void shutdown_client(struct sender_client *sc, struct sender_status *ss)
68 PARA_INFO_LOG("shutting down %s on fd %d\n", sc->name, sc->fd);
71 del_close_on_fork_list(sc->fd);
74 free(sc->private_data);
79 void shutdown_clients(struct sender_status *ss)
81 struct sender_client *sc, *tmp;
82 list_for_each_entry_safe(sc, tmp, &ss->client_list, node)
83 shutdown_client(sc, ss);
87 * Write a buffer to a non-blocking file descriptor.
89 * \param fd The file descriptor.
90 * \param buf the buffer to write.
91 * \param len the number of bytes of \a buf.
92 * \param max_bytes_per_write Do not write more than that many bytes at once.
94 * If \a max_bytes_per_write is non-zero, do not send more than that many bytes
97 * EAGAIN is not considered an error condition. For example CCID3 has a
98 * sending wait queue which fills up and is emptied asynchronously. The EAGAIN
99 * case means that there is currently no space in the wait queue, but this can
100 * change at any moment.
102 * \return Negative on errors, number of bytes written else.
104 static int write_nonblock(int fd, const char *buf, size_t len,
105 size_t max_bytes_per_write)
110 while (written < len) {
111 size_t num = len - written;
113 if (max_bytes_per_write && max_bytes_per_write < num)
114 num = max_bytes_per_write;
115 ret = write(fd, buf + written, num);
116 if (ret < 0 && errno == EAGAIN)
119 return -ERRNO_TO_PARA_ERROR(errno);
125 static int queue_chunk_or_shutdown(struct sender_client *sc,
126 struct sender_status *ss, long unsigned chunk_num,
129 int ret = cq_enqueue(sc->cq, chunk_num, sent);
131 shutdown_client(sc, ss);
135 /* return: negative on errors, zero if not everything was sent, one otherwise */
136 static int send_queued_chunks(struct sender_client *sc,
137 size_t max_bytes_per_write)
139 struct queued_chunk *qc;
140 while ((qc = cq_peek(sc->cq))) {
144 cq_get(qc, &buf, &len);
145 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
148 cq_update(sc->cq, ret);
157 * Send one chunk of audio data to a connected client.
159 * \param sc The client.
160 * \param ss The sender.
161 * \param max_bytes_per_write Split writes to chunks of at most that many bytes.
162 * \param current_chunk The number of the chunk to write.
163 * \param buf The data to write.
164 * \param len The number of bytes of \a buf.
166 * On errors, the client is shut down. If only a part of the buffer could be
167 * written, the remainder is put into the chunk queue for that client.
169 void send_chunk(struct sender_client *sc, struct sender_status *ss,
170 size_t max_bytes_per_write, long unsigned current_chunk,
171 const char *buf, size_t len)
175 if (!sc->header_sent && current_chunk) {
179 vss_get_header(&header_buf, &header_len);
180 if (header_buf && header_len > 0) {
181 ret = queue_chunk_or_shutdown(sc, ss, -1U, 0);
187 ret = send_queued_chunks(sc, max_bytes_per_write);
189 shutdown_client(sc, ss);
194 if (!ret) { /* still data left in the queue */
195 ret = queue_chunk_or_shutdown(sc, ss, current_chunk, 0);
198 ret = write_nonblock(sc->fd, buf, len, max_bytes_per_write);
200 shutdown_client(sc, ss);
204 ret = queue_chunk_or_shutdown(sc, ss, current_chunk, ret);
207 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
211 * Initialize a struct sender status.
213 * \param ss The struct to initialize.
214 * \param access_arg The array of access arguments given at the command line.
215 * \param num_access_args The number of elements in \a access_arg.
216 * \param port The tcp or dccp port to listen on.
217 * \param max_clients The maximal number of simultaneous connections.
218 * \param default_deny Whether a blacklist should be used for access control.
220 void init_sender_status(struct sender_status *ss, char **access_arg,
221 int num_access_args, int port, int max_clients, int default_deny)
224 INIT_LIST_HEAD(&ss->client_list);
226 acl_init(&ss->acl, access_arg, num_access_args);
228 ss->max_clients = max_clients;
229 ss->default_deny = default_deny;
233 * Return a string containing the current status of a sender.
235 * \param ss The sender.
236 * \param name Used for printing the header line.
238 * \return The string printed in the "si" command.
240 char *get_sender_info(struct sender_status *ss, char *name)
242 char *clnts = NULL, *ret;
243 struct sender_client *sc, *tmp_sc;
245 char *acl_contents = acl_get_contents(&ss->acl);
246 list_for_each_entry_safe(sc, tmp_sc, &ss->client_list, node) {
247 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
255 "\tnumber of connected clients: %d\n"
256 "\tmaximal number of clients: %d%s\n"
257 "\tconnected clients: %s\n"
258 "\taccess %s list: %s\n",
260 (ss->listen_fd >= 0)? "on" : "off",
264 ss->max_clients > 0? "" : " (unlimited)",
265 clnts? clnts : "(none)",
266 ss->default_deny? "allow" : "deny",
267 acl_contents? acl_contents : "(empty)"
275 * Allow connections from the given range of IP addresses.
277 * \param scd Contains the IP and the netmask.
278 * \param ss The sender.
280 * \sa generic_com_deny().
282 void generic_com_allow(struct sender_command_data *scd,
283 struct sender_status *ss)
285 acl_allow(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
289 * Deny connections from the given range of IP addresses.
291 * \param scd see \ref generic_com_allow().
292 * \param ss see \ref generic_com_allow().
294 * \sa generic_com_allow().
296 void generic_com_deny(struct sender_command_data *scd,
297 struct sender_status *ss)
299 acl_deny(scd->addr, scd->netmask, &ss->acl, ss->default_deny);
303 * Activate a paraslash sender.
305 * \param ss The sender to activate.
306 * \param protocol The symbolic name of the transport-layer protocol.
310 int generic_com_on(struct sender_status *ss, unsigned protocol)
314 if (ss->listen_fd >= 0)
316 ret = open_sender(protocol, ss->port);
324 * Deactivate a paraslash sender.
326 * Shutdown all connected clients and stop listening on the TCP/DCCP socket.
328 * \param ss The sender to deactivate.
330 * \sa \ref del_close_on_fork_list(), shutdown_clients().
332 void generic_com_off(struct sender_status *ss)
334 if (ss->listen_fd < 0)
336 PARA_NOTICE_LOG("closing port %d\n", ss->port);
337 close(ss->listen_fd);
338 del_close_on_fork_list(ss->listen_fd);
339 shutdown_clients(ss);
344 * Accept a connection on the socket this server is listening on.
346 * \param ss The sender whose listening fd is ready for reading.
348 * This must be called only if the socket fd of \a ss is ready for reading. It
349 * calls para_accept() to accept the connection and performs the following
350 * actions on the resulting file descriptor \a fd:
352 * - Checks whether the maximal number of connections are exceeded.
353 * - Sets \a fd to nonblocking mode.
354 * - Checks the acl of the sender to find out whether connections
355 * are allowed from the IP of the connecting peer.
356 * - Increases the number of connections for this sender.
357 * - Creates and initializes a new chunk queue for queuing network
358 * packets that can not be sent immediately.
359 * - Allocates a new struct sender_client and fills in its \a fd, \a cq
360 * and \a name members.
361 * - Adds \a fd to the list of connected clients for this sender.
362 * - Adds \a fd to the list of file descriptors that should be closed
363 * in the child process when the server calls fork().
365 * \return A pointer to the allocated sender_client structure on success, \p
368 * \sa \ref para_accept(), \ref mark_fd_nonblocking(), \ref acl_check_access(),
369 * \ref cq_new(), \ref add_close_on_fork_list().
371 struct sender_client *accept_sender_client(struct sender_status *ss)
373 struct sender_client *sc;
374 int fd, ret = para_accept(ss->listen_fd, NULL, 0);
376 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
380 ret = -E_MAX_CLIENTS;
381 if (ss->max_clients > 0 && ss->num_clients >= ss->max_clients)
383 ret = mark_fd_nonblocking(fd);
386 ret = acl_check_access(fd, &ss->acl, ss->default_deny);
390 sc = para_calloc(sizeof(*sc));
392 sc->name = make_message("%s", remote_name(fd));
393 sc->cq = cq_new(MAX_CQ_BYTES);
394 para_list_add(&sc->node, &ss->client_list);
395 add_close_on_fork_list(fd);
396 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", ss->num_clients,
400 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
406 * Get the generic help text.
408 * \return A dynamically allocated string containing the help text for
409 * a paraslash sender.
411 char *generic_sender_help(void)
415 "usage: {allow|deny} IP mask\n"
416 "example: allow 127.0.0.1 32\n"