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