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