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 40000
47 /** The list of connected clients. */
48 static struct list_head clients
;
49 /** The whitelist/blacklist. */
50 static struct list_head access_perm_list
;
52 /** Describes one client that connected the tcp port of the http sender. */
54 /** The file descriptor of the client. */
56 /** Address information about the client. */
57 struct sockaddr_in addr
;
58 /** The client's current status. */
59 enum http_status status
;
60 /** Non-zero if we included \a fd in the read set.*/
62 /** Non-zero if we included \a fd in the write set. */
64 /** The position of this client in the client list. */
65 struct list_head node
;
66 /** The list of pending chunks for this client. */
67 struct list_head chunk_queue
;
68 /** The number of pending bytes for this client. */
69 unsigned long cq_bytes
;
73 * Describes one queued chunk of the chunk queue.
75 * The send function of the http sender checks each client fd for writing. If a
76 * client fd is not ready, it tries to queue that chunk for this client until
77 * the number of queued bytes exceeds \p MAX_BACKLOG.
80 /** The number of the queued chunk, -1U means header. */
82 /** The number of bytes already sent. */
84 /** Position of the chunk in the chunk queue. */
85 struct list_head node
;
89 * Describes one entry in the blacklist/whitelist of the http sender.
92 /** The address to be black/whitelisted. */
94 /** The netmask for this entry. */
96 /** The position of this entry in the access_perm_list. */
97 struct list_head node
;
100 static int server_fd
= -1, numclients
;
101 static struct sender
*self
;
103 static void http_shutdown_client(struct http_client
*hc
, const char *msg
)
105 struct queued_chunk
*qc
, *tmp
;
106 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc
),
110 del_close_on_fork_list(hc
->fd
);
111 list_for_each_entry_safe(qc
, tmp
, &hc
->chunk_queue
, node
) {
119 static void http_shutdown_clients(void)
121 struct http_client
*hc
, *tmp
;
122 list_for_each_entry_safe(hc
, tmp
, &clients
, node
)
123 http_shutdown_client(hc
, "vss request");
126 static int http_send_msg(struct http_client
*hc
, const char *msg
)
128 int ret
= send_buffer(hc
->fd
, msg
);
131 http_shutdown_client(hc
, "send msg failed");
135 static void http_send_ok_msg(struct http_client
*hc
)
137 PARA_INFO_LOG("sending http ok message to fd %d\n", hc
->fd
);
138 http_send_msg(hc
, HTTP_OK_MSG
);
141 static int http_send_err_msg(struct http_client
*hc
)
143 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc
->fd
);
144 return http_send_msg(hc
, HTTP_ERR_MSG
);
147 static int queue_chunk(struct http_client
*hc
, long unsigned chunk_num
,
150 struct queued_chunk
*qc
;
155 if (chunk_num
!= -1U) {
156 ret
= vss_get_chunk(chunk_num
, &buf
, &len
);
160 buf
= vss_get_header(&len
);
161 if (hc
->cq_bytes
+ len
> MAX_BACKLOG
)
163 qc
= para_malloc(sizeof(struct queued_chunk
));
165 qc
->chunk_num
= chunk_num
;
167 list_add_tail(&qc
->node
, &hc
->chunk_queue
);
168 PARA_INFO_LOG("%lu bytes queued for fd %d\n", hc
->cq_bytes
, hc
->fd
);
172 static int send_queued_chunks(struct http_client
*hc
)
175 struct queued_chunk
*qc
, *tmp
;
177 if (list_empty(&hc
->chunk_queue
))
179 list_for_each_entry_safe(qc
, tmp
, &hc
->chunk_queue
, node
) {
182 ret
= write_ok(hc
->fd
);
184 return ret
? -E_WRITE_OK
: 0;
185 if (qc
->chunk_num
!= -1U) {
186 ret
= vss_get_chunk(qc
->chunk_num
, &buf
, &len
);
190 buf
= vss_get_header(&len
);
191 assert(len
&& len
> qc
->sent
);
192 ret
= write(hc
->fd
, buf
+ qc
->sent
, len
- qc
->sent
);
194 return -1; /* FIXME */
196 if (ret
!= len
- qc
->sent
) {
206 static int queue_chunk_or_shutdown(struct http_client
*hc
, long unsigned chunk_num
,
209 int ret
= queue_chunk(hc
, chunk_num
, sent
);
211 http_shutdown_client(hc
, "queue error");
215 static void http_send( long unsigned current_chunk
,
216 __a_unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
218 struct http_client
*hc
, *tmp
;
221 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
222 if (hc
->status
!= HTTP_STREAMING
&&
223 hc
->status
!= HTTP_READY_TO_STREAM
)
225 if (hc
->status
== HTTP_READY_TO_STREAM
) {
227 char *hbuf
= vss_get_header(&hlen
);
228 if (hbuf
&& hlen
> 0 && current_chunk
) {
229 /* need to send header */
230 PARA_INFO_LOG("queueing header: %d\n", hlen
);
231 if (queue_chunk_or_shutdown(hc
, -1U, 0) < 0)
234 PARA_INFO_LOG("no need to queue header\n");
235 hc
->status
= HTTP_STREAMING
;
237 ret
= send_queued_chunks(hc
);
239 http_shutdown_client(hc
, "queue send error");
244 if (!ret
|| write_ok(hc
->fd
) <= 0) {
245 PARA_INFO_LOG("fd %d not ready (%lu bytes queued),"
246 " trying to queue chunk\n", hc
->fd
,
248 queue_chunk_or_shutdown(hc
, current_chunk
, 0);
251 // PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
252 ret
= write(hc
->fd
, buf
, len
);
253 // PARA_DEBUG_LOG("ret: %d\n", ret);
255 http_shutdown_client(hc
, "send error");
259 queue_chunk_or_shutdown(hc
, current_chunk
, ret
);
263 static int host_in_access_perm_list(struct http_client
*hc
)
265 struct access_info
*ai
, *tmp
;
266 list_for_each_entry_safe(ai
, tmp
, &access_perm_list
, node
) {
267 unsigned mask
= ((~0U) >> ai
->netmask
);
268 if ((hc
->addr
.sin_addr
.s_addr
& mask
) == (ai
->addr
.s_addr
& mask
))
274 static void http_post_select(fd_set
*rfds
, fd_set
*wfds
)
277 struct http_client
*hc
, *tmp
;
280 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
282 // PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
283 switch (hc
->status
) {
284 case HTTP_STREAMING
: /* nothing to do */
285 case HTTP_READY_TO_STREAM
:
287 case HTTP_CONNECTED
: /* need to recv get request */
288 if (hc
->check_r
&& FD_ISSET(hc
->fd
, rfds
)) {
289 if (recv_pattern(hc
->fd
, HTTP_GET_MSG
, MAXLINE
)
291 hc
->status
= HTTP_INVALID_GET_REQUEST
;
293 hc
->status
= HTTP_GOT_GET_REQUEST
;
295 "received get request\n");
299 case HTTP_GOT_GET_REQUEST
: /* need to send ok msg */
300 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
)) {
301 hc
->status
= HTTP_SENT_OK_MSG
;
302 http_send_ok_msg(hc
);
305 case HTTP_INVALID_GET_REQUEST
: /* need to send err msg */
306 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
)) {
307 if (http_send_err_msg(hc
) >= 0)
308 http_shutdown_client(hc
,
309 "invalid get request");
312 case HTTP_SENT_OK_MSG
: /* need to send header? */
313 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
))
314 hc
->status
= HTTP_READY_TO_STREAM
;
318 if (!FD_ISSET(server_fd
, rfds
))
320 hc
= para_calloc(sizeof(struct http_client
));
321 err_msg
= "accept error";
322 hc
->fd
= para_accept(server_fd
, &hc
->addr
, sizeof(struct sockaddr_in
));
325 PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc
), hc
->fd
);
326 if (conf
.http_max_clients_arg
> 0 && numclients
>=
327 conf
.http_max_clients_arg
) {
328 err_msg
= "server full";
331 match
= host_in_access_perm_list(hc
);
332 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match
);
333 if ((match
&& !conf
.http_default_deny_given
) ||
334 (!match
&& conf
.http_default_deny_given
)) {
335 err_msg
= "permission denied";
338 hc
->status
= HTTP_CONNECTED
;
339 INIT_LIST_HEAD(&hc
->chunk_queue
);
340 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients
,
341 CLIENT_ADDR(hc
), hc
->fd
);
343 para_list_add(&hc
->node
, &clients
);
344 add_close_on_fork_list(hc
->fd
);
345 mark_fd_nonblock(hc
->fd
);
348 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
349 CLIENT_ADDR(hc
), err_msg
);
355 static void http_pre_select(int *max_fileno
, fd_set
*rfds
, fd_set
*wfds
)
357 struct http_client
*hc
, *tmp
;
361 para_fd_set(server_fd
, rfds
, max_fileno
);
362 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
363 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
366 switch (hc
->status
) {
368 case HTTP_READY_TO_STREAM
:
370 case HTTP_CONNECTED
: /* need to recv get request */
371 para_fd_set(hc
->fd
, rfds
, max_fileno
);
374 case HTTP_GOT_GET_REQUEST
: /* need to send ok msg */
375 case HTTP_INVALID_GET_REQUEST
: /* need to send err msg */
376 para_fd_set(hc
->fd
, wfds
, max_fileno
);
379 case HTTP_SENT_OK_MSG
:
381 break; /* wait until server starts playing */
382 para_fd_set(hc
->fd
, wfds
, max_fileno
);
389 static int open_tcp_port(int port
)
393 server_fd
= init_tcp_socket(port
);
395 http_shutdown_clients();
396 self
->status
= SENDER_OFF
;
399 ret
= mark_fd_nonblock(server_fd
);
401 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
404 self
->status
= SENDER_ON
;
405 add_close_on_fork_list(server_fd
);
409 static int http_com_on(__a_unused
struct sender_command_data
*scd
)
411 if (self
->status
== SENDER_ON
)
413 return open_tcp_port(conf
.http_port_arg
);
416 static int http_com_off(__a_unused
struct sender_command_data
*scd
)
418 self
->status
= SENDER_OFF
;
421 del_close_on_fork_list(server_fd
);
424 http_shutdown_clients();
428 static void del_perm_list_entry(struct sender_command_data
*scd
)
430 struct access_info
*ai
, *tmp
;
432 list_for_each_entry_safe(ai
, tmp
, &access_perm_list
, node
) {
433 char *nad
= para_strdup(inet_ntoa(ai
->addr
));
434 if (!strcmp(nad
, inet_ntoa(scd
->addr
)) &&
435 ai
->netmask
== scd
->netmask
) {
436 PARA_NOTICE_LOG("removing %s/%i from access list\n",
445 static void add_perm_list_entry(struct sender_command_data
*scd
)
447 struct access_info
*ai
= para_malloc(sizeof(struct access_info
));
448 ai
->addr
= scd
->addr
;
449 ai
->netmask
= scd
->netmask
;
450 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai
->addr
),
452 para_list_add(&ai
->node
, &access_perm_list
);
455 static int http_com_deny(struct sender_command_data
*scd
)
457 if (conf
.http_default_deny_given
)
458 del_perm_list_entry(scd
);
460 add_perm_list_entry(scd
);
464 static int http_com_allow(struct sender_command_data
*scd
)
466 if (conf
.http_default_deny_given
)
467 add_perm_list_entry(scd
);
469 del_perm_list_entry(scd
);
473 static char *http_info(void)
475 char *clnts
= NULL
, *ap
= NULL
, *ret
;
476 struct access_info
*ai
, *tmp_ai
;
477 struct http_client
*hc
, *tmp_hc
;
479 list_for_each_entry_safe(ai
, tmp_ai
, &access_perm_list
, node
) {
480 char *tmp
= make_message("%s%s/%d ", ap
? ap
: "",
481 inet_ntoa(ai
->addr
), ai
->netmask
);
485 list_for_each_entry_safe(hc
, tmp_hc
, &clients
, node
) {
486 char *tmp
= make_message("%s%s:%d ", clnts
? clnts
: "",
487 CLIENT_ADDR(hc
), CLIENT_PORT(hc
));
493 "http tcp port: %d\n"
495 "http maximal number of clients: %d%s\n"
496 "http connected clients: %s\n"
497 "http access %s list: %s\n",
498 (self
->status
== SENDER_ON
)? "on" : "off",
501 conf
.http_max_clients_arg
,
502 conf
.http_max_clients_arg
> 0? "" : " (unlimited)",
503 clnts
? clnts
: "(none)",
504 conf
.http_default_deny_given
? "allow" : "deny",
512 static void init_access_control_list(void)
515 struct sender_command_data scd
;
517 INIT_LIST_HEAD(&access_perm_list
);
518 for (i
= 0; i
< conf
.http_access_given
; i
++) {
519 char *arg
= para_strdup(conf
.http_access_arg
[i
]);
520 char *p
= strchr(arg
, '/');
524 if (!inet_aton(arg
, &scd
.addr
))
526 scd
.netmask
= atoi(++p
);
527 if (scd
.netmask
< 0 || scd
.netmask
> 32)
529 add_perm_list_entry(&scd
);
532 PARA_CRIT_LOG("syntax error for http_access option "
533 "#%d, ignoring\n", i
);
540 static char *http_help(void)
544 "usage: {allow|deny} IP mask\n"
545 "example: allow 127.0.0.1 32\n"
550 * The init function of the http sender.
552 * \param s Pointer to the http sender struct.
554 * It initializes all function pointers of \a s, the client list and the access
555 * control list. If the autostart option was given, the tcp port is opened.
557 void http_send_init(struct sender
*s
)
559 INIT_LIST_HEAD(&clients
);
562 s
->pre_select
= http_pre_select
;
563 s
->post_select
= http_post_select
;
564 s
->shutdown_clients
= http_shutdown_clients
;
566 s
->client_cmds
[SENDER_ON
] = http_com_on
;
567 s
->client_cmds
[SENDER_OFF
] = http_com_off
;
568 s
->client_cmds
[SENDER_DENY
] = http_com_deny
;
569 s
->client_cmds
[SENDER_ALLOW
] = http_com_allow
;
570 s
->client_cmds
[SENDER_ADD
] = NULL
;
571 s
->client_cmds
[SENDER_DELETE
] = NULL
;
573 init_access_control_list();
574 if (!conf
.http_no_autostart_given
)
575 open_tcp_port(conf
.http_port_arg
); /* ignore errors */
576 PARA_DEBUG_LOG("%s", "http sender init complete\n");