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 */
15 #include "server.cmdline.h"
23 #include "close_on_fork.h"
26 #include "chunk_queue.h"
28 /** Message sent to clients that do not send a valid get request. */
29 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
32 /** The possible states of a client from the server's POV. */
34 /** We accepted the connection on the tcp socket. */
36 /** Successfully received the get request. */
38 /** We sent the OK message back to the client. */
40 /** Connection established, we might need to send the audio file header. */
42 /** Connection is ready for sending audio data. */
44 /** We didn't receive a valid get request. */
45 HTTP_INVALID_GET_REQUEST
48 /** Clients will be kicked if there are more than that many bytes pending. */
49 #define MAX_BACKLOG 400000
50 /** The list of connected clients. */
51 static struct list_head clients
;
52 /** The whitelist/blacklist. */
53 static struct list_head access_perm_list
;
55 /** Describes one client that connected the tcp port of the http sender. */
57 /** The file descriptor of the client. */
59 /** The socket `name' of the client. */
61 /** The client's current status. */
62 enum http_status status
;
63 /** Non-zero if we included \a fd in the read set.*/
65 /** Non-zero if we included \a fd in the write set. */
67 /** The position of this client in the client list. */
68 struct list_head node
;
69 /** The list of pending chunks for this client. */
70 struct chunk_queue
*cq
;
74 * Describes one entry in the blacklist/whitelist of the http sender.
77 /** The address to be black/whitelisted. */
79 /** The netmask for this entry. */
81 /** The position of this entry in the access_perm_list. */
82 struct list_head node
;
85 static int server_fd
= -1, numclients
;
86 static struct sender
*self
;
89 static void http_shutdown_client(struct http_client
*hc
, const char *msg
)
91 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", hc
->name
, hc
->fd
,
96 del_close_on_fork_list(hc
->fd
);
102 static void http_shutdown_clients(void)
104 struct http_client
*hc
, *tmp
;
105 list_for_each_entry_safe(hc
, tmp
, &clients
, node
)
106 http_shutdown_client(hc
, "vss request");
109 static int http_send_msg(struct http_client
*hc
, const char *msg
)
111 int ret
= send_buffer(hc
->fd
, msg
);
114 http_shutdown_client(hc
, "send msg failed");
118 static void http_send_ok_msg(struct http_client
*hc
)
120 PARA_INFO_LOG("sending http ok message to fd %d\n", hc
->fd
);
121 http_send_msg(hc
, HTTP_OK_MSG
);
124 static int http_send_err_msg(struct http_client
*hc
)
126 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc
->fd
);
127 return http_send_msg(hc
, HTTP_ERR_MSG
);
130 static int send_queued_chunks(struct http_client
*hc
)
132 struct queued_chunk
*qc
;
133 while ((qc
= cq_peek(hc
->cq
))) {
136 int ret
= write_ok(hc
->fd
);
138 return ret
? -E_WRITE_OK
: 0;
139 cq_get(qc
, &buf
, &len
);
140 ret
= write(hc
->fd
, buf
, len
);
142 return -E_SEND_QUEUED_CHUNK
;
143 cq_update(hc
->cq
, ret
);
151 static int queue_chunk_or_shutdown(struct http_client
*hc
, long unsigned chunk_num
,
154 int ret
= cq_enqueue(hc
->cq
, chunk_num
, sent
);
156 http_shutdown_client(hc
, "queue error");
160 static void http_send( long unsigned current_chunk
,
161 __a_unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
163 struct http_client
*hc
, *tmp
;
166 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
167 if (hc
->status
!= HTTP_STREAMING
&&
168 hc
->status
!= HTTP_READY_TO_STREAM
)
170 if (hc
->status
== HTTP_READY_TO_STREAM
) {
172 char *hbuf
= vss_get_header(&hlen
);
173 if (hbuf
&& hlen
> 0 && current_chunk
) {
174 /* need to send header */
175 PARA_INFO_LOG("queueing header: %zu\n", hlen
);
176 if (queue_chunk_or_shutdown(hc
, -1U, 0) < 0)
179 PARA_INFO_LOG("no need to queue header\n");
180 hc
->status
= HTTP_STREAMING
;
182 ret
= send_queued_chunks(hc
);
184 http_shutdown_client(hc
, "queue send error");
189 if (!ret
|| write_ok(hc
->fd
) <= 0) {
190 queue_chunk_or_shutdown(hc
, current_chunk
, 0);
193 // PARA_DEBUG_LOG("sending %d -> %s\n", len, remote_name(hc->fd));
194 ret
= write(hc
->fd
, buf
, len
);
195 // PARA_DEBUG_LOG("ret: %d\n", ret);
197 http_shutdown_client(hc
, "send error");
201 queue_chunk_or_shutdown(hc
, current_chunk
, ret
);
205 static int host_in_access_perm_list(struct http_client
*hc
)
207 struct sockaddr_storage ss
;
208 socklen_t sslen
= sizeof(ss
);
210 if (getpeername(hc
->fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
211 PARA_ERROR_LOG("can not determine address family: %s\n", strerror(errno
));
212 } else if (ss
.ss_family
== AF_INET
) {
213 /* FIXME: access restriction is (currently) only supported for IPv4 */
214 struct access_info
*ai
, *tmp
;
215 struct in_addr client_addr
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
217 list_for_each_entry_safe(ai
, tmp
, &access_perm_list
, node
) {
218 unsigned mask
= ((~0U) >> ai
->netmask
);
219 if ((client_addr
.s_addr
& mask
) == (ai
->addr
.s_addr
& mask
))
226 static void http_post_select(fd_set
*rfds
, fd_set
*wfds
)
229 struct http_client
*hc
, *tmp
;
232 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
234 // PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
235 switch (hc
->status
) {
236 case HTTP_STREAMING
: /* nothing to do */
237 case HTTP_READY_TO_STREAM
:
239 case HTTP_CONNECTED
: /* need to recv get request */
240 if (hc
->check_r
&& FD_ISSET(hc
->fd
, rfds
)) {
241 if (recv_pattern(hc
->fd
, HTTP_GET_MSG
, MAXLINE
)
243 hc
->status
= HTTP_INVALID_GET_REQUEST
;
245 hc
->status
= HTTP_GOT_GET_REQUEST
;
247 "received get request\n");
251 case HTTP_GOT_GET_REQUEST
: /* need to send ok msg */
252 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
)) {
253 hc
->status
= HTTP_SENT_OK_MSG
;
254 http_send_ok_msg(hc
);
257 case HTTP_INVALID_GET_REQUEST
: /* need to send err msg */
258 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
)) {
259 if (http_send_err_msg(hc
) >= 0)
260 http_shutdown_client(hc
,
261 "invalid get request");
264 case HTTP_SENT_OK_MSG
: /* need to send header? */
265 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
))
266 hc
->status
= HTTP_READY_TO_STREAM
;
270 if (!FD_ISSET(server_fd
, rfds
))
272 hc
= para_calloc(sizeof(struct http_client
));
273 err_msg
= "accept error";
274 hc
->fd
= para_accept(server_fd
, NULL
, 0);
277 hc
->name
= make_message("%s", remote_name(hc
->fd
));
278 PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc
->name
, hc
->fd
);
279 if (conf
.http_max_clients_arg
> 0 && numclients
>=
280 conf
.http_max_clients_arg
) {
281 err_msg
= "server full";
284 match
= host_in_access_perm_list(hc
);
285 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match
);
286 if ((match
&& !conf
.http_default_deny_given
) ||
287 (!match
&& conf
.http_default_deny_given
)) {
288 err_msg
= "permission denied";
291 hc
->status
= HTTP_CONNECTED
;
292 hc
->cq
= cq_new(MAX_BACKLOG
);
294 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients
,
296 para_list_add(&hc
->node
, &clients
);
297 add_close_on_fork_list(hc
->fd
);
298 mark_fd_nonblocking(hc
->fd
);
301 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
308 static void http_pre_select(int *max_fileno
, fd_set
*rfds
, fd_set
*wfds
)
310 struct http_client
*hc
, *tmp
;
314 para_fd_set(server_fd
, rfds
, max_fileno
);
315 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
316 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
319 switch (hc
->status
) {
321 case HTTP_READY_TO_STREAM
:
323 case HTTP_CONNECTED
: /* need to recv get request */
324 para_fd_set(hc
->fd
, rfds
, max_fileno
);
327 case HTTP_GOT_GET_REQUEST
: /* need to send ok msg */
328 case HTTP_INVALID_GET_REQUEST
: /* need to send err msg */
329 para_fd_set(hc
->fd
, wfds
, max_fileno
);
332 case HTTP_SENT_OK_MSG
:
334 break; /* wait until server starts playing */
335 para_fd_set(hc
->fd
, wfds
, max_fileno
);
342 static int open_tcp_port(int port
)
346 server_fd
= para_listen(AF_UNSPEC
, IPPROTO_TCP
, port
);
348 http_shutdown_clients();
349 self
->status
= SENDER_OFF
;
352 ret
= mark_fd_nonblocking(server_fd
);
354 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
357 self
->status
= SENDER_ON
;
358 add_close_on_fork_list(server_fd
);
362 static int http_com_on(__a_unused
struct sender_command_data
*scd
)
364 if (self
->status
== SENDER_ON
)
366 return open_tcp_port(conf
.http_port_arg
);
369 static int http_com_off(__a_unused
struct sender_command_data
*scd
)
371 self
->status
= SENDER_OFF
;
374 del_close_on_fork_list(server_fd
);
377 http_shutdown_clients();
381 static void del_perm_list_entry(struct sender_command_data
*scd
)
383 struct access_info
*ai
, *tmp
;
385 list_for_each_entry_safe(ai
, tmp
, &access_perm_list
, node
) {
386 char *nad
= para_strdup(inet_ntoa(ai
->addr
));
387 if (!strcmp(nad
, inet_ntoa(scd
->addr
)) &&
388 ai
->netmask
== scd
->netmask
) {
389 PARA_NOTICE_LOG("removing %s/%i from access list\n",
398 static void add_perm_list_entry(struct sender_command_data
*scd
)
400 struct access_info
*ai
= para_malloc(sizeof(struct access_info
));
401 ai
->addr
= scd
->addr
;
402 ai
->netmask
= scd
->netmask
;
403 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai
->addr
),
405 para_list_add(&ai
->node
, &access_perm_list
);
408 static int http_com_deny(struct sender_command_data
*scd
)
410 if (conf
.http_default_deny_given
)
411 del_perm_list_entry(scd
);
413 add_perm_list_entry(scd
);
417 static int http_com_allow(struct sender_command_data
*scd
)
419 if (conf
.http_default_deny_given
)
420 add_perm_list_entry(scd
);
422 del_perm_list_entry(scd
);
426 static char *http_info(void)
428 char *clnts
= NULL
, *ap
= NULL
, *ret
;
429 struct access_info
*ai
, *tmp_ai
;
430 struct http_client
*hc
, *tmp_hc
;
432 list_for_each_entry_safe(ai
, tmp_ai
, &access_perm_list
, node
) {
433 char *tmp
= make_message("%s%s/%d ", ap
? ap
: "",
434 inet_ntoa(ai
->addr
), ai
->netmask
);
438 list_for_each_entry_safe(hc
, tmp_hc
, &clients
, node
) {
439 char *tmp
= make_message("%s%s ", clnts
? clnts
: "", hc
->name
);
445 "http tcp port: %d\n"
447 "http maximal number of clients: %d%s\n"
448 "http connected clients: %s\n"
449 "http access %s list: %s\n",
450 (self
->status
== SENDER_ON
)? "on" : "off",
453 conf
.http_max_clients_arg
,
454 conf
.http_max_clients_arg
> 0? "" : " (unlimited)",
455 clnts
? clnts
: "(none)",
456 conf
.http_default_deny_given
? "allow" : "deny",
464 static void init_access_control_list(void)
467 struct sender_command_data scd
;
469 INIT_LIST_HEAD(&access_perm_list
);
470 for (i
= 0; i
< conf
.http_access_given
; i
++) {
471 char *arg
= para_strdup(conf
.http_access_arg
[i
]);
472 char *p
= strchr(arg
, '/');
476 if (!inet_pton(AF_INET
, arg
, &scd
.addr
))
478 scd
.netmask
= atoi(++p
);
479 if (scd
.netmask
< 0 || scd
.netmask
> 32)
481 add_perm_list_entry(&scd
);
484 PARA_CRIT_LOG("syntax error for http_access option "
485 "#%d, ignoring\n", i
);
492 static char *http_help(void)
496 "usage: {allow|deny} IP mask\n"
497 "example: allow 127.0.0.1 32\n"
502 * The init function of the http sender.
504 * \param s Pointer to the http sender struct.
506 * It initializes all function pointers of \a s, the client list and the access
507 * control list. If the autostart option was given, the tcp port is opened.
509 void http_send_init(struct sender
*s
)
511 INIT_LIST_HEAD(&clients
);
514 s
->pre_select
= http_pre_select
;
515 s
->post_select
= http_post_select
;
516 s
->shutdown_clients
= http_shutdown_clients
;
518 s
->client_cmds
[SENDER_ON
] = http_com_on
;
519 s
->client_cmds
[SENDER_OFF
] = http_com_off
;
520 s
->client_cmds
[SENDER_DENY
] = http_com_deny
;
521 s
->client_cmds
[SENDER_ALLOW
] = http_com_allow
;
522 s
->client_cmds
[SENDER_ADD
] = NULL
;
523 s
->client_cmds
[SENDER_DELETE
] = NULL
;
525 init_access_control_list();
526 if (!conf
.http_no_autostart_given
)
527 open_tcp_port(conf
.http_port_arg
); /* ignore errors */
528 PARA_DEBUG_LOG("%s", "http sender init complete\n");