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