diff options
Diffstat (limited to 'ot_http.c')
| -rw-r--r-- | ot_http.c | 160 |
1 files changed, 62 insertions, 98 deletions
| @@ -15,7 +15,7 @@ | |||
| 15 | #include <unistd.h> | 15 | #include <unistd.h> |
| 16 | 16 | ||
| 17 | /* Libowfat */ | 17 | /* Libowfat */ |
| 18 | #include "iob.h" | 18 | #include "io.h" |
| 19 | 19 | ||
| 20 | /* Opentracker */ | 20 | /* Opentracker */ |
| 21 | #include "ot_accesslist.h" | 21 | #include "ot_accesslist.h" |
| @@ -25,27 +25,21 @@ | |||
| 25 | #include "ot_ip.h" | 25 | #include "ot_ip.h" |
| 26 | #include "ot_mutex.h" | 26 | #include "ot_mutex.h" |
| 27 | #include "ot_scan.h" | 27 | #include "ot_scan.h" |
| 28 | #include "ot_fdstate.h" | ||
| 28 | #include "ot_stats.h" | 29 | #include "ot_stats.h" |
| 29 | #include "scan_urlencoded_query.h" | 30 | #include "ot_urlencoded.h" |
| 30 | #include "trackerlogic.h" | 31 | #include "trackerlogic.h" |
| 31 | 32 | ||
| 32 | #ifdef WANT_NO_AUTO_FREE | ||
| 33 | #define OT_IOB_INIT(B) (bzero(B, sizeof(io_batch)), 0) | ||
| 34 | #else | ||
| 35 | #define OT_IOB_INIT(B) iob_init_autofree(B, 0) | ||
| 36 | #endif | ||
| 37 | |||
| 38 | #define OT_MAXMULTISCRAPE_COUNT 64 | 33 | #define OT_MAXMULTISCRAPE_COUNT 64 |
| 39 | #define OT_BATCH_LIMIT (1024 * 1024 * 16) | 34 | extern const char *g_redirecturl; |
| 40 | extern char *g_redirecturl; | ||
| 41 | 35 | ||
| 42 | char *g_stats_path; | 36 | const char *g_stats_path; |
| 43 | ssize_t g_stats_path_len; | 37 | ssize_t g_stats_path_len; |
| 44 | 38 | ||
| 45 | enum { SUCCESS_HTTP_HEADER_LENGTH = 80, SUCCESS_HTTP_SIZE_OFF = 17 }; | 39 | enum { SUCCESS_HTTP_HEADER_LENGTH = 80, SUCCESS_HTTP_SIZE_OFF = 17 }; |
| 46 | 40 | ||
| 47 | static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { | 41 | static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { |
| 48 | struct http_data *cookie = io_getcookie(sock); | 42 | struct http_data *cookie = fdstate_cookie(sock); |
| 49 | ssize_t written_size; | 43 | ssize_t written_size; |
| 50 | 44 | ||
| 51 | if (!cookie) { | 45 | if (!cookie) { |
| @@ -79,7 +73,6 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { | |||
| 79 | 73 | ||
| 80 | if (written_size < ws->reply_size) { | 74 | if (written_size < ws->reply_size) { |
| 81 | char *outbuf; | 75 | char *outbuf; |
| 82 | tai6464 t; | ||
| 83 | 76 | ||
| 84 | if (!(outbuf = malloc(ws->reply_size - written_size))) { | 77 | if (!(outbuf = malloc(ws->reply_size - written_size))) { |
| 85 | buf_reset(&cookie->request); | 78 | buf_reset(&cookie->request); |
| @@ -90,25 +83,21 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { | |||
| 90 | 83 | ||
| 91 | memcpy(outbuf, ws->reply + written_size, ws->reply_size - written_size); | 84 | memcpy(outbuf, ws->reply + written_size, ws->reply_size - written_size); |
| 92 | if (!cookie->batch) { | 85 | if (!cookie->batch) { |
| 93 | cookie->batch = malloc(sizeof(io_batch)); | 86 | cookie->batch = calloc(1, sizeof(ot_batch)); |
| 94 | if (!cookie->batch || OT_IOB_INIT(cookie->batch) == -1) { | 87 | if (!cookie->batch) { |
| 95 | free(cookie->batch); | ||
| 96 | free(outbuf); | 88 | free(outbuf); |
| 97 | buf_reset(&cookie->request); | 89 | buf_reset(&cookie->request); |
| 98 | free(cookie); | 90 | free(cookie); |
| 99 | io_close(sock); | 91 | io_close(sock); |
| 100 | return; | 92 | return; |
| 101 | } | 93 | } |
| 102 | |||
| 103 | cookie->batches = 1; | ||
| 104 | } | 94 | } |
| 105 | 95 | ||
| 106 | iob_addbuf_free(cookie->batch, outbuf, ws->reply_size - written_size); | 96 | batch_add(cookie->batch, outbuf, ws->reply_size - written_size); |
| 107 | 97 | ||
| 108 | /* writeable short data sockets just have a tcp timeout */ | 98 | /* writeable short data sockets just have a tcp timeout */ |
| 109 | if (!ws->keep_alive) { | 99 | if (!ws->keep_alive) { |
| 110 | taia_uint(&t, 0); | 100 | fdstate_set_expiry(sock, 0); |
| 111 | io_timeout(sock, t); | ||
| 112 | io_dontwantread(sock); | 101 | io_dontwantread(sock); |
| 113 | } | 102 | } |
| 114 | io_wantwrite(sock); | 103 | io_wantwrite(sock); |
| @@ -125,9 +114,9 @@ static void http_senddata(const int64_t sock, struct ot_workstruct *ws) { | |||
| 125 | #define HTTPERROR_404 return http_issue_error(sock, ws, CODE_HTTPERROR_404) | 114 | #define HTTPERROR_404 return http_issue_error(sock, ws, CODE_HTTPERROR_404) |
| 126 | #define HTTPERROR_500 return http_issue_error(sock, ws, CODE_HTTPERROR_500) | 115 | #define HTTPERROR_500 return http_issue_error(sock, ws, CODE_HTTPERROR_500) |
| 127 | ssize_t http_issue_error(const int64_t sock, struct ot_workstruct *ws, int code) { | 116 | ssize_t http_issue_error(const int64_t sock, struct ot_workstruct *ws, int code) { |
| 128 | char *error_code[] = {"302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", "402 Payment Required", | 117 | const char *error_code[] = {"302 Found", "400 Invalid Request", "400 Invalid Request", "400 Invalid Request", "402 Payment Required", |
| 129 | "403 Not Modest", "403 Access Denied", "404 Not Found", "500 Internal Server Error"}; | 118 | "403 Not Modest", "403 Access Denied", "404 Not Found", "500 Internal Server Error"}; |
| 130 | char *title = error_code[code]; | 119 | const char *title = error_code[code]; |
| 131 | 120 | ||
| 132 | ws->reply = ws->outbuf; | 121 | ws->reply = ws->outbuf; |
| 133 | if (code == CODE_HTTPERROR_302) | 122 | if (code == CODE_HTTPERROR_302) |
| @@ -145,12 +134,10 @@ ssize_t http_issue_error(const int64_t sock, struct ot_workstruct *ws, int code) | |||
| 145 | } | 134 | } |
| 146 | 135 | ||
| 147 | ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t iovec_entries, struct iovec *iovector, int is_partial) { | 136 | ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t iovec_entries, struct iovec *iovector, int is_partial) { |
| 148 | struct http_data *cookie = io_getcookie(sock); | 137 | struct http_data *cookie = fdstate_cookie(sock); |
| 149 | io_batch *current; | ||
| 150 | char *header; | 138 | char *header; |
| 151 | const char *encoding = ""; | 139 | const char *encoding = ""; |
| 152 | size_t i, header_size, size = iovec_length(&iovec_entries, (const struct iovec **)&iovector); | 140 | size_t header_size, size = iovec_length(&iovec_entries, (const struct iovec **)&iovector); |
| 153 | tai6464 t; | ||
| 154 | 141 | ||
| 155 | /* No cookie? Bad socket. Leave. */ | 142 | /* No cookie? Bad socket. Leave. */ |
| 156 | if (!cookie) { | 143 | if (!cookie) { |
| @@ -199,46 +186,28 @@ ssize_t http_sendiovecdata(const int64_t sock, struct ot_workstruct *ws, size_t | |||
| 199 | } | 186 | } |
| 200 | 187 | ||
| 201 | if (!cookie->batch) { | 188 | if (!cookie->batch) { |
| 202 | cookie->batch = malloc(sizeof(io_batch)); | 189 | cookie->batch = calloc(1, sizeof(ot_batch)); |
| 203 | if (!cookie->batch || OT_IOB_INIT(cookie->batch) == -1) { | 190 | if (!cookie->batch) { |
| 204 | free(cookie->batch); | ||
| 205 | free(header); | 191 | free(header); |
| 206 | iovec_free(&iovec_entries, &iovector); | 192 | iovec_free(&iovec_entries, &iovector); |
| 207 | HTTPERROR_500; | 193 | HTTPERROR_500; |
| 208 | } | 194 | } |
| 209 | cookie->batches = 1; | ||
| 210 | } | 195 | } |
| 211 | current = cookie->batch + cookie->batches - 1; | ||
| 212 | iob_addbuf_free(current, header, header_size); | ||
| 213 | 196 | ||
| 214 | /* Split huge iovectors into separate io_batches */ | 197 | batch_add(cookie->batch, header, header_size); |
| 215 | for (i = 0; i < (size_t)iovec_entries; ++i) { | 198 | batch_add_vector(cookie->batch, iovector, iovec_entries); |
| 216 | /* If the current batch's limit is reached, try to reallocate a new batch to work on */ | ||
| 217 | if (current->bytesleft > OT_BATCH_LIMIT) { | ||
| 218 | io_batch *new_batch = realloc(cookie->batch, (cookie->batches + 1) * sizeof(io_batch)); | ||
| 219 | if (new_batch) { | ||
| 220 | cookie->batch = new_batch; | ||
| 221 | if (OT_IOB_INIT(current) != -1) | ||
| 222 | current = cookie->batch + cookie->batches++; | ||
| 223 | } | ||
| 224 | } | ||
| 225 | iob_addbuf_free(current, iovector[i].iov_base, iovector[i].iov_len); | ||
| 226 | } | ||
| 227 | free(iovector); | 199 | free(iovector); |
| 200 | |||
| 228 | if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) | 201 | if (cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) |
| 229 | iob_addbuf(current, "\r\n", 2); | 202 | batch_add_copy(cookie->batch, "\r\n", 2); |
| 230 | } | 203 | } |
| 231 | 204 | ||
| 232 | if ((cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) && cookie->batch && !is_partial) { | 205 | if ((cookie->flag & STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER) && cookie->batch && !is_partial) { |
| 233 | current = cookie->batch + cookie->batches - 1; | 206 | batch_add_copy(cookie->batch, "0\r\n\r\n", 5); |
| 234 | iob_addbuf(current, "0\r\n\r\n", 5); | ||
| 235 | cookie->flag &= ~STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER; | 207 | cookie->flag &= ~STRUCT_HTTP_FLAG_CHUNKED_IN_TRANSFER; |
| 236 | } | 208 | } |
| 237 | 209 | ||
| 238 | /* writeable sockets timeout after 10 minutes */ | 210 | fdstate_set_expiry(sock, g_now_seconds + OT_CLIENT_TIMEOUT_SEND); |
| 239 | taia_now(&t); | ||
| 240 | taia_addsec(&t, &t, OT_CLIENT_TIMEOUT_SEND); | ||
| 241 | io_timeout(sock, t); | ||
| 242 | io_wantwrite(sock); | 211 | io_wantwrite(sock); |
| 243 | return 0; | 212 | return 0; |
| 244 | } | 213 | } |
| @@ -274,7 +243,7 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c | |||
| 274 | {"url", TASK_FULLSCRAPE_TPB_URLENCODED}, {"txt", TASK_FULLSCRAPE_TPB_ASCII}, | 243 | {"url", TASK_FULLSCRAPE_TPB_URLENCODED}, {"txt", TASK_FULLSCRAPE_TPB_ASCII}, |
| 275 | {"txtp", TASK_FULLSCRAPE_TPB_ASCII_PLUS}, {NULL, -3}}; | 244 | {"txtp", TASK_FULLSCRAPE_TPB_ASCII_PLUS}, {NULL, -3}}; |
| 276 | 245 | ||
| 277 | struct http_data *cookie = io_getcookie(sock); | 246 | struct http_data *cookie = fdstate_cookie(sock); |
| 278 | int mode = TASK_STATS_PEERS, scanon = 1, format = 0; | 247 | int mode = TASK_STATS_PEERS, scanon = 1, format = 0; |
| 279 | 248 | ||
| 280 | /* A socket without one is a dead socket, which is what http_sendiovecdata | 249 | /* A socket without one is a dead socket, which is what http_sendiovecdata |
| @@ -288,21 +257,21 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c | |||
| 288 | #endif | 257 | #endif |
| 289 | 258 | ||
| 290 | while (scanon) { | 259 | while (scanon) { |
| 291 | switch (scan_find_keywords(keywords_main, &read_ptr, SCAN_SEARCHPATH_PARAM)) { | 260 | switch (urlencoded_match_keyword(keywords_main, &read_ptr, SCAN_SEARCHPATH_PARAM)) { |
| 292 | case -2: | 261 | case -2: |
| 293 | scanon = 0; | 262 | scanon = 0; |
| 294 | break; /* TERMINATOR */ | 263 | break; /* TERMINATOR */ |
| 295 | case -1: | 264 | case -1: |
| 296 | HTTPERROR_400_PARAM; /* PARSE ERROR */ | 265 | HTTPERROR_400_PARAM; /* PARSE ERROR */ |
| 297 | case -3: | 266 | case -3: |
| 298 | scan_urlencoded_skipvalue(&read_ptr); | 267 | urlencoded_skip_value(&read_ptr); |
| 299 | break; | 268 | break; |
| 300 | case 1: /* matched "mode" */ | 269 | case 1: /* matched "mode" */ |
| 301 | if ((mode = scan_find_keywords(keywords_mode, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) | 270 | if ((mode = urlencoded_match_keyword(keywords_mode, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) |
| 302 | HTTPERROR_400_PARAM; | 271 | HTTPERROR_400_PARAM; |
| 303 | break; | 272 | break; |
| 304 | case 2: /* matched "format" */ | 273 | case 2: /* matched "format" */ |
| 305 | if ((format = scan_find_keywords(keywords_format, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) | 274 | if ((format = urlencoded_match_keyword(keywords_format, &read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) |
| 306 | HTTPERROR_400_PARAM; | 275 | HTTPERROR_400_PARAM; |
| 307 | break; | 276 | break; |
| 308 | case 3: | 277 | case 3: |
| @@ -317,7 +286,6 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c | |||
| 317 | } | 286 | } |
| 318 | 287 | ||
| 319 | if (mode == TASK_STATS_TPB) { | 288 | if (mode == TASK_STATS_TPB) { |
| 320 | tai6464 t; | ||
| 321 | #ifdef WANT_COMPRESSION_GZIP | 289 | #ifdef WANT_COMPRESSION_GZIP |
| 322 | ws->request[ws->request_size - 1] = 0; | 290 | ws->request[ws->request_size - 1] = 0; |
| 323 | #ifndef WANT_COMPRESSION_GZIP_ALWAYS | 291 | #ifndef WANT_COMPRESSION_GZIP_ALWAYS |
| @@ -333,8 +301,7 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c | |||
| 333 | cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; | 301 | cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; |
| 334 | 302 | ||
| 335 | /* Clients waiting for us should not easily timeout */ | 303 | /* Clients waiting for us should not easily timeout */ |
| 336 | taia_uint(&t, 0); | 304 | fdstate_set_expiry(sock, 0); |
| 337 | io_timeout(sock, t); | ||
| 338 | fullscrape_deliver(sock, format); | 305 | fullscrape_deliver(sock, format); |
| 339 | io_dontwantread(sock); | 306 | io_dontwantread(sock); |
| 340 | return ws->reply_size = -2; | 307 | return ws->reply_size = -2; |
| @@ -343,13 +310,11 @@ static ssize_t http_handle_stats(const int64_t sock, struct ot_workstruct *ws, c | |||
| 343 | 310 | ||
| 344 | /* default format for now */ | 311 | /* default format for now */ |
| 345 | if ((mode & TASK_CLASS_MASK) == TASK_STATS) { | 312 | if ((mode & TASK_CLASS_MASK) == TASK_STATS) { |
| 346 | tai6464 t; | ||
| 347 | 313 | ||
| 348 | cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK; | 314 | cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK; |
| 349 | 315 | ||
| 350 | /* Complex stats also include expensive memory debugging tools */ | 316 | /* Complex stats also include expensive memory debugging tools */ |
| 351 | taia_uint(&t, 0); | 317 | fdstate_set_expiry(sock, 0); |
| 352 | io_timeout(sock, t); | ||
| 353 | stats_deliver(sock, mode); | 318 | stats_deliver(sock, mode); |
| 354 | return ws->reply_size = -2; | 319 | return ws->reply_size = -2; |
| 355 | } | 320 | } |
| @@ -369,9 +334,8 @@ typedef struct { | |||
| 369 | 334 | ||
| 370 | #ifdef WANT_FULLSCRAPE | 335 | #ifdef WANT_FULLSCRAPE |
| 371 | static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct *ws) { | 336 | static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct *ws) { |
| 372 | struct http_data *cookie = io_getcookie(sock); | 337 | struct http_data *cookie = fdstate_cookie(sock); |
| 373 | int format = 0; | 338 | int format = 0; |
| 374 | tai6464 t; | ||
| 375 | 339 | ||
| 376 | #ifdef WANT_MODEST_FULLSCRAPES | 340 | #ifdef WANT_MODEST_FULLSCRAPES |
| 377 | { | 341 | { |
| @@ -430,9 +394,8 @@ static ssize_t http_handle_fullscrape(const int64_t sock, struct ot_workstruct * | |||
| 430 | /* Pass this task to the worker thread */ | 394 | /* Pass this task to the worker thread */ |
| 431 | cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; | 395 | cookie->flag |= STRUCT_HTTP_FLAG_WAITINGFORTASK | STRUCT_HTTP_FLAG_CHUNKED; |
| 432 | /* Clients waiting for us should not easily timeout */ | 396 | /* Clients waiting for us should not easily timeout */ |
| 433 | taia_uint(&t, 0); | 397 | fdstate_set_expiry(sock, 0); |
| 434 | io_timeout(sock, t); | 398 | fullscrape_deliver(sock, format); |
| 435 | fullscrape_deliver(sock, TASK_FULLSCRAPE | format); | ||
| 436 | io_dontwantread(sock); | 399 | io_dontwantread(sock); |
| 437 | return ws->reply_size = -2; | 400 | return ws->reply_size = -2; |
| 438 | } | 401 | } |
| @@ -455,18 +418,18 @@ static ssize_t http_handle_scrape(const int64_t sock, struct ot_workstruct *ws, | |||
| 455 | } | 418 | } |
| 456 | 419 | ||
| 457 | while (scanon) { | 420 | while (scanon) { |
| 458 | switch (scan_find_keywords(keywords_scrape, &read_ptr, SCAN_SEARCHPATH_PARAM)) { | 421 | switch (urlencoded_match_keyword(keywords_scrape, &read_ptr, SCAN_SEARCHPATH_PARAM)) { |
| 459 | case -2: | 422 | case -2: |
| 460 | scanon = 0; | 423 | scanon = 0; |
| 461 | break; /* TERMINATOR */ | 424 | break; /* TERMINATOR */ |
| 462 | default: | 425 | default: |
| 463 | HTTPERROR_400_PARAM; /* PARSE ERROR */ | 426 | HTTPERROR_400_PARAM; /* PARSE ERROR */ |
| 464 | case -3: | 427 | case -3: |
| 465 | scan_urlencoded_skipvalue(&read_ptr); | 428 | urlencoded_skip_value(&read_ptr); |
| 466 | break; | 429 | break; |
| 467 | case 1: /* matched "info_hash" */ | 430 | case 1: /* matched "info_hash" */ |
| 468 | /* ignore this, when we have less than 20 bytes */ | 431 | /* ignore this, when we have less than 20 bytes */ |
| 469 | if (scan_urlencoded_query(&read_ptr, (char *)(multiscrape_buf + numwant++), SCAN_SEARCHPATH_VALUE) != (ssize_t)sizeof(ot_hash)) | 432 | if (urlencoded_parse(&read_ptr, (char *)(multiscrape_buf + numwant++), SCAN_SEARCHPATH_VALUE) != (ssize_t)sizeof(ot_hash)) |
| 470 | HTTPERROR_400_PARAM; | 433 | HTTPERROR_400_PARAM; |
| 471 | break; | 434 | break; |
| 472 | } | 435 | } |
| @@ -491,7 +454,7 @@ unsigned long long numwants[201]; | |||
| 491 | #endif | 454 | #endif |
| 492 | 455 | ||
| 493 | #if defined(WANT_KEEPALIVE) || defined(WANT_IP_FROM_PROXY) | 456 | #if defined(WANT_KEEPALIVE) || defined(WANT_IP_FROM_PROXY) |
| 494 | static char *http_header(char *data, size_t byte_count, char *header) { | 457 | static char *http_header(char *data, size_t byte_count, const char *header) { |
| 495 | size_t i; | 458 | size_t i; |
| 496 | long sl = strlen(header); | 459 | long sl = strlen(header); |
| 497 | for (i = 0; i + sl + 2 < byte_count; ++i) { | 460 | for (i = 0; i + sl + 2 < byte_count; ++i) { |
| @@ -519,11 +482,12 @@ static ot_keywords keywords_announce[] = {{"port", 1}, {"left", 2}, {"event", | |||
| 519 | static ot_keywords keywords_announce_event[] = {{"completed", 1}, {"stopped", 2}, {NULL, -3}}; | 482 | static ot_keywords keywords_announce_event[] = {{"completed", 1}, {"stopped", 2}, {NULL, -3}}; |
| 520 | static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct *ws, char *read_ptr) { | 483 | static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct *ws, char *read_ptr) { |
| 521 | size_t numwant; | 484 | size_t numwant; |
| 522 | int tmp, scanon; | 485 | int64_t tmp; |
| 486 | int scanon; | ||
| 523 | unsigned short port = 0; | 487 | unsigned short port = 0; |
| 524 | char *write_ptr; | 488 | char *write_ptr; |
| 525 | ssize_t len; | 489 | ssize_t len; |
| 526 | struct http_data *cookie = io_getcookie(sock); | 490 | struct http_data *cookie = fdstate_cookie(sock); |
| 527 | 491 | ||
| 528 | /* This is to hack around stupid clients that send "announce ?info_hash" */ | 492 | /* This is to hack around stupid clients that send "announce ?info_hash" */ |
| 529 | if (read_ptr[-1] != '?') { | 493 | if (read_ptr[-1] != '?') { |
| @@ -538,7 +502,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 538 | if (accesslist_is_blessed(cookie->ip, OT_PERMISSION_MAY_PROXY)) { | 502 | if (accesslist_is_blessed(cookie->ip, OT_PERMISSION_MAY_PROXY)) { |
| 539 | ot_ip6 proxied_ip; | 503 | ot_ip6 proxied_ip; |
| 540 | char *fwd = http_header(ws->request, ws->header_size, "x-forwarded-for"); | 504 | char *fwd = http_header(ws->request, ws->header_size, "x-forwarded-for"); |
| 541 | if (fwd && ip_scan(fwd, proxied_ip)) { | 505 | if (fwd && ip_parse(fwd, proxied_ip)) { |
| 542 | OT_SETIP(ws->peer, proxied_ip); | 506 | OT_SETIP(ws->peer, proxied_ip); |
| 543 | } else | 507 | } else |
| 544 | OT_SETIP(ws->peer, cookie->ip); | 508 | OT_SETIP(ws->peer, cookie->ip); |
| @@ -555,32 +519,32 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 555 | scanon = 1; | 519 | scanon = 1; |
| 556 | 520 | ||
| 557 | while (scanon) { | 521 | while (scanon) { |
| 558 | switch (scan_find_keywords(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM)) { | 522 | switch (urlencoded_match_keyword(keywords_announce, &read_ptr, SCAN_SEARCHPATH_PARAM)) { |
| 559 | case -2: | 523 | case -2: |
| 560 | scanon = 0; | 524 | scanon = 0; |
| 561 | break; /* TERMINATOR */ | 525 | break; /* TERMINATOR */ |
| 562 | case -1: | 526 | case -1: |
| 563 | HTTPERROR_400_PARAM; /* PARSE ERROR */ | 527 | HTTPERROR_400_PARAM; /* PARSE ERROR */ |
| 564 | case -3: | 528 | case -3: |
| 565 | scan_urlencoded_skipvalue(&read_ptr); | 529 | urlencoded_skip_value(&read_ptr); |
| 566 | break; | 530 | break; |
| 567 | case 1: /* matched "port" */ | 531 | case 1: /* matched "port" */ |
| 568 | len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); | 532 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); |
| 569 | if ((len <= 0) || scan_fixed_int(write_ptr, len, &tmp) || (tmp > 0xffff)) | 533 | if ((len <= 0) || scan_int(write_ptr, len, &tmp) || (tmp > 0xffff)) |
| 570 | HTTPERROR_400_PARAM; | 534 | HTTPERROR_400_PARAM; |
| 571 | port = htons(tmp); | 535 | port = htons((uint16_t)tmp); |
| 572 | OT_SETPORT(&ws->peer, &port); | 536 | OT_SETPORT(&ws->peer, &port); |
| 573 | break; | 537 | break; |
| 574 | case 2: /* matched "left" */ | 538 | case 2: /* matched "left" */ |
| 575 | if ((len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) | 539 | if ((len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE)) <= 0) |
| 576 | HTTPERROR_400_PARAM; | 540 | HTTPERROR_400_PARAM; |
| 577 | if (scan_fixed_int(write_ptr, len, &tmp)) | 541 | if (scan_int(write_ptr, len, &tmp)) |
| 578 | tmp = 0; | 542 | tmp = 0; |
| 579 | if (!tmp) | 543 | if (!tmp) |
| 580 | OT_PEERFLAG(&ws->peer) |= PEER_FLAG_SEEDING; | 544 | OT_PEERFLAG(&ws->peer) |= PEER_FLAG_SEEDING; |
| 581 | break; | 545 | break; |
| 582 | case 3: /* matched "event" */ | 546 | case 3: /* matched "event" */ |
| 583 | switch (scan_find_keywords(keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE)) { | 547 | switch (urlencoded_match_keyword(keywords_announce_event, &read_ptr, SCAN_SEARCHPATH_VALUE)) { |
| 584 | case -1: | 548 | case -1: |
| 585 | HTTPERROR_400_PARAM; | 549 | HTTPERROR_400_PARAM; |
| 586 | case 1: /* matched "completed" */ | 550 | case 1: /* matched "completed" */ |
| @@ -594,18 +558,18 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 594 | } | 558 | } |
| 595 | break; | 559 | break; |
| 596 | case 4: /* matched "numwant" */ | 560 | case 4: /* matched "numwant" */ |
| 597 | len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); | 561 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); |
| 598 | if ((len <= 0) || scan_fixed_int(write_ptr, len, &tmp)) | 562 | if ((len <= 0) || scan_int(write_ptr, len, &tmp)) |
| 599 | HTTPERROR_400_PARAM; | 563 | HTTPERROR_400_PARAM; |
| 600 | if (tmp < 0) | 564 | if (tmp < 0) |
| 601 | tmp = 50; | 565 | tmp = 50; |
| 602 | if (tmp > 200) | 566 | if (tmp > 200) |
| 603 | tmp = 200; | 567 | tmp = 200; |
| 604 | numwant = tmp; | 568 | numwant = (size_t)tmp; |
| 605 | break; | 569 | break; |
| 606 | case 5: /* matched "compact" */ | 570 | case 5: /* matched "compact" */ |
| 607 | len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); | 571 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE); |
| 608 | if ((len <= 0) || scan_fixed_int(write_ptr, len, &tmp)) | 572 | if ((len <= 0) || scan_int(write_ptr, len, &tmp)) |
| 609 | HTTPERROR_400_PARAM; | 573 | HTTPERROR_400_PARAM; |
| 610 | if (!tmp) | 574 | if (!tmp) |
| 611 | HTTPERROR_400_COMPACT; | 575 | HTTPERROR_400_COMPACT; |
| @@ -614,7 +578,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 614 | if (ws->hash) | 578 | if (ws->hash) |
| 615 | HTTPERROR_400_DOUBLEHASH; | 579 | HTTPERROR_400_DOUBLEHASH; |
| 616 | /* ignore this, when we have less than 20 bytes */ | 580 | /* ignore this, when we have less than 20 bytes */ |
| 617 | if (scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) | 581 | if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) |
| 618 | HTTPERROR_400_PARAM; | 582 | HTTPERROR_400_PARAM; |
| 619 | ws->hash = (ot_hash *)write_ptr; | 583 | ws->hash = (ot_hash *)write_ptr; |
| 620 | break; | 584 | break; |
| @@ -622,9 +586,9 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 622 | case 7: /* matched "ip" */ | 586 | case 7: /* matched "ip" */ |
| 623 | { | 587 | { |
| 624 | char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; | 588 | char *tmp_buf1 = ws->reply, *tmp_buf2 = ws->reply + 16; |
| 625 | len = scan_urlencoded_query(&read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE); | 589 | len = urlencoded_parse(&read_ptr, tmp_buf2, SCAN_SEARCHPATH_VALUE); |
| 626 | tmp_buf2[len] = 0; | 590 | tmp_buf2[len] = 0; |
| 627 | if ((len <= 0) || !ip_scan(tmp_buf2, tmp_buf1)) | 591 | if ((len <= 0) || !ip_parse(tmp_buf2, tmp_buf1)) |
| 628 | HTTPERROR_400_PARAM; | 592 | HTTPERROR_400_PARAM; |
| 629 | OT_SETIP(&ws->peer, tmp_buf1); | 593 | OT_SETIP(&ws->peer, tmp_buf1); |
| 630 | } break; | 594 | } break; |
| @@ -638,7 +602,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 638 | uint64_t value; | 602 | uint64_t value; |
| 639 | signed short parsed, bits; | 603 | signed short parsed, bits; |
| 640 | 604 | ||
| 641 | len = scan_urlencoded_query(&read_ptr, tmp_buf, SCAN_SEARCHPATH_VALUE); | 605 | len = urlencoded_parse(&read_ptr, tmp_buf, SCAN_SEARCHPATH_VALUE); |
| 642 | tmp_buf[len] = 0; | 606 | tmp_buf[len] = 0; |
| 643 | if (len <= 0) | 607 | if (len <= 0) |
| 644 | HTTPERROR_400_PARAM; | 608 | HTTPERROR_400_PARAM; |
| @@ -646,13 +610,13 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 646 | loglist_reset(); | 610 | loglist_reset(); |
| 647 | return ws->reply_size = sprintf(ws->reply, "Successfully removed.\n"); | 611 | return ws->reply_size = sprintf(ws->reply, "Successfully removed.\n"); |
| 648 | } | 612 | } |
| 649 | parsed = ip_scan(tmp_buf, net.address); | 613 | parsed = ip_parse(tmp_buf, net.address); |
| 650 | if (!parsed) | 614 | if (!parsed) |
| 651 | HTTPERROR_400_PARAM; | 615 | HTTPERROR_400_PARAM; |
| 652 | if (tmp_buf[parsed++] != '/') | 616 | if (tmp_buf[parsed++] != '/') |
| 653 | bits = 128; | 617 | bits = 128; |
| 654 | else { | 618 | else { |
| 655 | parsed = scan_udec(tmp_buf + parsed, &value, 128); | 619 | parsed = scan_uint(tmp_buf + parsed, &value, 128); |
| 656 | if (!parsed) | 620 | if (!parsed) |
| 657 | HTTPERROR_400_PARAM; | 621 | HTTPERROR_400_PARAM; |
| 658 | bits = (signed short)value; | 622 | bits = (signed short)value; |
| @@ -667,7 +631,7 @@ static ssize_t http_handle_announce(const int64_t sock, struct ot_workstruct | |||
| 667 | #endif | 631 | #endif |
| 668 | case 9: /* matched "peer_id" */ | 632 | case 9: /* matched "peer_id" */ |
| 669 | /* ignore this, when we have less than 20 bytes */ | 633 | /* ignore this, when we have less than 20 bytes */ |
| 670 | if (scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) | 634 | if (urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_SEARCHPATH_VALUE) != 20) |
| 671 | HTTPERROR_400_PARAM; | 635 | HTTPERROR_400_PARAM; |
| 672 | ws->peer_id = write_ptr; | 636 | ws->peer_id = write_ptr; |
| 673 | break; | 637 | break; |
| @@ -700,7 +664,7 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) { | |||
| 700 | char *read_ptr = ws->request, *write_ptr; | 664 | char *read_ptr = ws->request, *write_ptr; |
| 701 | 665 | ||
| 702 | #ifdef WANT_FULLLOG_NETWORKS | 666 | #ifdef WANT_FULLLOG_NETWORKS |
| 703 | struct http_data *cookie = io_getcookie(sock); | 667 | struct http_data *cookie = fdstate_cookie(sock); |
| 704 | if (loglist_check_address(cookie->ip)) { | 668 | if (loglist_check_address(cookie->ip)) { |
| 705 | ot_log *log = malloc(sizeof(ot_log)); | 669 | ot_log *log = malloc(sizeof(ot_log)); |
| 706 | if (log) { | 670 | if (log) { |
| @@ -746,7 +710,7 @@ ssize_t http_handle_request(const int64_t sock, struct ot_workstruct *ws) { | |||
| 746 | In reality we abandoned requiring the url to be correct. This now | 710 | In reality we abandoned requiring the url to be correct. This now |
| 747 | only decodes url encoded characters, we check for announces and | 711 | only decodes url encoded characters, we check for announces and |
| 748 | scrapes by looking for "a*" or "sc" */ | 712 | scrapes by looking for "a*" or "sc" */ |
| 749 | len = scan_urlencoded_query(&read_ptr, write_ptr = read_ptr, SCAN_PATH); | 713 | len = urlencoded_parse(&read_ptr, write_ptr = read_ptr, SCAN_PATH); |
| 750 | 714 | ||
| 751 | /* If parsing returned an error, leave with not found */ | 715 | /* If parsing returned an error, leave with not found */ |
| 752 | if (g_redirecturl && (len == -2)) | 716 | if (g_redirecturl && (len == -2)) |
