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 */
13 #include "server.cmdline.h"
20 #include "close_on_fork.h"
25 #include "chunk_queue.h"
27 /** \cond convert sock_addr_in to ascii */
28 #define CLIENT_ADDR(hc) inet_ntoa((hc)->addr.sin_addr)
29 /* get the port number of a struct http_client */
30 #define CLIENT_PORT(hc) (hc)->addr.sin_port
31 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
34 /** The possible states of a client from the server's POV. */
36 /** We accepted the connection on the tcp socket. */
38 /** Successfully received the get request. */
40 /** We sent the OK message back to the client. */
42 /** Connection established, we might need to send the audio file header. */
44 /** Connection is ready for sending audio data. */
46 /** We didn't receive a valid get request. */
47 HTTP_INVALID_GET_REQUEST
50 /** Clients will be kicked if there are more than that many bytes pending. */
51 #define MAX_BACKLOG 400000
52 /** The list of connected clients. */
53 static struct list_head clients;
54 /** The whitelist/blacklist. */
55 static struct list_head access_perm_list;
57 /** Describes one client that connected the tcp port of the http sender. */
59 /** The file descriptor of the client. */
61 /** Address information about the client. */
62 struct sockaddr_in addr;
63 /** The client's current status. */
64 enum http_status status;
65 /** Non-zero if we included \a fd in the read set.*/
67 /** Non-zero if we included \a fd in the write set. */
69 /** The position of this client in the client list. */
70 struct list_head node;
71 /** The list of pending chunks for this client. */
72 struct chunk_queue *cq;
76 * Describes one entry in the blacklist/whitelist of the http sender.
79 /** The address to be black/whitelisted. */
81 /** The netmask for this entry. */
83 /** The position of this entry in the access_perm_list. */
84 struct list_head node;
87 static int server_fd = -1, numclients;
88 static struct sender *self;
91 static void http_shutdown_client(struct http_client *hc, const char *msg)
93 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
97 del_close_on_fork_list(hc->fd);
103 static void http_shutdown_clients(void)
105 struct http_client *hc, *tmp;
106 list_for_each_entry_safe(hc, tmp, &clients, node)
107 http_shutdown_client(hc, "vss request");
110 static int http_send_msg(struct http_client *hc, const char *msg)
112 int ret = send_buffer(hc->fd, msg);
115 http_shutdown_client(hc, "send msg failed");
119 static void http_send_ok_msg(struct http_client *hc)
121 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
122 http_send_msg(hc, HTTP_OK_MSG);
125 static int http_send_err_msg(struct http_client *hc)
127 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
128 return http_send_msg(hc, HTTP_ERR_MSG);
131 static int send_queued_chunks(struct http_client *hc)
133 struct queued_chunk *qc;
134 while ((qc = cq_peek(hc->cq))) {
137 int ret = write_ok(hc->fd);
139 return ret? -E_WRITE_OK : 0;
140 cq_get(qc, &buf, &len);
141 ret = write(hc->fd, buf, len);
143 return -E_SEND_QUEUED_CHUNK;
144 cq_update(hc->cq, ret);
152 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
155 int ret = cq_enqueue(hc->cq, chunk_num, sent);
157 http_shutdown_client(hc, "queue error");
161 static void http_send( long unsigned current_chunk,
162 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
164 struct http_client *hc, *tmp;
167 list_for_each_entry_safe(hc, tmp, &clients, node) {
168 if (hc->status != HTTP_STREAMING &&
169 hc->status != HTTP_READY_TO_STREAM)
171 if (hc->status == HTTP_READY_TO_STREAM) {
173 char *hbuf = vss_get_header(&hlen);
174 if (hbuf && hlen > 0 && current_chunk) {
175 /* need to send header */
176 PARA_INFO_LOG("queueing header: %zu\n", hlen);
177 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
180 PARA_INFO_LOG("no need to queue header\n");
181 hc->status = HTTP_STREAMING;
183 ret = send_queued_chunks(hc);
185 http_shutdown_client(hc, "queue send error");
190 if (!ret || write_ok(hc->fd) <= 0) {
191 queue_chunk_or_shutdown(hc, current_chunk, 0);
194 // PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
195 ret = write(hc->fd, buf, len);
196 // PARA_DEBUG_LOG("ret: %d\n", ret);
198 http_shutdown_client(hc, "send error");
202 queue_chunk_or_shutdown(hc, current_chunk, ret);
206 static int host_in_access_perm_list(struct http_client *hc)
208 struct access_info *ai, *tmp;
209 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
210 unsigned mask = ((~0U) >> ai->netmask);
211 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
217 static void http_post_select(fd_set *rfds, fd_set *wfds)
220 struct http_client *hc, *tmp;
223 list_for_each_entry_safe(hc, tmp, &clients, node) {
225 // PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
226 switch (hc->status) {
227 case HTTP_STREAMING: /* nothing to do */
228 case HTTP_READY_TO_STREAM:
230 case HTTP_CONNECTED: /* need to recv get request */
231 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
232 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
234 hc->status = HTTP_INVALID_GET_REQUEST;
236 hc->status = HTTP_GOT_GET_REQUEST;
238 "received get request\n");
242 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
243 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
244 hc->status = HTTP_SENT_OK_MSG;
245 http_send_ok_msg(hc);
248 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
249 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
250 if (http_send_err_msg(hc) >= 0)
251 http_shutdown_client(hc,
252 "invalid get request");
255 case HTTP_SENT_OK_MSG: /* need to send header? */
256 if (hc->check_w && FD_ISSET(hc->fd, wfds))
257 hc->status = HTTP_READY_TO_STREAM;
261 if (!FD_ISSET(server_fd, rfds))
263 hc = para_calloc(sizeof(struct http_client));
264 err_msg = "accept error";
265 hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
268 PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
269 if (conf.http_max_clients_arg > 0 && numclients >=
270 conf.http_max_clients_arg) {
271 err_msg = "server full";
274 match = host_in_access_perm_list(hc);
275 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
276 if ((match && !conf.http_default_deny_given) ||
277 (!match && conf.http_default_deny_given)) {
278 err_msg = "permission denied";
281 hc->status = HTTP_CONNECTED;
282 hc->cq = cq_new(MAX_BACKLOG);
283 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
284 CLIENT_ADDR(hc), hc->fd);
286 para_list_add(&hc->node, &clients);
287 add_close_on_fork_list(hc->fd);
288 mark_fd_nonblock(hc->fd);
291 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
292 CLIENT_ADDR(hc), err_msg);
298 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
300 struct http_client *hc, *tmp;
304 para_fd_set(server_fd, rfds, max_fileno);
305 list_for_each_entry_safe(hc, tmp, &clients, node) {
306 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
309 switch (hc->status) {
311 case HTTP_READY_TO_STREAM:
313 case HTTP_CONNECTED: /* need to recv get request */
314 para_fd_set(hc->fd, rfds, max_fileno);
317 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
318 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
319 para_fd_set(hc->fd, wfds, max_fileno);
322 case HTTP_SENT_OK_MSG:
324 break; /* wait until server starts playing */
325 para_fd_set(hc->fd, wfds, max_fileno);
332 static int open_tcp_port(int port)
336 server_fd = init_tcp_socket(port);
338 http_shutdown_clients();
339 self->status = SENDER_OFF;
342 ret = mark_fd_nonblock(server_fd);
344 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
347 self->status = SENDER_ON;
348 add_close_on_fork_list(server_fd);
352 static int http_com_on(__a_unused struct sender_command_data *scd)
354 if (self->status == SENDER_ON)
356 return open_tcp_port(conf.http_port_arg);
359 static int http_com_off(__a_unused struct sender_command_data *scd)
361 self->status = SENDER_OFF;
364 del_close_on_fork_list(server_fd);
367 http_shutdown_clients();
371 static void del_perm_list_entry(struct sender_command_data *scd)
373 struct access_info *ai, *tmp;
375 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
376 char *nad = para_strdup(inet_ntoa(ai->addr));
377 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
378 ai->netmask == scd->netmask) {
379 PARA_NOTICE_LOG("removing %s/%i from access list\n",
388 static void add_perm_list_entry(struct sender_command_data *scd)
390 struct access_info *ai = para_malloc(sizeof(struct access_info));
391 ai->addr = scd->addr;
392 ai->netmask = scd->netmask;
393 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
395 para_list_add(&ai->node, &access_perm_list);
398 static int http_com_deny(struct sender_command_data *scd)
400 if (conf.http_default_deny_given)
401 del_perm_list_entry(scd);
403 add_perm_list_entry(scd);
407 static int http_com_allow(struct sender_command_data *scd)
409 if (conf.http_default_deny_given)
410 add_perm_list_entry(scd);
412 del_perm_list_entry(scd);
416 static char *http_info(void)
418 char *clnts = NULL, *ap = NULL, *ret;
419 struct access_info *ai, *tmp_ai;
420 struct http_client *hc, *tmp_hc;
422 list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
423 char *tmp = make_message("%s%s/%d ", ap? ap : "",
424 inet_ntoa(ai->addr), ai->netmask);
428 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
429 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
430 CLIENT_ADDR(hc), CLIENT_PORT(hc));
436 "http tcp port: %d\n"
438 "http maximal number of clients: %d%s\n"
439 "http connected clients: %s\n"
440 "http access %s list: %s\n",
441 (self->status == SENDER_ON)? "on" : "off",
444 conf.http_max_clients_arg,
445 conf.http_max_clients_arg > 0? "" : " (unlimited)",
446 clnts? clnts : "(none)",
447 conf.http_default_deny_given? "allow" : "deny",
455 static void init_access_control_list(void)
458 struct sender_command_data scd;
460 INIT_LIST_HEAD(&access_perm_list);
461 for (i = 0; i < conf.http_access_given; i++) {
462 char *arg = para_strdup(conf.http_access_arg[i]);
463 char *p = strchr(arg, '/');
467 if (!inet_pton(AF_INET, arg, &scd.addr))
469 scd.netmask = atoi(++p);
470 if (scd.netmask < 0 || scd.netmask > 32)
472 add_perm_list_entry(&scd);
475 PARA_CRIT_LOG("syntax error for http_access option "
476 "#%d, ignoring\n", i);
483 static char *http_help(void)
487 "usage: {allow|deny} IP mask\n"
488 "example: allow 127.0.0.1 32\n"
493 * The init function of the http sender.
495 * \param s Pointer to the http sender struct.
497 * It initializes all function pointers of \a s, the client list and the access
498 * control list. If the autostart option was given, the tcp port is opened.
500 void http_send_init(struct sender *s)
502 INIT_LIST_HEAD(&clients);
505 s->pre_select = http_pre_select;
506 s->post_select = http_post_select;
507 s->shutdown_clients = http_shutdown_clients;
509 s->client_cmds[SENDER_ON] = http_com_on;
510 s->client_cmds[SENDER_OFF] = http_com_off;
511 s->client_cmds[SENDER_DENY] = http_com_deny;
512 s->client_cmds[SENDER_ALLOW] = http_com_allow;
513 s->client_cmds[SENDER_ADD] = NULL;
514 s->client_cmds[SENDER_DELETE] = NULL;
516 init_access_control_list();
517 if (!conf.http_no_autostart_given)
518 open_tcp_port(conf.http_port_arg); /* ignore errors */
519 PARA_DEBUG_LOG("%s", "http sender init complete\n");