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