summaryrefslogtreecommitdiff
path: root/opentracker.c
diff options
context:
space:
mode:
Diffstat (limited to 'opentracker.c')
-rw-r--r--opentracker.c54
1 files changed, 29 insertions, 25 deletions
diff --git a/opentracker.c b/opentracker.c
index bcc075c..af336aa 100644
--- a/opentracker.c
+++ b/opentracker.c
@@ -24,17 +24,17 @@
24/* Libowfat */ 24/* Libowfat */
25#include "io.h" 25#include "io.h"
26#include "iob.h" 26#include "iob.h"
27#include "ip6.h"
28#include "socket.h" 27#include "socket.h"
29 28
30/* Opentracker */ 29/* Opentracker */
31#include "ot_accesslist.h" 30#include "ot_accesslist.h"
32#include "ot_hex.h" 31#include "ot_hex.h"
33#include "ot_http.h" 32#include "ot_http.h"
33#include "ot_ip.h"
34#include "ot_livesync.h" 34#include "ot_livesync.h"
35#include "ot_mutex.h" 35#include "ot_mutex.h"
36#include "ot_scan.h"
37#include "ot_random.h" 36#include "ot_random.h"
37#include "ot_scan.h"
38#include "ot_stats.h" 38#include "ot_stats.h"
39#include "ot_udp.h" 39#include "ot_udp.h"
40#include "trackerlogic.h" 40#include "trackerlogic.h"
@@ -156,14 +156,14 @@ static ssize_t header_complete(char *request, ssize_t byte_count) {
156 return 0; 156 return 0;
157} 157}
158 158
159static void handle_dead(const int64 sock) { 159static void handle_dead(const int64_t sock) {
160 struct http_data *cookie = io_getcookie(sock); 160 struct http_data *cookie = io_getcookie(sock);
161 if (cookie) { 161 if (cookie) {
162 size_t i; 162 size_t i;
163 for (i = 0; i < cookie->batches; ++i) 163 for (i = 0; i < cookie->batches; ++i)
164 iob_reset(cookie->batch + i); 164 iob_reset(cookie->batch + i);
165 free(cookie->batch); 165 free(cookie->batch);
166 array_reset(&cookie->request); 166 buf_reset(&cookie->request);
167 if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER)) 167 if (cookie->flag & (STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER))
168 mutex_workqueue_canceltask(sock); 168 mutex_workqueue_canceltask(sock);
169 free(cookie); 169 free(cookie);
@@ -171,7 +171,7 @@ static void handle_dead(const int64 sock) {
171 io_close(sock); 171 io_close(sock);
172} 172}
173 173
174static void handle_read(const int64 sock, struct ot_workstruct *ws) { 174static void handle_read(const int64_t sock, struct ot_workstruct *ws) {
175 struct http_data *cookie = io_getcookie(sock); 175 struct http_data *cookie = io_getcookie(sock);
176 ssize_t byte_count = io_tryread(sock, ws->inbuf, G_INBUF_SIZE); 176 ssize_t byte_count = io_tryread(sock, ws->inbuf, G_INBUF_SIZE);
177 177
@@ -183,26 +183,30 @@ static void handle_read(const int64 sock, struct ot_workstruct *ws) {
183 if (byte_count == -1) 183 if (byte_count == -1)
184 return; 184 return;
185 185
186 ws->request = NULL;
187 ws->request_size = 0;
188 ws->header_size = 0;
189 ws->keep_alive = 0;
190
186 /* If we get the whole request in one packet, handle it without copying */ 191 /* If we get the whole request in one packet, handle it without copying */
187 if (!array_start(&cookie->request)) { 192 if (!cookie->request.data) {
188 if ((ws->header_size = header_complete(ws->inbuf, byte_count))) { 193 if ((ws->header_size = header_complete(ws->inbuf, byte_count))) {
189 ws->request = ws->inbuf; 194 ws->request = ws->inbuf;
190 ws->request_size = byte_count; 195 ws->request_size = byte_count;
191 http_handle_request(sock, ws); 196 http_handle_request(sock, ws);
192 } else 197 } else if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count))
193 array_catb(&cookie->request, ws->inbuf, (size_t)byte_count); 198 handle_dead(sock);
194 return; 199 return;
195 } 200 }
196 201
197 array_catb(&cookie->request, ws->inbuf, byte_count); 202 if (buf_cat(&cookie->request, ws->inbuf, (size_t)byte_count) || cookie->request.size > G_INBUF_SIZE) {
198 if (array_failed(&cookie->request) || array_bytes(&cookie->request) > 8192) {
199 http_issue_error(sock, ws, CODE_HTTPERROR_500); 203 http_issue_error(sock, ws, CODE_HTTPERROR_500);
200 return; 204 return;
201 } 205 }
202 206
203 while ((ws->header_size = header_complete(array_start(&cookie->request), array_bytes(&cookie->request)))) { 207 while ((ws->header_size = header_complete(cookie->request.data, (ssize_t)cookie->request.size))) {
204 ws->request = array_start(&cookie->request); 208 ws->request = cookie->request.data;
205 ws->request_size = array_bytes(&cookie->request); 209 ws->request_size = (ssize_t)cookie->request.size;
206 http_handle_request(sock, ws); 210 http_handle_request(sock, ws);
207#ifdef WANT_KEEPALIVE 211#ifdef WANT_KEEPALIVE
208 if (!ws->keep_alive) 212 if (!ws->keep_alive)
@@ -211,7 +215,7 @@ static void handle_read(const int64 sock, struct ot_workstruct *ws) {
211 } 215 }
212} 216}
213 217
214static void handle_write(const int64 sock) { 218static void handle_write(const int64_t sock) {
215 struct http_data *cookie = io_getcookie(sock); 219 struct http_data *cookie = io_getcookie(sock);
216 size_t i; 220 size_t i;
217 int chunked = 0; 221 int chunked = 0;
@@ -223,7 +227,7 @@ static void handle_write(const int64 sock) {
223 227
224 for (i = 0; i < cookie->batches; ++i) { 228 for (i = 0; i < cookie->batches; ++i) {
225 if (cookie->batch[i].bytesleft) { 229 if (cookie->batch[i].bytesleft) {
226 int64 res = iob_send(sock, cookie->batch + i); 230 int64_t res = iob_send(sock, cookie->batch + i);
227 231
228 if (res == -3) { 232 if (res == -3) {
229 handle_dead(sock); 233 handle_dead(sock);
@@ -246,9 +250,9 @@ static void handle_write(const int64 sock) {
246 handle_dead(sock); 250 handle_dead(sock);
247} 251}
248 252
249static void handle_accept(const int64 serversocket) { 253static void handle_accept(const int64_t serversocket) {
250 struct http_data *cookie; 254 struct http_data *cookie;
251 int64 sock; 255 int64_t sock;
252 ot_ip6 ip; 256 ot_ip6 ip;
253 uint16 port; 257 uint16 port;
254 tai6464 t; 258 tai6464 t;
@@ -301,7 +305,7 @@ static void *server_mainloop(void *args) {
301 random_bytes(ws.rand48_state, sizeof(ws.rand48_state)); 305 random_bytes(ws.rand48_state, sizeof(ws.rand48_state));
302 306
303 for (;;) { 307 for (;;) {
304 int64 sock; 308 int64_t sock;
305 309
306 io_wait(); 310 io_wait();
307 311
@@ -335,14 +339,14 @@ static void *server_mainloop(void *args) {
335} 339}
336 340
337static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) { 341static int64_t ot_try_bind(ot_ip6 ip, uint16_t port, PROTO_FLAG proto) {
338 int64 sock = proto == FLAG_TCP ? socket_tcp6() : socket_udp6(); 342 int64_t sock = proto == FLAG_TCP ? socket_tcp6() : socket_udp6();
339 343
340#ifdef _DEBUG 344#ifdef _DEBUG
341 { 345 {
342 char *protos[] = {"TCP", "UDP", "UDP mcast"}; 346 char *protos[] = {"TCP", "UDP", "UDP mcast"};
343 char _debug[512]; 347 char _debug[512];
344 int off = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[proto]); 348 int off = snprintf(_debug, sizeof(_debug), "Binding socket type %s to address [", protos[proto]);
345 off += fmt_ip6c(_debug + off, ip); 349 off += ip_format(_debug + off, ip);
346 snprintf(_debug + off, sizeof(_debug) - off, "]:%d...", port); 350 snprintf(_debug + off, sizeof(_debug) - off, "]:%d...", port);
347 fputs(_debug, stderr); 351 fputs(_debug, stderr);
348 } 352 }
@@ -390,14 +394,14 @@ static int scan_ip6_port(const char *src, ot_ip6 ip, uint16_t *port) {
390 ++s; 394 ++s;
391 if (*s == '[') 395 if (*s == '[')
392 ++s, ++bracket; /* for v6 style notation */ 396 ++s, ++bracket; /* for v6 style notation */
393 if (!(off = scan_ip6(s, ip))) 397 if (!(off = ip_scan(s, ip)))
394 return 0; 398 return 0;
395 s += off; 399 s += off;
396 if (bracket && *s == ']') 400 if (bracket && *s == ']')
397 ++s; 401 ++s;
398 if (*s == 0 || isspace(*s)) 402 if (*s == 0 || isspace(*s))
399 return s - src; 403 return s - src;
400 if (!ip6_isv4mapped(ip)) { 404 if (!ip_is_v4mapped(ip)) {
401 if (*s != ':' && *s != '.') 405 if (*s != ':' && *s != '.')
402 return 0; 406 return 0;
403 if (!bracket && *(s) == ':') 407 if (!bracket && *(s) == ':')
@@ -419,7 +423,7 @@ static int scan_ip6_net(const char *src, ot_net *net) {
419 int off; 423 int off;
420 while (isspace(*s)) 424 while (isspace(*s))
421 ++s; 425 ++s;
422 if (!(off = scan_ip6(s, net->address))) 426 if (!(off = ip_scan(s, net->address)))
423 return 0; 427 return 0;
424 s += off; 428 s += off;
425 if (*s != '/') 429 if (*s != '/')
@@ -429,7 +433,7 @@ static int scan_ip6_net(const char *src, ot_net *net) {
429 if (!(off = scan_udec(s, &value, 128))) 433 if (!(off = scan_udec(s, &value, 128)))
430 return 0; 434 return 0;
431 net->bits = (int)value; 435 net->bits = (int)value;
432 if (ip6_isv4mapped(net->address)) 436 if (ip_is_v4mapped(net->address))
433 net->bits += 96; 437 net->bits += 96;
434 if (net->bits > 128) 438 if (net->bits > 128)
435 return 0; 439 return 0;
@@ -757,7 +761,7 @@ int main(int argc, char **argv) {
757 scanon = 0; 761 scanon = 0;
758 break; 762 break;
759 case 'i': 763 case 'i':
760 if (!scan_ip6(optarg, serverip)) { 764 if (!ip_scan(optarg, serverip)) {
761 usage(argv[0]); 765 usage(argv[0]);
762 exit(1); 766 exit(1);
763 } 767 }