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 struct chunk_queue
*cq_init(size_t max_pending
)
177 struct chunk_queue
*cq
= para_malloc(sizeof(*cq
));
178 INIT_LIST_HEAD(&cq
->q
);
179 cq
->max_pending
= max_pending
;
184 void cq_destroy(struct chunk_queue
*cq
)
186 struct queued_chunk
*qc
, *tmp
;
187 list_for_each_entry_safe(qc
, tmp
, &cq
->q
, node
) {
194 static void http_shutdown_client(struct http_client
*hc
, const char *msg
)
196 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc
),
200 del_close_on_fork_list(hc
->fd
);
206 static void http_shutdown_clients(void)
208 struct http_client
*hc
, *tmp
;
209 list_for_each_entry_safe(hc
, tmp
, &clients
, node
)
210 http_shutdown_client(hc
, "vss request");
213 static int http_send_msg(struct http_client
*hc
, const char *msg
)
215 int ret
= send_buffer(hc
->fd
, msg
);
218 http_shutdown_client(hc
, "send msg failed");
222 static void http_send_ok_msg(struct http_client
*hc
)
224 PARA_INFO_LOG("sending http ok message to fd %d\n", hc
->fd
);
225 http_send_msg(hc
, HTTP_OK_MSG
);
228 static int http_send_err_msg(struct http_client
*hc
)
230 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc
->fd
);
231 return http_send_msg(hc
, HTTP_ERR_MSG
);
235 static int send_queued_chunks(struct http_client
*hc
)
237 struct queued_chunk
*qc
;
238 while ((qc
= cq_peek(hc
->cq
))) {
241 int ret
= write_ok(hc
->fd
);
243 return ret
? -E_WRITE_OK
: 0;
244 cq_get(qc
, &buf
, &len
);
245 ret
= write(hc
->fd
, buf
, len
);
247 return -1; /* FIXME */
248 cq_update(hc
->cq
, ret
);
256 static int queue_chunk_or_shutdown(struct http_client
*hc
, long unsigned chunk_num
,
259 int ret
= cq_enqueue(hc
->cq
, chunk_num
, sent
);
261 http_shutdown_client(hc
, "queue error");
265 static void http_send( long unsigned current_chunk
,
266 __a_unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
268 struct http_client
*hc
, *tmp
;
271 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
272 if (hc
->status
!= HTTP_STREAMING
&&
273 hc
->status
!= HTTP_READY_TO_STREAM
)
275 if (hc
->status
== HTTP_READY_TO_STREAM
) {
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)
284 PARA_INFO_LOG("no need to queue header\n");
285 hc
->status
= HTTP_STREAMING
;
287 ret
= send_queued_chunks(hc
);
289 http_shutdown_client(hc
, "queue send error");
294 if (!ret
|| write_ok(hc
->fd
) <= 0) {
295 queue_chunk_or_shutdown(hc
, current_chunk
, 0);
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);
302 http_shutdown_client(hc
, "send error");
306 queue_chunk_or_shutdown(hc
, current_chunk
, ret
);
310 static int host_in_access_perm_list(struct http_client
*hc
)
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
))
321 static void http_post_select(fd_set
*rfds
, fd_set
*wfds
)
324 struct http_client
*hc
, *tmp
;
327 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
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
:
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
)
338 hc
->status
= HTTP_INVALID_GET_REQUEST
;
340 hc
->status
= HTTP_GOT_GET_REQUEST
;
342 "received get request\n");
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
);
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");
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
;
365 if (!FD_ISSET(server_fd
, rfds
))
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
));
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";
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";
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
);
390 para_list_add(&hc
->node
, &clients
);
391 add_close_on_fork_list(hc
->fd
);
392 mark_fd_nonblock(hc
->fd
);
395 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
396 CLIENT_ADDR(hc
), err_msg
);
402 static void http_pre_select(int *max_fileno
, fd_set
*rfds
, fd_set
*wfds
)
404 struct http_client
*hc
, *tmp
;
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);
413 switch (hc
->status
) {
415 case HTTP_READY_TO_STREAM
:
417 case HTTP_CONNECTED
: /* need to recv get request */
418 para_fd_set(hc
->fd
, rfds
, max_fileno
);
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
);
426 case HTTP_SENT_OK_MSG
:
428 break; /* wait until server starts playing */
429 para_fd_set(hc
->fd
, wfds
, max_fileno
);
436 static int open_tcp_port(int port
)
440 server_fd
= init_tcp_socket(port
);
442 http_shutdown_clients();
443 self
->status
= SENDER_OFF
;
446 ret
= mark_fd_nonblock(server_fd
);
448 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
451 self
->status
= SENDER_ON
;
452 add_close_on_fork_list(server_fd
);
456 static int http_com_on(__a_unused
struct sender_command_data
*scd
)
458 if (self
->status
== SENDER_ON
)
460 return open_tcp_port(conf
.http_port_arg
);
463 static int http_com_off(__a_unused
struct sender_command_data
*scd
)
465 self
->status
= SENDER_OFF
;
468 del_close_on_fork_list(server_fd
);
471 http_shutdown_clients();
475 static void del_perm_list_entry(struct sender_command_data
*scd
)
477 struct access_info
*ai
, *tmp
;
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",
492 static void add_perm_list_entry(struct sender_command_data
*scd
)
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
),
499 para_list_add(&ai
->node
, &access_perm_list
);
502 static int http_com_deny(struct sender_command_data
*scd
)
504 if (conf
.http_default_deny_given
)
505 del_perm_list_entry(scd
);
507 add_perm_list_entry(scd
);
511 static int http_com_allow(struct sender_command_data
*scd
)
513 if (conf
.http_default_deny_given
)
514 add_perm_list_entry(scd
);
516 del_perm_list_entry(scd
);
520 static char *http_info(void)
522 char *clnts
= NULL
, *ap
= NULL
, *ret
;
523 struct access_info
*ai
, *tmp_ai
;
524 struct http_client
*hc
, *tmp_hc
;
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
);
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
));
540 "http tcp port: %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",
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",
559 static void init_access_control_list(void)
562 struct sender_command_data scd
;
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
, '/');
571 if (!inet_aton(arg
, &scd
.addr
))
573 scd
.netmask
= atoi(++p
);
574 if (scd
.netmask
< 0 || scd
.netmask
> 32)
576 add_perm_list_entry(&scd
);
579 PARA_CRIT_LOG("syntax error for http_access option "
580 "#%d, ignoring\n", i
);
587 static char *http_help(void)
591 "usage: {allow|deny} IP mask\n"
592 "example: allow 127.0.0.1 32\n"
597 * The init function of the http sender.
599 * \param s Pointer to the http sender struct.
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.
604 void http_send_init(struct sender
*s
)
606 INIT_LIST_HEAD(&clients
);
609 s
->pre_select
= http_pre_select
;
610 s
->post_select
= http_post_select
;
611 s
->shutdown_clients
= http_shutdown_clients
;
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
;
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");