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