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