]> git.tuebingen.mpg.de Git - paraslash.git/blob - http_send.c
40f0c0af2ee5995e7901c9aeffdb38feeb2f14fe
[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 "list.h"
22 #include "send.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 static int listen_fd = -1, numclients;
53
54 struct private_http_sender_data {
55         enum http_status status;
56 };
57
58 static int http_send_msg(struct sender_client *sc, const char *msg)
59 {
60         int ret = send_buffer(sc->fd, msg);
61
62         if (ret < 0)
63                 shutdown_client(sc);
64         return ret;
65 }
66
67 static void http_send_ok_msg(struct sender_client *sc)
68 {
69         PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
70         http_send_msg(sc, HTTP_OK_MSG);
71 }
72
73 static int http_send_err_msg(struct sender_client *sc)
74 {
75         PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
76         return http_send_msg(sc, HTTP_ERR_MSG);
77 }
78
79 static void http_shutdown_clients(void)
80 {
81         struct sender_client *sc, *tmp;
82         list_for_each_entry_safe(sc, tmp, &clients, node)
83                 shutdown_client(sc);
84 }
85
86 static void http_send(long unsigned current_chunk,
87         __a_unused long unsigned chunks_sent, const char *buf, size_t len)
88 {
89         struct sender_client *sc, *tmp;
90
91         list_for_each_entry_safe(sc, tmp, &clients, node) {
92                 struct private_http_sender_data *phsd = sc->private_data;
93                 if (phsd->status != HTTP_STREAMING)
94                         continue;
95                 send_chunk(sc, 0, current_chunk, buf, len);
96         }
97 }
98
99 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
100 {
101         int ret, i = -1, match;
102         struct sender_client *sc, *tmp;
103         struct private_http_sender_data *phsd;
104
105         if (listen_fd < 0)
106                 return;
107         list_for_each_entry_safe(sc, tmp, &clients, node) {
108                 i++;
109                 phsd = sc->private_data;
110 //              PARA_DEBUG_LOG("handling client %d: %s\n", i, remote_name(sc->fd));
111                 switch (phsd->status) {
112                 case HTTP_STREAMING: /* nothing to do */
113                         break;
114                 case HTTP_CONNECTED: /* need to recv get request */
115                         if (FD_ISSET(sc->fd, rfds)) {
116                                 if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE)
117                                                 < 0) {
118                                         phsd->status = HTTP_INVALID_GET_REQUEST;
119                                 } else {
120                                         phsd->status = HTTP_GOT_GET_REQUEST;
121                                         PARA_INFO_LOG("%s",
122                                                 "received get request\n");
123                                 }
124                         }
125                         break;
126                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
127                         phsd->status = HTTP_STREAMING;
128                         http_send_ok_msg(sc);
129                         break;
130                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
131                         if (http_send_err_msg(sc) >= 0)
132                                 shutdown_client(sc);
133                         break;
134                 }
135         }
136         if (!FD_ISSET(listen_fd, rfds))
137                 return;
138         ret = para_accept(listen_fd, NULL, 0);
139         if (ret < 0)
140                 goto err_out;
141         sc = para_calloc(sizeof(*sc));
142         sc->fd = ret;
143         sc->name = make_message("%s", remote_name(sc->fd));
144         PARA_NOTICE_LOG("connection from %s (fd %d)\n", sc->name, sc->fd);
145         ret = -E_MAX_CLIENTS;
146         if (conf.http_max_clients_arg > 0 && numclients >=
147                         conf.http_max_clients_arg) {
148                 goto err_out;
149         }
150         match = acl_lookup(sc->fd, &http_acl);
151         PARA_DEBUG_LOG("acl lookup returned %d\n", match);
152         ret = -E_ACL_PERM;
153         if ((match && !conf.http_default_deny_given) ||
154                         (!match && conf.http_default_deny_given))
155                 goto err_out;
156         ret = mark_fd_nonblocking(sc->fd);
157         if (ret < 0)
158                 goto err_out;
159         phsd = para_malloc(sizeof(*phsd));
160         sc->private_data = phsd;
161         phsd->status = HTTP_CONNECTED;
162         sc->cq = cq_new(MAX_BACKLOG);
163         numclients++;
164         PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
165                 sc->name, sc->fd);
166         para_list_add(&sc->node, &clients);
167         add_close_on_fork_list(sc->fd);
168         return;
169 err_out:
170         PARA_WARNING_LOG("%s\n", para_strerror(-ret));
171         if (sc->fd > 0)
172                 close(sc->fd);
173         free(sc);
174 }
175
176 static void http_pre_select(int *max_fileno, fd_set *rfds, __a_unused fd_set *wfds)
177 {
178         struct sender_client *sc, *tmp;
179
180         if (listen_fd < 0)
181                 return;
182         para_fd_set(listen_fd, rfds, max_fileno);
183         list_for_each_entry_safe(sc, tmp, &clients, node) {
184                 struct private_http_sender_data *phsd = sc->private_data;
185                 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
186                         para_fd_set(sc->fd, rfds, max_fileno);
187         }
188 }
189
190 static int http_com_on(__a_unused struct sender_command_data *scd)
191 {
192         if (listen_fd >= 0)
193                 return 1;
194         return open_sender(IPPROTO_TCP, conf.http_port_arg);
195 }
196
197 static int http_com_off(__a_unused struct sender_command_data *scd)
198 {
199         if (listen_fd < 0)
200                 return 1;
201         PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
202         close(listen_fd);
203         del_close_on_fork_list(listen_fd);
204         http_shutdown_clients();
205         listen_fd = -1;
206         return 1;
207 }
208
209 static int http_com_deny(struct sender_command_data *scd)
210 {
211         if (conf.http_default_deny_given)
212                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
213         else
214                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
215         return 1;
216 }
217
218 static int http_com_allow(struct sender_command_data *scd)
219 {
220         if (conf.http_default_deny_given)
221                 acl_add_entry(&http_acl, scd->addr, scd->netmask);
222         else
223                 acl_del_entry(&http_acl, scd->addr, scd->netmask);
224         return 1;
225 }
226
227 static char *http_info(void)
228 {
229         char *clnts = NULL, *ret;
230         struct sender_client *sc, *tmp_sc;
231
232         char *acl_contents = acl_get_contents(&http_acl);
233         list_for_each_entry_safe(sc, tmp_sc, &clients, node) {
234                 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
235                 free(clnts);
236                 clnts = tmp;
237         }
238         ret = make_message(
239                 "http status: %s\n"
240                 "http tcp port: %d\n"
241                 "http clients: %d\n"
242                 "http maximal number of clients: %d%s\n"
243                 "http connected clients: %s\n"
244                 "http access %s list: %s\n",
245                 (listen_fd >= 0)? "on" : "off",
246                 conf.http_port_arg,
247                 numclients,
248                 conf.http_max_clients_arg,
249                 conf.http_max_clients_arg > 0? "" : " (unlimited)",
250                 clnts? clnts : "(none)",
251                 conf.http_default_deny_given? "allow" : "deny",
252                 acl_contents? acl_contents : "(none)"
253         );
254         free(acl_contents);
255         free(clnts);
256         return ret;
257 }
258
259 static char *http_help(void)
260 {
261         return make_message(
262                 "usage: {on|off}\n"
263                 "usage: {allow|deny} IP mask\n"
264                 "example: allow 127.0.0.1 32\n"
265         );
266 }
267
268 /**
269  * The init function of the http sender.
270  *
271  * \param s Pointer to the http sender struct.
272  *
273  * It initializes all function pointers of \a s, the client list and the access
274  * control list. If the autostart option was given, the tcp port is opened.
275  */
276 void http_send_init(struct sender *s)
277 {
278         int ret;
279         INIT_LIST_HEAD(&clients);
280         s->info = http_info;
281         s->send = http_send;
282         s->pre_select = http_pre_select;
283         s->post_select = http_post_select;
284         s->shutdown_clients = http_shutdown_clients;
285         s->help = http_help;
286         s->client_cmds[SENDER_ON] = http_com_on;
287         s->client_cmds[SENDER_OFF] = http_com_off;
288         s->client_cmds[SENDER_DENY] = http_com_deny;
289         s->client_cmds[SENDER_ALLOW] = http_com_allow;
290         s->client_cmds[SENDER_ADD] = NULL;
291         s->client_cmds[SENDER_DELETE] = NULL;
292         acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
293         if (conf.http_no_autostart_given)
294                 return;
295         ret = open_sender(IPPROTO_TCP, conf.http_port_arg);
296         if (ret < 0)
297                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
298         else
299                 listen_fd = ret;
300 }