paraslash 0.3.0
[paraslash.git] / dccp_send.c
1 /*
2  * Copyright (C) 2006-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file dccp_send.c paraslash's dccp sender */
8
9 /*
10  * based on server.c of dccp-cs-0.01.tar.bz2,
11  * (C) 2005 Ian McDonald <imcdnzl@gmail.com>
12  */
13
14 #include <sys/types.h>
15 #include <dirent.h>
16
17 #include "para.h"
18 #include "error.h"
19 #include "string.h"
20 #include "afh.h"
21 #include "afs.h"
22 #include "server.h"
23 #include "net.h"
24 #include "list.h"
25 #include "vss.h"
26 #include "send.h"
27 #include "fd.h"
28 #include "close_on_fork.h"
29 #include "chunk_queue.h"
30 #include "server.cmdline.h"
31
32 /** the list of connected clients **/
33 static struct list_head clients;
34 static int listen_fd = -1;
35 static struct sender *self;
36
37 /** Maximal number of bytes in a chunk queue. */
38 #define DCCP_MAX_PENDING_BYTES 40000
39
40 /** describes one connected client */
41 struct dccp_client {
42         /** the dccp socket */
43         int fd;
44         /** The socket `name' of the client. */
45         char *name;
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 */
49         int header_sent;
50         /** The list of pending chunks for this client. */
51         struct chunk_queue *cq;
52 };
53
54 static void dccp_pre_select( int *max_fileno, fd_set *rfds,
55                 __a_unused fd_set *wfds)
56 {
57         if (listen_fd < 0)
58                 return;
59         FD_SET(listen_fd, rfds);
60         *max_fileno = PARA_MAX(*max_fileno, listen_fd);
61 }
62
63 static void dccp_post_select(fd_set *rfds, __a_unused fd_set *wfds)
64 {
65         struct dccp_client *dc;
66         int ret;
67
68         if (!FD_ISSET(listen_fd, rfds))
69                 return;
70         ret = para_accept(listen_fd, NULL, 0);
71         if (ret < 0) {
72                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
73                 return;
74         }
75         /*
76          * Bypass unused CCID paths: the sender does not receive application data
77          * from the client; by shutting down this unused communication path we can
78          * reduce processing costs a bit. See analogous comment in dccp_recv.c.
79          */
80         if (shutdown(ret, SHUT_RD) < 0) {
81                 PARA_ERROR_LOG("shutdown(SHUT_RD): %s\n", strerror(errno));
82                 return;
83         }
84         dc = para_calloc(sizeof(struct dccp_client));
85         dc->fd = ret;
86         dc->name = make_message("%s", remote_name(dc->fd));
87         PARA_NOTICE_LOG("connection from %s\n", dc->name);
88         para_list_add(&dc->node, &clients);
89         add_close_on_fork_list(dc->fd);
90         mark_fd_nonblocking(dc->fd);
91         dc->cq = cq_new(DCCP_MAX_PENDING_BYTES);
92 }
93
94 static int dccp_open(void)
95 {
96         int ret = para_listen(AF_UNSPEC, IPPROTO_DCCP, conf.dccp_port_arg);
97
98         if (ret < 0)
99                 return ret;
100         listen_fd = ret;
101         add_close_on_fork_list(listen_fd);
102         mark_fd_nonblocking(listen_fd);
103         return 1;
104 }
105
106 static void dccp_shutdown_client(struct dccp_client *dc)
107 {
108         PARA_DEBUG_LOG("shutting down %s (fd %d)\n", dc->name, dc->fd);
109         free(dc->name);
110         close(dc->fd);
111         del_close_on_fork_list(dc->fd);
112         cq_destroy(dc->cq);
113         list_del(&dc->node);
114         free(dc);
115 }
116
117 /*
118  * ret: Negative on errors, zero if nothing was written and write returned
119  * EAGAIN, number of bytes written else.
120  */
121 static int dccp_write(int fd, const char *buf, size_t len)
122 {
123         size_t written = 0;
124         int ret = 0;
125
126         while (written < len) {
127                 ret = write(fd, buf + written, PARA_MIN(1024, len - written));
128                 /*
129                  * Error handling: CCID3 has a sending wait queue which fills up and is
130                  * emptied asynchronously. The EAGAIN case means that there is currently
131                  * no space in the wait queue, but this can change at any moment and is
132                  * thus not an error condition.
133                  */
134                 if (ret < 0 && errno == EAGAIN)
135                         return written;
136                 if (ret < 0) {
137                         PARA_ERROR_LOG("%s\n", strerror(errno));
138                         return -E_DCCP_WRITE;
139                 }
140                 written += ret;
141         }
142         return written;
143 }
144
145 static int queue_chunk_or_shutdown(struct dccp_client *dc, long unsigned chunk_num,
146         size_t sent)
147 {
148         int ret = cq_enqueue(dc->cq, chunk_num, sent);
149         if (ret < 0) {
150                 PARA_NOTICE_LOG("enqueue error\n");
151                 dccp_shutdown_client(dc);
152         }
153         return ret;
154 }
155
156 static int send_queued_chunks(struct dccp_client *dc)
157 {
158         struct queued_chunk *qc;
159         while ((qc = cq_peek(dc->cq))) {
160                 char *buf;
161                 size_t len;
162                 int ret;
163                 cq_get(qc, &buf, &len);
164                 ret = dccp_write(dc->fd, buf, len);
165                 if (ret < 0)
166                         return ret;
167                 cq_update(dc->cq, ret);
168                 if (ret != len)
169                         return 1;
170                 cq_dequeue(dc->cq);
171         }
172         return 1;
173 }
174
175 static void dccp_send(long unsigned current_chunk,
176                 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
177 {
178         struct dccp_client *dc, *tmp;
179         int ret;
180         char *header_buf;
181         size_t header_len;
182
183         if (listen_fd < 0 || !len)
184                 return;
185
186         list_for_each_entry_safe(dc, tmp, &clients, node) {
187                 if (!dc->header_sent && current_chunk) {
188                         header_buf = vss_get_header(&header_len);
189                         if (header_buf && header_len > 0) {
190                                 if (queue_chunk_or_shutdown(dc, -1U, 0) < 0)
191                                         continue;
192                         }
193                         dc->header_sent = 1;
194                 }
195                 ret = send_queued_chunks(dc);
196                 if (ret < 0) {
197                         dccp_shutdown_client(dc);
198                         continue;
199                 }
200 //              PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
201                 ret = dccp_write(dc->fd, buf, len);
202                 if (ret < 0) {
203                         PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
204                         dccp_shutdown_client(dc);
205                         continue;
206                 }
207                 if (ret != len)
208                         queue_chunk_or_shutdown(dc, current_chunk, ret);
209         }
210 }
211
212 static void dccp_shutdown_clients(void)
213 {
214         struct dccp_client *dc, *tmp;
215
216         list_for_each_entry_safe(dc, tmp, &clients, node)
217                 dccp_shutdown_client(dc);
218 }
219
220 static char *dccp_info(void)
221 {
222         static char *buf;
223         int num_clients = 0;
224         struct dccp_client *dc, *tmp;
225
226         free(buf);
227         list_for_each_entry_safe(dc, tmp, &clients, node)
228                 num_clients++;
229         buf = make_message("dccp connected clients: %d\n", num_clients);
230         return buf;
231 }
232
233 static char *dccp_help(void)
234 {
235         return make_message("no help available\n");
236 }
237
238 /**
239  * the init function of the dccp sender
240  *
241  * \param s pointer to the dccp sender struct
242  *
243  * It initializes all function pointers of \a s and starts
244  * listening on the given port.
245  */
246 void dccp_send_init(struct sender *s)
247 {
248         int ret;
249
250         INIT_LIST_HEAD(&clients);
251         s->info = dccp_info;
252         s->send = dccp_send;
253         s->pre_select = dccp_pre_select;
254         s->post_select = dccp_post_select;
255         s->shutdown_clients = dccp_shutdown_clients;
256         s->help = dccp_help;
257         s->client_cmds[SENDER_ON] = NULL;
258         s->client_cmds[SENDER_OFF] = NULL;
259         s->client_cmds[SENDER_DENY] = NULL;
260         s->client_cmds[SENDER_ALLOW] = NULL;
261         s->client_cmds[SENDER_ADD] = NULL;
262         s->client_cmds[SENDER_DELETE] = NULL;
263         self = s;
264         ret = dccp_open();
265         if (ret < 0) {
266                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
267                 s->status = SENDER_OFF;
268         } else
269                 s->status = SENDER_ON;
270 }