user_list.c: Simplify populate_user_list().
[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, fd;
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 phsd = sc->private_data;
109 switch (phsd->status) {
110 case HTTP_STREAMING: /* nothing to do */
111 break;
112 case HTTP_CONNECTED: /* need to recv get request */
113 if (FD_ISSET(sc->fd, rfds)) {
114 if (recv_pattern(sc->fd, HTTP_GET_MSG, MAXLINE)
115 < 0) {
116 phsd->status = HTTP_INVALID_GET_REQUEST;
117 } else {
118 phsd->status = HTTP_GOT_GET_REQUEST;
119 PARA_INFO_LOG("%s",
120 "received get request\n");
121 }
122 }
123 break;
124 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
125 phsd->status = HTTP_STREAMING;
126 http_send_ok_msg(sc);
127 break;
128 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
129 if (http_send_err_msg(sc) >= 0)
130 shutdown_client(sc);
131 break;
132 }
133 }
134 if (!FD_ISSET(listen_fd, rfds))
135 return;
136 ret = para_accept(listen_fd, NULL, 0);
137 if (ret < 0) {
138 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
139 return;
140 }
141 fd = ret;
142 ret = -E_MAX_CLIENTS;
143 if (conf.http_max_clients_arg > 0 && numclients >=
144 conf.http_max_clients_arg) {
145 goto err_out;
146 }
147 ret = mark_fd_nonblocking(fd);
148 if (ret < 0)
149 goto err_out;
150 ret = acl_check_access(fd, &http_acl, conf.http_default_deny_given);
151 if (ret < 0)
152 goto err_out;
153 numclients++;
154 sc = para_calloc(sizeof(*sc));
155 sc->fd = fd;
156 sc->name = make_message("%s", remote_name(fd));
157 PARA_NOTICE_LOG("connection from %s (fd %d)\n", sc->name, fd);
158 phsd = para_malloc(sizeof(*phsd));
159 sc->private_data = phsd;
160 phsd->status = HTTP_CONNECTED;
161 sc->cq = cq_new(MAX_BACKLOG);
162 PARA_INFO_LOG("accepted client #%d: %s (fd %d)\n", numclients,
163 sc->name, fd);
164 para_list_add(&sc->node, &clients);
165 add_close_on_fork_list(fd);
166 return;
167 err_out:
168 PARA_WARNING_LOG("%s\n", para_strerror(-ret));
169 close(fd);
170 }
171
172 static void http_pre_select(int *max_fileno, fd_set *rfds, __a_unused fd_set *wfds)
173 {
174 struct sender_client *sc, *tmp;
175
176 if (listen_fd < 0)
177 return;
178 para_fd_set(listen_fd, rfds, max_fileno);
179 list_for_each_entry_safe(sc, tmp, &clients, node) {
180 struct private_http_sender_data *phsd = sc->private_data;
181 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
182 para_fd_set(sc->fd, rfds, max_fileno);
183 }
184 }
185
186 static int http_com_on(__a_unused struct sender_command_data *scd)
187 {
188 if (listen_fd >= 0)
189 return 1;
190 return open_sender(IPPROTO_TCP, conf.http_port_arg);
191 }
192
193 static int http_com_off(__a_unused struct sender_command_data *scd)
194 {
195 if (listen_fd < 0)
196 return 1;
197 PARA_NOTICE_LOG("closing http port %d\n", conf.http_port_arg);
198 close(listen_fd);
199 del_close_on_fork_list(listen_fd);
200 http_shutdown_clients();
201 listen_fd = -1;
202 return 1;
203 }
204
205 static int http_com_deny(struct sender_command_data *scd)
206 {
207 acl_deny(scd->addr, scd->netmask, &http_acl,
208 conf.http_default_deny_given);
209 return 1;
210 }
211
212 static int http_com_allow(struct sender_command_data *scd)
213 {
214 acl_allow(scd->addr, scd->netmask, &http_acl,
215 conf.http_default_deny_given);
216 return 1;
217 }
218
219 static char *http_info(void)
220 {
221 char *clnts = NULL, *ret;
222 struct sender_client *sc, *tmp_sc;
223
224 char *acl_contents = acl_get_contents(&http_acl);
225 list_for_each_entry_safe(sc, tmp_sc, &clients, node) {
226 char *tmp = make_message("%s%s ", clnts? clnts : "", sc->name);
227 free(clnts);
228 clnts = tmp;
229 }
230 ret = make_message(
231 "http status: %s\n"
232 "http tcp port: %d\n"
233 "http clients: %d\n"
234 "http maximal number of clients: %d%s\n"
235 "http connected clients: %s\n"
236 "http access %s list: %s\n",
237 (listen_fd >= 0)? "on" : "off",
238 conf.http_port_arg,
239 numclients,
240 conf.http_max_clients_arg,
241 conf.http_max_clients_arg > 0? "" : " (unlimited)",
242 clnts? clnts : "(none)",
243 conf.http_default_deny_given? "allow" : "deny",
244 acl_contents? acl_contents : "(none)"
245 );
246 free(acl_contents);
247 free(clnts);
248 return ret;
249 }
250
251 static char *http_help(void)
252 {
253 return make_message(
254 "usage: {on|off}\n"
255 "usage: {allow|deny} IP mask\n"
256 "example: allow 127.0.0.1 32\n"
257 );
258 }
259
260 /**
261 * The init function of the http sender.
262 *
263 * \param s Pointer to the http sender struct.
264 *
265 * It initializes all function pointers of \a s, the client list and the access
266 * control list. If the autostart option was given, the tcp port is opened.
267 */
268 void http_send_init(struct sender *s)
269 {
270 int ret;
271 INIT_LIST_HEAD(&clients);
272 s->info = http_info;
273 s->send = http_send;
274 s->pre_select = http_pre_select;
275 s->post_select = http_post_select;
276 s->shutdown_clients = http_shutdown_clients;
277 s->help = http_help;
278 s->client_cmds[SENDER_ON] = http_com_on;
279 s->client_cmds[SENDER_OFF] = http_com_off;
280 s->client_cmds[SENDER_DENY] = http_com_deny;
281 s->client_cmds[SENDER_ALLOW] = http_com_allow;
282 s->client_cmds[SENDER_ADD] = NULL;
283 s->client_cmds[SENDER_DELETE] = NULL;
284 acl_init(&http_acl, conf.http_access_arg, conf.http_access_given);
285 if (conf.http_no_autostart_given)
286 return;
287 ret = open_sender(IPPROTO_TCP, conf.http_port_arg);
288 if (ret < 0)
289 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
290 else
291 listen_fd = ret;
292 }