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 */
36 HTTP_INVALID_GET_REQUEST
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;
46 /** describes one client that connected the tcp port of the http sender */
48 /** the file descriptor of the client */
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 */
56 /** non-zero if we included \a fd in the write set */
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;
67 * describes one queued data packet for a client
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.
73 struct queued_packet {
74 /** the length of the packet in bytes */
76 /** pointer to the packet data */
78 /** position of the packet in the packet list */
79 struct list_head node;
83 * describes one entry in the blacklist/whitelist of the http sender
86 /** the address to be black/whitelisted */
88 /** the netmask for this entry */
90 /** the position of this entry in the access_perm_list */
91 struct list_head node;
94 static int server_fd = -1, numclients;
95 static struct sender *self;
98 static void http_shutdown_client(struct http_client *hc, const char *msg)
100 struct queued_packet *qp, *tmp;
101 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
105 del_close_on_fork_list(hc->fd);
106 list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
115 static void http_shutdown_clients(void)
117 struct http_client *hc, *tmp;
118 list_for_each_entry_safe(hc, tmp, &clients, node)
119 http_shutdown_client(hc, "vss request");
122 static int http_send_msg(struct http_client *hc, const char *msg)
124 int ret = send_buffer(hc->fd, msg);
127 http_shutdown_client(hc, "send msg failed");
131 static void http_send_ok_msg(struct http_client *hc)
133 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
134 http_send_msg(hc, HTTP_OK_MSG);
137 static int http_send_err_msg(struct http_client *hc)
139 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
140 return http_send_msg(hc, HTTP_ERR_MSG);
143 static int queue_packet(struct http_client *hc, const char *buf, size_t len)
145 struct queued_packet *qp;
146 if (hc->pq_bytes + len > MAX_BACKLOG) {
147 http_shutdown_client(hc, "packet queue overrun");
150 qp = para_malloc(sizeof(struct queued_packet));
152 qp->packet = para_malloc(len);
153 memcpy(qp->packet, buf, 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);
160 static int send_queued_packets(struct http_client *hc)
163 struct queued_packet *qp, *tmp;
165 if (list_empty(&hc->packet_queue))
167 list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
168 ret = write_ok(hc->fd);
170 return ret? -E_WRITE_OK : 0;
171 ret = write(hc->fd, qp->packet, qp->len);
174 if (ret != qp->len) {
176 memmove(qp->packet, qp->packet + ret, qp->len);
179 hc->pq_bytes -= qp->len;
187 static void http_send( long unsigned current_chunk,
188 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
190 struct http_client *hc, *tmp;
193 list_for_each_entry_safe(hc, tmp, &clients, node) {
194 if (hc->status != HTTP_STREAMING &&
195 hc->status != HTTP_READY_TO_STREAM)
197 if (hc->status == HTTP_READY_TO_STREAM) {
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)
206 PARA_INFO_LOG("%s", "no need to queue header\n");
207 hc->status = HTTP_STREAMING;
209 ret = send_queued_packets(hc);
211 http_shutdown_client(hc, "send error");
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,
220 queue_packet(hc, buf, len);
223 // PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
224 ret = write(hc->fd, buf, len);
226 http_shutdown_client(hc, "send error");
230 queue_packet(hc, buf + ret, len - ret);
234 static int host_in_access_perm_list(struct http_client *hc)
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))
245 static void http_post_select(fd_set *rfds, fd_set *wfds)
248 struct http_client *hc, *tmp;
251 list_for_each_entry_safe(hc, tmp, &clients, node) {
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:
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)
262 hc->status = HTTP_INVALID_GET_REQUEST;
264 hc->status = HTTP_GOT_GET_REQUEST;
266 "received get request\n");
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);
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");
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;
289 if (!FD_ISSET(server_fd, rfds))
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));
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";
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";
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);
314 para_list_add(&hc->node, &clients);
315 add_close_on_fork_list(hc->fd);
316 mark_fd_nonblock(hc->fd);
319 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
320 CLIENT_ADDR(hc), err_msg);
326 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
328 struct http_client *hc, *tmp;
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);
337 switch (hc->status) {
339 case HTTP_READY_TO_STREAM:
341 case HTTP_CONNECTED: /* need to recv get request */
342 para_fd_set(hc->fd, rfds, max_fileno);
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);
350 case HTTP_SENT_OK_MSG:
352 break; /* wait until server starts playing */
353 para_fd_set(hc->fd, wfds, max_fileno);
360 static int open_tcp_port(int port)
364 server_fd = init_tcp_socket(port);
366 http_shutdown_clients();
367 self->status = SENDER_OFF;
370 ret = mark_fd_nonblock(server_fd);
372 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
375 self->status = SENDER_ON;
376 add_close_on_fork_list(server_fd);
380 static int http_com_on(__a_unused struct sender_command_data *scd)
382 if (self->status == SENDER_ON)
384 return open_tcp_port(conf.http_port_arg);
387 static int http_com_off(__a_unused struct sender_command_data *scd)
389 self->status = SENDER_OFF;
392 del_close_on_fork_list(server_fd);
395 http_shutdown_clients();
399 static void del_perm_list_entry(struct sender_command_data *scd)
401 struct access_info *ai, *tmp;
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",
416 static void add_perm_list_entry(struct sender_command_data *scd)
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),
423 para_list_add(&ai->node, &access_perm_list);
426 static int http_com_deny(struct sender_command_data *scd)
428 if (conf.http_default_deny_given)
429 del_perm_list_entry(scd);
431 add_perm_list_entry(scd);
435 static int http_com_allow(struct sender_command_data *scd)
437 if (conf.http_default_deny_given)
438 add_perm_list_entry(scd);
440 del_perm_list_entry(scd);
444 static char *http_info(void)
446 char *clnts = NULL, *ap = NULL, *ret;
447 struct access_info *ai, *tmp_ai;
448 struct http_client *hc, *tmp_hc;
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);
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));
464 "http tcp port: %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",
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",
483 static void init_access_control_list(void)
486 struct sender_command_data scd;
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, '/');
495 if (!inet_aton(arg, &scd.addr))
497 scd.netmask = atoi(++p);
498 if (scd.netmask < 0 || scd.netmask > 32)
500 add_perm_list_entry(&scd);
503 PARA_CRIT_LOG("syntax error for http_access option "
504 "#%d, ignoring\n", i);
511 static char *http_help(void)
515 "usage: {allow|deny} IP mask\n"
516 "example: allow 127.0.0.1 32\n"
521 * the init function of the http sender
523 * \param s pointer to the http sender struct
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.
528 void http_send_init(struct sender *s)
530 INIT_LIST_HEAD(&clients);
533 s->pre_select = http_pre_select;
534 s->post_select = http_post_select;
535 s->shutdown_clients = http_shutdown_clients;
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;
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");