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 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
31 /** The possible states of a client from the server's POV. */
33 /** We accepted the connection on the tcp socket. */
35 /** Successfully received the get request. */
37 /** We sent the OK message back to the client. */
39 /** Connection established, we might need to send the audio file header. */
41 /** Connection is ready for sending audio data. */
43 /** We didn't receive a valid get request. */
44 HTTP_INVALID_GET_REQUEST
47 /** Clients will be kicked if there are more than that many bytes pending. */
48 #define MAX_BACKLOG 400000
49 /** The list of connected clients. */
50 static struct list_head clients
;
51 /** The whitelist/blacklist. */
52 static struct list_head access_perm_list
;
54 /** Describes one client that connected the tcp port of the http sender. */
56 /** The file descriptor of the client. */
58 /** The socket `name' of the client. */
60 /** The client's current status. */
61 enum http_status status
;
62 /** Non-zero if we included \a fd in the read set.*/
64 /** Non-zero if we included \a fd in the write set. */
66 /** The position of this client in the client list. */
67 struct list_head node
;
68 /** The list of pending chunks for this client. */
69 struct chunk_queue
*cq
;
73 * Describes one entry in the blacklist/whitelist of the http sender.
76 /** The address to be black/whitelisted. */
78 /** The netmask for this entry. */
80 /** The position of this entry in the access_perm_list. */
81 struct list_head node
;
84 static int server_fd
= -1, numclients
;
85 static struct sender
*self
;
88 static void http_shutdown_client(struct http_client
*hc
, const char *msg
)
90 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", hc
->name
, hc
->fd
,
95 del_close_on_fork_list(hc
->fd
);
101 static void http_shutdown_clients(void)
103 struct http_client
*hc
, *tmp
;
104 list_for_each_entry_safe(hc
, tmp
, &clients
, node
)
105 http_shutdown_client(hc
, "vss request");
108 static int http_send_msg(struct http_client
*hc
, const char *msg
)
110 int ret
= send_buffer(hc
->fd
, msg
);
113 http_shutdown_client(hc
, "send msg failed");
117 static void http_send_ok_msg(struct http_client
*hc
)
119 PARA_INFO_LOG("sending http ok message to fd %d\n", hc
->fd
);
120 http_send_msg(hc
, HTTP_OK_MSG
);
123 static int http_send_err_msg(struct http_client
*hc
)
125 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc
->fd
);
126 return http_send_msg(hc
, HTTP_ERR_MSG
);
129 static int send_queued_chunks(struct http_client
*hc
)
131 struct queued_chunk
*qc
;
132 while ((qc
= cq_peek(hc
->cq
))) {
135 int ret
= write_ok(hc
->fd
);
137 return ret
? -E_WRITE_OK
: 0;
138 cq_get(qc
, &buf
, &len
);
139 ret
= write(hc
->fd
, buf
, len
);
141 return -E_SEND_QUEUED_CHUNK
;
142 cq_update(hc
->cq
, ret
);
150 static int queue_chunk_or_shutdown(struct http_client
*hc
, long unsigned chunk_num
,
153 int ret
= cq_enqueue(hc
->cq
, chunk_num
, sent
);
155 http_shutdown_client(hc
, "queue error");
159 static void http_send( long unsigned current_chunk
,
160 __a_unused
long unsigned chunks_sent
, const char *buf
, size_t len
)
162 struct http_client
*hc
, *tmp
;
165 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
166 if (hc
->status
!= HTTP_STREAMING
&&
167 hc
->status
!= HTTP_READY_TO_STREAM
)
169 if (hc
->status
== HTTP_READY_TO_STREAM
) {
171 char *hbuf
= vss_get_header(&hlen
);
172 if (hbuf
&& hlen
> 0 && current_chunk
) {
173 /* need to send header */
174 PARA_INFO_LOG("queueing header: %zu\n", hlen
);
175 if (queue_chunk_or_shutdown(hc
, -1U, 0) < 0)
178 PARA_INFO_LOG("no need to queue header\n");
179 hc
->status
= HTTP_STREAMING
;
181 ret
= send_queued_chunks(hc
);
183 http_shutdown_client(hc
, "queue send error");
188 if (!ret
|| write_ok(hc
->fd
) <= 0) {
189 queue_chunk_or_shutdown(hc
, current_chunk
, 0);
192 // PARA_DEBUG_LOG("sending %d -> %s\n", len, remote_name(hc->fd));
193 ret
= write(hc
->fd
, buf
, len
);
194 // PARA_DEBUG_LOG("ret: %d\n", ret);
196 http_shutdown_client(hc
, "send error");
200 queue_chunk_or_shutdown(hc
, current_chunk
, ret
);
204 static int host_in_access_perm_list(struct http_client
*hc
)
206 struct sockaddr_storage ss
;
207 socklen_t sslen
= sizeof(ss
);
209 if (getpeername(hc
->fd
, (struct sockaddr
*)&ss
, &sslen
) < 0) {
210 PARA_ERROR_LOG("can not determine address family: %s\n", strerror(errno
));
211 } else if (ss
.ss_family
== AF_INET
) {
212 /* FIXME: access restriction is (currently) only supported for IPv4 */
213 struct access_info
*ai
, *tmp
;
214 struct in_addr client_addr
= ((struct sockaddr_in
*)&ss
)->sin_addr
;
216 list_for_each_entry_safe(ai
, tmp
, &access_perm_list
, node
) {
217 unsigned mask
= ((~0U) >> ai
->netmask
);
218 if ((client_addr
.s_addr
& mask
) == (ai
->addr
.s_addr
& mask
))
225 static void http_post_select(fd_set
*rfds
, fd_set
*wfds
)
228 struct http_client
*hc
, *tmp
;
231 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
233 // PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
234 switch (hc
->status
) {
235 case HTTP_STREAMING
: /* nothing to do */
236 case HTTP_READY_TO_STREAM
:
238 case HTTP_CONNECTED
: /* need to recv get request */
239 if (hc
->check_r
&& FD_ISSET(hc
->fd
, rfds
)) {
240 if (recv_pattern(hc
->fd
, HTTP_GET_MSG
, MAXLINE
)
242 hc
->status
= HTTP_INVALID_GET_REQUEST
;
244 hc
->status
= HTTP_GOT_GET_REQUEST
;
246 "received get request\n");
250 case HTTP_GOT_GET_REQUEST
: /* need to send ok msg */
251 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
)) {
252 hc
->status
= HTTP_SENT_OK_MSG
;
253 http_send_ok_msg(hc
);
256 case HTTP_INVALID_GET_REQUEST
: /* need to send err msg */
257 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
)) {
258 if (http_send_err_msg(hc
) >= 0)
259 http_shutdown_client(hc
,
260 "invalid get request");
263 case HTTP_SENT_OK_MSG
: /* need to send header? */
264 if (hc
->check_w
&& FD_ISSET(hc
->fd
, wfds
))
265 hc
->status
= HTTP_READY_TO_STREAM
;
269 if (!FD_ISSET(server_fd
, rfds
))
271 hc
= para_calloc(sizeof(struct http_client
));
272 err_msg
= "accept error";
273 hc
->fd
= para_accept(server_fd
, NULL
, 0);
276 hc
->name
= make_message("%s", remote_name(hc
->fd
));
277 PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc
->name
, hc
->fd
);
278 if (conf
.http_max_clients_arg
> 0 && numclients
>=
279 conf
.http_max_clients_arg
) {
280 err_msg
= "server full";
283 match
= host_in_access_perm_list(hc
);
284 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match
);
285 if ((match
&& !conf
.http_default_deny_given
) ||
286 (!match
&& conf
.http_default_deny_given
)) {
287 err_msg
= "permission denied";
290 hc
->status
= HTTP_CONNECTED
;
291 hc
->cq
= cq_new(MAX_BACKLOG
);
293 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients
,
295 para_list_add(&hc
->node
, &clients
);
296 add_close_on_fork_list(hc
->fd
);
297 mark_fd_nonblocking(hc
->fd
);
300 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
307 static void http_pre_select(int *max_fileno
, fd_set
*rfds
, fd_set
*wfds
)
309 struct http_client
*hc
, *tmp
;
313 para_fd_set(server_fd
, rfds
, max_fileno
);
314 list_for_each_entry_safe(hc
, tmp
, &clients
, node
) {
315 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
318 switch (hc
->status
) {
320 case HTTP_READY_TO_STREAM
:
322 case HTTP_CONNECTED
: /* need to recv get request */
323 para_fd_set(hc
->fd
, rfds
, max_fileno
);
326 case HTTP_GOT_GET_REQUEST
: /* need to send ok msg */
327 case HTTP_INVALID_GET_REQUEST
: /* need to send err msg */
328 para_fd_set(hc
->fd
, wfds
, max_fileno
);
331 case HTTP_SENT_OK_MSG
:
333 break; /* wait until server starts playing */
334 para_fd_set(hc
->fd
, wfds
, max_fileno
);
341 static int open_tcp_port(int port
)
345 server_fd
= para_listen(AF_UNSPEC
, IPPROTO_TCP
, port
);
347 http_shutdown_clients();
348 self
->status
= SENDER_OFF
;
351 ret
= mark_fd_nonblocking(server_fd
);
353 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret
));
356 self
->status
= SENDER_ON
;
357 add_close_on_fork_list(server_fd
);
361 static int http_com_on(__a_unused
struct sender_command_data
*scd
)
363 if (self
->status
== SENDER_ON
)
365 return open_tcp_port(conf
.http_port_arg
);
368 static int http_com_off(__a_unused
struct sender_command_data
*scd
)
370 self
->status
= SENDER_OFF
;
373 del_close_on_fork_list(server_fd
);
376 http_shutdown_clients();
380 static void del_perm_list_entry(struct sender_command_data
*scd
)
382 struct access_info
*ai
, *tmp
;
384 list_for_each_entry_safe(ai
, tmp
, &access_perm_list
, node
) {
385 char *nad
= para_strdup(inet_ntoa(ai
->addr
));
386 if (!strcmp(nad
, inet_ntoa(scd
->addr
)) &&
387 ai
->netmask
== scd
->netmask
) {
388 PARA_NOTICE_LOG("removing %s/%i from access list\n",
397 static void add_perm_list_entry(struct sender_command_data
*scd
)
399 struct access_info
*ai
= para_malloc(sizeof(struct access_info
));
400 ai
->addr
= scd
->addr
;
401 ai
->netmask
= scd
->netmask
;
402 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai
->addr
),
404 para_list_add(&ai
->node
, &access_perm_list
);
407 static int http_com_deny(struct sender_command_data
*scd
)
409 if (conf
.http_default_deny_given
)
410 del_perm_list_entry(scd
);
412 add_perm_list_entry(scd
);
416 static int http_com_allow(struct sender_command_data
*scd
)
418 if (conf
.http_default_deny_given
)
419 add_perm_list_entry(scd
);
421 del_perm_list_entry(scd
);
425 static char *http_info(void)
427 char *clnts
= NULL
, *ap
= NULL
, *ret
;
428 struct access_info
*ai
, *tmp_ai
;
429 struct http_client
*hc
, *tmp_hc
;
431 list_for_each_entry_safe(ai
, tmp_ai
, &access_perm_list
, node
) {
432 char *tmp
= make_message("%s%s/%d ", ap
? ap
: "",
433 inet_ntoa(ai
->addr
), ai
->netmask
);
437 list_for_each_entry_safe(hc
, tmp_hc
, &clients
, node
) {
438 char *tmp
= make_message("%s%s ", clnts
? clnts
: "", hc
->name
);
444 "http tcp port: %d\n"
446 "http maximal number of clients: %d%s\n"
447 "http connected clients: %s\n"
448 "http access %s list: %s\n",
449 (self
->status
== SENDER_ON
)? "on" : "off",
452 conf
.http_max_clients_arg
,
453 conf
.http_max_clients_arg
> 0? "" : " (unlimited)",
454 clnts
? clnts
: "(none)",
455 conf
.http_default_deny_given
? "allow" : "deny",
463 static void init_access_control_list(void)
466 struct sender_command_data scd
;
468 INIT_LIST_HEAD(&access_perm_list
);
469 for (i
= 0; i
< conf
.http_access_given
; i
++) {
470 char *arg
= para_strdup(conf
.http_access_arg
[i
]);
471 char *p
= strchr(arg
, '/');
475 if (!inet_pton(AF_INET
, arg
, &scd
.addr
))
477 scd
.netmask
= atoi(++p
);
478 if (scd
.netmask
< 0 || scd
.netmask
> 32)
480 add_perm_list_entry(&scd
);
483 PARA_CRIT_LOG("syntax error for http_access option "
484 "#%d, ignoring\n", i
);
491 static char *http_help(void)
495 "usage: {allow|deny} IP mask\n"
496 "example: allow 127.0.0.1 32\n"
501 * The init function of the http sender.
503 * \param s Pointer to the http sender struct.
505 * It initializes all function pointers of \a s, the client list and the access
506 * control list. If the autostart option was given, the tcp port is opened.
508 void http_send_init(struct sender
*s
)
510 INIT_LIST_HEAD(&clients
);
513 s
->pre_select
= http_pre_select
;
514 s
->post_select
= http_post_select
;
515 s
->shutdown_clients
= http_shutdown_clients
;
517 s
->client_cmds
[SENDER_ON
] = http_com_on
;
518 s
->client_cmds
[SENDER_OFF
] = http_com_off
;
519 s
->client_cmds
[SENDER_DENY
] = http_com_deny
;
520 s
->client_cmds
[SENDER_ALLOW
] = http_com_allow
;
521 s
->client_cmds
[SENDER_ADD
] = NULL
;
522 s
->client_cmds
[SENDER_DELETE
] = NULL
;
524 init_access_control_list();
525 if (!conf
.http_no_autostart_given
)
526 open_tcp_port(conf
.http_port_arg
); /* ignore errors */
527 PARA_DEBUG_LOG("%s", "http sender init complete\n");