]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_send.c
8f834f70a7ef7f32325c6474ef4f43e4e929ebea
[paraslash.git] / http_send.c
1 /*
2  * Copyright (C) 2005-2008 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 #include "acl.h"
28
29 /** Message sent to clients that do not send a valid get request. */
30 #define HTTP_ERR_MSG "HTTP/1.0 400 Bad Request\n"
31
32
33 /** The possible states of a client from the server's POV. */
34 enum http_status {
35         /** We accepted the connection on the tcp socket. */
36         HTTP_CONNECTED,
37         /** Successfully received the get request. */
38         HTTP_GOT_GET_REQUEST,
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 400000
47 /** The list of connected clients. */
48 static struct list_head clients;
49 /** The whitelist/blacklist. */
50 static struct list_head http_acl;
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         /** The socket `name' of the client. */
57         char *name;
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         /** The position of this client in the client list. */
63         struct list_head node;
64         /** non-zero if audio file header has been sent */
65         int header_sent;
66         /** The list of pending chunks for this client. */
67         struct chunk_queue *cq;
68 };
69
70 static int listen_fd = -1, numclients;
71
72
73 static void http_shutdown_client(struct http_client *hc, const char *msg)
74 {
75         PARA_INFO_LOG("shutting down %s on fd %d (%s)\n", hc->name, hc->fd,
76                 msg);
77         numclients--;
78         free(hc->name);
79         close(hc->fd);
80         del_close_on_fork_list(hc->fd);
81         cq_destroy(hc->cq);
82         list_del(&hc->node);
83         free(hc);
84 }
85
86 static void http_shutdown_clients(void)
87 {
88         struct http_client *hc, *tmp;
89         list_for_each_entry_safe(hc, tmp, &clients, node)
90                 http_shutdown_client(hc, "vss request");
91 }
92
93 static int http_send_msg(struct http_client *hc, const char *msg)
94 {
95         int ret = send_buffer(hc->fd, msg);
96
97         if (ret < 0)
98                 http_shutdown_client(hc, "send msg failed");
99         return ret;
100 }
101
102 static void http_send_ok_msg(struct http_client *hc)
103 {
104         PARA_INFO_LOG("sending http ok message to fd %d\n", hc->fd);
105         http_send_msg(hc, HTTP_OK_MSG);
106 }
107
108 static int http_send_err_msg(struct http_client *hc)
109 {
110         PARA_NOTICE_LOG("sending bad request message to fd %d\n", hc->fd);
111         return http_send_msg(hc, HTTP_ERR_MSG);
112 }
113
114 /*
115  * ret: Negative on errors, zero if nothing was written and write returned
116  * EAGAIN, number of bytes written else.
117  */
118 static int http_write(int fd, const char *buf, size_t len)
119 {
120         size_t written = 0;
121
122         while (written < len) {
123                 int ret = write(fd, buf + written, len - written);
124                 if (ret < 0 && errno == EAGAIN)
125                         return written;
126                 if (ret < 0)
127                         return -ERRNO_TO_PARA_ERROR(errno);
128                 written += ret;
129         }
130         return written;
131 }
132
133
134 static int send_queued_chunks(struct http_client *hc)
135 {
136         struct queued_chunk *qc;
137         while ((qc = cq_peek(hc->cq))) {
138                 char *buf;
139                 size_t len;
140                 int ret;
141                 cq_get(qc, &buf, &len);
142                 ret = http_write(hc->fd, buf, len);
143                 if (ret < 0)
144                         return ret;
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                         continue;
171                 if (!hc->header_sent && current_chunk) {
172                         size_t hlen;
173                         char *hbuf = vss_get_header(&hlen);
174                         if (hbuf && hlen > 0) { /* need to send header */
175                                 PARA_INFO_LOG("queueing header: %zu\n", hlen);
176                                 if (queue_chunk_or_shutdown(hc, -1U, 0) < 0)
177                                         continue;
178                         } else
179                                 PARA_INFO_LOG("no need to queue header\n");
180                         hc->header_sent = 1;
181                 }
182                 ret = send_queued_chunks(hc);
183                 if (ret < 0) {
184                         http_shutdown_client(hc, "queue send error");
185                         continue;
186                 }
187                 if (!len)
188                         continue;
189                 ret = http_write(hc->fd, buf, len);
190                 if (ret < 0) {
191                         http_shutdown_client(hc, "send error");
192                         continue;
193                 }
194                 if (ret != len)
195                         queue_chunk_or_shutdown(hc, current_chunk, ret);
196         }
197 }
198
199 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
200 {
201         int i = -1, match;
202         struct http_client *hc, *tmp;
203         const char *err_msg;
204
205         if (listen_fd < 0)
206                 return;
207         list_for_each_entry_safe(hc, tmp, &clients, node) {
208                 i++;
209 //              PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(hc->fd));
210                 switch (hc->status) {
211                 case HTTP_STREAMING: /* nothing to do */
212                         break;
213                 case HTTP_CONNECTED: /* need to recv get request */
214                         if (hc->check_r && FD_ISSET(hc->fd, rfds)) {
215                                 if (recv_pattern(hc->fd, HTTP_GET_MSG, MAXLINE)
216                                                 < 0) {
217                                         hc->status = HTTP_INVALID_GET_REQUEST;
218                                 } else {
219                                         hc->status = HTTP_GOT_GET_REQUEST;
220                                         PARA_INFO_LOG("%s",
221                                                 "received get request\n");
222                                 }
223                         }
224                         break;
225                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
226                         hc->status = HTTP_STREAMING;
227                         http_send_ok_msg(hc);
228                         break;
229                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
230                         if (http_send_err_msg(hc) >= 0)
231                                 http_shutdown_client(hc, "invalid get request");
232                         break;
233                 }
234         }
235         if (!FD_ISSET(listen_fd, rfds))
236                 return;
237         hc = para_calloc(sizeof(struct http_client));
238         err_msg = "accept error";
239         hc->fd = para_accept(listen_fd, NULL, 0);
240         if (hc->fd <= 0)
241                 goto err_out;
242         hc->name = make_message("%s", remote_name(hc->fd));
243         PARA_NOTICE_LOG("connection from %s (fd %d)\n", hc->name, hc->fd);
244         if (conf.http_max_clients_arg > 0 && numclients >=
245                         conf.http_max_clients_arg) {
246                 err_msg = "server full";
247                 goto err_out;
248         }
249         match = acl_lookup(hc->fd, &http_acl);
250         PARA_DEBUG_LOG("acl lookup returned %d\n", match);
251         if ((match && !conf.http_default_deny_given) ||
252                         (!match && conf.http_default_deny_given)) {
253                 err_msg = "permission denied";
254                 goto err_out;
255         }
256         err_msg = "failed to mark fd non-blocking";
257         if (mark_fd_nonblocking(hc->fd) < 0)
258                 goto err_out;
259         hc->status = HTTP_CONNECTED;
260         hc->cq = cq_new(MAX_BACKLOG);
261         numclients++;
262         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
263                 hc->name, hc->fd);
264         para_list_add(&hc->node, &clients);
265         add_close_on_fork_list(hc->fd);
266         return;
267 err_out:
268         PARA_WARNING_LOG("ignoring connect request from %s (%s)\n",
269                 hc->name, err_msg);
270         if (hc->fd > 0)
271                 close(hc->fd);
272         free(hc);
273 }
274
275 static void http_pre_select(int *max_fileno, fd_set *rfds, __a_unused fd_set *wfds)
276 {
277         struct http_client *hc, *tmp;
278
279         if (listen_fd < 0)
280                 return;
281         para_fd_set(listen_fd, rfds, max_fileno);
282         list_for_each_entry_safe(hc, tmp, &clients, node) {
283                 //PARA_DEBUG_LOG("hc %p on fd %d: status %d\n", hc, hc->fd, hc->status);
284                 hc->check_r = 0;
285                 switch (hc->status) {
286                 case HTTP_STREAMING:
287                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
288                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
289                         break;
290                 case HTTP_CONNECTED: /* need to recv get request */
291                         para_fd_set(hc->fd, rfds, max_fileno);
292                         hc->check_r = 1;
293                         break;
294                 }
295         }
296 }
297
298 static int http_open(void)
299 {
300         int ret;
301
302         listen_fd = para_listen(AF_UNSPEC, IPPROTO_TCP, conf.http_port_arg);
303         if (listen_fd < 0)
304                 return listen_fd;
305         ret = mark_fd_nonblocking(listen_fd);
306         if (ret < 0) {
307                 PARA_EMERG_LOG("%s\n", para_strerror(-ret));
308                 exit(EXIT_FAILURE);
309         }
310         add_close_on_fork_list(listen_fd);
311         return 1;
312 }
313
314 static int http_com_on(__a_unused struct sender_command_data *scd)
315 {
316         if (listen_fd >= 0)
317                 return 1;
318         return http_open();
319 }
320
321 static int http_com_off(__a_unused struct sender_command_data *scd)
322 {
323         if (listen_fd < 0)
324                 return 1;
325         PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
326         close(listen_fd);
327         del_close_on_fork_list(listen_fd);
328         http_shutdown_clients();
329         listen_fd = -1;
330         return 1;
331 }
332
333 static int http_com_deny(struct sender_command_data *scd)
334 {
335         if (conf.http_default_deny_given)
336                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
337         else
338                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
339         return 1;
340 }
341
342 static int http_com_allow(struct sender_command_data *scd)
343 {
344         if (conf.http_default_deny_given)
345                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
346         else
347                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
348         return 1;
349 }
350
351 static char *http_info(void)
352 {
353         char *clnts = NULL, *ret;
354         struct http_client *hc, *tmp_hc;
355
356         char *acl_contents = acl_get_contents(&http_acl);
357         list_for_each_entry_safe(hc, tmp_hc, &clients, node) {
358                 char *tmp = make_message("%s%s ", clnts? clnts : "", hc->name);
359                 free(clnts);
360                 clnts = tmp;
361         }
362         ret = make_message(
363                 "http status: %s\n"
364                 "http tcp port: %d\n"
365                 "http clients: %d\n"
366                 "http maximal number of clients: %d%s\n"
367                 "http connected clients: %s\n"
368                 "http access %s list: %s\n",
369                 (listen_fd >= 0)? "on" : "off",
370                 conf.http_port_arg,
371                 numclients,
372                 conf.http_max_clients_arg,
373                 conf.http_max_clients_arg > 0? "" : " (unlimited)",
374                 clnts? clnts : "(none)",
375                 conf.http_default_deny_given? "allow" : "deny",
376                 acl_contents? acl_contents : "(none)"
377         );
378         free(acl_contents);
379         free(clnts);
380         return ret;
381 }
382
383 static char *http_help(void)
384 {
385         return make_message(
386                 "usage: {on|off}\n"
387                 "usage: {allow|deny} IP mask\n"
388                 "example: allow 127.0.0.1 32\n"
389         );
390 }
391
392 /**
393  * The init function of the http sender.
394  *
395  * \param s Pointer to the http sender struct.
396  *
397  * It initializes all function pointers of \a s, the client list and the access
398  * control list. If the autostart option was given, the tcp port is opened.
399  */
400 void http_send_init(struct sender *s)
401 {
402         INIT_LIST_HEAD(&clients);
403         s->info = http_info;
404         s->send = http_send;
405         s->pre_select = http_pre_select;
406         s->post_select = http_post_select;
407         s->shutdown_clients = http_shutdown_clients;
408         s->help = http_help;
409         s->client_cmds[SENDER_ON] = http_com_on;
410         s->client_cmds[SENDER_OFF] = http_com_off;
411         s->client_cmds[SENDER_DENY] = http_com_deny;
412         s->client_cmds[SENDER_ALLOW] = http_com_allow;
413         s->client_cmds[SENDER_ADD] = NULL;
414         s->client_cmds[SENDER_DELETE] = NULL;
415         acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
416         if (!conf.http_no_autostart_given)
417                 http_open(); /* ignore errors */
418         PARA_DEBUG_LOG("%s", "http sender init complete\n");
419 }