f0ba168957c4a76a7eac9b969c6d71c3f36a0fcf
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 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
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 connected client.
59 * \param sc The client to be shut down.
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.
65 void shutdown_client(struct sender_client
*sc
, struct sender_status
*ss
)
67 PARA_INFO_LOG("shutting down %s on fd %d\n", sc
->name
, sc
->fd
);
70 del_close_on_fork_list(sc
->fd
);
73 free(sc
->private_data
);
78 void shutdown_clients(struct sender_status
*ss
)
80 struct sender_client
*sc
, *tmp
;
81 list_for_each_entry_safe(sc
, tmp
, &ss
->client_list
, node
)
82 shutdown_client(sc
, ss
);
86 * Write a buffer to a non-blocking file descriptor.
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.
93 * If \a max_bytes_per_write is non-zero, do not send more than that many bytes
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.
101 * \return Negative on errors, number of bytes written else.
103 static int write_nonblock(int fd
, const char *buf
, size_t len
,
104 size_t max_bytes_per_write
)
109 while (written
< len
) {
110 size_t num
= len
- written
;
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
)
118 return -ERRNO_TO_PARA_ERROR(errno
);
124 static int queue_chunk_or_shutdown(struct sender_client
*sc
,
125 struct sender_status
*ss
, long unsigned chunk_num
,
128 int ret
= cq_enqueue(sc
->cq
, chunk_num
, sent
);
130 shutdown_client(sc
, ss
);
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
)
138 struct queued_chunk
*qc
;
139 while ((qc
= cq_peek(sc
->cq
))) {
143 cq_get(qc
, &buf
, &len
);
144 ret
= write_nonblock(sc
->fd
, buf
, len
, max_bytes_per_write
);
147 cq_update(sc
->cq
, ret
);
156 * Send one chunk of audio data to a connected client.
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.
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.
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
)
173 if (!sc
->header_sent
&& current_chunk
) {
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);
183 ret
= send_queued_chunks(sc
, max_bytes_per_write
);
185 shutdown_client(sc
, ss
);
190 if (!ret
) { /* still data left in the queue */
191 ret
= queue_chunk_or_shutdown(sc
, ss
, current_chunk
, 0);
194 ret
= write_nonblock(sc
->fd
, buf
, len
, max_bytes_per_write
);
196 shutdown_client(sc
, ss
);
200 ret
= queue_chunk_or_shutdown(sc
, ss
, current_chunk
, ret
);
203 PARA_NOTICE_LOG("%s\n", para_strerror(-ret
));
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
)
210 INIT_LIST_HEAD(&ss
->client_list
);
212 acl_init(&ss
->acl
, access_arg
, num_access_args
);
214 ss
->max_clients
= max_clients
;
215 ss
->default_deny
= default_deny
;
218 char *get_sender_info(struct sender_status
*ss
, char *name
)
220 char *clnts
= NULL
, *ret
;
221 struct sender_client
*sc
, *tmp_sc
;
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
);
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",
238 (ss
->listen_fd
>= 0)? "on" : "off",
242 ss
->max_clients
> 0? "" : " (unlimited)",
243 clnts
? clnts
: "(none)",
244 ss
->default_deny
? "allow" : "deny",
245 acl_contents
? acl_contents
: "(empty)"
252 void generic_com_allow(struct sender_command_data
*scd
,
253 struct sender_status
*ss
)
255 acl_allow(scd
->addr
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
258 void generic_com_deny(struct sender_command_data
*scd
,
259 struct sender_status
*ss
)
261 acl_deny(scd
->addr
, scd
->netmask
, &ss
->acl
, ss
->default_deny
);
264 int generic_com_on(struct sender_status
*ss
, unsigned protocol
)
268 if (ss
->listen_fd
>= 0)
270 ret
= open_sender(protocol
, ss
->port
);
277 void generic_com_off(struct sender_status
*ss
)
279 if (ss
->listen_fd
< 0)
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
);
288 struct sender_client
*accept_sender_client(struct sender_status
*ss
)
290 struct sender_client
*sc
;
291 int fd
, ret
= para_accept(ss
->listen_fd
, NULL
, 0);
293 PARA_ERROR_LOG("%s\n", para_strerror(-ret
));
297 ret
= -E_MAX_CLIENTS
;
298 if (ss
->max_clients
> 0 && ss
->num_clients
>= ss
->max_clients
)
300 ret
= mark_fd_nonblocking(fd
);
303 ret
= acl_check_access(fd
, &ss
->acl
, ss
->default_deny
);
307 sc
= para_calloc(sizeof(*sc
));
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
,
317 PARA_WARNING_LOG("%s\n", para_strerror(-ret
));
322 char *generic_sender_help(void)
326 "usage: {allow|deny} IP mask\n"
327 "example: allow 127.0.0.1 32\n"