00001
00017 #include "includes.h"
00018 #include <fcntl.h>
00019
00020 #include "common.h"
00021 #include "eloop.h"
00022 #include "httpread.h"
00023 #include "http_server.h"
00024
00025 #define HTTP_SERVER_TIMEOUT 30
00026 #define HTTP_SERVER_MAX_REQ_LEN 8000
00027 #define HTTP_SERVER_MAX_CONNECTIONS 10
00028
00029 struct http_request {
00030 struct http_request *next;
00031 struct http_server *srv;
00032 int fd;
00033 struct sockaddr_in cli;
00034 struct httpread *hread;
00035 };
00036
00037 struct http_server {
00038 void (*cb)(void *ctx, struct http_request *req);
00039 void *cb_ctx;
00040
00041 int fd;
00042 int port;
00043
00044 struct http_request *requests;
00045 unsigned int request_count;
00046 };
00047
00048
00049 static void http_request_cb(struct httpread *handle, void *cookie,
00050 enum httpread_event en)
00051 {
00052 struct http_request *req = cookie;
00053 struct http_server *srv = req->srv;
00054
00055 if (en == HTTPREAD_EVENT_FILE_READY) {
00056 wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d received",
00057 inet_ntoa(req->cli.sin_addr),
00058 ntohs(req->cli.sin_port));
00059 srv->cb(srv->cb_ctx, req);
00060 return;
00061 }
00062 wpa_printf(MSG_DEBUG, "HTTP: Request from %s:%d could not be received "
00063 "completely", inet_ntoa(req->cli.sin_addr),
00064 ntohs(req->cli.sin_port));
00065 http_request_deinit(req);
00066 }
00067
00068
00069 static struct http_request * http_request_init(struct http_server *srv, int fd,
00070 struct sockaddr_in *cli)
00071 {
00072 struct http_request *req;
00073
00074 if (srv->request_count >= HTTP_SERVER_MAX_CONNECTIONS) {
00075 wpa_printf(MSG_DEBUG, "HTTP: Too many concurrent requests");
00076 return NULL;
00077 }
00078
00079 req = os_zalloc(sizeof(*req));
00080 if (req == NULL)
00081 return NULL;
00082
00083 req->srv = srv;
00084 req->fd = fd;
00085 req->cli = *cli;
00086
00087 req->hread = httpread_create(req->fd, http_request_cb, req,
00088 HTTP_SERVER_MAX_REQ_LEN,
00089 HTTP_SERVER_TIMEOUT);
00090 if (req->hread == NULL) {
00091 http_request_deinit(req);
00092 return NULL;
00093 }
00094
00095 return req;
00096 }
00097
00098
00099 void http_request_deinit(struct http_request *req)
00100 {
00101 struct http_request *r, *p;
00102 struct http_server *srv;
00103
00104 if (req == NULL)
00105 return;
00106
00107 srv = req->srv;
00108 p = NULL;
00109 r = srv->requests;
00110 while (r) {
00111 if (r == req) {
00112 if (p)
00113 p->next = r->next;
00114 else
00115 srv->requests = r->next;
00116 srv->request_count--;
00117 break;
00118 }
00119 p = r;
00120 r = r->next;
00121 }
00122
00123 httpread_destroy(req->hread);
00124 close(req->fd);
00125 os_free(req);
00126 }
00127
00128
00129 static void http_request_free_all(struct http_request *req)
00130 {
00131 struct http_request *prev;
00132 while (req) {
00133 prev = req;
00134 req = req->next;
00135 http_request_deinit(prev);
00136 }
00137 }
00138
00139
00140 void http_request_send(struct http_request *req, struct wpabuf *resp)
00141 {
00142 int res;
00143
00144 wpa_printf(MSG_DEBUG, "HTTP: Send %lu byte response to %s:%d",
00145 (unsigned long) wpabuf_len(resp),
00146 inet_ntoa(req->cli.sin_addr),
00147 ntohs(req->cli.sin_port));
00148
00149 res = send(req->fd, wpabuf_head(resp), wpabuf_len(resp), 0);
00150 if (res < 0) {
00151 wpa_printf(MSG_DEBUG, "HTTP: Send failed: %s",
00152 strerror(errno));
00153 } else if ((size_t) res < wpabuf_len(resp)) {
00154 wpa_printf(MSG_DEBUG, "HTTP: Sent only %d of %lu bytes",
00155 res, (unsigned long) wpabuf_len(resp));
00156
00157 }
00158
00159 wpabuf_free(resp);
00160 }
00161
00162
00163 void http_request_send_and_deinit(struct http_request *req,
00164 struct wpabuf *resp)
00165 {
00166 http_request_send(req, resp);
00167 http_request_deinit(req);
00168 }
00169
00170
00171 enum httpread_hdr_type http_request_get_type(struct http_request *req)
00172 {
00173 return httpread_hdr_type_get(req->hread);
00174 }
00175
00176
00177 char * http_request_get_uri(struct http_request *req)
00178 {
00179 return httpread_uri_get(req->hread);
00180 }
00181
00182
00183 char * http_request_get_hdr(struct http_request *req)
00184 {
00185 return httpread_hdr_get(req->hread);
00186 }
00187
00188
00189 char * http_request_get_data(struct http_request *req)
00190 {
00191 return httpread_data_get(req->hread);
00192 }
00193
00194
00195 char * http_request_get_hdr_line(struct http_request *req, const char *tag)
00196 {
00197 return httpread_hdr_line_get(req->hread, tag);
00198 }
00199
00200
00201 struct sockaddr_in * http_request_get_cli_addr(struct http_request *req)
00202 {
00203 return &req->cli;
00204 }
00205
00206
00207 static void http_server_cb(int sd, void *eloop_ctx, void *sock_ctx)
00208 {
00209 struct sockaddr_in addr;
00210 socklen_t addr_len = sizeof(addr);
00211 struct http_server *srv = eloop_ctx;
00212 int conn;
00213 struct http_request *req;
00214
00215 conn = accept(srv->fd, (struct sockaddr *) &addr, &addr_len);
00216 if (conn < 0) {
00217 wpa_printf(MSG_DEBUG, "HTTP: Failed to accept new connection: "
00218 "%s", strerror(errno));
00219 return;
00220 }
00221 wpa_printf(MSG_DEBUG, "HTTP: Connection from %s:%d",
00222 inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
00223
00224 req = http_request_init(srv, conn, &addr);
00225 if (req == NULL) {
00226 close(conn);
00227 return;
00228 }
00229
00230 req->next = srv->requests;
00231 srv->requests = req;
00232 srv->request_count++;
00233 }
00234
00235
00236 struct http_server * http_server_init(struct in_addr *addr, int port,
00237 void (*cb)(void *ctx,
00238 struct http_request *req),
00239 void *cb_ctx)
00240 {
00241 struct sockaddr_in sin;
00242 struct http_server *srv;
00243
00244 srv = os_zalloc(sizeof(*srv));
00245 if (srv == NULL)
00246 return NULL;
00247 srv->cb = cb;
00248 srv->cb_ctx = cb_ctx;
00249
00250 srv->fd = socket(AF_INET, SOCK_STREAM, 0);
00251 if (srv->fd < 0)
00252 goto fail;
00253 if (fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0)
00254 goto fail;
00255 if (port < 0)
00256 srv->port = 49152;
00257 else
00258 srv->port = port;
00259
00260 os_memset(&sin, 0, sizeof(sin));
00261 sin.sin_family = AF_INET;
00262 sin.sin_addr.s_addr = addr->s_addr;
00263
00264 for (;;) {
00265 sin.sin_port = htons(srv->port);
00266 if (bind(srv->fd, (struct sockaddr *) &sin, sizeof(sin)) == 0)
00267 break;
00268 if (errno == EADDRINUSE) {
00269
00270 if (++srv->port == 65535 || port >= 0)
00271 goto fail;
00272 continue;
00273 }
00274 wpa_printf(MSG_DEBUG, "HTTP: Failed to bind server port %d: "
00275 "%s", srv->port, strerror(errno));
00276 goto fail;
00277 }
00278 if (listen(srv->fd, 10 ) < 0)
00279 goto fail;
00280 if (fcntl(srv->fd, F_SETFL, O_NONBLOCK) < 0)
00281 goto fail;
00282 if (eloop_register_sock(srv->fd, EVENT_TYPE_READ, http_server_cb,
00283 srv, NULL))
00284 goto fail;
00285
00286 wpa_printf(MSG_DEBUG, "HTTP: Started server on %s:%d",
00287 inet_ntoa(*addr), srv->port);
00288
00289 return srv;
00290
00291 fail:
00292 http_server_deinit(srv);
00293 return NULL;
00294 }
00295
00296
00297 void http_server_deinit(struct http_server *srv)
00298 {
00299 if (srv == NULL)
00300 return;
00301 if (srv->fd >= 0) {
00302 eloop_unregister_sock(srv->fd, EVENT_TYPE_READ);
00303 close(srv->fd);
00304 }
00305 http_request_free_all(srv->requests);
00306
00307 os_free(srv);
00308 }
00309
00310
00311 int http_server_get_port(struct http_server *srv)
00312 {
00313 return srv->port;
00314 }
00315