sender: Deplete ACLs on exit.
[paraslash.git] / http_send.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file http_send.c paraslash's http sender */
4
5 #include <netinet/in.h>
6 #include <sys/socket.h>
7 #include <regex.h>
8 #include <sys/types.h>
9 #include <arpa/inet.h>
10 #include <sys/un.h>
11 #include <netdb.h>
12 #include <lopsub.h>
13
14 #include "server.lsg.h"
15 #include "para.h"
16 #include "error.h"
17 #include "string.h"
18 #include "afh.h"
19 #include "server.h"
20 #include "http.h"
21 #include "list.h"
22 #include "send.h"
23 #include "sched.h"
24 #include "vss.h"
25 #include "net.h"
26 #include "fd.h"
27 #include "chunk_queue.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 /** The possible states of a client from the server's POV. */
33 enum http_client_status {
34         /** We accepted the connection on the tcp socket. */
35         HTTP_CONNECTED,
36         /** Successfully received the get request. */
37         HTTP_GOT_GET_REQUEST,
38         /** Connection is ready for sending audio data. */
39         HTTP_STREAMING,
40         /** We didn't receive a valid get request. */
41         HTTP_INVALID_GET_REQUEST
42 };
43
44 /** For each connected client, a structure of this type is maintained. */
45 struct private_http_sender_data {
46         /** The current state of this client. */
47         enum http_client_status status;
48 };
49
50 static struct sender_status http_sender_status, *hss = &http_sender_status;
51
52 static int http_send_msg(struct sender_client *sc, const char *msg)
53 {
54         int ret = write_buffer(sc->fd, msg);
55
56         if (ret < 0)
57                 shutdown_client(sc, hss);
58         return ret;
59 }
60
61 static void http_send_ok_msg(struct sender_client *sc)
62 {
63         PARA_INFO_LOG("sending http ok message to fd %d\n", sc->fd);
64         http_send_msg(sc, HTTP_OK_MSG);
65 }
66
67 static int http_send_err_msg(struct sender_client *sc)
68 {
69         PARA_NOTICE_LOG("sending bad request message to fd %d\n", sc->fd);
70         return http_send_msg(sc, HTTP_ERR_MSG);
71 }
72
73 static void http_shutdown_clients(void)
74 {
75         shutdown_clients(hss);
76 }
77
78 static void http_shutdown(void)
79 {
80         http_shutdown_clients();
81         generic_acl_deplete(&hss->acl);
82 }
83
84 static int queue_chunk_or_shutdown(struct sender_client *sc,
85                 struct sender_status *ss, const char *buf, size_t num_bytes)
86 {
87         int ret = cq_enqueue(sc->cq, buf, num_bytes);
88         if (ret < 0)
89                 shutdown_client(sc, ss);
90         return ret;
91 }
92
93 /**
94  * Send one chunk of audio data to a connected client.
95  *
96  * \param sc The client.
97  * \param ss The sender.
98  * \param current_chunk The number of the chunk to write.
99  * \param buf The data to write.
100  * \param len The number of bytes of \a buf.
101  * \param header_buf The audio file header.
102  * \param header_len The number of bytes of \a header_buf.
103  *
104  * On errors, the client is shut down. If only a part of the buffer could be
105  * written, the remainder is put into the chunk queue for that client.
106  */
107 static void http_send_chunk(struct sender_client *sc, struct sender_status *ss,
108                 long unsigned current_chunk, const char *buf, size_t len,
109                 const char *header_buf, size_t header_len)
110 {
111         int ret;
112
113         if (!sc->header_sent && current_chunk) {
114                 if (header_buf && header_len > 0) {
115                         ret = queue_chunk_or_shutdown(sc, ss, header_buf, header_len);
116                         if (ret < 0)
117                                 goto out;
118                 }
119         }
120         sc->header_sent = 1;
121         ret = send_queued_chunks(sc->fd, sc->cq);
122         if (ret < 0) {
123                 shutdown_client(sc, ss);
124                 goto out;
125         }
126         if (!len)
127                 goto out;
128         if (!ret) { /* still data left in the queue */
129                 ret = queue_chunk_or_shutdown(sc, ss, buf, len);
130                 goto out;
131         }
132         ret = xwrite(sc->fd, buf, len);
133         if (ret < 0) {
134                 shutdown_client(sc, ss);
135                 goto out;
136         }
137         if (ret != len)
138                 ret = queue_chunk_or_shutdown(sc, ss, buf + ret, len - ret);
139 out:
140         if (ret < 0)
141                 PARA_NOTICE_LOG("%s\n", para_strerror(-ret));
142 }
143
144 static void http_send(long unsigned current_chunk,
145         __a_unused long unsigned chunks_sent, const char *buf, size_t len,
146         const char *header_buf, size_t header_len)
147 {
148         struct sender_client *sc, *tmp;
149
150         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
151                 struct private_http_sender_data *phsd = sc->private_data;
152
153                 if (phsd->status == HTTP_STREAMING)
154                         http_send_chunk(sc, hss, current_chunk, buf, len,
155                                    header_buf, header_len);
156         }
157 }
158
159 static void http_post_select(fd_set *rfds, __a_unused fd_set *wfds)
160 {
161         struct sender_client *sc, *tmp;
162         struct private_http_sender_data *phsd;
163         int ret;
164
165         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
166                 phsd = sc->private_data;
167                 switch (phsd->status) {
168                 case HTTP_STREAMING: /* nothing to do */
169                         break;
170                 case HTTP_CONNECTED: /* need to recv get request */
171                         ret = read_pattern(sc->fd, HTTP_GET_MSG, MAXLINE, rfds);
172                         if (ret < 0)
173                                 phsd->status = HTTP_INVALID_GET_REQUEST;
174                         else if (ret > 0) {
175                                 phsd->status = HTTP_GOT_GET_REQUEST;
176                                 PARA_INFO_LOG("received get request\n");
177                         }
178                         break;
179                 case HTTP_GOT_GET_REQUEST: /* need to send ok msg */
180                         phsd->status = HTTP_STREAMING;
181                         http_send_ok_msg(sc);
182                         break;
183                 case HTTP_INVALID_GET_REQUEST: /* need to send err msg */
184                         if (http_send_err_msg(sc) >= 0)
185                                 shutdown_client(sc, hss);
186                         break;
187                 }
188         }
189         sc = accept_sender_client(hss, rfds);
190         if (!sc)
191                 return;
192         phsd = para_malloc(sizeof(*phsd));
193         sc->private_data = phsd;
194         phsd->status = HTTP_CONNECTED;
195 }
196
197 static void http_pre_select(int *max_fileno, fd_set *rfds, fd_set *wfds)
198 {
199         struct sender_client *sc, *tmp;
200
201         if (hss->listen_fd < 0)
202                 return;
203         para_fd_set(hss->listen_fd, rfds, max_fileno);
204         list_for_each_entry_safe(sc, tmp, &hss->client_list, node) {
205                 struct private_http_sender_data *phsd = sc->private_data;
206                 if (phsd->status == HTTP_CONNECTED) /* need to recv get request */
207                         para_fd_set(sc->fd, rfds, max_fileno);
208                 if (phsd->status == HTTP_GOT_GET_REQUEST ||
209                                 phsd->status == HTTP_INVALID_GET_REQUEST)
210                         para_fd_set(sc->fd, wfds, max_fileno);
211         }
212 }
213
214 static int http_com_on(__a_unused struct sender_command_data *scd)
215 {
216         return generic_com_on(hss, IPPROTO_TCP);
217 }
218
219 static int http_com_off(__a_unused struct sender_command_data *scd)
220 {
221         generic_com_off(hss);
222         return 1;
223 }
224
225 static int http_com_deny(struct sender_command_data *scd)
226 {
227         generic_com_deny(scd, hss);
228         return 1;
229 }
230
231 static int http_com_allow(struct sender_command_data *scd)
232 {
233         generic_com_allow(scd, hss);
234         return 1;
235 }
236
237 static char *http_status(void)
238 {
239         return generic_sender_status(hss, "http");
240 }
241
242 /*
243  * Initialize the client list and the access control list, and optionally
244  * listen on the tcp port.
245  */
246 static void http_send_init(void)
247 {
248         int ret;
249
250         init_sender_status(hss, OPT_RESULT(HTTP_ACCESS),
251                 OPT_UINT32_VAL(HTTP_PORT), OPT_UINT32_VAL(HTTP_MAX_CLIENTS),
252                 OPT_GIVEN(HTTP_DEFAULT_DENY));
253         if (OPT_GIVEN(HTTP_NO_AUTOSTART))
254                 return;
255         ret = generic_com_on(hss, IPPROTO_TCP);
256         if (ret < 0)
257                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
258 }
259
260 /**
261  * The HTTP sender.
262  *
263  * This is the only sender which does not FEC-encode the stream. This is not
264  * necessary because HTTP sits on top of TCP, a reliable transport which
265  * retransmits lost packets automatically. The sender employs per-client queues
266  * which queue chunks of audio data if they can not be sent immediately because
267  * the write operation would block. Most methods of the sender are implemented
268  * as wrappers for the generic functions defined in \ref send_common.c.
269  */
270 const struct sender http_sender = {
271         .name = "http",
272         .init = http_send_init,
273         .shutdown = http_shutdown,
274         .pre_select = http_pre_select,
275         .post_select = http_post_select,
276         .send = http_send,
277         .shutdown_clients = http_shutdown_clients,
278         .client_cmds = {
279                 [SENDER_on] = http_com_on,
280                 [SENDER_off] = http_com_off,
281                 [SENDER_deny] = http_com_deny,
282                 [SENDER_allow] = http_com_allow,
283         },
284         .help = generic_sender_help,
285         .status = http_status,
286 };