2 * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file dccp_send.c paraslash's dccp sender */
10 * based on server.c of dccp-cs-0.01.tar.bz2,
11 * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
14 #include <sys/types.h>
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
30 #include "server.cmdline.h"
32 /** the list of connected clients **/
33 static struct list_head clients;
34 static int listen_fd = -1;
35 static struct sender *self;
37 /** Maximal number of bytes in a chunk queue. */
38 #define DCCP_MAX_PENDING_BYTES 40000
40 /** describes one connected client */
42 /** the dccp socket */
44 /** The socket `name' of the client. */
46 /** the position of this client in the client list */
47 struct list_head node;
48 /** non-zero if audio file header has been sent */
50 /** The list of pending chunks for this client. */
51 struct chunk_queue *cq;
54 static void dccp_pre_select( int *max_fileno, fd_set *rfds,
55 __a_unused fd_set *wfds)
59 FD_SET(listen_fd, rfds);
60 *max_fileno = PARA_MAX(*max_fileno, listen_fd);
63 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
65 struct dccp_client *dc;
68 if (listen_fd < 0 || !FD_ISSET(listen_fd, rfds))
70 ret = para_accept(listen_fd, NULL, 0);
72 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
77 * Bypass unused CCID paths: the sender does not receive application data
78 * from the client; by shutting down this unused communication path we can
79 * reduce processing costs a bit. See analogous comment in dccp_recv.c.
81 if (shutdown(fd, SHUT_RD) < 0) {
82 PARA_ERROR_LOG("shutdown(SHUT_RD): %s\n", strerror(errno));
85 dc = para_calloc(sizeof(struct dccp_client));
87 dc->name = make_message("%s", remote_name(dc->fd));
88 PARA_NOTICE_LOG("connection from %s\n", dc->name);
89 para_list_add(&dc->node, &clients);
90 add_close_on_fork_list(dc->fd);
91 mark_fd_nonblocking(dc->fd);
92 dc->cq = cq_new(DCCP_MAX_PENDING_BYTES);
98 static int dccp_open(void)
100 int ret = para_listen(AF_UNSPEC, IPPROTO_DCCP, conf.dccp_port_arg);
105 ret = mark_fd_nonblocking(listen_fd);
107 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
110 add_close_on_fork_list(listen_fd);
114 static void dccp_shutdown_client(struct dccp_client *dc)
116 PARA_DEBUG_LOG("shutting down %s (fd %d)\n", dc->name, dc->fd);
119 del_close_on_fork_list(dc->fd);
126 * ret: Negative on errors, zero if nothing was written and write returned
127 * EAGAIN, number of bytes written else.
129 static int dccp_write(int fd, const char *buf, size_t len)
134 while (written < len) {
135 ret = write(fd, buf + written, PARA_MIN(1024, len - written));
137 * Error handling: CCID3 has a sending wait queue which fills up and is
138 * emptied asynchronously. The EAGAIN case means that there is currently
139 * no space in the wait queue, but this can change at any moment and is
140 * thus not an error condition.
142 if (ret < 0 && errno == EAGAIN)
145 PARA_ERROR_LOG("%s\n", strerror(errno));
146 return -E_DCCP_WRITE;
153 static int queue_chunk_or_shutdown(struct dccp_client *dc, long unsigned chunk_num,
156 int ret = cq_enqueue(dc->cq, chunk_num, sent);
158 PARA_NOTICE_LOG("enqueue error\n");
159 dccp_shutdown_client(dc);
164 static int send_queued_chunks(struct dccp_client *dc)
166 struct queued_chunk *qc;
167 while ((qc = cq_peek(dc->cq))) {
171 cq_get(qc, &buf, &len);
172 ret = dccp_write(dc->fd, buf, len);
175 cq_update(dc->cq, ret);
183 static void dccp_send(long unsigned current_chunk,
184 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
186 struct dccp_client *dc, *tmp;
191 if (listen_fd < 0 || !len)
194 list_for_each_entry_safe(dc, tmp, &clients, node) {
195 if (!dc->header_sent && current_chunk) {
196 header_buf = vss_get_header(&header_len);
197 if (header_buf && header_len > 0) {
198 if (queue_chunk_or_shutdown(dc, -1U, 0) < 0)
203 ret = send_queued_chunks(dc);
205 dccp_shutdown_client(dc);
208 // PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
209 ret = dccp_write(dc->fd, buf, len);
211 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
212 dccp_shutdown_client(dc);
216 queue_chunk_or_shutdown(dc, current_chunk, ret);
220 static void dccp_shutdown_clients(void)
222 struct dccp_client *dc, *tmp;
224 list_for_each_entry_safe(dc, tmp, &clients, node)
225 dccp_shutdown_client(dc);
228 static char *dccp_info(void)
232 struct dccp_client *dc, *tmp;
235 list_for_each_entry_safe(dc, tmp, &clients, node)
237 buf = make_message("dccp connected clients: %d\n", num_clients);
241 static char *dccp_help(void)
243 return make_message("no help available\n");
247 * the init function of the dccp sender
249 * \param s pointer to the dccp sender struct
251 * It initializes all function pointers of \a s and starts
252 * listening on the given port.
254 void dccp_send_init(struct sender *s)
258 INIT_LIST_HEAD(&clients);
261 s->pre_select = dccp_pre_select;
262 s->post_select = dccp_post_select;
263 s->shutdown_clients = dccp_shutdown_clients;
265 s->client_cmds[SENDER_ON] = NULL;
266 s->client_cmds[SENDER_OFF] = NULL;
267 s->client_cmds[SENDER_DENY] = NULL;
268 s->client_cmds[SENDER_ALLOW] = NULL;
269 s->client_cmds[SENDER_ADD] = NULL;
270 s->client_cmds[SENDER_DELETE] = NULL;
274 PARA_ERROR_LOG("%s\n", para_strerror(-ret));