aacdec: no need to read the offset table.
[paraslash.git] / http_send.c
1 /*
2  * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file http_send.c paraslash's http sender */
20
21
22 #include "server.cmdline.h"
23 #include "server.h"
24 #include "http.h"
25 #include "afs.h"
26 #include "send.h"
27 #include "list.h"
28 #include "close_on_fork.h"
29 #include "error.h"
30 #include "net.h"
31 #include "string.h"
32 #include "fd.h"
33
34 /** \cond convert sock_addr_in to ascii */
35 #define CLIENT_ADDR(hc) inet_ntoa((hc)->addr.sin_addr)
36 /* get the port number of a struct http_client */
37 #define CLIENT_PORT(hc) (hc)->addr.sin_port
38 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
39 /** \endcond */
40
41 extern struct gengetopt_args_info conf;
42
43 /** the possible states of a client from the server's POV */
44 enum http_status {
45         HTTP_CONNECTED,
46         HTTP_GOT_GET_REQUEST,
47         HTTP_SENT_OK_MSG,
48         HTTP_READY_TO_STREAM,
49         HTTP_STREAMING,
50         HTTP_INVALID_GET_REQUEST
51 };
52
53 /** clients will be kicked if there are more than that many bytes pending */
54 #define MAX_BACKLOG 40000
55 /** the list of connected clients **/
56 static struct list_head clients;
57 /** the whitelist/blacklist */
58 static struct list_head access_perm_list;
59
60 /** describes one client that connected the tcp port of the http sender */
61 struct http_client {
62 /** the file descriptor of the client */
63         int fd;
64 /** address information about the client */
65         struct sockaddr_in addr;
66 /** the client's current status */
67         enum http_status status;
68 /** non-zero if we included \a fd in the read set */
69         int check_r;
70 /** non-zero if we included \a fd in the write set */
71         int check_w;
72 /** the position of this client in the client list */
73         struct list_head node;
74 /** the list of pending packets for this client */
75         struct list_head packet_queue;
76 /** the number of pending bytes for this client */
77         unsigned long pq_bytes;
78 };
79
80 /**
81  * describes one queued data packet for a client
82  *
83  * The send function of the http sender checks each client fd for writing. If a
84  * client fd is not ready, it tries to queue that packet for this client until
85  * the number of queued bytes exceeds \p MAX_BACKLOG.
86  */
87 struct queued_packet {
88 /** the length of the packet in bytes */
89         unsigned int len;
90 /** pointer to the packet data */
91         char *packet;
92 /** position of the packet in the packet list */
93         struct list_head node;
94 };
95
96 /**
97  * describes one entry in the blacklist/whitelist of the http sender
98  */
99 struct access_info {
100         /** the address to be black/whitelisted */
101         struct in_addr addr;
102         /** the netmask for this entry */
103         int netmask;
104         /** the position of this entry in the access_perm_list */
105         struct list_head node;
106 };
107
108 static int server_fd = -1, numclients;
109 static struct sender *self;
110
111
112 static void http_shutdown_client(struct http_client *hc, const char *msg)
113 {
114         struct queued_packet *qp, *tmp;
115         PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
116                 hc->fd, msg);
117         numclients--;
118         close(hc->fd);
119
120         list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
121                 free(qp->packet);
122                 list_del(&qp->node);
123                 free(qp);
124         }
125         list_del(&hc->node);
126         free(hc);
127         return;
128 }
129
130 static void http_shutdown_clients_real(void)
131 {
132         struct http_client *hc, *tmp;
133         list_for_each_entry_safe(hc, tmp, &clients, node)
134                 http_shutdown_client(hc, "afs request");
135 }
136 static void http_shutdown_clients(void)
137 {
138         struct http_client *hc, *tmp;
139         list_for_each_entry_safe(hc, tmp, &clients, node)
140                 if (hc->status == HTTP_STREAMING)
141                         http_shutdown_client(hc, "afs request");
142 }
143
144 static int http_send_msg(struct http_client *hc, const char *msg)
145 {
146         int ret = send_buffer(hc->fd, msg);
147
148         if (ret < 0)
149                 http_shutdown_client(hc, "send msg failed");
150         return ret;
151 }
152
153 static void http_send_ok_msg(struct http_client *hc)
154 {
155         PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
156         http_send_msg(hc, HTTP_OK_MSG);
157 }
158
159 static int http_send_err_msg(struct http_client *hc)
160 {
161         PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
162         return http_send_msg(hc, HTTP_ERR_MSG);
163 }
164
165 static int queue_packet(struct http_client *hc, const char *buf, size_t len)
166 {
167         struct queued_packet *qp;
168         if (hc->pq_bytes + len > MAX_BACKLOG) {
169                 http_shutdown_client(hc, "packet queue overrun");
170                 return -E_QUEUE;
171         }
172         qp = para_malloc(sizeof(struct queued_packet));
173         hc->pq_bytes += len;
174         qp->packet = para_malloc(len);
175         memcpy(qp->packet, buf, len);
176         qp->len = len;
177         list_add_tail(&qp->node, &hc->packet_queue);
178         PARA_INFO_LOG("%lu bytes queued for fd %d\n", hc->pq_bytes, hc->fd);
179         return 1;
180 }
181
182 static int send_queued_packets(struct http_client *hc)
183 {
184         int ret;
185         struct queued_packet *qp, *tmp;
186
187         if (list_empty(&hc->packet_queue))
188                 return 1;
189         list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
190                 ret = write_ok(hc->fd);
191                 if (ret <= 0)
192                         return ret? -E_WRITE_OK : 0;
193                 ret = write(hc->fd, qp->packet, qp->len);
194                 if (ret < 0)
195                         return ret;
196                 if (ret != qp->len) {
197                         qp->len -= ret;
198                         memmove(qp->packet, qp->packet + ret, qp->len);
199                         return 0;
200                 }
201                 hc->pq_bytes -= qp->len;
202                 free(qp->packet);
203                 list_del(&qp->node);
204                 free(qp);
205         }
206         return 1;
207 }
208
209 static void http_send(__a_unused struct audio_format *af,
210                 long unsigned current_chunk,
211                 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
212 {
213         struct http_client *hc, *tmp;
214         int ret;
215
216         list_for_each_entry_safe(hc, tmp, &clients, node) {
217                 if (hc->status != HTTP_STREAMING &&
218                                 hc->status != HTTP_READY_TO_STREAM)
219                         continue;
220                 if (hc->status == HTTP_READY_TO_STREAM) {
221                         if (af->get_header_info && current_chunk) {
222                                 /* need to send header */
223                                 int hlen;
224                                 char *buf = af->get_header_info(&hlen);
225                                 if (!buf || hlen <= 0)
226                                         continue; /* header not yet available */
227                                 PARA_INFO_LOG("queueing header: %d\n", hlen);
228                                 if (queue_packet(hc, buf, hlen) < 0)
229                                         continue;
230                         } else
231                                 PARA_INFO_LOG("%s", "no need to queue header\n");
232                         hc->status = HTTP_STREAMING;
233                 }
234                 ret = send_queued_packets(hc);
235                 if (ret < 0) {
236                         http_shutdown_client(hc, "send error");
237                         continue;
238                 }
239                 if (!len)
240                         continue;
241                 if (!ret || write_ok(hc->fd) <= 0) {
242                         PARA_INFO_LOG("fd %d not ready (%lu bytes queued),"
243                                 " trying to queue packet\n", hc->fd,
244                                 hc->pq_bytes);
245                         queue_packet(hc, buf, len);
246                         continue;
247                 }
248 //              PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
249                 ret = write(hc->fd, buf, len);
250                 if (ret < 0) {
251                         http_shutdown_client(hc, "send error");
252                         continue;
253                 }
254                 if (ret != len)
255                         queue_packet(hc, buf + ret, len - ret);
256         }
257 }
258
259 static int host_in_access_perm_list(struct http_client *hc)
260 {
261         struct access_info *ai, *tmp;
262         list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
263                 unsigned mask = ((~0) >> ai->netmask);
264                 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
265                         return 1;
266         }
267         return 0;
268 }
269
270 static void http_post_select(__a_unused struct audio_format *af, fd_set *rfds,
271                 fd_set *wfds)
272 {
273         int i = -1, match;
274         struct http_client *hc, *tmp;
275         const char *err_msg;
276
277         list_for_each_entry_safe(hc, tmp, &clients, node) {
278                 i++;
279 //              PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
280                 switch (hc->status) {
281                 case HTTP_STREAMING: /* nothing to do */
282                 case HTTP_READY_TO_STREAM:
283                         break;
284                 case HTTP_CONNECTED: /* need to recv get request */
285                         if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
286                                 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
287                                                 < 0) {
288                                         hc->status = HTTP_INVALID_GET_REQUEST;
289                                 } else {
290                                         hc->status = HTTP_GOT_GET_REQUEST;
291                                         PARA_INFO_LOG("%s",
292                                                 "received get request\n");
293                                 }
294                         }
295                         break;
296                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
297                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
298                                 hc->status = HTTP_SENT_OK_MSG;
299                                 http_send_ok_msg(hc);
300                         }
301                         break;
302                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
303                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
304                                 if (http_send_err_msg(hc) >= 0)
305                                         http_shutdown_client(hc,
306                                                 "invalid get request");
307                         }
308                         break;
309                 case HTTP_SENT_OK_MSG: /* need to send header? */
310                         if (hc->check_w && FD_ISSET(hc->fd, wfds))
311                                 hc->status = HTTP_READY_TO_STREAM;
312                         break;
313                 }
314         }
315         if (!FD_ISSET(server_fd, rfds))
316                 return;
317         hc = para_calloc(sizeof(struct http_client));
318         err_msg = "accept error";
319         hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
320         if (hc->fd <= 0)
321                 goto err_out;
322         PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
323         if (conf.http_max_clients_arg > 0 && numclients >=
324                         conf.http_max_clients_arg) {
325                 err_msg = "server full";
326                 goto err_out;
327         }
328         match = host_in_access_perm_list(hc);
329         PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
330         if ((match && !conf.http_default_deny_given) ||
331                         (!match && conf.http_default_deny_given)) {
332                 err_msg = "permission denied";
333                 goto err_out;
334         }
335         hc->status = HTTP_CONNECTED;
336         INIT_LIST_HEAD(&hc->packet_queue);
337         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
338                 CLIENT_ADDR(hc), hc->fd);
339         numclients++;
340         list_add(&hc->node, &clients);
341         return;
342 err_out:
343         PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
344                 CLIENT_ADDR(hc), err_msg);
345         if (hc->fd > 0)
346                 close(hc->fd);
347         free(hc);
348 }
349
350 static void http_pre_select(struct audio_format *af, int *max_fileno, fd_set *rfds,
351                 fd_set *wfds)
352 {
353         struct http_client *hc, *tmp;
354
355         if (server_fd < 0)
356                 return;
357         para_fd_set(server_fd, rfds, max_fileno);
358         list_for_each_entry_safe(hc, tmp, &clients, node) {
359                 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
360                 hc->check_r = 0;
361                 hc->check_w = 0;
362                 switch (hc->status) {
363                 case HTTP_STREAMING:
364                 case HTTP_READY_TO_STREAM:
365                         break;
366                 case HTTP_CONNECTED: /* need to recv get request */
367                         para_fd_set(hc->fd, rfds, max_fileno);
368                         hc->check_r = 1;
369                         break;
370                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
371                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
372                         para_fd_set(hc->fd, wfds, max_fileno);
373                         hc->check_w = 1;
374                         break;
375                 case HTTP_SENT_OK_MSG:
376                         if (!af || !afs_playing())
377                                 break; /* wait until server starts playing */
378                         para_fd_set(hc->fd, wfds, max_fileno);
379                         hc->check_w = 1;
380                         break;
381                 }
382         }
383 }
384
385 static int open_tcp_port(int port)
386 {
387         server_fd = init_tcp_socket(port);
388         if (server_fd < 0) {
389                 http_shutdown_clients_real();
390                 self->status = SENDER_OFF;
391                 return server_fd;
392         }
393         self->status = SENDER_ON;
394         add_close_on_fork_list(server_fd);
395         return 1;
396 }
397
398 static int http_com_on(__a_unused struct sender_command_data *scd)
399 {
400         if (self->status == SENDER_ON)
401                 return 1;
402         return open_tcp_port(conf.http_port_arg);
403 }
404
405 static int http_com_off(__a_unused struct sender_command_data *scd)
406 {
407         self->status = SENDER_OFF;
408         if (server_fd > 0) {
409                 close(server_fd);
410                 del_close_on_fork_list(server_fd);
411                 server_fd = -1;
412         }
413         http_shutdown_clients_real();
414         return 1;
415 }
416
417 static void del_perm_list_entry(struct sender_command_data *scd)
418 {
419         struct access_info *ai, *tmp;
420
421         list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
422                 char *nad = para_strdup(inet_ntoa(ai->addr));
423                 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
424                                 ai->netmask == scd->netmask) {
425                         PARA_NOTICE_LOG("removing %s/%i from access list\n",
426                                 nad, ai->netmask);
427                         list_del(&ai->node);
428                         free(ai);
429                 }
430                 free(nad);
431         }
432 }
433
434 static void add_perm_list_entry(struct sender_command_data *scd)
435 {
436         struct access_info *ai = para_malloc(sizeof(struct access_info));
437         ai->addr = scd->addr;
438         ai->netmask = scd->netmask;
439         PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
440                 ai->netmask);
441         list_add(&ai->node, &access_perm_list);
442 }
443
444 static int http_com_deny(struct sender_command_data *scd)
445 {
446         if (conf.http_default_deny_given)
447                 del_perm_list_entry(scd);
448         else
449                 add_perm_list_entry(scd);
450         return 1;
451 }
452
453 static int http_com_allow(struct sender_command_data *scd)
454 {
455         if (conf.http_default_deny_given)
456                 add_perm_list_entry(scd);
457         else
458                 del_perm_list_entry(scd);
459         return 1;
460 }
461
462 static char *http_info(void)
463 {
464         char *clnts = NULL, *ap = NULL, *ret;
465         struct access_info *ai, *tmp_ai;
466         struct http_client *hc, *tmp_hc;
467
468         list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
469                 char *tmp = make_message("%s%s/%d ", ap? ap : "",
470                         inet_ntoa(ai->addr), ai->netmask);
471                 free(ap);
472                 ap = tmp;
473         }
474         list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
475                 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
476                         CLIENT_ADDR(hc), CLIENT_PORT(hc));
477                 free(clnts);
478                 clnts = tmp;
479         }
480         ret = make_message(
481                 "http status: %s\n"
482                 "http tcp port: %d\n"
483                 "http clients: %d\n"
484                 "http maximal number of clients: %d%s\n"
485                 "http connected clients: %s\n"
486                 "http access %s list: %s\n",
487                 (self->status == SENDER_ON)? "on" : "off",
488                 conf.http_port_arg,
489                 numclients,
490                 conf.http_max_clients_arg,
491                 conf.http_max_clients_arg > 0? "" : " (unlimited)",
492                 clnts? clnts : "(none)",
493                 conf.http_default_deny_given? "allow" : "deny",
494                 ap? ap : "(none)"
495         );
496         free(ap);
497         free(clnts);
498         return ret;
499 }
500
501 static void init_access_control_list(void)
502 {
503         int i;
504         struct sender_command_data scd;
505
506         INIT_LIST_HEAD(&access_perm_list);
507         for (i = 0; i < conf.http_access_given; i++) {
508                 char *arg = para_strdup(conf.http_access_arg[i]);
509                 char *p = strchr(arg, '/');
510                 if (!p)
511                         goto err;
512                 *p = '\0';
513                 if (!inet_aton(arg, &scd.addr))
514                         goto err;
515                 scd.netmask = atoi(++p);
516                 if (scd.netmask < 0 || scd.netmask > 32)
517                         goto err;
518                 add_perm_list_entry(&scd);
519                 goto success;
520 err:
521                 PARA_CRIT_LOG("syntax error for http_access option "
522                         "#%d, ignoring\n", i);
523 success:
524                 free(arg);
525                 continue;
526         }
527 }
528
529 static char *http_help(void)
530 {
531         return make_message(
532                 "usage: {on|off}\n"
533                 "usage: {allow|deny} IP mask\n"
534                 "example: allow 127.0.0.1 32\n"
535         );
536 }
537
538 /**
539  * the init function of the http sender
540  *
541  * \param s pointer to the http sender struct
542  *
543  * It initializes all function pointers of \a s, init the client list and the
544  * acess control list as well. If autostart is wanted, open the tcp port.
545  */
546 void http_send_init(struct sender *s)
547 {
548         INIT_LIST_HEAD(&clients);
549         s->info = http_info;
550         s->send = http_send;
551         s->pre_select = http_pre_select;
552         s->post_select = http_post_select;
553         s->shutdown_clients = http_shutdown_clients;
554         s->help = http_help;
555         s->client_cmds[SENDER_ON] = http_com_on;
556         s->client_cmds[SENDER_OFF] = http_com_off;
557         s->client_cmds[SENDER_DENY] = http_com_deny;
558         s->client_cmds[SENDER_ALLOW] = http_com_allow;
559         s->client_cmds[SENDER_ADD] = NULL;
560         s->client_cmds[SENDER_DELETE] = NULL;
561         self = s;
562         init_access_control_list();
563         if (!conf.http_no_autostart_given)
564                 open_tcp_port(conf.http_port_arg); /* ignore errors */
565         PARA_DEBUG_LOG("%s", "http sender init complete\n");
566 }