dccp_send: Fix a fd leak.
[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         dc = para_calloc(sizeof(struct dccp_client));
86         dc->fd = ret;
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);
93         return;
94 err:
95         close(fd);
96 }
97
98 static int dccp_open(void)
99 {
100         int ret = para_listen(AF_UNSPEC, IPPROTO_DCCP, conf.dccp_port_arg);
101
102         if (ret < 0)
103                 return ret;
104         listen_fd = ret;
105         ret = mark_fd_nonblocking(listen_fd);
106         if (ret < 0) {
107                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
108                 exit(EXIT_FAILURE);
109         }
110         add_close_on_fork_list(listen_fd);
111         return 1;
112 }
113
114 static void dccp_shutdown_client(struct dccp_client *dc)
115 {
116         PARA_DEBUG_LOG("shutting down %s (fd %d)\n", dc->name, dc->fd);
117         free(dc->name);
118         close(dc->fd);
119         del_close_on_fork_list(dc->fd);
120         cq_destroy(dc->cq);
121         list_del(&dc->node);
122         free(dc);
123 }
124
125 /*
126  * ret: Negative on errors, zero if nothing was written and write returned
127  * EAGAIN, number of bytes written else.
128  */
129 static int dccp_write(int fd, const char *buf, size_t len)
130 {
131         size_t written = 0;
132         int ret = 0;
133
134         while (written < len) {
135                 ret = write(fd, buf + written, PARA_MIN(1024, len - written));
136                 /*
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.
141                  */
142                 if (ret < 0 && errno == EAGAIN)
143                         return written;
144                 if (ret < 0) {
145                         PARA_ERROR_LOG("%s\n", strerror(errno));
146                         return -E_DCCP_WRITE;
147                 }
148                 written += ret;
149         }
150         return written;
151 }
152
153 static int queue_chunk_or_shutdown(struct dccp_client *dc, long unsigned chunk_num,
154         size_t sent)
155 {
156         int ret = cq_enqueue(dc->cq, chunk_num, sent);
157         if (ret < 0) {
158                 PARA_NOTICE_LOG("enqueue error\n");
159                 dccp_shutdown_client(dc);
160         }
161         return ret;
162 }
163
164 static int send_queued_chunks(struct dccp_client *dc)
165 {
166         struct queued_chunk *qc;
167         while ((qc = cq_peek(dc->cq))) {
168                 char *buf;
169                 size_t len;
170                 int ret;
171                 cq_get(qc, &buf, &len);
172                 ret = dccp_write(dc->fd, buf, len);
173                 if (ret < 0)
174                         return ret;
175                 cq_update(dc->cq, ret);
176                 if (ret != len)
177                         return 1;
178                 cq_dequeue(dc->cq);
179         }
180         return 1;
181 }
182
183 static void dccp_send(long unsigned current_chunk,
184                 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
185 {
186         struct dccp_client *dc, *tmp;
187         int ret;
188         char *header_buf;
189         size_t header_len;
190
191         if (listen_fd < 0 || !len)
192                 return;
193
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)
199                                         continue;
200                         }
201                         dc->header_sent = 1;
202                 }
203                 ret = send_queued_chunks(dc);
204                 if (ret < 0) {
205                         dccp_shutdown_client(dc);
206                         continue;
207                 }
208 //              PARA_DEBUG_LOG("writing %d bytes to fd %d\n", len, dc->fd);
209                 ret = dccp_write(dc->fd, buf, len);
210                 if (ret < 0) {
211                         PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
212                         dccp_shutdown_client(dc);
213                         continue;
214                 }
215                 if (ret != len)
216                         queue_chunk_or_shutdown(dc, current_chunk, ret);
217         }
218 }
219
220 static void dccp_shutdown_clients(void)
221 {
222         struct dccp_client *dc, *tmp;
223
224         list_for_each_entry_safe(dc, tmp, &clients, node)
225                 dccp_shutdown_client(dc);
226 }
227
228 static char *dccp_info(void)
229 {
230         static char *buf;
231         int num_clients = 0;
232         struct dccp_client *dc, *tmp;
233
234         free(buf);
235         list_for_each_entry_safe(dc, tmp, &clients, node)
236                 num_clients++;
237         buf = make_message("dccp connected clients: %d\n", num_clients);
238         return buf;
239 }
240
241 static char *dccp_help(void)
242 {
243         return make_message("no help available\n");
244 }
245
246 /**
247  * the init function of the dccp sender
248  *
249  * \param s pointer to the dccp sender struct
250  *
251  * It initializes all function pointers of \a s and starts
252  * listening on the given port.
253  */
254 void dccp_send_init(struct sender *s)
255 {
256         int ret;
257
258         INIT_LIST_HEAD(&clients);
259         s->info = dccp_info;
260         s->send = dccp_send;
261         s->pre_select = dccp_pre_select;
262         s->post_select = dccp_post_select;
263         s->shutdown_clients = dccp_shutdown_clients;
264         s->help = dccp_help;
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;
271         self = s;
272         ret = dccp_open();
273         if (ret < 0)
274                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
275 }