http_send.c: Trivial whitespace and spelling cleanups.
[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 /** Te list of pending packets for this client. */
67 struct list_head packet_queue;
68 /** The number of pending bytes for this client. */
69 unsigned long pq_bytes;
70 };
71
72 /**
73 * Describes one queued data packet for a client.
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 packet for this client until
77 * the number of queued bytes exceeds \p MAX_BACKLOG.
78 */
79 struct queued_packet {
80 /** The length of the packet in bytes. */
81 unsigned int len;
82 /** Pointer to the packet data. */
83 char *packet;
84 /** Position of the packet in the packet list. */
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_packet *qp, *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(qp, tmp, &hc->packet_queue, node) {
112 free(qp->packet);
113 list_del(&qp->node);
114 free(qp);
115 }
116 list_del(&hc->node);
117 free(hc);
118 }
119
120 static void http_shutdown_clients(void)
121 {
122 struct http_client *hc, *tmp;
123 list_for_each_entry_safe(hc, tmp, &clients, node)
124 http_shutdown_client(hc, "vss request");
125 }
126
127 static int http_send_msg(struct http_client *hc, const char *msg)
128 {
129 int ret = send_buffer(hc->fd, msg);
130
131 if (ret < 0)
132 http_shutdown_client(hc, "send msg failed");
133 return ret;
134 }
135
136 static void http_send_ok_msg(struct http_client *hc)
137 {
138 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
139 http_send_msg(hc, HTTP_OK_MSG);
140 }
141
142 static int http_send_err_msg(struct http_client *hc)
143 {
144 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
145 return http_send_msg(hc, HTTP_ERR_MSG);
146 }
147
148 static int queue_packet(struct http_client *hc, const char *buf, size_t len)
149 {
150 struct queued_packet *qp;
151 if (hc->pq_bytes + len > MAX_BACKLOG) {
152 http_shutdown_client(hc, "packet queue overrun");
153 return -E_QUEUE;
154 }
155 qp = para_malloc(sizeof(struct queued_packet));
156 hc->pq_bytes += len;
157 qp->packet = para_malloc(len);
158 memcpy(qp->packet, buf, len);
159 qp->len = len;
160 list_add_tail(&qp->node, &hc->packet_queue);
161 PARA_INFO_LOG("%lu bytes queued for fd %d\n", hc->pq_bytes, hc->fd);
162 return 1;
163 }
164
165 static int send_queued_packets(struct http_client *hc)
166 {
167 int ret;
168 struct queued_packet *qp, *tmp;
169
170 if (list_empty(&hc->packet_queue))
171 return 1;
172 list_for_each_entry_safe(qp, tmp, &hc->packet_queue, node) {
173 ret = write_ok(hc->fd);
174 if (ret <= 0)
175 return ret? -E_WRITE_OK : 0;
176 ret = write(hc->fd, qp->packet, qp->len);
177 if (ret < 0)
178 return ret;
179 if (ret != qp->len) {
180 qp->len -= ret;
181 memmove(qp->packet, qp->packet + ret, qp->len);
182 return 0;
183 }
184 hc->pq_bytes -= qp->len;
185 free(qp->packet);
186 list_del(&qp->node);
187 free(qp);
188 }
189 return 1;
190 }
191
192 static void http_send( long unsigned current_chunk,
193 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
194 {
195 struct http_client *hc, *tmp;
196 int ret;
197
198 list_for_each_entry_safe(hc, tmp, &clients, node) {
199 if (hc->status != HTTP_STREAMING &&
200 hc->status != HTTP_READY_TO_STREAM)
201 continue;
202 if (hc->status == HTTP_READY_TO_STREAM) {
203 unsigned hlen;
204 char *hbuf = vss_get_header(&hlen);
205 if (hbuf && hlen > 0 && current_chunk) {
206 /* need to send header */
207 PARA_INFO_LOG("queueing header: %d\n", hlen);
208 if (queue_packet(hc, hbuf, hlen) < 0)
209 continue;
210 } else
211 PARA_INFO_LOG("%s", "no need to queue header\n");
212 hc->status = HTTP_STREAMING;
213 }
214 ret = send_queued_packets(hc);
215 if (ret < 0) {
216 http_shutdown_client(hc, "send error");
217 continue;
218 }
219 if (!len)
220 continue;
221 if (!ret || write_ok(hc->fd) <= 0) {
222 PARA_INFO_LOG("fd %d not ready (%lu bytes queued),"
223 " trying to queue packet\n", hc->fd,
224 hc->pq_bytes);
225 queue_packet(hc, buf, len);
226 continue;
227 }
228 // PARA_DEBUG_LOG("sending %d -> %s\n", len, CLIENT_ADDR(hc));
229 ret = write(hc->fd, buf, len);
230 if (ret < 0) {
231 http_shutdown_client(hc, "send error");
232 continue;
233 }
234 if (ret != len)
235 queue_packet(hc, buf + ret, len - ret);
236 }
237 }
238
239 static int host_in_access_perm_list(struct http_client *hc)
240 {
241 struct access_info *ai, *tmp;
242 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
243 unsigned mask = ((~0U) >> ai->netmask);
244 if ((hc->addr.sin_addr.s_addr & mask) == (ai->addr.s_addr & mask))
245 return 1;
246 }
247 return 0;
248 }
249
250 static void http_post_select(fd_set *rfds, fd_set *wfds)
251 {
252 int i = -1, match;
253 struct http_client *hc, *tmp;
254 const char *err_msg;
255
256 list_for_each_entry_safe(hc, tmp, &clients, node) {
257 i++;
258 // PARA_DEBUG_LOG("handling client %d: %s\n", i, CLIENT_ADDR(hc));
259 switch (hc->status) {
260 case HTTP_STREAMING: /* nothing to do */
261 case HTTP_READY_TO_STREAM:
262 break;
263 case HTTP_CONNECTED: /* need to recv get request */
264 if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
265 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
266 < 0) {
267 hc->status = HTTP_INVALID_GET_REQUEST;
268 } else {
269 hc->status = HTTP_GOT_GET_REQUEST;
270 PARA_INFO_LOG("%s",
271 "received get request\n");
272 }
273 }
274 break;
275 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
276 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
277 hc->status = HTTP_SENT_OK_MSG;
278 http_send_ok_msg(hc);
279 }
280 break;
281 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
282 if (hc->check_w && FD_ISSET(hc->fd, wfds)) {
283 if (http_send_err_msg(hc) >= 0)
284 http_shutdown_client(hc,
285 "invalid get request");
286 }
287 break;
288 case HTTP_SENT_OK_MSG: /* need to send header? */
289 if (hc->check_w && FD_ISSET(hc->fd, wfds))
290 hc->status = HTTP_READY_TO_STREAM;
291 break;
292 }
293 }
294 if (!FD_ISSET(server_fd, rfds))
295 return;
296 hc = para_calloc(sizeof(struct http_client));
297 err_msg = "accept error";
298 hc->fd = para_accept(server_fd, &hc->addr, sizeof(struct sockaddr_in));
299 if (hc->fd <= 0)
300 goto err_out;
301 PARA_NOTICE_LOG("connection from %s (fd %d)\n", CLIENT_ADDR(hc), hc->fd);
302 if (conf.http_max_clients_arg > 0 && numclients >=
303 conf.http_max_clients_arg) {
304 err_msg = "server full";
305 goto err_out;
306 }
307 match = host_in_access_perm_list(hc);
308 PARA_DEBUG_LOG("host_in_access_perm_list: %d\n", match);
309 if ((match && !conf.http_default_deny_given) ||
310 (!match && conf.http_default_deny_given)) {
311 err_msg = "permission denied";
312 goto err_out;
313 }
314 hc->status = HTTP_CONNECTED;
315 INIT_LIST_HEAD(&hc->packet_queue);
316 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
317 CLIENT_ADDR(hc), hc->fd);
318 numclients++;
319 para_list_add(&hc->node, &clients);
320 add_close_on_fork_list(hc->fd);
321 mark_fd_nonblock(hc->fd);
322 return;
323 err_out:
324 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
325 CLIENT_ADDR(hc), err_msg);
326 if (hc->fd > 0)
327 close(hc->fd);
328 free(hc);
329 }
330
331 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
332 {
333 struct http_client *hc, *tmp;
334
335 if (server_fd < 0)
336 return;
337 para_fd_set(server_fd, rfds, max_fileno);
338 list_for_each_entry_safe(hc, tmp, &clients, node) {
339 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
340 hc->check_r = 0;
341 hc->check_w = 0;
342 switch (hc->status) {
343 case HTTP_STREAMING:
344 case HTTP_READY_TO_STREAM:
345 break;
346 case HTTP_CONNECTED: /* need to recv get request */
347 para_fd_set(hc->fd, rfds, max_fileno);
348 hc->check_r = 1;
349 break;
350 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
351 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
352 para_fd_set(hc->fd, wfds, max_fileno);
353 hc->check_w = 1;
354 break;
355 case HTTP_SENT_OK_MSG:
356 if (!vss_playing())
357 break; /* wait until server starts playing */
358 para_fd_set(hc->fd, wfds, max_fileno);
359 hc->check_w = 1;
360 break;
361 }
362 }
363 }
364
365 static int open_tcp_port(int port)
366 {
367 int ret;
368
369 server_fd = init_tcp_socket(port);
370 if (server_fd < 0) {
371 http_shutdown_clients();
372 self->status = SENDER_OFF;
373 return server_fd;
374 }
375 ret = mark_fd_nonblock(server_fd);
376 if (ret < 0) {
377 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
378 exit(EXIT_FAILURE);
379 }
380 self->status = SENDER_ON;
381 add_close_on_fork_list(server_fd);
382 return 1;
383 }
384
385 static int http_com_on(__a_unused struct sender_command_data *scd)
386 {
387 if (self->status == SENDER_ON)
388 return 1;
389 return open_tcp_port(conf.http_port_arg);
390 }
391
392 static int http_com_off(__a_unused struct sender_command_data *scd)
393 {
394 self->status = SENDER_OFF;
395 if (server_fd > 0) {
396 close(server_fd);
397 del_close_on_fork_list(server_fd);
398 server_fd = -1;
399 }
400 http_shutdown_clients();
401 return 1;
402 }
403
404 static void del_perm_list_entry(struct sender_command_data *scd)
405 {
406 struct access_info *ai, *tmp;
407
408 list_for_each_entry_safe(ai, tmp, &access_perm_list, node) {
409 char *nad = para_strdup(inet_ntoa(ai->addr));
410 if (!strcmp(nad, inet_ntoa(scd->addr)) &&
411 ai->netmask == scd->netmask) {
412 PARA_NOTICE_LOG("removing %s/%i from access list\n",
413 nad, ai->netmask);
414 list_del(&ai->node);
415 free(ai);
416 }
417 free(nad);
418 }
419 }
420
421 static void add_perm_list_entry(struct sender_command_data *scd)
422 {
423 struct access_info *ai = para_malloc(sizeof(struct access_info));
424 ai->addr = scd->addr;
425 ai->netmask = scd->netmask;
426 PARA_INFO_LOG("adding %s/%i to access list\n", inet_ntoa(ai->addr),
427 ai->netmask);
428 para_list_add(&ai->node, &access_perm_list);
429 }
430
431 static int http_com_deny(struct sender_command_data *scd)
432 {
433 if (conf.http_default_deny_given)
434 del_perm_list_entry(scd);
435 else
436 add_perm_list_entry(scd);
437 return 1;
438 }
439
440 static int http_com_allow(struct sender_command_data *scd)
441 {
442 if (conf.http_default_deny_given)
443 add_perm_list_entry(scd);
444 else
445 del_perm_list_entry(scd);
446 return 1;
447 }
448
449 static char *http_info(void)
450 {
451 char *clnts = NULL, *ap = NULL, *ret;
452 struct access_info *ai, *tmp_ai;
453 struct http_client *hc, *tmp_hc;
454
455 list_for_each_entry_safe(ai, tmp_ai, &access_perm_list, node) {
456 char *tmp = make_message("%s%s/%d ", ap? ap : "",
457 inet_ntoa(ai->addr), ai->netmask);
458 free(ap);
459 ap = tmp;
460 }
461 list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
462 char *tmp = make_message("%s%s:%d ", clnts? clnts : "",
463 CLIENT_ADDR(hc), CLIENT_PORT(hc));
464 free(clnts);
465 clnts = tmp;
466 }
467 ret = make_message(
468 "http status: %s\n"
469 "http tcp port: %d\n"
470 "http clients: %d\n"
471 "http maximal number of clients: %d%s\n"
472 "http connected clients: %s\n"
473 "http access %s list: %s\n",
474 (self->status == SENDER_ON)? "on" : "off",
475 conf.http_port_arg,
476 numclients,
477 conf.http_max_clients_arg,
478 conf.http_max_clients_arg > 0? "" : " (unlimited)",
479 clnts? clnts : "(none)",
480 conf.http_default_deny_given? "allow" : "deny",
481 ap? ap : "(none)"
482 );
483 free(ap);
484 free(clnts);
485 return ret;
486 }
487
488 static void init_access_control_list(void)
489 {
490 int i;
491 struct sender_command_data scd;
492
493 INIT_LIST_HEAD(&access_perm_list);
494 for (i = 0; i < conf.http_access_given; i++) {
495 char *arg = para_strdup(conf.http_access_arg[i]);
496 char *p = strchr(arg, '/');
497 if (!p)
498 goto err;
499 *p = '\0';
500 if (!inet_aton(arg, &scd.addr))
501 goto err;
502 scd.netmask = atoi(++p);
503 if (scd.netmask < 0 || scd.netmask > 32)
504 goto err;
505 add_perm_list_entry(&scd);
506 goto success;
507 err:
508 PARA_CRIT_LOG("syntax error for http_access option "
509 "#%d, ignoring\n", i);
510 success:
511 free(arg);
512 continue;
513 }
514 }
515
516 static char *http_help(void)
517 {
518 return make_message(
519 "usage: {on|off}\n"
520 "usage: {allow|deny} IP mask\n"
521 "example: allow 127.0.0.1 32\n"
522 );
523 }
524
525 /**
526 * The init function of the http sender.
527 *
528 * \param s Pointer to the http sender struct.
529 *
530 * It initializes all function pointers of \a s, the client list and the access
531 * control list. If the autostart option was given, the tcp port is opened.
532 */
533 void http_send_init(struct sender *s)
534 {
535 INIT_LIST_HEAD(&clients);
536 s->info = http_info;
537 s->send = http_send;
538 s->pre_select = http_pre_select;
539 s->post_select = http_post_select;
540 s->shutdown_clients = http_shutdown_clients;
541 s->help = http_help;
542 s->client_cmds[SENDER_ON] = http_com_on;
543 s->client_cmds[SENDER_OFF] = http_com_off;
544 s->client_cmds[SENDER_DENY] = http_com_deny;
545 s->client_cmds[SENDER_ALLOW] = http_com_allow;
546 s->client_cmds[SENDER_ADD] = NULL;
547 s->client_cmds[SENDER_DELETE] = NULL;
548 self = s;
549 init_access_control_list();
550 if (!conf.http_no_autostart_given)
551 open_tcp_port(conf.http_port_arg); /* ignore errors */
552 PARA_DEBUG_LOG("%s", "http sender init complete\n");
553 }