18dbcf02cSchristos /*
28dbcf02cSchristos * http_server - HTTP server
38dbcf02cSchristos * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
48dbcf02cSchristos *
562a52023Schristos * This software may be distributed under the terms of the BSD license.
662a52023Schristos * See README for more details.
78dbcf02cSchristos */
88dbcf02cSchristos
98dbcf02cSchristos #include "includes.h"
108dbcf02cSchristos #include <fcntl.h>
118dbcf02cSchristos
128dbcf02cSchristos #include "common.h"
138dbcf02cSchristos #include "eloop.h"
148dbcf02cSchristos #include "httpread.h"
158dbcf02cSchristos #include "http_server.h"
168dbcf02cSchristos
178dbcf02cSchristos #define HTTP_SERVER_TIMEOUT 30
188dbcf02cSchristos #define HTTP_SERVER_MAX_REQ_LEN 8000
198dbcf02cSchristos #define HTTP_SERVER_MAX_CONNECTIONS 10
208dbcf02cSchristos
218dbcf02cSchristos struct http_request {
228dbcf02cSchristos struct http_request *next;
238dbcf02cSchristos struct http_server *srv;
248dbcf02cSchristos int fd;
258dbcf02cSchristos struct sockaddr_in cli;
268dbcf02cSchristos struct httpread *hread;
278dbcf02cSchristos };
288dbcf02cSchristos
298dbcf02cSchristos struct http_server {
308dbcf02cSchristos void (*cb)(void *ctx, struct http_request *req);
318dbcf02cSchristos void *cb_ctx;
328dbcf02cSchristos
338dbcf02cSchristos int fd;
348dbcf02cSchristos int port;
358dbcf02cSchristos
368dbcf02cSchristos struct http_request *requests;
378dbcf02cSchristos unsigned int request_count;
388dbcf02cSchristos };
398dbcf02cSchristos
408dbcf02cSchristos
http_request_cb(struct httpread * handle,void * cookie,enum httpread_event en)418dbcf02cSchristos static void http_request_cb(struct httpread *handle, void *cookie,
428dbcf02cSchristos enum httpread_event en)
438dbcf02cSchristos {
448dbcf02cSchristos struct http_request *req = cookie;
458dbcf02cSchristos struct http_server *srv = req->srv;
468dbcf02cSchristos
478dbcf02cSchristos if (en == HTTPREAD_EVENT_FILE_READY) {
488dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d received",
498dbcf02cSchristos inet_ntoa(req->cli.sin_addr),
508dbcf02cSchristos ntohs(req->cli.sin_port));
518dbcf02cSchristos srv->cb(srv->cb_ctx, req);
528dbcf02cSchristos return;
538dbcf02cSchristos }
548dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d could not be received "
558dbcf02cSchristos "completely", inet_ntoa(req->cli.sin_addr),
568dbcf02cSchristos ntohs(req->cli.sin_port));
578dbcf02cSchristos http_request_deinit(req);
588dbcf02cSchristos }
598dbcf02cSchristos
608dbcf02cSchristos
http_request_init(struct http_server * srv,int fd,struct sockaddr_in * cli)618dbcf02cSchristos static struct http_request * http_request_init(struct http_server *srv, int fd,
628dbcf02cSchristos struct sockaddr_in *cli)
638dbcf02cSchristos {
648dbcf02cSchristos struct http_request *req;
658dbcf02cSchristos
668dbcf02cSchristos if (srv->request_count >= HTTP_SERVER_MAX_CONNECTIONS) {
678dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Too many concurrent requests");
688dbcf02cSchristos return NULL;
698dbcf02cSchristos }
708dbcf02cSchristos
718dbcf02cSchristos req = os_zalloc(sizeof(*req));
728dbcf02cSchristos if (req == NULL)
738dbcf02cSchristos return NULL;
748dbcf02cSchristos
758dbcf02cSchristos req->srv = srv;
768dbcf02cSchristos req->fd = fd;
778dbcf02cSchristos req->cli = *cli;
788dbcf02cSchristos
798dbcf02cSchristos req->hread = httpread_create(req->fd, http_request_cb, req,
808dbcf02cSchristos HTTP_SERVER_MAX_REQ_LEN,
818dbcf02cSchristos HTTP_SERVER_TIMEOUT);
828dbcf02cSchristos if (req->hread == NULL) {
838dbcf02cSchristos http_request_deinit(req);
848dbcf02cSchristos return NULL;
858dbcf02cSchristos }
868dbcf02cSchristos
878dbcf02cSchristos return req;
888dbcf02cSchristos }
898dbcf02cSchristos
908dbcf02cSchristos
http_request_deinit(struct http_request * req)918dbcf02cSchristos void http_request_deinit(struct http_request *req)
928dbcf02cSchristos {
938dbcf02cSchristos struct http_request *r, *p;
948dbcf02cSchristos struct http_server *srv;
958dbcf02cSchristos
968dbcf02cSchristos if (req == NULL)
978dbcf02cSchristos return;
988dbcf02cSchristos
998dbcf02cSchristos srv = req->srv;
1008dbcf02cSchristos p = NULL;
1018dbcf02cSchristos r = srv->requests;
1028dbcf02cSchristos while (r) {
1038dbcf02cSchristos if (r == req) {
1048dbcf02cSchristos if (p)
1058dbcf02cSchristos p->next = r->next;
1068dbcf02cSchristos else
1078dbcf02cSchristos srv->requests = r->next;
1088dbcf02cSchristos srv->request_count--;
1098dbcf02cSchristos break;
1108dbcf02cSchristos }
1118dbcf02cSchristos p = r;
1128dbcf02cSchristos r = r->next;
1138dbcf02cSchristos }
1148dbcf02cSchristos
1158dbcf02cSchristos httpread_destroy(req->hread);
1168dbcf02cSchristos close(req->fd);
1178dbcf02cSchristos os_free(req);
1188dbcf02cSchristos }
1198dbcf02cSchristos
1208dbcf02cSchristos
http_request_free_all(struct http_request * req)1218dbcf02cSchristos static void http_request_free_all(struct http_request *req)
1228dbcf02cSchristos {
1238dbcf02cSchristos struct http_request *prev;
1248dbcf02cSchristos while (req) {
1258dbcf02cSchristos prev = req;
1268dbcf02cSchristos req = req->next;
1278dbcf02cSchristos http_request_deinit(prev);
1288dbcf02cSchristos }
1298dbcf02cSchristos }
1308dbcf02cSchristos
1318dbcf02cSchristos
http_request_send(struct http_request * req,struct wpabuf * resp)1328dbcf02cSchristos void http_request_send(struct http_request *req, struct wpabuf *resp)
1338dbcf02cSchristos {
1348dbcf02cSchristos int res;
1358dbcf02cSchristos
1368dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Send %lu byte response to %s:%d",
1378dbcf02cSchristos (unsigned long) wpabuf_len(resp),
1388dbcf02cSchristos inet_ntoa(req->cli.sin_addr),
1398dbcf02cSchristos ntohs(req->cli.sin_port));
1408dbcf02cSchristos
1418dbcf02cSchristos res = send(req->fd, wpabuf_head(resp), wpabuf_len(resp), 0);
1428dbcf02cSchristos if (res < 0) {
1438dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Send failed: %s",
1448dbcf02cSchristos strerror(errno));
1458dbcf02cSchristos } else if ((size_t) res < wpabuf_len(resp)) {
1468dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Sent only %d of %lu bytes",
1478dbcf02cSchristos res, (unsigned long) wpabuf_len(resp));
1488dbcf02cSchristos /* TODO: add eloop handler for sending rest of the data */
1498dbcf02cSchristos }
1508dbcf02cSchristos
1518dbcf02cSchristos wpabuf_free(resp);
1528dbcf02cSchristos }
1538dbcf02cSchristos
1548dbcf02cSchristos
http_request_send_and_deinit(struct http_request * req,struct wpabuf * resp)1558dbcf02cSchristos void http_request_send_and_deinit(struct http_request *req,
1568dbcf02cSchristos struct wpabuf *resp)
1578dbcf02cSchristos {
1588dbcf02cSchristos http_request_send(req, resp);
1598dbcf02cSchristos http_request_deinit(req);
1608dbcf02cSchristos }
1618dbcf02cSchristos
1628dbcf02cSchristos
http_request_get_type(struct http_request * req)1638dbcf02cSchristos enum httpread_hdr_type http_request_get_type(struct http_request *req)
1648dbcf02cSchristos {
1658dbcf02cSchristos return httpread_hdr_type_get(req->hread);
1668dbcf02cSchristos }
1678dbcf02cSchristos
1688dbcf02cSchristos
http_request_get_uri(struct http_request * req)1698dbcf02cSchristos char * http_request_get_uri(struct http_request *req)
1708dbcf02cSchristos {
1718dbcf02cSchristos return httpread_uri_get(req->hread);
1728dbcf02cSchristos }
1738dbcf02cSchristos
1748dbcf02cSchristos
http_request_get_hdr(struct http_request * req)1758dbcf02cSchristos char * http_request_get_hdr(struct http_request *req)
1768dbcf02cSchristos {
1778dbcf02cSchristos return httpread_hdr_get(req->hread);
1788dbcf02cSchristos }
1798dbcf02cSchristos
1808dbcf02cSchristos
http_request_get_data(struct http_request * req)1818dbcf02cSchristos char * http_request_get_data(struct http_request *req)
1828dbcf02cSchristos {
1838dbcf02cSchristos return httpread_data_get(req->hread);
1848dbcf02cSchristos }
1858dbcf02cSchristos
1868dbcf02cSchristos
http_request_get_hdr_line(struct http_request * req,const char * tag)1878dbcf02cSchristos char * http_request_get_hdr_line(struct http_request *req, const char *tag)
1888dbcf02cSchristos {
1898dbcf02cSchristos return httpread_hdr_line_get(req->hread, tag);
1908dbcf02cSchristos }
1918dbcf02cSchristos
1928dbcf02cSchristos
http_request_get_cli_addr(struct http_request * req)1938dbcf02cSchristos struct sockaddr_in * http_request_get_cli_addr(struct http_request *req)
1948dbcf02cSchristos {
1958dbcf02cSchristos return &req->cli;
1968dbcf02cSchristos }
1978dbcf02cSchristos
1988dbcf02cSchristos
http_server_cb(int sd,void * eloop_ctx,void * sock_ctx)1998dbcf02cSchristos static void http_server_cb(int sd, void *eloop_ctx, void *sock_ctx)
2008dbcf02cSchristos {
2018dbcf02cSchristos struct sockaddr_in addr;
2028dbcf02cSchristos socklen_t addr_len = sizeof(addr);
2038dbcf02cSchristos struct http_server *srv = eloop_ctx;
2048dbcf02cSchristos int conn;
2058dbcf02cSchristos struct http_request *req;
2068dbcf02cSchristos
2078dbcf02cSchristos conn = accept(srv->fd, (struct sockaddr *) &addr, &addr_len);
2088dbcf02cSchristos if (conn < 0) {
2098dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Failed to accept new connection: "
2108dbcf02cSchristos "%s", strerror(errno));
2118dbcf02cSchristos return;
2128dbcf02cSchristos }
2138dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Connection from %s:%d",
2148dbcf02cSchristos inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
2158dbcf02cSchristos
2168dbcf02cSchristos req = http_request_init(srv, conn, &addr);
2178dbcf02cSchristos if (req == NULL) {
2188dbcf02cSchristos close(conn);
2198dbcf02cSchristos return;
2208dbcf02cSchristos }
2218dbcf02cSchristos
2228dbcf02cSchristos req->next = srv->requests;
2238dbcf02cSchristos srv->requests = req;
2248dbcf02cSchristos srv->request_count++;
2258dbcf02cSchristos }
2268dbcf02cSchristos
2278dbcf02cSchristos
http_server_init(struct in_addr * addr,int port,void (* cb)(void * ctx,struct http_request * req),void * cb_ctx)2288dbcf02cSchristos struct http_server * http_server_init(struct in_addr *addr, int port,
2298dbcf02cSchristos void (*cb)(void *ctx,
2308dbcf02cSchristos struct http_request *req),
2318dbcf02cSchristos void *cb_ctx)
2328dbcf02cSchristos {
2338dbcf02cSchristos struct sockaddr_in sin;
2348dbcf02cSchristos struct http_server *srv;
23536d97821Schristos int on = 1;
2368dbcf02cSchristos
2378dbcf02cSchristos srv = os_zalloc(sizeof(*srv));
2388dbcf02cSchristos if (srv == NULL)
2398dbcf02cSchristos return NULL;
2408dbcf02cSchristos srv->cb = cb;
2418dbcf02cSchristos srv->cb_ctx = cb_ctx;
2428dbcf02cSchristos
2438dbcf02cSchristos srv->fd = socket(AF_INET, SOCK_STREAM, 0);
2448dbcf02cSchristos if (srv->fd < 0)
2458dbcf02cSchristos goto fail;
24636d97821Schristos
24736d97821Schristos if (setsockopt(srv->fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
24836d97821Schristos {
24936d97821Schristos wpa_printf(MSG_DEBUG,
25036d97821Schristos "HTTP: setsockopt(SO_REUSEADDR) failed: %s",
25136d97821Schristos strerror(errno));
25236d97821Schristos /* try to continue anyway */
25336d97821Schristos }
25436d97821Schristos
2558dbcf02cSchristos if (fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0)
2568dbcf02cSchristos goto fail;
2578dbcf02cSchristos if (port < 0)
2588dbcf02cSchristos srv->port = 49152;
2598dbcf02cSchristos else
2608dbcf02cSchristos srv->port = port;
2618dbcf02cSchristos
2628dbcf02cSchristos os_memset(&sin, 0, sizeof(sin));
2638dbcf02cSchristos sin.sin_family = AF_INET;
2648dbcf02cSchristos sin.sin_addr.s_addr = addr->s_addr;
2658dbcf02cSchristos
2668dbcf02cSchristos for (;;) {
2678dbcf02cSchristos sin.sin_port = htons(srv->port);
2688dbcf02cSchristos if (bind(srv->fd, (struct sockaddr *) &sin, sizeof(sin)) == 0)
2698dbcf02cSchristos break;
2708dbcf02cSchristos if (errno == EADDRINUSE) {
2718dbcf02cSchristos /* search for unused port */
2728dbcf02cSchristos if (++srv->port == 65535 || port >= 0)
2738dbcf02cSchristos goto fail;
2748dbcf02cSchristos continue;
2758dbcf02cSchristos }
2768dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Failed to bind server port %d: "
2778dbcf02cSchristos "%s", srv->port, strerror(errno));
2788dbcf02cSchristos goto fail;
2798dbcf02cSchristos }
280*928750b6Schristos if (listen(srv->fd, 10 /* max backlog */) < 0 ||
281*928750b6Schristos fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0 ||
282*928750b6Schristos eloop_register_sock(srv->fd, EVENT_TYPE_READ, http_server_cb,
2838dbcf02cSchristos srv, NULL))
2848dbcf02cSchristos goto fail;
2858dbcf02cSchristos
2868dbcf02cSchristos wpa_printf(MSG_DEBUG, "HTTP: Started server on %s:%d",
2878dbcf02cSchristos inet_ntoa(*addr), srv->port);
2888dbcf02cSchristos
2898dbcf02cSchristos return srv;
2908dbcf02cSchristos
2918dbcf02cSchristos fail:
2928dbcf02cSchristos http_server_deinit(srv);
2938dbcf02cSchristos return NULL;
2948dbcf02cSchristos }
2958dbcf02cSchristos
2968dbcf02cSchristos
http_server_deinit(struct http_server * srv)2978dbcf02cSchristos void http_server_deinit(struct http_server *srv)
2988dbcf02cSchristos {
2998dbcf02cSchristos if (srv == NULL)
3008dbcf02cSchristos return;
3018dbcf02cSchristos if (srv->fd >= 0) {
3028dbcf02cSchristos eloop_unregister_sock(srv->fd, EVENT_TYPE_READ);
3038dbcf02cSchristos close(srv->fd);
3048dbcf02cSchristos }
3058dbcf02cSchristos http_request_free_all(srv->requests);
3068dbcf02cSchristos
3078dbcf02cSchristos os_free(srv);
3088dbcf02cSchristos }
3098dbcf02cSchristos
3108dbcf02cSchristos
http_server_get_port(struct http_server * srv)3118dbcf02cSchristos int http_server_get_port(struct http_server *srv)
3128dbcf02cSchristos {
3138dbcf02cSchristos return srv->port;
3148dbcf02cSchristos }
315