Fix a bug in the mood line parser.
[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 #include <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "error.h"
14 #include "string.h"
15 #include "server.cmdline.h"
16 #include "afh.h"
17 #include "afs.h"
18 #include "server.h"
19 #include "http.h"
20 #include "vss.h"
21 #include "send.h"
22 #include "list.h"
23 #include "close_on_fork.h"
24 #include "net.h"
25 #include "fd.h"
26 #include "chunk_queue.h"
27
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"
33 /** \endcond */
34
35 /** The possible states of a client from the server's POV. */
36 enum http_status {
37 /** We accepted the connection on the tcp socket. */
38 HTTP_CONNECTED,
39 /** Successfully received the get request. */
40 HTTP_GOT_GET_REQUEST,
41 /** We sent the OK message back to the client. */
42 HTTP_SENT_OK_MSG,
43 /** Connection established, we might need to send the audio file header. */
44 HTTP_READY_TO_STREAM,
45 /** Connection is ready for sending audio data. */
46 HTTP_STREAMING,
47 /** We didn't receive a valid get request. */
48 HTTP_INVALID_GET_REQUEST
49 };
50
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;
57
58 /** Describes one client that connected the tcp port of the http sender. */
59 struct http_client {
60 /** The file descriptor of the client. */
61 int fd;
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.*/
67 int check_r;
68 /** Non-zero if we included \a fd in the write set. */
69 int check_w;
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;
74 };
75
76 /**
77 * Describes one entry in the blacklist/whitelist of the http sender.
78 */
79 struct access_info {
80 /** The address to be black/whitelisted. */
81 struct in_addr addr;
82 /** The netmask for this entry. */
83 unsigned netmask;
84 /** The position of this entry in the access_perm_list. */
85 struct list_head node;
86 };
87
88 static int server_fd = -1, numclients;
89 static struct sender *self;
90
91
92 static void http_shutdown_client(struct http_client *hc, const char *msg)
93 {
94 PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", CLIENT_ADDR(hc),
95 hc->fd, msg);
96 numclients--;
97 close(hc->fd);
98 del_close_on_fork_list(hc->fd);
99 cq_destroy(hc->cq);
100 list_del(&hc->node);
101 free(hc);
102 }
103
104 static void http_shutdown_clients(void)
105 {
106 struct http_client *hc, *tmp;
107 list_for_each_entry_safe(hc, tmp, &clients, node)
108 http_shutdown_client(hc, "vss request");
109 }
110
111 static int http_send_msg(struct http_client *hc, const char *msg)
112 {
113 int ret = send_buffer(hc->fd, msg);
114
115 if (ret < 0)
116 http_shutdown_client(hc, "send msg failed");
117 return ret;
118 }
119
120 static void http_send_ok_msg(struct http_client *hc)
121 {
122 PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
123 http_send_msg(hc, HTTP_OK_MSG);
124 }
125
126 static int http_send_err_msg(struct http_client *hc)
127 {
128 PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
129 return http_send_msg(hc, HTTP_ERR_MSG);
130 }
131
132 static int send_queued_chunks(struct http_client *hc)
133 {
134 struct queued_chunk *qc;
135 while ((qc = cq_peek(hc->cq))) {
136 char *buf;
137 size_t len;
138 int ret = write_ok(hc->fd);
139 if (ret <= 0)
140 return ret? -E_WRITE_OK : 0;
141 cq_get(qc, &buf, &len);
142 ret = write(hc->fd, buf, len);
143 if (ret < 0)
144 return -E_SEND_QUEUED_CHUNK;
145 cq_update(hc->cq, ret);
146 if (ret != len)
147 return 1;
148 cq_dequeue(hc->cq);
149 }
150 return 1;
151 }
152
153 static int queue_chunk_or_shutdown(struct http_client *hc, long unsigned chunk_num,
154 size_t sent)
155 {
156 int ret = cq_enqueue(hc->cq, chunk_num, sent);
157 if (ret < 0)
158 http_shutdown_client(hc, "queue error");
159 return ret;
160 }
161
162 static void http_send( long unsigned current_chunk,
163 __a_unused long unsigned chunks_sent, const char *buf, size_t len)
164 {
165 struct http_client *hc, *tmp;
166 int ret;
167
168 list_for_each_entry_safe(hc, tmp, &clients, node) {
169 if (hc->status != HTTP_STREAMING &&
170 hc->status != HTTP_READY_TO_STREAM)
171 continue;
172 if (hc->status == HTTP_READY_TO_STREAM) {
173 size_t hlen;
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)
179 continue;
180 } else
181 PARA_INFO_LOG("no need to queue header\n");
182 hc->status = HTTP_STREAMING;
183 }
184 ret = send_queued_chunks(hc);
185 if (ret < 0) {
186 http_shutdown_client(hc, "queue send error");
187 continue;
188 }
189 if (!len)
190 continue;
191 if (!ret || write_ok(hc->fd) <= 0) {
192 queue_chunk_or_shutdown(hc, current_chunk, 0);
193 continue;
194 }
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);
198 if (ret < 0) {
199 http_shutdown_client(hc, "send error");
200 continue;
201 }
202 if (ret != len)
203 queue_chunk_or_shutdown(hc, current_chunk, ret);
204 }
205 }
206
207 static int host_in_access_perm_list(struct http_client *hc)
208 {
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))
213 return 1;
214 }
215 return 0;
216 }
217
218 static void http_post_select(fd_set *rfds, fd_set *wfds)
219 {
220 int i = -1, match;
221 struct http_client *hc, *tmp;
222 const char *err_msg;
223
224 list_for_each_entry_safe(hc, tmp, &clients, node) {
225 i++;
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:
230 break;
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)
234 < 0) {
235 hc->status = HTTP_INVALID_GET_REQUEST;
236 } else {
237 hc->status = HTTP_GOT_GET_REQUEST;
238 PARA_INFO_LOG("%s",
239 "received get request\n");
240 }
241 }
242 break;
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);
247 }
248 break;
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");
254 }
255 break;
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;
259 break;
260 }
261 }
262 if (!FD_ISSET(server_fd, rfds))
263 return;
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));
267 if (hc->fd <= 0)
268 goto err_out;
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";
273 goto err_out;
274 }
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";
280 goto err_out;
281 }
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);
286 numclients++;
287 para_list_add(&hc->node, &clients);
288 add_close_on_fork_list(hc->fd);
289 mark_fd_nonblock(hc->fd);
290 return;
291 err_out:
292 PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
293 CLIENT_ADDR(hc), err_msg);
294 if (hc->fd > 0)
295 close(hc->fd);
296 free(hc);
297 }
298
299 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
300 {
301 struct http_client *hc, *tmp;
302
303 if (server_fd < 0)
304 return;
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);
308 hc->check_r = 0;
309 hc->check_w = 0;
310 switch (hc->status) {
311 case HTTP_STREAMING:
312 case HTTP_READY_TO_STREAM:
313 break;
314 case HTTP_CONNECTED: /* need to recv get request */
315 para_fd_set(hc->fd, rfds, max_fileno);
316 hc->check_r = 1;
317 break;
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);
321 hc->check_w = 1;
322 break;
323 case HTTP_SENT_OK_MSG:
324 if (!vss_playing())
325 break; /* wait until server starts playing */
326 para_fd_set(hc->fd, wfds, max_fileno);
327 hc->check_w = 1;
328 break;
329 }
330 }
331 }
332
333 static int open_tcp_port(int port)
334 {
335 int ret;
336
337 server_fd = init_tcp_socket(port);
338 if (server_fd < 0) {
339 http_shutdown_clients();
340 self->status = SENDER_OFF;
341 return server_fd;
342 }
343 ret = mark_fd_nonblock(server_fd);
344 if (ret < 0) {
345 PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret));
346 exit(EXIT_FAILURE);
347 }
348 self->status = SENDER_ON;
349 add_close_on_fork_list(server_fd);
350 return 1;
351 }
352
353 static int http_com_on(__a_unused struct sender_command_data *scd)
354 {
355 if (self->status == SENDER_ON)
356 return 1;
357 return open_tcp_port(conf.http_port_arg);
358 }
359
360 static int http_com_off(__a_unused struct sender_command_data *scd)
361 {
362 self->status = SENDER_OFF;
363 if (server_fd > 0) {
364 close(server_fd);
365 del_close_on_fork_list(server_fd);
366 server_fd = -1;
367 }
368 http_shutdown_clients();
369 return 1;
370 }
371
372 static void del_perm_list_entry(struct sender_command_data *scd)
373 {
374 struct access_info *ai, *tmp;
375
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",
381 nad, ai->netmask);
382 list_del(&ai->node);
383 free(ai);
384 }
385 free(nad);
386 }
387 }
388
389 static void add_perm_list_entry(struct sender_command_data *scd)
390 {
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),
395 ai->netmask);
396 para_list_add(&ai->node, &access_perm_list);
397 }
398
399 static int http_com_deny(struct sender_command_data *scd)
400 {
401 if (conf.http_default_deny_given)
402 del_perm_list_entry(scd);
403 else
404 add_perm_list_entry(scd);
405 return 1;
406 }
407
408 static int http_com_allow(struct sender_command_data *scd)
409 {
410 if (conf.http_default_deny_given)
411 add_perm_list_entry(scd);
412 else
413 del_perm_list_entry(scd);
414 return 1;
415 }
416
417 static char *http_info(void)
418 {
419 char *clnts = NULL, *ap = NULL, *ret;
420 struct access_info *ai, *tmp_ai;
421 struct http_client *hc, *tmp_hc;
422
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);
426 free(ap);
427 ap = tmp;
428 }
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));
432 free(clnts);
433 clnts = tmp;
434 }
435 ret = make_message(
436 "http status: %s\n"
437 "http tcp port: %d\n"
438 "http clients: %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",
443 conf.http_port_arg,
444 numclients,
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",
449 ap? ap : "(none)"
450 );
451 free(ap);
452 free(clnts);
453 return ret;
454 }
455
456 static void init_access_control_list(void)
457 {
458 int i;
459 struct sender_command_data scd;
460
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, '/');
465 if (!p)
466 goto err;
467 *p = '\0';
468 if (!inet_pton(AF_INET, arg, &scd.addr))
469 goto err;
470 scd.netmask = atoi(++p);
471 if (scd.netmask < 0 || scd.netmask > 32)
472 goto err;
473 add_perm_list_entry(&scd);
474 goto success;
475 err:
476 PARA_CRIT_LOG("syntax error for http_access option "
477 "#%d, ignoring\n", i);
478 success:
479 free(arg);
480 continue;
481 }
482 }
483
484 static char *http_help(void)
485 {
486 return make_message(
487 "usage: {on|off}\n"
488 "usage: {allow|deny} IP mask\n"
489 "example: allow 127.0.0.1 32\n"
490 );
491 }
492
493 /**
494 * The init function of the http sender.
495 *
496 * \param s Pointer to the http sender struct.
497 *
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.
500 */
501 void http_send_init(struct sender *s)
502 {
503 INIT_LIST_HEAD(&clients);
504 s->info = http_info;
505 s->send = http_send;
506 s->pre_select = http_pre_select;
507 s->post_select = http_post_select;
508 s->shutdown_clients = http_shutdown_clients;
509 s->help = http_help;
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;
516 self = s;
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");
521 }