]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_send.c
25d9ac18cefdd5353909864aa6ec1001061d9170
[paraslash.git] / http_send.c
1 /*
2  * Copyright (C) 2005-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file http_send.c paraslash's http sender */
8
9 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "string.h"
15 #include "server.cmdline.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "server.h"
19 #include "http.h"
20 #include "vss.h"
21 #include "send.h"
22 #include "list.h"
23 #include "close_on_fork.h"
24 #include "net.h"
25 #include "fd.h"
26 #include "chunk_queue.h"
27 #include "acl.h"
28
29 /** Message sent to clients that do not send a valid get request. */
30 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
31
32
33 /** The possible states of a client from the server's POV. */
34 enum http_status {
35         /** We accepted the connection on the tcp socket. */
36         HTTP_CONNECTED,
37         /** Successfully received the get request. */
38         HTTP_GOT_GET_REQUEST,
39         /** We sent the OK message back to the client. */
40         HTTP_SENT_OK_MSG,
41         /** Connection is ready for sending audio data. */
42         HTTP_STREAMING,
43         /** We didn't receive a valid get request. */
44         HTTP_INVALID_GET_REQUEST
45 };
46
47 /** Clients will be kicked if there are more than that many bytes pending. */
48 #define MAX_BACKLOG 400000
49 /** The list of connected clients. */
50 static struct list_head clients;
51 /** The whitelist/blacklist. */
52 static struct list_head http_acl;
53
54 /** Describes one client that connected the tcp port of the http sender. */
55 struct http_client {
56         /** The file descriptor of the client. */
57         int fd;
58         /** The socket `name' of the client. */
59         char *name;
60         /** The client's current status. */
61         enum http_status status;
62         /** Non-zero if we included \a fd in the read set.*/
63         int check_r;
64         /** Non-zero if we included \a fd in the write set. */
65         int check_w;
66         /** The position of this client in the client list. */
67         struct list_head node;
68         /** non-zero if audio file header has been sent */
69         int header_sent;
70         /** The list of pending chunks for this client. */
71         struct chunk_queue *cq;
72 };
73
74 static int listen_fd = -1, numclients;
75
76
77 static void http_shutdown_client(struct http_client *hc, const char *msg)
78 {
79         PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", hc->name, hc->fd,
80                 msg);
81         numclients--;
82         free(hc->name);
83         close(hc->fd);
84         del_close_on_fork_list(hc->fd);
85         cq_destroy(hc->cq);
86         list_del(&hc->node);
87         free(hc);
88 }
89
90 static void http_shutdown_clients(void)
91 {
92         struct http_client *hc, *tmp;
93         list_for_each_entry_safe(hc, tmp, &clients, node)
94                 http_shutdown_client(hc, "vss request");
95 }
96
97 static int http_send_msg(struct http_client *hc, const char *msg)
98 {
99         int ret = send_buffer(hc->fd, msg);
100
101         if (ret < 0)
102                 http_shutdown_client(hc, "send msg failed");
103         return ret;
104 }
105
106 static void http_send_ok_msg(struct http_client *hc)
107 {
108         PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
109         http_send_msg(hc, HTTP_OK_MSG);
110 }
111
112 static int http_send_err_msg(struct http_client *hc)
113 {
114         PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
115         return http_send_msg(hc, HTTP_ERR_MSG);
116 }
117
118 /*
119  * ret: Negative on errors, zero if nothing was written and write returned
120  * EAGAIN, number of bytes written else.
121  */
122 static int http_write(int fd, const char *buf, size_t len)
123 {
124         size_t written = 0;
125
126         while (written < len) {
127                 int ret = write(fd, buf + written, len - written);
128                 if (ret < 0 && errno == EAGAIN)
129                         return written;
130                 if (ret < 0)
131                         return -ERRNO_TO_PARA_ERROR(errno);
132                 written += ret;
133         }
134         return written;
135 }
136
137
138 static int send_queued_chunks(struct http_client *hc)
139 {
140         struct queued_chunk *qc;
141         while ((qc = cq_peek(hc->cq))) {
142                 char *buf;
143                 size_t len;
144                 int ret;
145                 cq_get(qc, &buf, &len);
146                 ret = http_write(hc->fd, buf, len);
147                 if (ret < 0)
148                         return ret;
149                 cq_update(hc->cq, ret);
150                 if (ret != len)
151                         return 1;
152                 cq_dequeue(hc->cq);
153         }
154         return 1;
155 }
156
157 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
158         size_t sent)
159 {
160         int ret = cq_enqueue(hc->cq, chunk_num, sent);
161         if (ret < 0)
162                 http_shutdown_client(hc, "queue error");
163         return ret;
164 }
165
166 static void http_send(long unsigned current_chunk,
167         __a_unused long unsigned chunks_sent, const char *buf, size_t len)
168 {
169         struct http_client *hc, *tmp;
170         int ret;
171
172         list_for_each_entry_safe(hc, tmp, &clients, node) {
173                 if (hc->status != HTTP_STREAMING)
174                         continue;
175                 if (!hc->header_sent && current_chunk) {
176                         size_t hlen;
177                         char *hbuf = vss_get_header(&hlen);
178                         if (hbuf && hlen > 0) { /* need to send header */
179                                 PARA_INFO_LOG("queueing header: %zu\n", hlen);
180                                 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
181                                         continue;
182                         } else
183                                 PARA_INFO_LOG("no need to queue header\n");
184                         hc->header_sent = 1;
185                 }
186                 ret = send_queued_chunks(hc);
187                 if (ret < 0) {
188                         http_shutdown_client(hc, "queue send error");
189                         continue;
190                 }
191                 if (!len)
192                         continue;
193                 ret = http_write(hc->fd, buf, len);
194                 if (ret < 0) {
195                         http_shutdown_client(hc, "send error");
196                         continue;
197                 }
198                 if (ret != len)
199                         queue_chunk_or_shutdown(hc, current_chunk, ret);
200         }
201 }
202
203 static void http_post_select(fd_set *rfds, fd_set *wfds)
204 {
205         int i = -1, match;
206         struct http_client *hc, *tmp;
207         const char *err_msg;
208
209         if (listen_fd < 0)
210                 return;
211         list_for_each_entry_safe(hc, tmp, &clients, node) {
212                 i++;
213 //              PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
214                 switch (hc->status) {
215                 case HTTP_STREAMING: /* nothing to do */
216                         break;
217                 case HTTP_CONNECTED: /* need to recv get request */
218                         if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
219                                 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
220                                                 < 0) {
221                                         hc->status = HTTP_INVALID_GET_REQUEST;
222                                 } else {
223                                         hc->status = HTTP_GOT_GET_REQUEST;
224                                         PARA_INFO_LOG("%s",
225                                                 "received get request\n");
226                                 }
227                         }
228                         break;
229                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
230                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
231                                 hc->status = HTTP_SENT_OK_MSG;
232                                 http_send_ok_msg(hc);
233                         }
234                         break;
235                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
236                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
237                                 if (http_send_err_msg(hc) >= 0)
238                                         http_shutdown_client(hc,
239                                                 "invalid get request");
240                         }
241                         break;
242                 case HTTP_SENT_OK_MSG: /* need to send header? */
243                         if (hc->check_w && FD_ISSET(hc->fd, wfds))
244                                 hc->status = HTTP_STREAMING;
245                         break;
246                 }
247         }
248         if (!FD_ISSET(listen_fd, rfds))
249                 return;
250         hc = para_calloc(sizeof(struct http_client));
251         err_msg = "accept error";
252         hc->fd = para_accept(listen_fd, NULL, 0);
253         if (hc->fd <= 0)
254                 goto err_out;
255         hc->name = make_message("%s", remote_name(hc->fd));
256         PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc->name, hc->fd);
257         if (conf.http_max_clients_arg > 0 && numclients >=
258                         conf.http_max_clients_arg) {
259                 err_msg = "server full";
260                 goto err_out;
261         }
262         match = acl_lookup(hc->fd, &http_acl);
263         PARA_DEBUG_LOG("acl lookup returned %d\n", match);
264         if ((match && !conf.http_default_deny_given) ||
265                         (!match && conf.http_default_deny_given)) {
266                 err_msg = "permission denied";
267                 goto err_out;
268         }
269         err_msg = "failed to mark fd non-blocking";
270         if (mark_fd_nonblocking(hc->fd) < 0)
271                 goto err_out;
272         hc->status = HTTP_CONNECTED;
273         hc->cq = cq_new(MAX_BACKLOG);
274         numclients++;
275         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
276                 hc->name, hc->fd);
277         para_list_add(&hc->node, &clients);
278         add_close_on_fork_list(hc->fd);
279         return;
280 err_out:
281         PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
282                 hc->name, err_msg);
283         if (hc->fd > 0)
284                 close(hc->fd);
285         free(hc);
286 }
287
288 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
289 {
290         struct http_client *hc, *tmp;
291
292         if (listen_fd < 0)
293                 return;
294         para_fd_set(listen_fd, rfds, max_fileno);
295         list_for_each_entry_safe(hc, tmp, &clients, node) {
296                 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
297                 hc->check_r = 0;
298                 hc->check_w = 0;
299                 switch (hc->status) {
300                 case HTTP_STREAMING:
301                         break;
302                 case HTTP_CONNECTED: /* need to recv get request */
303                         para_fd_set(hc->fd, rfds, max_fileno);
304                         hc->check_r = 1;
305                         break;
306                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
307                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
308                         para_fd_set(hc->fd, wfds, max_fileno);
309                         hc->check_w = 1;
310                         break;
311                 case HTTP_SENT_OK_MSG:
312                         if (!vss_playing())
313                                 break; /* wait until server starts playing */
314                         para_fd_set(hc->fd, wfds, max_fileno);
315                         hc->check_w = 1;
316                         break;
317                 }
318         }
319 }
320
321 static int http_open(void)
322 {
323         int ret;
324
325         listen_fd = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.http_port_arg);
326         if (listen_fd < 0)
327                 return listen_fd;
328         ret = mark_fd_nonblocking(listen_fd);
329         if (ret < 0) {
330                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
331                 exit(EXIT_FAILURE);
332         }
333         add_close_on_fork_list(listen_fd);
334         return 1;
335 }
336
337 static int http_com_on(__a_unused struct sender_command_data *scd)
338 {
339         if (listen_fd >= 0)
340                 return 1;
341         return http_open();
342 }
343
344 static int http_com_off(__a_unused struct sender_command_data *scd)
345 {
346         if (listen_fd < 0)
347                 return 1;
348         PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
349         close(listen_fd);
350         del_close_on_fork_list(listen_fd);
351         http_shutdown_clients();
352         listen_fd = -1;
353         return 1;
354 }
355
356 static int http_com_deny(struct sender_command_data *scd)
357 {
358         if (conf.http_default_deny_given)
359                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
360         else
361                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
362         return 1;
363 }
364
365 static int http_com_allow(struct sender_command_data *scd)
366 {
367         if (conf.http_default_deny_given)
368                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
369         else
370                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
371         return 1;
372 }
373
374 static char *http_info(void)
375 {
376         char *clnts = NULL, *ret;
377         struct http_client *hc, *tmp_hc;
378
379         char *acl_contents = acl_get_contents(&http_acl);
380         list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
381                 char *tmp = make_message("%s%s ", clnts? clnts : "", hc->name);
382                 free(clnts);
383                 clnts = tmp;
384         }
385         ret = make_message(
386                 "http status: %s\n"
387                 "http tcp port: %d\n"
388                 "http clients: %d\n"
389                 "http maximal number of clients: %d%s\n"
390                 "http connected clients: %s\n"
391                 "http access %s list: %s\n",
392                 (listen_fd >= 0)? "on" : "off",
393                 conf.http_port_arg,
394                 numclients,
395                 conf.http_max_clients_arg,
396                 conf.http_max_clients_arg > 0? "" : " (unlimited)",
397                 clnts? clnts : "(none)",
398                 conf.http_default_deny_given? "allow" : "deny",
399                 acl_contents? acl_contents : "(none)"
400         );
401         free(acl_contents);
402         free(clnts);
403         return ret;
404 }
405
406 static char *http_help(void)
407 {
408         return make_message(
409                 "usage: {on|off}\n"
410                 "usage: {allow|deny} IP mask\n"
411                 "example: allow 127.0.0.1 32\n"
412         );
413 }
414
415 /**
416  * The init function of the http sender.
417  *
418  * \param s Pointer to the http sender struct.
419  *
420  * It initializes all function pointers of \a s, the client list and the access
421  * control list. If the autostart option was given, the tcp port is opened.
422  */
423 void http_send_init(struct sender *s)
424 {
425         INIT_LIST_HEAD(&clients);
426         s->info = http_info;
427         s->send = http_send;
428         s->pre_select = http_pre_select;
429         s->post_select = http_post_select;
430         s->shutdown_clients = http_shutdown_clients;
431         s->help = http_help;
432         s->client_cmds[SENDER_ON] = http_com_on;
433         s->client_cmds[SENDER_OFF] = http_com_off;
434         s->client_cmds[SENDER_DENY] = http_com_deny;
435         s->client_cmds[SENDER_ALLOW] = http_com_allow;
436         s->client_cmds[SENDER_ADD] = NULL;
437         s->client_cmds[SENDER_DELETE] = NULL;
438         acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
439         if (!conf.http_no_autostart_given)
440                 http_open(); /* ignore errors */
441         PARA_DEBUG_LOG("%s", "http sender init complete\n");
442 }