]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_send.c
Abstract out chunk_queue code.
[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         /** We accepted the connection on the tcp socket. */
32         HTTP_CONNECTED,
33         /** Successfully received the get request. */
34         HTTP_GOT_GET_REQUEST,
35         /** We sent the OK message back to the client. */
36         HTTP_SENT_OK_MSG,
37         /** Connection established, we might need to send the audio file header. */
38         HTTP_READY_TO_STREAM,
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 access_perm_list;
51
52 struct chunk_queue{
53         /** The list of pending chunks for this client. */
54         struct list_head q;
55         /** The number of pending bytes for this client. */
56         unsigned long num_pending;
57         unsigned long max_pending;
58 };
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 chunks for this client. */
75         struct chunk_queue cq;
76 };
77
78 /**
79  * Describes one queued chunk of the chunk queue.
80  *
81  * The send function of the http sender checks each client fd for writing. If a
82  * client fd is not ready, it tries to queue that chunk for this client until
83  * the number of queued bytes exceeds \p MAX_BACKLOG.
84  */
85 struct queued_chunk {
86         /** The number of the queued chunk, -1U means header. */
87         unsigned chunk_num;
88         /** The number of bytes already sent. */
89         unsigned sent;
90         /** Position of the chunk in the chunk queue. */
91         struct list_head node;
92 };
93
94 /**
95  * Describes one entry in the blacklist/whitelist of the http sender.
96  */
97 struct access_info {
98         /** The address to be black/whitelisted. */
99         struct in_addr addr;
100         /** The netmask for this entry. */
101         unsigned netmask;
102         /** The position of this entry in the access_perm_list. */
103         struct list_head node;
104 };
105
106 static int server_fd = -1, numclients;
107 static struct sender *self;
108
109
110 static int cq_enqueue(struct chunk_queue *cq, long unsigned chunk_num,
111         size_t sent)
112 {
113         struct queued_chunk *qc;
114         char *buf;
115         size_t len;
116         int ret;
117
118         if (chunk_num != -1U) {
119                 ret = vss_get_chunk(chunk_num, &buf, &len);
120                 if (ret < 0)
121                         return ret;
122         } else
123                 buf = vss_get_header(&len);
124         if (cq->num_pending + len > cq->max_pending)
125                 return -E_QUEUE;
126         qc = para_malloc(sizeof(struct queued_chunk));
127         cq->num_pending += len;
128         qc->chunk_num = chunk_num;
129         qc->sent = sent;
130         list_add_tail(&qc->node, &cq->q);
131         PARA_DEBUG_LOG("%lu bytes queued for %p\n", cq->num_pending, &cq->q);
132         return 1;
133 }
134
135 static struct queued_chunk *cq_peek(struct chunk_queue *cq)
136 {
137         if (list_empty(&cq->q))
138                 return NULL;
139         return list_entry(cq->q.next, struct queued_chunk, node);
140 }
141
142 int cq_dequeue(struct chunk_queue *cq)
143 {
144         struct queued_chunk *qc = cq_peek(cq);
145         assert(qc);
146         list_del(&qc->node);
147         free(qc);
148         return 1;
149 }
150
151 void cq_update(struct chunk_queue *cq, size_t sent)
152 {
153         struct queued_chunk *qc = cq_peek(cq);
154         assert(qc);
155         qc->sent += sent;
156         cq->num_pending -= sent;
157 }
158
159 int cq_get(struct queued_chunk *qc, char **buf, size_t *len)
160 {
161         int ret;
162
163         if (qc->chunk_num != -1U) {
164                 ret = vss_get_chunk(qc->chunk_num, buf, len);
165                 if (ret < 0)
166                         return ret;
167         } else
168                 *buf = vss_get_header(len);
169         assert(*len > qc->sent);
170         *buf += qc->sent;
171         *len -= qc->sent;
172         return 1;
173 }
174
175 void cq_init(struct chunk_queue *cq, size_t max_pending)
176 {
177         INIT_LIST_HEAD(&cq->q);
178         cq->max_pending = max_pending;
179         cq->num_pending = 0;
180 }
181
182 void cq_destroy(struct chunk_queue *cq)
183 {
184         struct queued_chunk *qc, *tmp;
185         list_for_each_entry_safe(qc, tmp, &cq->q, node) {
186                 list_del(&qc->node);
187                 free(qc);
188         }
189 }
190
191 static void http_shutdown_client(struct http_client *hc, const char *msg)
192 {
193         PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
194                 hc->fd, msg);
195         numclients--;
196         close(hc->fd);
197         del_close_on_fork_list(hc->fd);
198         cq_destroy(&hc->cq);
199         list_del(&hc->node);
200         free(hc);
201 }
202
203 static void http_shutdown_clients(void)
204 {
205         struct http_client *hc, *tmp;
206         list_for_each_entry_safe(hc, tmp, &clients, node)
207                 http_shutdown_client(hc, "vss request");
208 }
209
210 static int http_send_msg(struct http_client *hc, const char *msg)
211 {
212         int ret = send_buffer(hc->fd, msg);
213
214         if (ret < 0)
215                 http_shutdown_client(hc, "send msg failed");
216         return ret;
217 }
218
219 static void http_send_ok_msg(struct http_client *hc)
220 {
221         PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
222         http_send_msg(hc, HTTP_OK_MSG);
223 }
224
225 static int http_send_err_msg(struct http_client *hc)
226 {
227         PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
228         return http_send_msg(hc, HTTP_ERR_MSG);
229 }
230
231
232 static int send_queued_chunks(struct http_client *hc)
233 {
234         struct queued_chunk *qc;
235         while ((qc = cq_peek(&hc->cq))) {
236                 char *buf;
237                 size_t len;
238                 int ret = write_ok(hc->fd);
239                 if (ret <= 0)
240                         return ret? -E_WRITE_OK : 0;
241                 cq_get(qc, &buf, &len);
242                 ret = write(hc->fd, buf, len);
243                 if (ret < 0)
244                         return -1; /* FIXME */
245                 cq_update(&hc->cq, ret);
246                 if (ret != len)
247                         return 1;
248                 cq_dequeue(&hc->cq);
249         }
250         return 1;
251 }
252
253 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
254         size_t sent)
255 {
256         int ret = cq_enqueue(&hc->cq, chunk_num, sent);
257         if (ret < 0)
258                 http_shutdown_client(hc, "queue error");
259         return ret;
260 }
261
262 static void http_send( long unsigned current_chunk,
263         __a_unused long unsigned chunks_sent, const char *buf, size_t len)
264 {
265         struct http_client *hc, *tmp;
266         int ret;
267
268         list_for_each_entry_safe(hc, tmp, &clients, node) {
269                 if (hc->status != HTTP_STREAMING &&
270                                 hc->status != HTTP_READY_TO_STREAM)
271                         continue;
272                 if (hc->status == HTTP_READY_TO_STREAM) {
273                         unsigned hlen;
274                         char *hbuf = vss_get_header(&hlen);
275                         if (hbuf && hlen > 0 && current_chunk) {
276                                 /* need to send header */
277                                 PARA_INFO_LOG("queueing header: %d\n", hlen);
278                                 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
279                                         continue;
280                         } else
281                                 PARA_INFO_LOG("no need to queue header\n");
282                         hc->status = HTTP_STREAMING;
283                 }
284                 ret = send_queued_chunks(hc);
285                 if (ret < 0) {
286                         http_shutdown_client(hc, "queue send error");
287                         continue;
288                 }
289                 if (!len)
290                         continue;
291                 if (!ret || write_ok(hc->fd) <= 0) {
292                         queue_chunk_or_shutdown(hc, current_chunk, 0);
293                         continue;
294                 }
295 //              PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
296                 ret = write(hc->fd, buf, len);
297 //              PARA_DEBUG_LOG("ret: %d\n", ret);
298                 if (ret < 0) {
299                         http_shutdown_client(hc, "send error");
300                         continue;
301                 }
302                 if (ret != len)
303                         queue_chunk_or_shutdown(hc, current_chunk, ret);
304         }
305 }
306
307 static int host_in_access_perm_list(struct http_client *hc)
308 {
309         struct access_info *ai, *tmp;
310         list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
311                 unsigned mask = ((~0U) >> ai->netmask);
312                 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
313                         return 1;
314         }
315         return 0;
316 }
317
318 static void http_post_select(fd_set *rfds, fd_set *wfds)
319 {
320         int i = -1, match;
321         struct http_client *hc, *tmp;
322         const char *err_msg;
323
324         list_for_each_entry_safe(hc, tmp, &clients, node) {
325                 i++;
326 //              PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
327                 switch (hc->status) {
328                 case HTTP_STREAMING: /* nothing to do */
329                 case HTTP_READY_TO_STREAM:
330                         break;
331                 case HTTP_CONNECTED: /* need to recv get request */
332                         if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
333                                 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
334                                                 < 0) {
335                                         hc->status = HTTP_INVALID_GET_REQUEST;
336                                 } else {
337                                         hc->status = HTTP_GOT_GET_REQUEST;
338                                         PARA_INFO_LOG("%s",
339                                                 "received get request\n");
340                                 }
341                         }
342                         break;
343                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
344                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
345                                 hc->status = HTTP_SENT_OK_MSG;
346                                 http_send_ok_msg(hc);
347                         }
348                         break;
349                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
350                         if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
351                                 if (http_send_err_msg(hc) >= 0)
352                                         http_shutdown_client(hc,
353                                                 "invalid get request");
354                         }
355                         break;
356                 case HTTP_SENT_OK_MSG: /* need to send header? */
357                         if (hc->check_w && FD_ISSET(hc->fd, wfds))
358                                 hc->status = HTTP_READY_TO_STREAM;
359                         break;
360                 }
361         }
362         if (!FD_ISSET(server_fd, rfds))
363                 return;
364         hc = para_calloc(sizeof(struct http_client));
365         err_msg = "accept error";
366         hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
367         if (hc->fd <= 0)
368                 goto err_out;
369         PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
370         if (conf.http_max_clients_arg > 0 && numclients >=
371                         conf.http_max_clients_arg) {
372                 err_msg = "server full";
373                 goto err_out;
374         }
375         match = host_in_access_perm_list(hc);
376         PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
377         if ((match && !conf.http_default_deny_given) ||
378                         (!match && conf.http_default_deny_given)) {
379                 err_msg = "permission denied";
380                 goto err_out;
381         }
382         hc->status = HTTP_CONNECTED;
383         cq_init(&hc->cq, MAX_BACKLOG);
384         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
385                 CLIENT_ADDR(hc), hc->fd);
386         numclients++;
387         para_list_add(&hc->node, &clients);
388         add_close_on_fork_list(hc->fd);
389         mark_fd_nonblock(hc->fd);
390         return;
391 err_out:
392         PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
393                 CLIENT_ADDR(hc), err_msg);
394         if (hc->fd > 0)
395                 close(hc->fd);
396         free(hc);
397 }
398
399 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
400 {
401         struct http_client *hc, *tmp;
402
403         if (server_fd < 0)
404                 return;
405         para_fd_set(server_fd, rfds, max_fileno);
406         list_for_each_entry_safe(hc, tmp, &clients, node) {
407                 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
408                 hc->check_r = 0;
409                 hc->check_w = 0;
410                 switch (hc->status) {
411                 case HTTP_STREAMING:
412                 case HTTP_READY_TO_STREAM:
413                         break;
414                 case HTTP_CONNECTED: /* need to recv get request */
415                         para_fd_set(hc->fd, rfds, max_fileno);
416                         hc->check_r = 1;
417                         break;
418                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
419                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
420                         para_fd_set(hc->fd, wfds, max_fileno);
421                         hc->check_w = 1;
422                         break;
423                 case HTTP_SENT_OK_MSG:
424                         if (!vss_playing())
425                                 break; /* wait until server starts playing */
426                         para_fd_set(hc->fd, wfds, max_fileno);
427                         hc->check_w = 1;
428                         break;
429                 }
430         }
431 }
432
433 static int open_tcp_port(int port)
434 {
435         int ret;
436
437         server_fd = init_tcp_socket(port);
438         if (server_fd < 0) {
439                 http_shutdown_clients();
440                 self->status = SENDER_OFF;
441                 return server_fd;
442         }
443         ret = mark_fd_nonblock(server_fd);
444         if (ret < 0) {
445                 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
446                 exit(EXIT_FAILURE);
447         }
448         self->status = SENDER_ON;
449         add_close_on_fork_list(server_fd);
450         return 1;
451 }
452
453 static int http_com_on(__a_unused struct sender_command_data *scd)
454 {
455         if (self->status == SENDER_ON)
456                 return 1;
457         return open_tcp_port(conf.http_port_arg);
458 }
459
460 static int http_com_off(__a_unused struct sender_command_data *scd)
461 {
462         self->status = SENDER_OFF;
463         if (server_fd > 0) {
464                 close(server_fd);
465                 del_close_on_fork_list(server_fd);
466                 server_fd = -1;
467         }
468         http_shutdown_clients();
469         return 1;
470 }
471
472 static void del_perm_list_entry(struct sender_command_data *scd)
473 {
474         struct access_info *ai, *tmp;
475
476         list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
477                 char *nad = para_strdup(inet_ntoa(ai->addr));
478                 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
479                                 ai->netmask == scd->netmask) {
480                         PARA_NOTICE_LOG("removing %s/%i from access list\n",
481                                 nad, ai->netmask);
482                         list_del(&ai->node);
483                         free(ai);
484                 }
485                 free(nad);
486         }
487 }
488
489 static void add_perm_list_entry(struct sender_command_data *scd)
490 {
491         struct access_info *ai = para_malloc(sizeof(struct access_info));
492         ai->addr = scd->addr;
493         ai->netmask = scd->netmask;
494         PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
495                 ai->netmask);
496         para_list_add(&ai->node, &access_perm_list);
497 }
498
499 static int http_com_deny(struct sender_command_data *scd)
500 {
501         if (conf.http_default_deny_given)
502                 del_perm_list_entry(scd);
503         else
504                 add_perm_list_entry(scd);
505         return 1;
506 }
507
508 static int http_com_allow(struct sender_command_data *scd)
509 {
510         if (conf.http_default_deny_given)
511                 add_perm_list_entry(scd);
512         else
513                 del_perm_list_entry(scd);
514         return 1;
515 }
516
517 static char *http_info(void)
518 {
519         char *clnts = NULL, *ap = NULL, *ret;
520         struct access_info *ai, *tmp_ai;
521         struct http_client *hc, *tmp_hc;
522
523         list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
524                 char *tmp = make_message("%s%s/%d ", ap? ap : "",
525                         inet_ntoa(ai->addr), ai->netmask);
526                 free(ap);
527                 ap = tmp;
528         }
529         list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
530                 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
531                         CLIENT_ADDR(hc), CLIENT_PORT(hc));
532                 free(clnts);
533                 clnts = tmp;
534         }
535         ret = make_message(
536                 "http status: %s\n"
537                 "http tcp port: %d\n"
538                 "http clients: %d\n"
539                 "http maximal number of clients: %d%s\n"
540                 "http connected clients: %s\n"
541                 "http access %s list: %s\n",
542                 (self->status == SENDER_ON)? "on" : "off",
543                 conf.http_port_arg,
544                 numclients,
545                 conf.http_max_clients_arg,
546                 conf.http_max_clients_arg > 0? "" : " (unlimited)",
547                 clnts? clnts : "(none)",
548                 conf.http_default_deny_given? "allow" : "deny",
549                 ap? ap : "(none)"
550         );
551         free(ap);
552         free(clnts);
553         return ret;
554 }
555
556 static void init_access_control_list(void)
557 {
558         int i;
559         struct sender_command_data scd;
560
561         INIT_LIST_HEAD(&access_perm_list);
562         for (i = 0; i < conf.http_access_given; i++) {
563                 char *arg = para_strdup(conf.http_access_arg[i]);
564                 char *p = strchr(arg, '/');
565                 if (!p)
566                         goto err;
567                 *p = '\0';
568                 if (!inet_aton(arg, &scd.addr))
569                         goto err;
570                 scd.netmask = atoi(++p);
571                 if (scd.netmask < 0 || scd.netmask > 32)
572                         goto err;
573                 add_perm_list_entry(&scd);
574                 goto success;
575 err:
576                 PARA_CRIT_LOG("syntax error for http_access option "
577                         "#%d, ignoring\n", i);
578 success:
579                 free(arg);
580                 continue;
581         }
582 }
583
584 static char *http_help(void)
585 {
586         return make_message(
587                 "usage: {on|off}\n"
588                 "usage: {allow|deny} IP mask\n"
589                 "example: allow 127.0.0.1 32\n"
590         );
591 }
592
593 /**
594  * The init function of the http sender.
595  *
596  * \param s Pointer to the http sender struct.
597  *
598  * It initializes all function pointers of \a s, the client list and the access
599  * control list. If the autostart option was given, the tcp port is opened.
600  */
601 void http_send_init(struct sender *s)
602 {
603         INIT_LIST_HEAD(&clients);
604         s->info = http_info;
605         s->send = http_send;
606         s->pre_select = http_pre_select;
607         s->post_select = http_post_select;
608         s->shutdown_clients = http_shutdown_clients;
609         s->help = http_help;
610         s->client_cmds[SENDER_ON] = http_com_on;
611         s->client_cmds[SENDER_OFF] = http_com_off;
612         s->client_cmds[SENDER_DENY] = http_com_deny;
613         s->client_cmds[SENDER_ALLOW] = http_com_allow;
614         s->client_cmds[SENDER_ADD] = NULL;
615         s->client_cmds[SENDER_DELETE] = NULL;
616         self = s;
617         init_access_control_list();
618         if (!conf.http_no_autostart_given)
619                 open_tcp_port(conf.http_port_arg); /* ignore errors */
620         PARA_DEBUG_LOG("%s", "http sender init complete\n");
621 }