]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_send.c
http_send: shutdown clients even if status is not yet HTTP_STREAMING
[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 }
128
129 static void http_shutdown_clients_real(void)
130 {
131         struct http_client *hc, *tmp;
132         list_for_each_entry_safe(hc, tmp, &clients, node)
133                 http_shutdown_client(hc, "afs request");
134 }
135 static void http_shutdown_clients(void)
136 {
137         struct http_client *hc, *tmp;
138         list_for_each_entry_safe(hc, tmp, &clients, node)
139                 http_shutdown_client(hc, "afs request");
140 }
141
142 static int http_send_msg(struct http_client *hc, const char *msg)
143 {
144         int ret = send_buffer(hc->fd, msg);
145
146         if (ret < 0)
147                 http_shutdown_client(hc, "send msg failed");
148         return ret;
149 }
150
151 static void http_send_ok_msg(struct http_client *hc)
152 {
153         PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
154         http_send_msg(hc, HTTP_OK_MSG);
155 }
156
157 static int http_send_err_msg(struct http_client *hc)
158 {
159         PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
160         return http_send_msg(hc, HTTP_ERR_MSG);
161 }
162
163 static int queue_packet(struct http_client *hc, const char *buf, size_t len)
164 {
165         struct queued_packet *qp;
166         if (hc->pq_bytes + len > MAX_BACKLOG) {
167                 http_shutdown_client(hc, "packet queue overrun");
168                 return -E_QUEUE;
169         }
170         qp = para_malloc(sizeof(struct queued_packet));
171         hc->pq_bytes += len;
172         qp->packet = para_malloc(len);
173         memcpy(qp->packet, buf, len);
174         qp->len = len;
175         list_add_tail(&qp->node, &hc->packet_queue);
176         PARA_INFO_LOG("%lu bytes queued for fd %d\n", hc->pq_bytes, hc->fd);
177         return 1;
178 }
179
180 static int send_queued_packets(struct http_client *hc)
181 {
182         int ret;
183         struct queued_packet *qp, *tmp;
184
185         if (list_empty(&hc->packet_queue))
186                 return 1;
187         list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
188                 ret = write_ok(hc->fd);
189                 if (ret <= 0)
190                         return ret? -E_WRITE_OK : 0;
191                 ret = write(hc->fd, qp->packet, qp->len);
192                 if (ret < 0)
193                         return ret;
194                 if (ret != qp->len) {
195                         qp->len -= ret;
196                         memmove(qp->packet, qp->packet + ret, qp->len);
197                         return 0;
198                 }
199                 hc->pq_bytes -= qp->len;
200                 free(qp->packet);
201                 list_del(&qp->node);
202                 free(qp);
203         }
204         return 1;
205 }
206
207 static void http_send( long unsigned current_chunk,
208         __a_unused long unsigned chunks_sent, const char *buf, size_t len)
209 {
210         struct http_client *hc, *tmp;
211         int ret;
212
213         list_for_each_entry_safe(hc, tmp, &clients, node) {
214                 if (hc->status != HTTP_STREAMING &&
215                                 hc->status != HTTP_READY_TO_STREAM)
216                         continue;
217                 if (hc->status == HTTP_READY_TO_STREAM) {
218                         int hlen;
219                         char *buf = afs_get_header(&hlen);
220                         if (buf && hlen > 0 && current_chunk) {
221                                 /* need to send header */
222                                 PARA_INFO_LOG("queueing header: %d\n", hlen);
223                                 if (queue_packet(hc, buf, hlen) < 0)
224                                         continue;
225                         } else
226                                 PARA_INFO_LOG("%s", "no need to queue header\n");
227                         hc->status = HTTP_STREAMING;
228                 }
229                 ret = send_queued_packets(hc);
230                 if (ret < 0) {
231                         http_shutdown_client(hc, "send error");
232                         continue;
233                 }
234                 if (!len)
235                         continue;
236                 if (!ret || write_ok(hc->fd) <= 0) {
237                         PARA_INFO_LOG("fd %d not ready (%lu bytes queued),"
238                                 " trying to queue packet\n", hc->fd,
239                                 hc->pq_bytes);
240                         queue_packet(hc, buf, len);
241                         continue;
242                 }
243 //              PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
244                 ret = write(hc->fd, buf, len);
245                 if (ret < 0) {
246                         http_shutdown_client(hc, "send error");
247                         continue;
248                 }
249                 if (ret != len)
250                         queue_packet(hc, buf + ret, len - ret);
251         }
252 }
253
254 static int host_in_access_perm_list(struct http_client *hc)
255 {
256         struct access_info *ai, *tmp;
257         list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
258                 unsigned mask = ((~0) >> ai->netmask);
259                 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
260                         return 1;
261         }
262         return 0;
263 }
264
265 static void http_post_select(fd_set *rfds, fd_set *wfds)
266 {
267         int i = -1, match;
268         struct http_client *hc, *tmp;
269         const char *err_msg;
270
271         list_for_each_entry_safe(hc, tmp, &clients, node) {
272                 i++;
273 //              PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
274                 switch (hc->status) {
275                 case HTTP_STREAMING: /* nothing to do */
276                 case HTTP_READY_TO_STREAM:
277                         break;
278                 case HTTP_CONNECTED: /* need to recv get request */
279                         if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
280                                 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
281                                                 < 0) {
282                                         hc->status = HTTP_INVALID_GET_REQUEST;
283                                 } else {
284                                         hc->status = HTTP_GOT_GET_REQUEST;
285                                         PARA_INFO_LOG("%s",
286                                                 "received get request\n");
287                                 }
288                         }
289                         break;
290                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
291                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
292                                 hc->status = HTTP_SENT_OK_MSG;
293                                 http_send_ok_msg(hc);
294                         }
295                         break;
296                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
297                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
298                                 if (http_send_err_msg(hc) >= 0)
299                                         http_shutdown_client(hc,
300                                                 "invalid get request");
301                         }
302                         break;
303                 case HTTP_SENT_OK_MSG: /* need to send header? */
304                         if (hc->check_w && FD_ISSET(hc->fd, wfds))
305                                 hc->status = HTTP_READY_TO_STREAM;
306                         break;
307                 }
308         }
309         if (!FD_ISSET(server_fd, rfds))
310                 return;
311         hc = para_calloc(sizeof(struct http_client));
312         err_msg = "accept error";
313         hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
314         if (hc->fd <= 0)
315                 goto err_out;
316         PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
317         if (conf.http_max_clients_arg > 0 && numclients >=
318                         conf.http_max_clients_arg) {
319                 err_msg = "server full";
320                 goto err_out;
321         }
322         match = host_in_access_perm_list(hc);
323         PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
324         if ((match && !conf.http_default_deny_given) ||
325                         (!match && conf.http_default_deny_given)) {
326                 err_msg = "permission denied";
327                 goto err_out;
328         }
329         hc->status = HTTP_CONNECTED;
330         INIT_LIST_HEAD(&hc->packet_queue);
331         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
332                 CLIENT_ADDR(hc), hc->fd);
333         numclients++;
334         list_add(&hc->node, &clients);
335         return;
336 err_out:
337         PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
338                 CLIENT_ADDR(hc), err_msg);
339         if (hc->fd > 0)
340                 close(hc->fd);
341         free(hc);
342 }
343
344 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
345 {
346         struct http_client *hc, *tmp;
347
348         if (server_fd < 0)
349                 return;
350         para_fd_set(server_fd, rfds, max_fileno);
351         list_for_each_entry_safe(hc, tmp, &clients, node) {
352                 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
353                 hc->check_r = 0;
354                 hc->check_w = 0;
355                 switch (hc->status) {
356                 case HTTP_STREAMING:
357                 case HTTP_READY_TO_STREAM:
358                         break;
359                 case HTTP_CONNECTED: /* need to recv get request */
360                         para_fd_set(hc->fd, rfds, max_fileno);
361                         hc->check_r = 1;
362                         break;
363                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
364                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
365                         para_fd_set(hc->fd, wfds, max_fileno);
366                         hc->check_w = 1;
367                         break;
368                 case HTTP_SENT_OK_MSG:
369                         if (!afs_playing())
370                                 break; /* wait until server starts playing */
371                         para_fd_set(hc->fd, wfds, max_fileno);
372                         hc->check_w = 1;
373                         break;
374                 }
375         }
376 }
377
378 static int open_tcp_port(int port)
379 {
380         server_fd = init_tcp_socket(port);
381         if (server_fd < 0) {
382                 http_shutdown_clients_real();
383                 self->status = SENDER_OFF;
384                 return server_fd;
385         }
386         self->status = SENDER_ON;
387         add_close_on_fork_list(server_fd);
388         return 1;
389 }
390
391 static int http_com_on(__a_unused struct sender_command_data *scd)
392 {
393         if (self->status == SENDER_ON)
394                 return 1;
395         return open_tcp_port(conf.http_port_arg);
396 }
397
398 static int http_com_off(__a_unused struct sender_command_data *scd)
399 {
400         self->status = SENDER_OFF;
401         if (server_fd > 0) {
402                 close(server_fd);
403                 del_close_on_fork_list(server_fd);
404                 server_fd = -1;
405         }
406         http_shutdown_clients_real();
407         return 1;
408 }
409
410 static void del_perm_list_entry(struct sender_command_data *scd)
411 {
412         struct access_info *ai, *tmp;
413
414         list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
415                 char *nad = para_strdup(inet_ntoa(ai->addr));
416                 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
417                                 ai->netmask == scd->netmask) {
418                         PARA_NOTICE_LOG("removing %s/%i from access list\n",
419                                 nad, ai->netmask);
420                         list_del(&ai->node);
421                         free(ai);
422                 }
423                 free(nad);
424         }
425 }
426
427 static void add_perm_list_entry(struct sender_command_data *scd)
428 {
429         struct access_info *ai = para_malloc(sizeof(struct access_info));
430         ai->addr = scd->addr;
431         ai->netmask = scd->netmask;
432         PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
433                 ai->netmask);
434         list_add(&ai->node, &access_perm_list);
435 }
436
437 static int http_com_deny(struct sender_command_data *scd)
438 {
439         if (conf.http_default_deny_given)
440                 del_perm_list_entry(scd);
441         else
442                 add_perm_list_entry(scd);
443         return 1;
444 }
445
446 static int http_com_allow(struct sender_command_data *scd)
447 {
448         if (conf.http_default_deny_given)
449                 add_perm_list_entry(scd);
450         else
451                 del_perm_list_entry(scd);
452         return 1;
453 }
454
455 static char *http_info(void)
456 {
457         char *clnts = NULL, *ap = NULL, *ret;
458         struct access_info *ai, *tmp_ai;
459         struct http_client *hc, *tmp_hc;
460
461         list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
462                 char *tmp = make_message("%s%s/%d ", ap? ap : "",
463                         inet_ntoa(ai->addr), ai->netmask);
464                 free(ap);
465                 ap = tmp;
466         }
467         list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
468                 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
469                         CLIENT_ADDR(hc), CLIENT_PORT(hc));
470                 free(clnts);
471                 clnts = tmp;
472         }
473         ret = make_message(
474                 "http status: %s\n"
475                 "http tcp port: %d\n"
476                 "http clients: %d\n"
477                 "http maximal number of clients: %d%s\n"
478                 "http connected clients: %s\n"
479                 "http access %s list: %s\n",
480                 (self->status == SENDER_ON)? "on" : "off",
481                 conf.http_port_arg,
482                 numclients,
483                 conf.http_max_clients_arg,
484                 conf.http_max_clients_arg > 0? "" : " (unlimited)",
485                 clnts? clnts : "(none)",
486                 conf.http_default_deny_given? "allow" : "deny",
487                 ap? ap : "(none)"
488         );
489         free(ap);
490         free(clnts);
491         return ret;
492 }
493
494 static void init_access_control_list(void)
495 {
496         int i;
497         struct sender_command_data scd;
498
499         INIT_LIST_HEAD(&access_perm_list);
500         for (i = 0; i < conf.http_access_given; i++) {
501                 char *arg = para_strdup(conf.http_access_arg[i]);
502                 char *p = strchr(arg, '/');
503                 if (!p)
504                         goto err;
505                 *p = '\0';
506                 if (!inet_aton(arg, &scd.addr))
507                         goto err;
508                 scd.netmask = atoi(++p);
509                 if (scd.netmask < 0 || scd.netmask > 32)
510                         goto err;
511                 add_perm_list_entry(&scd);
512                 goto success;
513 err:
514                 PARA_CRIT_LOG("syntax error for http_access option "
515                         "#%d, ignoring\n", i);
516 success:
517                 free(arg);
518                 continue;
519         }
520 }
521
522 static char *http_help(void)
523 {
524         return make_message(
525                 "usage: {on|off}\n"
526                 "usage: {allow|deny} IP mask\n"
527                 "example: allow 127.0.0.1 32\n"
528         );
529 }
530
531 /**
532  * the init function of the http sender
533  *
534  * \param s pointer to the http sender struct
535  *
536  * It initializes all function pointers of \a s, init the client list and the
537  * acess control list as well. If autostart is wanted, open the tcp port.
538  */
539 void http_send_init(struct sender *s)
540 {
541         INIT_LIST_HEAD(&clients);
542         s->info = http_info;
543         s->send = http_send;
544         s->pre_select = http_pre_select;
545         s->post_select = http_post_select;
546         s->shutdown_clients = http_shutdown_clients;
547         s->help = http_help;
548         s->client_cmds[SENDER_ON] = http_com_on;
549         s->client_cmds[SENDER_OFF] = http_com_off;
550         s->client_cmds[SENDER_DENY] = http_com_deny;
551         s->client_cmds[SENDER_ALLOW] = http_com_allow;
552         s->client_cmds[SENDER_ADD] = NULL;
553         s->client_cmds[SENDER_DELETE] = NULL;
554         self = s;
555         init_access_control_list();
556         if (!conf.http_no_autostart_given)
557                 open_tcp_port(conf.http_port_arg); /* ignore errors */
558         PARA_DEBUG_LOG("%s", "http sender init complete\n");
559 }