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 /** \cond convert sock_addr_in to ascii */
29 #define CLIENT_ADDR(hc) inet_ntoa((hc)->addr.sin_addr)
30 /* get the port number of a struct http_client */
31 #define CLIENT_PORT(hc) (hc)->addr.sin_port
32 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
35 /** The possible states of a client from the server's POV. */
37 /** We accepted the connection on the tcp socket. */
39 /** Successfully received the get request. */
41 /** We sent the OK message back to the client. */
43 /** Connection established, we might need to send the audio file header. */
45 /** Connection is ready for sending audio data. */
47 /** We didn't receive a valid get request. */
48 HTTP_INVALID_GET_REQUEST
51 /** Clients will be kicked if there are more than that many bytes pending. */
52 #define MAX_BACKLOG 400000
53 /** The list of connected clients. */
54 static struct list_head clients;
55 /** The whitelist/blacklist. */
56 static struct list_head access_perm_list;
58 /** Describes one client that connected the tcp port of the http sender. */
60 /** The file descriptor of the client. */
62 /** Address information about the client. */
63 struct sockaddr_in addr;
64 /** The client's current status. */
65 enum http_status status;
66 /** Non-zero if we included \a fd in the read set.*/
68 /** Non-zero if we included \a fd in the write set. */
70 /** The position of this client in the client list. */
71 struct list_head node;
72 /** The list of pending chunks for this client. */
73 struct chunk_queue *cq;
77 * Describes one entry in the blacklist/whitelist of the http sender.
80 /** The address to be black/whitelisted. */
82 /** The netmask for this entry. */
84 /** The position of this entry in the access_perm_list. */
85 struct list_head node;
88 static int server_fd = -1, numclients;
89 static struct sender *self;
92 static void http_shutdown_client(struct http_client *hc, const char *msg)
94 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
98 del_close_on_fork_list(hc->fd);
104 static void http_shutdown_clients(void)
106 struct http_client *hc, *tmp;
107 list_for_each_entry_safe(hc, tmp, &clients, node)
108 http_shutdown_client(hc, "vss request");
111 static int http_send_msg(struct http_client *hc, const char *msg)
113 int ret = send_buffer(hc->fd, msg);
116 http_shutdown_client(hc, "send msg failed");
120 static void http_send_ok_msg(struct http_client *hc)
122 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
123 http_send_msg(hc, HTTP_OK_MSG);
126 static int http_send_err_msg(struct http_client *hc)
128 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
129 return http_send_msg(hc, HTTP_ERR_MSG);
132 static int send_queued_chunks(struct http_client *hc)
134 struct queued_chunk *qc;
135 while ((qc = cq_peek(hc->cq))) {
138 int ret = write_ok(hc->fd);
140 return ret? -E_WRITE_OK : 0;
141 cq_get(qc, &buf, &len);
142 ret = write(hc->fd, buf, len);
144 return -E_SEND_QUEUED_CHUNK;
145 cq_update(hc->cq, ret);
153 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
156 int ret = cq_enqueue(hc->cq, chunk_num, sent);
158 http_shutdown_client(hc, "queue error");
162 static void http_send( long unsigned current_chunk,
163 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
165 struct http_client *hc, *tmp;
168 list_for_each_entry_safe(hc, tmp, &clients, node) {
169 if (hc->status != HTTP_STREAMING &&
170 hc->status != HTTP_READY_TO_STREAM)
172 if (hc->status == HTTP_READY_TO_STREAM) {
174 char *hbuf = vss_get_header(&hlen);
175 if (hbuf && hlen > 0 && current_chunk) {
176 /* need to send header */
177 PARA_INFO_LOG("queueing header: %zu\n", hlen);
178 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
181 PARA_INFO_LOG("no need to queue header\n");
182 hc->status = HTTP_STREAMING;
184 ret = send_queued_chunks(hc);
186 http_shutdown_client(hc, "queue send error");
191 if (!ret || write_ok(hc->fd) <= 0) {
192 queue_chunk_or_shutdown(hc, current_chunk, 0);
195 // PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
196 ret = write(hc->fd, buf, len);
197 // PARA_DEBUG_LOG("ret: %d\n", ret);
199 http_shutdown_client(hc, "send error");
203 queue_chunk_or_shutdown(hc, current_chunk, ret);
207 static int host_in_access_perm_list(struct http_client *hc)
209 struct access_info *ai, *tmp;
210 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
211 unsigned mask = ((~0U) >> ai->netmask);
212 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
218 static void http_post_select(fd_set *rfds, fd_set *wfds)
221 struct http_client *hc, *tmp;
224 list_for_each_entry_safe(hc, tmp, &clients, node) {
226 // PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
227 switch (hc->status) {
228 case HTTP_STREAMING: /* nothing to do */
229 case HTTP_READY_TO_STREAM:
231 case HTTP_CONNECTED: /* need to recv get request */
232 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
233 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
235 hc->status = HTTP_INVALID_GET_REQUEST;
237 hc->status = HTTP_GOT_GET_REQUEST;
239 "received get request\n");
243 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
244 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
245 hc->status = HTTP_SENT_OK_MSG;
246 http_send_ok_msg(hc);
249 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
250 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
251 if (http_send_err_msg(hc) >= 0)
252 http_shutdown_client(hc,
253 "invalid get request");
256 case HTTP_SENT_OK_MSG: /* need to send header? */
257 if (hc->check_w && FD_ISSET(hc->fd, wfds))
258 hc->status = HTTP_READY_TO_STREAM;
262 if (!FD_ISSET(server_fd, rfds))
264 hc = para_calloc(sizeof(struct http_client));
265 err_msg = "accept error";
266 hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
269 PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
270 if (conf.http_max_clients_arg > 0 && numclients >=
271 conf.http_max_clients_arg) {
272 err_msg = "server full";
275 match = host_in_access_perm_list(hc);
276 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
277 if ((match && !conf.http_default_deny_given) ||
278 (!match && conf.http_default_deny_given)) {
279 err_msg = "permission denied";
282 hc->status = HTTP_CONNECTED;
283 hc->cq = cq_new(MAX_BACKLOG);
284 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
285 CLIENT_ADDR(hc), hc->fd);
287 para_list_add(&hc->node, &clients);
288 add_close_on_fork_list(hc->fd);
289 mark_fd_nonblock(hc->fd);
292 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
293 CLIENT_ADDR(hc), err_msg);
299 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
301 struct http_client *hc, *tmp;
305 para_fd_set(server_fd, rfds, max_fileno);
306 list_for_each_entry_safe(hc, tmp, &clients, node) {
307 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
310 switch (hc->status) {
312 case HTTP_READY_TO_STREAM:
314 case HTTP_CONNECTED: /* need to recv get request */
315 para_fd_set(hc->fd, rfds, max_fileno);
318 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
319 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
320 para_fd_set(hc->fd, wfds, max_fileno);
323 case HTTP_SENT_OK_MSG:
325 break; /* wait until server starts playing */
326 para_fd_set(hc->fd, wfds, max_fileno);
333 static int open_tcp_port(int port)
337 server_fd = init_tcp_socket(port);
339 http_shutdown_clients();
340 self->status = SENDER_OFF;
343 ret = mark_fd_nonblock(server_fd);
345 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
348 self->status = SENDER_ON;
349 add_close_on_fork_list(server_fd);
353 static int http_com_on(__a_unused struct sender_command_data *scd)
355 if (self->status == SENDER_ON)
357 return open_tcp_port(conf.http_port_arg);
360 static int http_com_off(__a_unused struct sender_command_data *scd)
362 self->status = SENDER_OFF;
365 del_close_on_fork_list(server_fd);
368 http_shutdown_clients();
372 static void del_perm_list_entry(struct sender_command_data *scd)
374 struct access_info *ai, *tmp;
376 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
377 char *nad = para_strdup(inet_ntoa(ai->addr));
378 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
379 ai->netmask == scd->netmask) {
380 PARA_NOTICE_LOG("removing %s/%i from access list\n",
389 static void add_perm_list_entry(struct sender_command_data *scd)
391 struct access_info *ai = para_malloc(sizeof(struct access_info));
392 ai->addr = scd->addr;
393 ai->netmask = scd->netmask;
394 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
396 para_list_add(&ai->node, &access_perm_list);
399 static int http_com_deny(struct sender_command_data *scd)
401 if (conf.http_default_deny_given)
402 del_perm_list_entry(scd);
404 add_perm_list_entry(scd);
408 static int http_com_allow(struct sender_command_data *scd)
410 if (conf.http_default_deny_given)
411 add_perm_list_entry(scd);
413 del_perm_list_entry(scd);
417 static char *http_info(void)
419 char *clnts = NULL, *ap = NULL, *ret;
420 struct access_info *ai, *tmp_ai;
421 struct http_client *hc, *tmp_hc;
423 list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
424 char *tmp = make_message("%s%s/%d ", ap? ap : "",
425 inet_ntoa(ai->addr), ai->netmask);
429 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
430 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
431 CLIENT_ADDR(hc), CLIENT_PORT(hc));
437 "http tcp port: %d\n"
439 "http maximal number of clients: %d%s\n"
440 "http connected clients: %s\n"
441 "http access %s list: %s\n",
442 (self->status == SENDER_ON)? "on" : "off",
445 conf.http_max_clients_arg,
446 conf.http_max_clients_arg > 0? "" : " (unlimited)",
447 clnts? clnts : "(none)",
448 conf.http_default_deny_given? "allow" : "deny",
456 static void init_access_control_list(void)
459 struct sender_command_data scd;
461 INIT_LIST_HEAD(&access_perm_list);
462 for (i = 0; i < conf.http_access_given; i++) {
463 char *arg = para_strdup(conf.http_access_arg[i]);
464 char *p = strchr(arg, '/');
468 if (!inet_pton(AF_INET, arg, &scd.addr))
470 scd.netmask = atoi(++p);
471 if (scd.netmask < 0 || scd.netmask > 32)
473 add_perm_list_entry(&scd);
476 PARA_CRIT_LOG("syntax error for http_access option "
477 "#%d, ignoring\n", i);
484 static char *http_help(void)
488 "usage: {allow|deny} IP mask\n"
489 "example: allow 127.0.0.1 32\n"
494 * The init function of the http sender.
496 * \param s Pointer to the http sender struct.
498 * It initializes all function pointers of \a s, the client list and the access
499 * control list. If the autostart option was given, the tcp port is opened.
501 void http_send_init(struct sender *s)
503 INIT_LIST_HEAD(&clients);
506 s->pre_select = http_pre_select;
507 s->post_select = http_post_select;
508 s->shutdown_clients = http_shutdown_clients;
510 s->client_cmds[SENDER_ON] = http_com_on;
511 s->client_cmds[SENDER_OFF] = http_com_off;
512 s->client_cmds[SENDER_DENY] = http_com_deny;
513 s->client_cmds[SENDER_ALLOW] = http_com_allow;
514 s->client_cmds[SENDER_ADD] = NULL;
515 s->client_cmds[SENDER_DELETE] = NULL;
517 init_access_control_list();
518 if (!conf.http_no_autostart_given)
519 open_tcp_port(conf.http_port_arg); /* ignore errors */
520 PARA_DEBUG_LOG("%s", "http sender init complete\n");