command_util.sh: Add missing newline.
[paraslash.git] / dccp_send.c
1 /*
2  * Copyright (C) 2006-2007 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         dc = para_calloc(sizeof(struct dccp_client));
76         dc->fd = ret;
77         dc->name = make_message("%s", remote_name(dc->fd));
78         PARA_NOTICE_LOG("connection from %s\n", dc->name);
79         para_list_add(&dc->node, &clients);
80         add_close_on_fork_list(dc->fd);
81         mark_fd_nonblocking(dc->fd);
82         dc->cq = cq_new(DCCP_MAX_PENDING_BYTES);
83 }
84
85 static int dccp_open(void)
86 {
87         int ret = para_listen(AF_UNSPEC, IPPROTO_DCCP, conf.dccp_port_arg);
88
89         if (ret < 0)
90                 return ret;
91         listen_fd = ret;
92         add_close_on_fork_list(listen_fd);
93         mark_fd_nonblocking(listen_fd);
94         return 1;
95 }
96
97 static void dccp_shutdown_client(struct dccp_client *dc)
98 {
99         PARA_DEBUG_LOG("shutting down %s (fd %d)\n", dc->name, dc->fd);
100         free(dc->name);
101         close(dc->fd);
102         del_close_on_fork_list(dc->fd);
103         cq_destroy(dc->cq);
104         list_del(&dc->node);
105         free(dc);
106 }
107
108 /*
109  * ret: Negative on errors, zero if nothing was written and write returned
110  * EAGAIN, number of bytes written else.
111  */
112 static int dccp_write(int fd, const char *buf, size_t len)
113 {
114         size_t written = 0;
115         int ret = 0;
116
117         while (written < len) {
118                 ret = write(fd, buf + written, PARA_MIN(1024, len - written));
119                 /*
120                  * Error handling: CCID3 has a sending wait queue which fills up and is
121                  * emptied asynchronously. The EAGAIN case means that there is currently
122                  * no space in the wait queue, but this can change at any moment and is
123                  * thus not an error condition.
124                  */
125                 if (ret < 0 && errno == EAGAIN)
126                         return written;
127                 if (ret < 0) {
128                         PARA_ERROR_LOG("%s\n", strerror(errno));
129                         return -E_DCCP_WRITE;
130                 }
131                 written += ret;
132         }
133         return written;
134 }
135
136 static int queue_chunk_or_shutdown(struct dccp_client *dc, long unsigned chunk_num,
137         size_t sent)
138 {
139         int ret = cq_enqueue(dc->cq, chunk_num, sent);
140         if (ret < 0) {
141                 PARA_NOTICE_LOG("enqueue error\n");
142                 dccp_shutdown_client(dc);
143         }
144         return ret;
145 }
146
147 static int send_queued_chunks(struct dccp_client *dc)
148 {
149         struct queued_chunk *qc;
150         while ((qc = cq_peek(dc->cq))) {
151                 char *buf;
152                 size_t len;
153                 int ret;
154                 cq_get(qc, &buf, &len);
155                 ret = dccp_write(dc->fd, buf, len);
156                 if (ret < 0)
157                         return ret;
158                 cq_update(dc->cq, ret);
159                 if (ret != len)
160                         return 1;
161                 cq_dequeue(dc->cq);
162         }
163         return 1;
164 }
165
166 static void dccp_send(long unsigned current_chunk,
167                 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
168 {
169         struct dccp_client *dc, *tmp;
170         int ret;
171         char *header_buf;
172         size_t header_len;
173
174         if (listen_fd < 0 || !len)
175                 return;
176
177         list_for_each_entry_safe(dc, tmp, &clients, node) {
178                 if (!dc->header_sent && current_chunk) {
179                         header_buf = vss_get_header(&header_len);
180                         if (header_buf && header_len > 0) {
181                                 if (queue_chunk_or_shutdown(dc, -1U, 0) < 0)
182                                         continue;
183                         }
184                         dc->header_sent = 1;
185                 }
186                 ret = send_queued_chunks(dc);
187                 if (ret < 0) {
188                         dccp_shutdown_client(dc);
189                         continue;
190                 }
191 //              PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
192                 ret = dccp_write(dc->fd, buf, len);
193                 if (ret < 0) {
194                         PARA_NOTICE_LOG("%s\n", PARA_STRERROR(-ret));
195                         dccp_shutdown_client(dc);
196                         continue;
197                 }
198                 if (ret != len)
199                         queue_chunk_or_shutdown(dc, current_chunk, ret);
200         }
201 }
202
203 static void dccp_shutdown_clients(void)
204 {
205         struct dccp_client *dc, *tmp;
206
207         list_for_each_entry_safe(dc, tmp, &clients, node)
208                 dccp_shutdown_client(dc);
209 }
210
211 static char *dccp_info(void)
212 {
213         static char *buf;
214         int num_clients = 0;
215         struct dccp_client *dc, *tmp;
216
217         free(buf);
218         list_for_each_entry_safe(dc, tmp, &clients, node)
219                 num_clients++;
220         buf = make_message("dccp connected clients: %d\n", num_clients);
221         return buf;
222 }
223
224 static char *dccp_help(void)
225 {
226         return make_message("no help available\n");
227 }
228
229 /**
230  * the init function of the dccp sender
231  *
232  * \param s pointer to the dccp sender struct
233  *
234  * It initializes all function pointers of \a s and starts
235  * listening on the given port.
236  */
237 void dccp_send_init(struct sender *s)
238 {
239         int ret;
240
241         INIT_LIST_HEAD(&clients);
242         s->info = dccp_info;
243         s->send = dccp_send;
244         s->pre_select = dccp_pre_select;
245         s->post_select = dccp_post_select;
246         s->shutdown_clients = dccp_shutdown_clients;
247         s->help = dccp_help;
248         s->client_cmds[SENDER_ON] = NULL;
249         s->client_cmds[SENDER_OFF] = NULL;
250         s->client_cmds[SENDER_DENY] = NULL;
251         s->client_cmds[SENDER_ALLOW] = NULL;
252         s->client_cmds[SENDER_ADD] = NULL;
253         s->client_cmds[SENDER_DELETE] = NULL;
254         self = s;
255         ret = dccp_open();
256         if (ret < 0) {
257                 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
258                 s->status = SENDER_OFF;
259         } else
260                 s->status = SENDER_ON;
261 }