414c37530a66a6f93171fc0579dcaafd7e8f7df0
[paraslash.git] / http_send.c
1 /*
2 * Copyright (C) 2005-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file http_send.c paraslash's http sender */
8
9
10 #include "server.cmdline.h"
11 #include "server.h"
12 #include "http.h"
13 #include "vss.h"
14 #include "send.h"
15 #include "list.h"
16 #include "close_on_fork.h"
17 #include "error.h"
18 #include "net.h"
19 #include "string.h"
20 #include "fd.h"
21
22 /** \cond convert sock_addr_in to ascii */
23 #define CLIENT_ADDR(hc) inet_ntoa((hc)->addr.sin_addr)
24 /* get the port number of a struct http_client */
25 #define CLIENT_PORT(hc) (hc)->addr.sin_port
26 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
27 /** \endcond */
28
29 /** The possible states of a client from the server's POV. */
30 enum http_status {
31 /** We accepted the connection on the tcp socket. */
32 HTTP_CONNECTED,
33 /** Successfully received the get request. */
34 HTTP_GOT_GET_REQUEST,
35 /** We sent the OK message back to the client. */
36 HTTP_SENT_OK_MSG,
37 /** Connection established, we might need to send the audio file header. */
38 HTTP_READY_TO_STREAM,
39 /** Connection is ready for sending audio data. */
40 HTTP_STREAMING,
41 /** We didn't receive a valid get request. */
42 HTTP_INVALID_GET_REQUEST
43 };
44
45 /** Clients will be kicked if there are more than that many bytes pending. */
46 #define MAX_BACKLOG 40000
47 /** The list of connected clients. */
48 static struct list_head clients;
49 /** The whitelist/blacklist. */
50 static struct list_head access_perm_list;
51
52 /** Describes one client that connected the tcp port of the http sender. */
53 struct http_client {
54 /** The file descriptor of the client. */
55 int fd;
56 /** Address information about the client. */
57 struct sockaddr_in addr;
58 /** The client's current status. */
59 enum http_status status;
60 /** Non-zero if we included \a fd in the read set.*/
61 int check_r;
62 /** Non-zero if we included \a fd in the write set. */
63 int check_w;
64 /** The position of this client in the client list. */
65 struct list_head node;
66 /** The list of pending chunks for this client. */
67 struct list_head chunk_queue;
68 /** The number of pending bytes for this client. */
69 unsigned long cq_bytes;
70 };
71
72 /**
73 * Describes one queued chunk of the chunk queue.
74 *
75 * The send function of the http sender checks each client fd for writing. If a
76 * client fd is not ready, it tries to queue that chunk for this client until
77 * the number of queued bytes exceeds \p MAX_BACKLOG.
78 */
79 struct queued_chunk {
80 /** The number of the queued chunk, -1U means header. */
81 unsigned chunk_num;
82 /** The number of bytes already sent. */
83 unsigned sent;
84 /** Position of the chunk in the chunk queue. */
85 struct list_head node;
86 };
87
88 /**
89 * Describes one entry in the blacklist/whitelist of the http sender.
90 */
91 struct access_info {
92 /** The address to be black/whitelisted. */
93 struct in_addr addr;
94 /** The netmask for this entry. */
95 unsigned netmask;
96 /** The position of this entry in the access_perm_list. */
97 struct list_head node;
98 };
99
100 static int server_fd = -1, numclients;
101 static struct sender *self;
102
103 static void http_shutdown_client(struct http_client *hc, const char *msg)
104 {
105 struct queued_chunk *qc, *tmp;
106 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
107 hc->fd, msg);
108 numclients--;
109 close(hc->fd);
110 del_close_on_fork_list(hc->fd);
111 list_for_each_entry_safe(qc, tmp, &hc->chunk_queue, node) {
112 list_del(&qc->node);
113 free(qc);
114 }
115 list_del(&hc->node);
116 free(hc);
117 }
118
119 static void http_shutdown_clients(void)
120 {
121 struct http_client *hc, *tmp;
122 list_for_each_entry_safe(hc, tmp, &clients, node)
123 http_shutdown_client(hc, "vss request");
124 }
125
126 static int http_send_msg(struct http_client *hc, const char *msg)
127 {
128 int ret = send_buffer(hc->fd, msg);
129
130 if (ret < 0)
131 http_shutdown_client(hc, "send msg failed");
132 return ret;
133 }
134
135 static void http_send_ok_msg(struct http_client *hc)
136 {
137 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
138 http_send_msg(hc, HTTP_OK_MSG);
139 }
140
141 static int http_send_err_msg(struct http_client *hc)
142 {
143 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
144 return http_send_msg(hc, HTTP_ERR_MSG);
145 }
146
147 static int queue_chunk(struct http_client *hc, long unsigned chunk_num,
148 size_t sent)
149 {
150 struct queued_chunk *qc;
151 char *buf;
152 size_t len;
153 int ret;
154
155 if (chunk_num != -1U) {
156 ret = vss_get_chunk(chunk_num, &buf, &len);
157 if (ret < 0)
158 return ret;
159 } else
160 buf = vss_get_header(&len);
161 if (hc->cq_bytes + len > MAX_BACKLOG)
162 return -E_QUEUE;
163 qc = para_malloc(sizeof(struct queued_chunk));
164 hc->cq_bytes += len;
165 qc->chunk_num = chunk_num;
166 qc->sent = sent;
167 list_add_tail(&qc->node, &hc->chunk_queue);
168 PARA_INFO_LOG("%lu bytes queued for fd %d\n", hc->cq_bytes, hc->fd);
169 return 1;
170 }
171
172 static int send_queued_chunks(struct http_client *hc)
173 {
174 int ret;
175 struct queued_chunk *qc, *tmp;
176
177 if (list_empty(&hc->chunk_queue))
178 return 1;
179 list_for_each_entry_safe(qc, tmp, &hc->chunk_queue, node) {
180 char *buf;
181 size_t len;
182 ret = write_ok(hc->fd);
183 if (ret <= 0)
184 return ret? -E_WRITE_OK : 0;
185 if (qc->chunk_num != -1U) {
186 ret = vss_get_chunk(qc->chunk_num, &buf, &len);
187 if (ret < 0)
188 return ret;
189 } else
190 buf = vss_get_header(&len);
191 assert(len && len > qc->sent);
192 ret = write(hc->fd, buf + qc->sent, len - qc->sent);
193 if (ret < 0)
194 return -1; /* FIXME */
195 hc->cq_bytes -= ret;
196 if (ret != len - qc->sent) {
197 qc->sent += ret;
198 return 0;
199 }
200 list_del(&qc->node);
201 free(qc);
202 }
203 return 1;
204 }
205
206 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
207 size_t sent)
208 {
209 int ret = queue_chunk(hc, chunk_num, sent);
210 if (ret < 0)
211 http_shutdown_client(hc, "queue error");
212 return ret;
213 }
214
215 static void http_send( long unsigned current_chunk,
216 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
217 {
218 struct http_client *hc, *tmp;
219 int ret;
220
221 list_for_each_entry_safe(hc, tmp, &clients, node) {
222 if (hc->status != HTTP_STREAMING &&
223 hc->status != HTTP_READY_TO_STREAM)
224 continue;
225 if (hc->status == HTTP_READY_TO_STREAM) {
226 unsigned hlen;
227 char *hbuf = vss_get_header(&hlen);
228 if (hbuf && hlen > 0 && current_chunk) {
229 /* need to send header */
230 PARA_INFO_LOG("queueing header: %d\n", hlen);
231 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
232 continue;
233 } else
234 PARA_INFO_LOG("no need to queue header\n");
235 hc->status = HTTP_STREAMING;
236 }
237 ret = send_queued_chunks(hc);
238 if (ret < 0) {
239 http_shutdown_client(hc, "queue send error");
240 continue;
241 }
242 if (!len)
243 continue;
244 if (!ret || write_ok(hc->fd) <= 0) {
245 PARA_INFO_LOG("fd %d not ready (%lu bytes queued),"
246 " trying to queue chunk\n", hc->fd,
247 hc->cq_bytes);
248 queue_chunk_or_shutdown(hc, current_chunk, 0);
249 continue;
250 }
251 // PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
252 ret = write(hc->fd, buf, len);
253 // PARA_DEBUG_LOG("ret: %d\n", ret);
254 if (ret < 0) {
255 http_shutdown_client(hc, "send error");
256 continue;
257 }
258 if (ret != len)
259 queue_chunk_or_shutdown(hc, current_chunk, ret);
260 }
261 }
262
263 static int host_in_access_perm_list(struct http_client *hc)
264 {
265 struct access_info *ai, *tmp;
266 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
267 unsigned mask = ((~0U) >> ai->netmask);
268 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
269 return 1;
270 }
271 return 0;
272 }
273
274 static void http_post_select(fd_set *rfds, fd_set *wfds)
275 {
276 int i = -1, match;
277 struct http_client *hc, *tmp;
278 const char *err_msg;
279
280 list_for_each_entry_safe(hc, tmp, &clients, node) {
281 i++;
282 // PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
283 switch (hc->status) {
284 case HTTP_STREAMING: /* nothing to do */
285 case HTTP_READY_TO_STREAM:
286 break;
287 case HTTP_CONNECTED: /* need to recv get request */
288 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
289 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
290 < 0) {
291 hc->status = HTTP_INVALID_GET_REQUEST;
292 } else {
293 hc->status = HTTP_GOT_GET_REQUEST;
294 PARA_INFO_LOG("%s",
295 "received get request\n");
296 }
297 }
298 break;
299 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
300 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
301 hc->status = HTTP_SENT_OK_MSG;
302 http_send_ok_msg(hc);
303 }
304 break;
305 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
306 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
307 if (http_send_err_msg(hc) >= 0)
308 http_shutdown_client(hc,
309 "invalid get request");
310 }
311 break;
312 case HTTP_SENT_OK_MSG: /* need to send header? */
313 if (hc->check_w && FD_ISSET(hc->fd, wfds))
314 hc->status = HTTP_READY_TO_STREAM;
315 break;
316 }
317 }
318 if (!FD_ISSET(server_fd, rfds))
319 return;
320 hc = para_calloc(sizeof(struct http_client));
321 err_msg = "accept error";
322 hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
323 if (hc->fd <= 0)
324 goto err_out;
325 PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
326 if (conf.http_max_clients_arg > 0 && numclients >=
327 conf.http_max_clients_arg) {
328 err_msg = "server full";
329 goto err_out;
330 }
331 match = host_in_access_perm_list(hc);
332 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
333 if ((match && !conf.http_default_deny_given) ||
334 (!match && conf.http_default_deny_given)) {
335 err_msg = "permission denied";
336 goto err_out;
337 }
338 hc->status = HTTP_CONNECTED;
339 INIT_LIST_HEAD(&hc->chunk_queue);
340 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
341 CLIENT_ADDR(hc), hc->fd);
342 numclients++;
343 para_list_add(&hc->node, &clients);
344 add_close_on_fork_list(hc->fd);
345 mark_fd_nonblock(hc->fd);
346 return;
347 err_out:
348 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
349 CLIENT_ADDR(hc), err_msg);
350 if (hc->fd > 0)
351 close(hc->fd);
352 free(hc);
353 }
354
355 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
356 {
357 struct http_client *hc, *tmp;
358
359 if (server_fd < 0)
360 return;
361 para_fd_set(server_fd, rfds, max_fileno);
362 list_for_each_entry_safe(hc, tmp, &clients, node) {
363 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
364 hc->check_r = 0;
365 hc->check_w = 0;
366 switch (hc->status) {
367 case HTTP_STREAMING:
368 case HTTP_READY_TO_STREAM:
369 break;
370 case HTTP_CONNECTED: /* need to recv get request */
371 para_fd_set(hc->fd, rfds, max_fileno);
372 hc->check_r = 1;
373 break;
374 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
375 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
376 para_fd_set(hc->fd, wfds, max_fileno);
377 hc->check_w = 1;
378 break;
379 case HTTP_SENT_OK_MSG:
380 if (!vss_playing())
381 break; /* wait until server starts playing */
382 para_fd_set(hc->fd, wfds, max_fileno);
383 hc->check_w = 1;
384 break;
385 }
386 }
387 }
388
389 static int open_tcp_port(int port)
390 {
391 int ret;
392
393 server_fd = init_tcp_socket(port);
394 if (server_fd < 0) {
395 http_shutdown_clients();
396 self->status = SENDER_OFF;
397 return server_fd;
398 }
399 ret = mark_fd_nonblock(server_fd);
400 if (ret < 0) {
401 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
402 exit(EXIT_FAILURE);
403 }
404 self->status = SENDER_ON;
405 add_close_on_fork_list(server_fd);
406 return 1;
407 }
408
409 static int http_com_on(__a_unused struct sender_command_data *scd)
410 {
411 if (self->status == SENDER_ON)
412 return 1;
413 return open_tcp_port(conf.http_port_arg);
414 }
415
416 static int http_com_off(__a_unused struct sender_command_data *scd)
417 {
418 self->status = SENDER_OFF;
419 if (server_fd > 0) {
420 close(server_fd);
421 del_close_on_fork_list(server_fd);
422 server_fd = -1;
423 }
424 http_shutdown_clients();
425 return 1;
426 }
427
428 static void del_perm_list_entry(struct sender_command_data *scd)
429 {
430 struct access_info *ai, *tmp;
431
432 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
433 char *nad = para_strdup(inet_ntoa(ai->addr));
434 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
435 ai->netmask == scd->netmask) {
436 PARA_NOTICE_LOG("removing %s/%i from access list\n",
437 nad, ai->netmask);
438 list_del(&ai->node);
439 free(ai);
440 }
441 free(nad);
442 }
443 }
444
445 static void add_perm_list_entry(struct sender_command_data *scd)
446 {
447 struct access_info *ai = para_malloc(sizeof(struct access_info));
448 ai->addr = scd->addr;
449 ai->netmask = scd->netmask;
450 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
451 ai->netmask);
452 para_list_add(&ai->node, &access_perm_list);
453 }
454
455 static int http_com_deny(struct sender_command_data *scd)
456 {
457 if (conf.http_default_deny_given)
458 del_perm_list_entry(scd);
459 else
460 add_perm_list_entry(scd);
461 return 1;
462 }
463
464 static int http_com_allow(struct sender_command_data *scd)
465 {
466 if (conf.http_default_deny_given)
467 add_perm_list_entry(scd);
468 else
469 del_perm_list_entry(scd);
470 return 1;
471 }
472
473 static char *http_info(void)
474 {
475 char *clnts = NULL, *ap = NULL, *ret;
476 struct access_info *ai, *tmp_ai;
477 struct http_client *hc, *tmp_hc;
478
479 list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
480 char *tmp = make_message("%s%s/%d ", ap? ap : "",
481 inet_ntoa(ai->addr), ai->netmask);
482 free(ap);
483 ap = tmp;
484 }
485 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
486 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
487 CLIENT_ADDR(hc), CLIENT_PORT(hc));
488 free(clnts);
489 clnts = tmp;
490 }
491 ret = make_message(
492 "http status: %s\n"
493 "http tcp port: %d\n"
494 "http clients: %d\n"
495 "http maximal number of clients: %d%s\n"
496 "http connected clients: %s\n"
497 "http access %s list: %s\n",
498 (self->status == SENDER_ON)? "on" : "off",
499 conf.http_port_arg,
500 numclients,
501 conf.http_max_clients_arg,
502 conf.http_max_clients_arg > 0? "" : " (unlimited)",
503 clnts? clnts : "(none)",
504 conf.http_default_deny_given? "allow" : "deny",
505 ap? ap : "(none)"
506 );
507 free(ap);
508 free(clnts);
509 return ret;
510 }
511
512 static void init_access_control_list(void)
513 {
514 int i;
515 struct sender_command_data scd;
516
517 INIT_LIST_HEAD(&access_perm_list);
518 for (i = 0; i < conf.http_access_given; i++) {
519 char *arg = para_strdup(conf.http_access_arg[i]);
520 char *p = strchr(arg, '/');
521 if (!p)
522 goto err;
523 *p = '\0';
524 if (!inet_aton(arg, &scd.addr))
525 goto err;
526 scd.netmask = atoi(++p);
527 if (scd.netmask < 0 || scd.netmask > 32)
528 goto err;
529 add_perm_list_entry(&scd);
530 goto success;
531 err:
532 PARA_CRIT_LOG("syntax error for http_access option "
533 "#%d, ignoring\n", i);
534 success:
535 free(arg);
536 continue;
537 }
538 }
539
540 static char *http_help(void)
541 {
542 return make_message(
543 "usage: {on|off}\n"
544 "usage: {allow|deny} IP mask\n"
545 "example: allow 127.0.0.1 32\n"
546 );
547 }
548
549 /**
550 * The init function of the http sender.
551 *
552 * \param s Pointer to the http sender struct.
553 *
554 * It initializes all function pointers of \a s, the client list and the access
555 * control list. If the autostart option was given, the tcp port is opened.
556 */
557 void http_send_init(struct sender *s)
558 {
559 INIT_LIST_HEAD(&clients);
560 s->info = http_info;
561 s->send = http_send;
562 s->pre_select = http_pre_select;
563 s->post_select = http_post_select;
564 s->shutdown_clients = http_shutdown_clients;
565 s->help = http_help;
566 s->client_cmds[SENDER_ON] = http_com_on;
567 s->client_cmds[SENDER_OFF] = http_com_off;
568 s->client_cmds[SENDER_DENY] = http_com_deny;
569 s->client_cmds[SENDER_ALLOW] = http_com_allow;
570 s->client_cmds[SENDER_ADD] = NULL;
571 s->client_cmds[SENDER_DELETE] = NULL;
572 self = s;
573 init_access_control_list();
574 if (!conf.http_no_autostart_given)
575 open_tcp_port(conf.http_port_arg); /* ignore errors */
576 PARA_DEBUG_LOG("%s", "http sender init complete\n");
577 }