2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file http_send.c paraslash's http sender */
10 #include "server.cmdline.h"
16 #include "close_on_fork.h"
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"
29 /** The possible states of a client from the server's POV. */
31 /** We accepted the connection on the tcp socket. */
33 /** Successfully received the get request. */
35 /** We sent the OK message back to the client. */
37 /** Connection established, we might need to send the audio file header. */
39 /** Connection is ready for sending audio data. */
41 /** We didn't receive a valid get request. */
42 HTTP_INVALID_GET_REQUEST
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;
53 /** The list of pending chunks for this client. */
55 /** The number of pending bytes for this client. */
56 unsigned long num_pending;
57 unsigned long max_pending;
60 /** Describes one client that connected the tcp port of the http sender. */
62 /** The file descriptor of the client. */
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.*/
70 /** Non-zero if we included \a fd in the write set. */
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;
79 * Describes one queued chunk of the chunk queue.
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.
86 /** The number of the queued chunk, -1U means header. */
88 /** The number of bytes already sent. */
90 /** Position of the chunk in the chunk queue. */
91 struct list_head node;
95 * Describes one entry in the blacklist/whitelist of the http sender.
98 /** The address to be black/whitelisted. */
100 /** The netmask for this entry. */
102 /** The position of this entry in the access_perm_list. */
103 struct list_head node;
106 static int server_fd = -1, numclients;
107 static struct sender *self;
110 static int cq_enqueue(struct chunk_queue *cq, long unsigned chunk_num,
113 struct queued_chunk *qc;
118 if (chunk_num != -1U) {
119 ret = vss_get_chunk(chunk_num, &buf, &len);
123 buf = vss_get_header(&len);
124 if (cq->num_pending + len > cq->max_pending)
126 qc = para_malloc(sizeof(struct queued_chunk));
127 cq->num_pending += len;
128 qc->chunk_num = chunk_num;
130 list_add_tail(&qc->node, &cq->q);
131 PARA_DEBUG_LOG("%lu bytes queued for %p\n", cq->num_pending, &cq->q);
135 static struct queued_chunk *cq_peek(struct chunk_queue *cq)
137 if (list_empty(&cq->q))
139 return list_entry(cq->q.next, struct queued_chunk, node);
142 int cq_dequeue(struct chunk_queue *cq)
144 struct queued_chunk *qc = cq_peek(cq);
151 void cq_update(struct chunk_queue *cq, size_t sent)
153 struct queued_chunk *qc = cq_peek(cq);
156 cq->num_pending -= sent;
159 int cq_get(struct queued_chunk *qc, char **buf, size_t *len)
163 if (qc->chunk_num != -1U) {
164 ret = vss_get_chunk(qc->chunk_num, buf, len);
168 *buf = vss_get_header(len);
169 assert(*len > qc->sent);
175 void cq_init(struct chunk_queue *cq, size_t max_pending)
177 INIT_LIST_HEAD(&cq->q);
178 cq->max_pending = max_pending;
182 void cq_destroy(struct chunk_queue *cq)
184 struct queued_chunk *qc, *tmp;
185 list_for_each_entry_safe(qc, tmp, &cq->q, node) {
191 static void http_shutdown_client(struct http_client *hc, const char *msg)
193 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
197 del_close_on_fork_list(hc->fd);
203 static void http_shutdown_clients(void)
205 struct http_client *hc, *tmp;
206 list_for_each_entry_safe(hc, tmp, &clients, node)
207 http_shutdown_client(hc, "vss request");
210 static int http_send_msg(struct http_client *hc, const char *msg)
212 int ret = send_buffer(hc->fd, msg);
215 http_shutdown_client(hc, "send msg failed");
219 static void http_send_ok_msg(struct http_client *hc)
221 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
222 http_send_msg(hc, HTTP_OK_MSG);
225 static int http_send_err_msg(struct http_client *hc)
227 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
228 return http_send_msg(hc, HTTP_ERR_MSG);
232 static int send_queued_chunks(struct http_client *hc)
234 struct queued_chunk *qc;
235 while ((qc = cq_peek(&hc->cq))) {
238 int ret = write_ok(hc->fd);
240 return ret? -E_WRITE_OK : 0;
241 cq_get(qc, &buf, &len);
242 ret = write(hc->fd, buf, len);
244 return -1; /* FIXME */
245 cq_update(&hc->cq, ret);
253 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
256 int ret = cq_enqueue(&hc->cq, chunk_num, sent);
258 http_shutdown_client(hc, "queue error");
262 static void http_send( long unsigned current_chunk,
263 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
265 struct http_client *hc, *tmp;
268 list_for_each_entry_safe(hc, tmp, &clients, node) {
269 if (hc->status != HTTP_STREAMING &&
270 hc->status != HTTP_READY_TO_STREAM)
272 if (hc->status == HTTP_READY_TO_STREAM) {
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)
281 PARA_INFO_LOG("no need to queue header\n");
282 hc->status = HTTP_STREAMING;
284 ret = send_queued_chunks(hc);
286 http_shutdown_client(hc, "queue send error");
291 if (!ret || write_ok(hc->fd) <= 0) {
292 queue_chunk_or_shutdown(hc, current_chunk, 0);
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);
299 http_shutdown_client(hc, "send error");
303 queue_chunk_or_shutdown(hc, current_chunk, ret);
307 static int host_in_access_perm_list(struct http_client *hc)
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))
318 static void http_post_select(fd_set *rfds, fd_set *wfds)
321 struct http_client *hc, *tmp;
324 list_for_each_entry_safe(hc, tmp, &clients, node) {
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:
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)
335 hc->status = HTTP_INVALID_GET_REQUEST;
337 hc->status = HTTP_GOT_GET_REQUEST;
339 "received get request\n");
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);
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");
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;
362 if (!FD_ISSET(server_fd, rfds))
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));
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";
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";
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);
387 para_list_add(&hc->node, &clients);
388 add_close_on_fork_list(hc->fd);
389 mark_fd_nonblock(hc->fd);
392 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
393 CLIENT_ADDR(hc), err_msg);
399 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
401 struct http_client *hc, *tmp;
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);
410 switch (hc->status) {
412 case HTTP_READY_TO_STREAM:
414 case HTTP_CONNECTED: /* need to recv get request */
415 para_fd_set(hc->fd, rfds, max_fileno);
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);
423 case HTTP_SENT_OK_MSG:
425 break; /* wait until server starts playing */
426 para_fd_set(hc->fd, wfds, max_fileno);
433 static int open_tcp_port(int port)
437 server_fd = init_tcp_socket(port);
439 http_shutdown_clients();
440 self->status = SENDER_OFF;
443 ret = mark_fd_nonblock(server_fd);
445 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
448 self->status = SENDER_ON;
449 add_close_on_fork_list(server_fd);
453 static int http_com_on(__a_unused struct sender_command_data *scd)
455 if (self->status == SENDER_ON)
457 return open_tcp_port(conf.http_port_arg);
460 static int http_com_off(__a_unused struct sender_command_data *scd)
462 self->status = SENDER_OFF;
465 del_close_on_fork_list(server_fd);
468 http_shutdown_clients();
472 static void del_perm_list_entry(struct sender_command_data *scd)
474 struct access_info *ai, *tmp;
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",
489 static void add_perm_list_entry(struct sender_command_data *scd)
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),
496 para_list_add(&ai->node, &access_perm_list);
499 static int http_com_deny(struct sender_command_data *scd)
501 if (conf.http_default_deny_given)
502 del_perm_list_entry(scd);
504 add_perm_list_entry(scd);
508 static int http_com_allow(struct sender_command_data *scd)
510 if (conf.http_default_deny_given)
511 add_perm_list_entry(scd);
513 del_perm_list_entry(scd);
517 static char *http_info(void)
519 char *clnts = NULL, *ap = NULL, *ret;
520 struct access_info *ai, *tmp_ai;
521 struct http_client *hc, *tmp_hc;
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);
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));
537 "http tcp port: %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",
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",
556 static void init_access_control_list(void)
559 struct sender_command_data scd;
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, '/');
568 if (!inet_aton(arg, &scd.addr))
570 scd.netmask = atoi(++p);
571 if (scd.netmask < 0 || scd.netmask > 32)
573 add_perm_list_entry(&scd);
576 PARA_CRIT_LOG("syntax error for http_access option "
577 "#%d, ignoring\n", i);
584 static char *http_help(void)
588 "usage: {allow|deny} IP mask\n"
589 "example: allow 127.0.0.1 32\n"
594 * The init function of the http sender.
596 * \param s Pointer to the http sender struct.
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.
601 void http_send_init(struct sender *s)
603 INIT_LIST_HEAD(&clients);
606 s->pre_select = http_pre_select;
607 s->post_select = http_post_select;
608 s->shutdown_clients = http_shutdown_clients;
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;
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");