1 /*
2 * http_server - HTTP server
3 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9 #include "includes.h"
10 #include <fcntl.h>
11
12 #include "common.h"
13 #include "eloop.h"
14 #include "httpread.h"
15 #include "http_server.h"
16
17 #define HTTP_SERVER_TIMEOUT 30
18 #define HTTP_SERVER_MAX_REQ_LEN 8000
19 #define HTTP_SERVER_MAX_CONNECTIONS 10
20
21 struct http_request {
22 struct http_request *next;
23 struct http_server *srv;
24 int fd;
25 struct sockaddr_in cli;
26 struct httpread *hread;
27 };
28
29 struct http_server {
30 void (*cb)(void *ctx, struct http_request *req);
31 void *cb_ctx;
32
33 int fd;
34 int port;
35
36 struct http_request *requests;
37 unsigned int request_count;
38 };
39
40
http_request_cb(struct httpread * handle,void * cookie,enum httpread_event en)41 static void http_request_cb(struct httpread *handle, void *cookie,
42 enum httpread_event en)
43 {
44 struct http_request *req = cookie;
45 struct http_server *srv = req->srv;
46
47 if (en == HTTPREAD_EVENT_FILE_READY) {
48 wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d received",
49 inet_ntoa(req->cli.sin_addr),
50 ntohs(req->cli.sin_port));
51 srv->cb(srv->cb_ctx, req);
52 return;
53 }
54 wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d could not be received "
55 "completely", inet_ntoa(req->cli.sin_addr),
56 ntohs(req->cli.sin_port));
57 http_request_deinit(req);
58 }
59
60
http_request_init(struct http_server * srv,int fd,struct sockaddr_in * cli)61 static struct http_request * http_request_init(struct http_server *srv, int fd,
62 struct sockaddr_in *cli)
63 {
64 struct http_request *req;
65
66 if (srv->request_count >= HTTP_SERVER_MAX_CONNECTIONS) {
67 wpa_printf(MSG_DEBUG, "HTTP: Too many concurrent requests");
68 close(fd);
69 return NULL;
70 }
71
72 req = os_zalloc(sizeof(*req));
73 if (!req) {
74 close(fd);
75 return NULL;
76 }
77
78 req->srv = srv;
79 req->fd = fd;
80 req->cli = *cli;
81
82 req->hread = httpread_create(req->fd, http_request_cb, req,
83 HTTP_SERVER_MAX_REQ_LEN,
84 HTTP_SERVER_TIMEOUT);
85 if (req->hread == NULL) {
86 http_request_deinit(req);
87 return NULL;
88 }
89
90 return req;
91 }
92
93
http_request_deinit(struct http_request * req)94 void http_request_deinit(struct http_request *req)
95 {
96 struct http_request *r, *p;
97 struct http_server *srv;
98
99 if (req == NULL)
100 return;
101
102 srv = req->srv;
103 p = NULL;
104 r = srv->requests;
105 while (r) {
106 if (r == req) {
107 if (p)
108 p->next = r->next;
109 else
110 srv->requests = r->next;
111 srv->request_count--;
112 break;
113 }
114 p = r;
115 r = r->next;
116 }
117
118 httpread_destroy(req->hread);
119 close(req->fd);
120 os_free(req);
121 }
122
123
http_request_free_all(struct http_request * req)124 static void http_request_free_all(struct http_request *req)
125 {
126 struct http_request *prev;
127 while (req) {
128 prev = req;
129 req = req->next;
130 http_request_deinit(prev);
131 }
132 }
133
134
http_request_send(struct http_request * req,struct wpabuf * resp)135 void http_request_send(struct http_request *req, struct wpabuf *resp)
136 {
137 int res;
138
139 wpa_printf(MSG_DEBUG, "HTTP: Send %lu byte response to %s:%d",
140 (unsigned long) wpabuf_len(resp),
141 inet_ntoa(req->cli.sin_addr),
142 ntohs(req->cli.sin_port));
143
144 res = send(req->fd, wpabuf_head(resp), wpabuf_len(resp), 0);
145 if (res < 0) {
146 wpa_printf(MSG_DEBUG, "HTTP: Send failed: %s",
147 strerror(errno));
148 } else if ((size_t) res < wpabuf_len(resp)) {
149 wpa_printf(MSG_DEBUG, "HTTP: Sent only %d of %lu bytes",
150 res, (unsigned long) wpabuf_len(resp));
151 /* TODO: add eloop handler for sending rest of the data */
152 }
153
154 wpabuf_free(resp);
155 }
156
157
http_request_send_and_deinit(struct http_request * req,struct wpabuf * resp)158 void http_request_send_and_deinit(struct http_request *req,
159 struct wpabuf *resp)
160 {
161 http_request_send(req, resp);
162 http_request_deinit(req);
163 }
164
165
http_request_get_type(struct http_request * req)166 enum httpread_hdr_type http_request_get_type(struct http_request *req)
167 {
168 return httpread_hdr_type_get(req->hread);
169 }
170
171
http_request_get_uri(struct http_request * req)172 char * http_request_get_uri(struct http_request *req)
173 {
174 return httpread_uri_get(req->hread);
175 }
176
177
http_request_get_hdr(struct http_request * req)178 char * http_request_get_hdr(struct http_request *req)
179 {
180 return httpread_hdr_get(req->hread);
181 }
182
183
http_request_get_data(struct http_request * req)184 char * http_request_get_data(struct http_request *req)
185 {
186 return httpread_data_get(req->hread);
187 }
188
189
http_request_get_hdr_line(struct http_request * req,const char * tag)190 char * http_request_get_hdr_line(struct http_request *req, const char *tag)
191 {
192 return httpread_hdr_line_get(req->hread, tag);
193 }
194
195
http_request_get_cli_addr(struct http_request * req)196 struct sockaddr_in * http_request_get_cli_addr(struct http_request *req)
197 {
198 return &req->cli;
199 }
200
201
http_server_cb(int sd,void * eloop_ctx,void * sock_ctx)202 static void http_server_cb(int sd, void *eloop_ctx, void *sock_ctx)
203 {
204 struct sockaddr_in addr;
205 socklen_t addr_len = sizeof(addr);
206 struct http_server *srv = eloop_ctx;
207 int conn;
208 struct http_request *req;
209
210 conn = accept(srv->fd, (struct sockaddr *) &addr, &addr_len);
211 if (conn < 0) {
212 wpa_printf(MSG_DEBUG, "HTTP: Failed to accept new connection: "
213 "%s", strerror(errno));
214 return;
215 }
216 wpa_printf(MSG_DEBUG, "HTTP: Connection from %s:%d",
217 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
218
219 req = http_request_init(srv, conn, &addr);
220 if (!req)
221 return;
222
223 req->next = srv->requests;
224 srv->requests = req;
225 srv->request_count++;
226 }
227
228
http_server_init(struct in_addr * addr,int port,void (* cb)(void * ctx,struct http_request * req),void * cb_ctx)229 struct http_server * http_server_init(struct in_addr *addr, int port,
230 void (*cb)(void *ctx,
231 struct http_request *req),
232 void *cb_ctx)
233 {
234 struct sockaddr_in sin;
235 struct http_server *srv;
236 int on = 1;
237
238 srv = os_zalloc(sizeof(*srv));
239 if (srv == NULL)
240 return NULL;
241 srv->cb = cb;
242 srv->cb_ctx = cb_ctx;
243
244 srv->fd = socket(AF_INET, SOCK_STREAM, 0);
245 if (srv->fd < 0)
246 goto fail;
247
248 if (setsockopt(srv->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
249 {
250 wpa_printf(MSG_DEBUG,
251 "HTTP: setsockopt(SO_REUSEADDR) failed: %s",
252 strerror(errno));
253 /* try to continue anyway */
254 }
255
256 if (fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0)
257 goto fail;
258 if (port < 0)
259 srv->port = 49152;
260 else
261 srv->port = port;
262
263 os_memset(&sin, 0, sizeof(sin));
264 sin.sin_family = AF_INET;
265 sin.sin_addr.s_addr = addr->s_addr;
266
267 for (;;) {
268 sin.sin_port = htons(srv->port);
269 if (bind(srv->fd, (struct sockaddr *) &sin, sizeof(sin)) == 0)
270 break;
271 if (errno == EADDRINUSE) {
272 /* search for unused port */
273 if (++srv->port == 65535 || port >= 0)
274 goto fail;
275 continue;
276 }
277 wpa_printf(MSG_DEBUG, "HTTP: Failed to bind server port %d: "
278 "%s", srv->port, strerror(errno));
279 goto fail;
280 }
281 if (listen(srv->fd, 10 /* max backlog */) < 0 ||
282 fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0 ||
283 eloop_register_sock(srv->fd, EVENT_TYPE_READ, http_server_cb,
284 srv, NULL))
285 goto fail;
286
287 wpa_printf(MSG_DEBUG, "HTTP: Started server on %s:%d",
288 inet_ntoa(*addr), srv->port);
289
290 return srv;
291
292 fail:
293 http_server_deinit(srv);
294 return NULL;
295 }
296
297
http_server_deinit(struct http_server * srv)298 void http_server_deinit(struct http_server *srv)
299 {
300 if (srv == NULL)
301 return;
302 if (srv->fd >= 0) {
303 eloop_unregister_sock(srv->fd, EVENT_TYPE_READ);
304 close(srv->fd);
305 }
306 http_request_free_all(srv->requests);
307
308 os_free(srv);
309 }
310
311
http_server_get_port(struct http_server * srv)312 int http_server_get_port(struct http_server *srv)
313 {
314 return srv->port;
315 }
316