2 * Copyright (C) 2005-2006 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file http_send.c paraslash's http sender */
22 #include "server.cmdline.h"
28 #include "close_on_fork.h"
34 /** \cond convert sock_addr_in to ascii */
35 #define CLIENT_ADDR(hc) inet_ntoa((hc)->addr.sin_addr)
36 /* get the port number of a struct http_client */
37 #define CLIENT_PORT(hc) (hc)->addr.sin_port
38 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
41 extern struct gengetopt_args_info conf;
43 /** the possible states of a client from the server's POV */
50 HTTP_INVALID_GET_REQUEST
53 /** clients will be kicked if there are more than that many bytes pending */
54 #define MAX_BACKLOG 40000
55 /** the list of connected clients **/
56 static struct list_head clients;
57 /** the whitelist/blacklist */
58 static struct list_head access_perm_list;
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 packets for this client */
75 struct list_head packet_queue;
76 /** the number of pending bytes for this client */
77 unsigned long pq_bytes;
81 * describes one queued data packet for a client
83 * The send function of the http sender checks each client fd for writing. If a
84 * client fd is not ready, it tries to queue that packet for this client until
85 * the number of queued bytes exceeds \p MAX_BACKLOG.
87 struct queued_packet {
88 /** the length of the packet in bytes */
90 /** pointer to the packet data */
92 /** position of the packet in the packet list */
93 struct list_head node;
97 * describes one entry in the blacklist/whitelist of the http sender
100 /** the address to be black/whitelisted */
102 /** the netmask for this entry */
104 /** the position of this entry in the access_perm_list */
105 struct list_head node;
108 static int server_fd = -1, numclients;
109 static struct sender *self;
112 static void http_shutdown_client(struct http_client *hc, const char *msg)
114 struct queued_packet *qp, *tmp;
115 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
120 list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
130 static void http_shutdown_clients_real(void)
132 struct http_client *hc, *tmp;
133 list_for_each_entry_safe(hc, tmp, &clients, node)
134 http_shutdown_client(hc, "afs request");
136 static void http_shutdown_clients(void)
138 struct http_client *hc, *tmp;
139 list_for_each_entry_safe(hc, tmp, &clients, node)
140 if (hc->status == HTTP_STREAMING)
141 http_shutdown_client(hc, "afs request");
144 static int http_send_msg(struct http_client *hc, const char *msg)
146 int ret = send_buffer(hc->fd, msg);
149 http_shutdown_client(hc, "send msg failed");
153 static void http_send_ok_msg(struct http_client *hc)
155 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
156 http_send_msg(hc, HTTP_OK_MSG);
159 static int http_send_err_msg(struct http_client *hc)
161 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
162 return http_send_msg(hc, HTTP_ERR_MSG);
165 static int queue_packet(struct http_client *hc, const char *buf, size_t len)
167 struct queued_packet *qp;
168 if (hc->pq_bytes + len > MAX_BACKLOG) {
169 http_shutdown_client(hc, "packet queue overrun");
172 qp = para_malloc(sizeof(struct queued_packet));
174 qp->packet = para_malloc(len);
175 memcpy(qp->packet, buf, len);
177 list_add_tail(&qp->node, &hc->packet_queue);
178 PARA_INFO_LOG("%lu bytes queued for fd %d\n", hc->pq_bytes, hc->fd);
182 static int send_queued_packets(struct http_client *hc)
185 struct queued_packet *qp, *tmp;
187 if (list_empty(&hc->packet_queue))
189 list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
190 ret = write_ok(hc->fd);
192 return ret? -E_WRITE_OK : 0;
193 ret = write(hc->fd, qp->packet, qp->len);
196 if (ret != qp->len) {
198 memmove(qp->packet, qp->packet + ret, qp->len);
201 hc->pq_bytes -= qp->len;
209 static void http_send( long unsigned current_chunk,
210 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
212 struct http_client *hc, *tmp;
215 list_for_each_entry_safe(hc, tmp, &clients, node) {
216 if (hc->status != HTTP_STREAMING &&
217 hc->status != HTTP_READY_TO_STREAM)
219 if (hc->status == HTTP_READY_TO_STREAM) {
221 char *buf = afs_get_header(&hlen);
222 if (buf && hlen > 0 && current_chunk) {
223 /* need to send header */
224 PARA_INFO_LOG("queueing header: %d\n", hlen);
225 if (queue_packet(hc, buf, hlen) < 0)
228 PARA_INFO_LOG("%s", "no need to queue header\n");
229 hc->status = HTTP_STREAMING;
231 ret = send_queued_packets(hc);
233 http_shutdown_client(hc, "send error");
238 if (!ret || write_ok(hc->fd) <= 0) {
239 PARA_INFO_LOG("fd %d not ready (%lu bytes queued),"
240 " trying to queue packet\n", hc->fd,
242 queue_packet(hc, buf, len);
245 // PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
246 ret = write(hc->fd, buf, len);
248 http_shutdown_client(hc, "send error");
252 queue_packet(hc, buf + ret, len - ret);
256 static int host_in_access_perm_list(struct http_client *hc)
258 struct access_info *ai, *tmp;
259 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
260 unsigned mask = ((~0) >> ai->netmask);
261 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
267 static void http_post_select(fd_set *rfds, fd_set *wfds)
270 struct http_client *hc, *tmp;
273 list_for_each_entry_safe(hc, tmp, &clients, node) {
275 // PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
276 switch (hc->status) {
277 case HTTP_STREAMING: /* nothing to do */
278 case HTTP_READY_TO_STREAM:
280 case HTTP_CONNECTED: /* need to recv get request */
281 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
282 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
284 hc->status = HTTP_INVALID_GET_REQUEST;
286 hc->status = HTTP_GOT_GET_REQUEST;
288 "received get request\n");
292 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
293 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
294 hc->status = HTTP_SENT_OK_MSG;
295 http_send_ok_msg(hc);
298 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
299 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
300 if (http_send_err_msg(hc) >= 0)
301 http_shutdown_client(hc,
302 "invalid get request");
305 case HTTP_SENT_OK_MSG: /* need to send header? */
306 if (hc->check_w && FD_ISSET(hc->fd, wfds))
307 hc->status = HTTP_READY_TO_STREAM;
311 if (!FD_ISSET(server_fd, rfds))
313 hc = para_calloc(sizeof(struct http_client));
314 err_msg = "accept error";
315 hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
318 PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
319 if (conf.http_max_clients_arg > 0 && numclients >=
320 conf.http_max_clients_arg) {
321 err_msg = "server full";
324 match = host_in_access_perm_list(hc);
325 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
326 if ((match && !conf.http_default_deny_given) ||
327 (!match && conf.http_default_deny_given)) {
328 err_msg = "permission denied";
331 hc->status = HTTP_CONNECTED;
332 INIT_LIST_HEAD(&hc->packet_queue);
333 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
334 CLIENT_ADDR(hc), hc->fd);
336 list_add(&hc->node, &clients);
339 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
340 CLIENT_ADDR(hc), err_msg);
346 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
348 struct http_client *hc, *tmp;
352 para_fd_set(server_fd, rfds, max_fileno);
353 list_for_each_entry_safe(hc, tmp, &clients, node) {
354 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
357 switch (hc->status) {
359 case HTTP_READY_TO_STREAM:
361 case HTTP_CONNECTED: /* need to recv get request */
362 para_fd_set(hc->fd, rfds, max_fileno);
365 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
366 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
367 para_fd_set(hc->fd, wfds, max_fileno);
370 case HTTP_SENT_OK_MSG:
372 break; /* wait until server starts playing */
373 para_fd_set(hc->fd, wfds, max_fileno);
380 static int open_tcp_port(int port)
382 server_fd = init_tcp_socket(port);
384 http_shutdown_clients_real();
385 self->status = SENDER_OFF;
388 self->status = SENDER_ON;
389 add_close_on_fork_list(server_fd);
393 static int http_com_on(__a_unused struct sender_command_data *scd)
395 if (self->status == SENDER_ON)
397 return open_tcp_port(conf.http_port_arg);
400 static int http_com_off(__a_unused struct sender_command_data *scd)
402 self->status = SENDER_OFF;
405 del_close_on_fork_list(server_fd);
408 http_shutdown_clients_real();
412 static void del_perm_list_entry(struct sender_command_data *scd)
414 struct access_info *ai, *tmp;
416 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
417 char *nad = para_strdup(inet_ntoa(ai->addr));
418 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
419 ai->netmask == scd->netmask) {
420 PARA_NOTICE_LOG("removing %s/%i from access list\n",
429 static void add_perm_list_entry(struct sender_command_data *scd)
431 struct access_info *ai = para_malloc(sizeof(struct access_info));
432 ai->addr = scd->addr;
433 ai->netmask = scd->netmask;
434 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
436 list_add(&ai->node, &access_perm_list);
439 static int http_com_deny(struct sender_command_data *scd)
441 if (conf.http_default_deny_given)
442 del_perm_list_entry(scd);
444 add_perm_list_entry(scd);
448 static int http_com_allow(struct sender_command_data *scd)
450 if (conf.http_default_deny_given)
451 add_perm_list_entry(scd);
453 del_perm_list_entry(scd);
457 static char *http_info(void)
459 char *clnts = NULL, *ap = NULL, *ret;
460 struct access_info *ai, *tmp_ai;
461 struct http_client *hc, *tmp_hc;
463 list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
464 char *tmp = make_message("%s%s/%d ", ap? ap : "",
465 inet_ntoa(ai->addr), ai->netmask);
469 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
470 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
471 CLIENT_ADDR(hc), CLIENT_PORT(hc));
477 "http tcp port: %d\n"
479 "http maximal number of clients: %d%s\n"
480 "http connected clients: %s\n"
481 "http access %s list: %s\n",
482 (self->status == SENDER_ON)? "on" : "off",
485 conf.http_max_clients_arg,
486 conf.http_max_clients_arg > 0? "" : " (unlimited)",
487 clnts? clnts : "(none)",
488 conf.http_default_deny_given? "allow" : "deny",
496 static void init_access_control_list(void)
499 struct sender_command_data scd;
501 INIT_LIST_HEAD(&access_perm_list);
502 for (i = 0; i < conf.http_access_given; i++) {
503 char *arg = para_strdup(conf.http_access_arg[i]);
504 char *p = strchr(arg, '/');
508 if (!inet_aton(arg, &scd.addr))
510 scd.netmask = atoi(++p);
511 if (scd.netmask < 0 || scd.netmask > 32)
513 add_perm_list_entry(&scd);
516 PARA_CRIT_LOG("syntax error for http_access option "
517 "#%d, ignoring\n", i);
524 static char *http_help(void)
528 "usage: {allow|deny} IP mask\n"
529 "example: allow 127.0.0.1 32\n"
534 * the init function of the http sender
536 * \param s pointer to the http sender struct
538 * It initializes all function pointers of \a s, init the client list and the
539 * acess control list as well. If autostart is wanted, open the tcp port.
541 void http_send_init(struct sender *s)
543 INIT_LIST_HEAD(&clients);
546 s->pre_select = http_pre_select;
547 s->post_select = http_post_select;
548 s->shutdown_clients = http_shutdown_clients;
550 s->client_cmds[SENDER_ON] = http_com_on;
551 s->client_cmds[SENDER_OFF] = http_com_off;
552 s->client_cmds[SENDER_DENY] = http_com_deny;
553 s->client_cmds[SENDER_ALLOW] = http_com_allow;
554 s->client_cmds[SENDER_ADD] = NULL;
555 s->client_cmds[SENDER_DELETE] = NULL;
557 init_access_control_list();
558 if (!conf.http_no_autostart_given)
559 open_tcp_port(conf.http_port_arg); /* ignore errors */
560 PARA_DEBUG_LOG("%s", "http sender init complete\n");