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