diff options
| -rw-r--r-- | opentracker.c | 235 | ||||
| -rw-r--r-- | ot_accesslist.c | 1 | ||||
| -rw-r--r-- | ot_fullscrape.c | 19 | ||||
| -rw-r--r-- | ot_http.c | 1 | ||||
| -rw-r--r-- | ot_iovec.c | 2 | ||||
| -rw-r--r-- | ot_livesync.c | 21 | ||||
| -rw-r--r-- | ot_livesync.h | 6 | ||||
| -rw-r--r-- | ot_mutex.c | 21 | ||||
| -rw-r--r-- | ot_stats.c | 9 | ||||
| -rw-r--r-- | ot_sync.c | 10 | ||||
| -rw-r--r-- | ot_vector.c | 16 | ||||
| -rw-r--r-- | proxy.c | 20 | ||||
| -rw-r--r-- | scan_urlencoded_query.c | 3 | ||||
| -rw-r--r-- | trackerlogic.c | 33 |
14 files changed, 243 insertions, 154 deletions
diff --git a/opentracker.c b/opentracker.c index f63db72..89bfd3e 100644 --- a/opentracker.c +++ b/opentracker.c | |||
| @@ -5,7 +5,6 @@ | |||
| 5 | $Id$ */ | 5 | $Id$ */ |
| 6 | 6 | ||
| 7 | /* System */ | 7 | /* System */ |
| 8 | #include <arpa/inet.h> | ||
| 9 | #include <ctype.h> | 8 | #include <ctype.h> |
| 10 | #include <errno.h> | 9 | #include <errno.h> |
| 11 | #include <pthread.h> | 10 | #include <pthread.h> |
| @@ -21,7 +20,6 @@ | |||
| 21 | #endif | 20 | #endif |
| 22 | 21 | ||
| 23 | /* Libowfat */ | 22 | /* Libowfat */ |
| 24 | #include "byte.h" | ||
| 25 | #include "io.h" | 23 | #include "io.h" |
| 26 | #include "iob.h" | 24 | #include "iob.h" |
| 27 | #include "ip6.h" | 25 | #include "ip6.h" |
| @@ -438,8 +436,82 @@ static int scan_ip6_net(const char *src, ot_net *net) { | |||
| 438 | return off + s - src; | 436 | return off + s - src; |
| 439 | } | 437 | } |
| 440 | 438 | ||
| 439 | typedef enum { | ||
| 440 | CONFIG_UNKNOWN, | ||
| 441 | CONFIG_TRACKER_ROOTDIR, | ||
| 442 | CONFIG_TRACKER_USER, | ||
| 443 | CONFIG_LISTEN_TCP_UDP, | ||
| 444 | CONFIG_LISTEN_TCP, | ||
| 445 | CONFIG_LISTEN_UDP, | ||
| 446 | CONFIG_LISTEN_UDP_WORKERS, | ||
| 447 | CONFIG_ACCESS_WHITELIST, | ||
| 448 | CONFIG_ACCESS_BLACKLIST, | ||
| 449 | CONFIG_ACCESS_FIFO_ADD, | ||
| 450 | CONFIG_ACCESS_FIFO_DELETE, | ||
| 451 | CONFIG_ACCESS_STATS, | ||
| 452 | CONFIG_ACCESS_STATS_PATH, | ||
| 453 | CONFIG_ACCESS_PROXY, | ||
| 454 | CONFIG_TRACKER_REDIRECT_URL, | ||
| 455 | CONFIG_LIVESYNC_CLUSTER_NODE_IP, | ||
| 456 | CONFIG_LIVESYNC_CLUSTER_LISTEN, | ||
| 457 | } config_kind; | ||
| 458 | |||
| 459 | typedef struct { | ||
| 460 | const char *name; | ||
| 461 | size_t len; | ||
| 462 | config_kind kind; | ||
| 463 | } config_key; | ||
| 464 | |||
| 465 | #define CONFIG_KEY(s, k) { s, sizeof(s) - 1, k } | ||
| 466 | |||
| 467 | static const config_key config_keys[] = { | ||
| 468 | CONFIG_KEY("tracker.rootdir", CONFIG_TRACKER_ROOTDIR), | ||
| 469 | CONFIG_KEY("tracker.user", CONFIG_TRACKER_USER), | ||
| 470 | CONFIG_KEY("listen.tcp_udp", CONFIG_LISTEN_TCP_UDP), | ||
| 471 | CONFIG_KEY("listen.tcp", CONFIG_LISTEN_TCP), | ||
| 472 | CONFIG_KEY("listen.udp", CONFIG_LISTEN_UDP), | ||
| 473 | CONFIG_KEY("listen.udp.workers", CONFIG_LISTEN_UDP_WORKERS), | ||
| 474 | #ifdef WANT_ACCESSLIST_WHITE | ||
| 475 | CONFIG_KEY("access.whitelist", CONFIG_ACCESS_WHITELIST), | ||
| 476 | #elif defined(WANT_ACCESSLIST_BLACK) | ||
| 477 | CONFIG_KEY("access.blacklist", CONFIG_ACCESS_BLACKLIST), | ||
| 478 | #endif | ||
| 479 | #ifdef WANT_DYNAMIC_ACCESSLIST | ||
| 480 | CONFIG_KEY("access.fifo_add", CONFIG_ACCESS_FIFO_ADD), | ||
| 481 | CONFIG_KEY("access.fifo_delete", CONFIG_ACCESS_FIFO_DELETE), | ||
| 482 | #endif | ||
| 483 | #ifdef WANT_RESTRICT_STATS | ||
| 484 | CONFIG_KEY("access.stats", CONFIG_ACCESS_STATS), | ||
| 485 | #endif | ||
| 486 | CONFIG_KEY("access.stats_path", CONFIG_ACCESS_STATS_PATH), | ||
| 487 | #ifdef WANT_IP_FROM_PROXY | ||
| 488 | CONFIG_KEY("access.proxy", CONFIG_ACCESS_PROXY), | ||
| 489 | #endif | ||
| 490 | CONFIG_KEY("tracker.redirect_url", CONFIG_TRACKER_REDIRECT_URL), | ||
| 491 | #ifdef WANT_SYNC_LIVE | ||
| 492 | CONFIG_KEY("livesync.cluster.node_ip", CONFIG_LIVESYNC_CLUSTER_NODE_IP), | ||
| 493 | CONFIG_KEY("livesync.cluster.listen", CONFIG_LIVESYNC_CLUSTER_LISTEN), | ||
| 494 | #endif | ||
| 495 | }; | ||
| 496 | static const size_t config_entry_count = sizeof(config_keys) / sizeof(config_keys[0]); | ||
| 497 | |||
| 498 | static config_kind parse_config_key(char **p) { | ||
| 499 | size_t i; | ||
| 500 | for (i = 0; i < config_entry_count; ++i) { | ||
| 501 | const config_key *key = &config_keys[i]; | ||
| 502 | if (strncmp(*p, key->name, key->len) == 0 && isspace((unsigned char)(*p)[key->len])) { | ||
| 503 | *p += key->len + 1; | ||
| 504 | /* Skip white spaces */ | ||
| 505 | while (isspace((unsigned char)**p)) | ||
| 506 | ++(*p); | ||
| 507 | return key->kind; | ||
| 508 | } | ||
| 509 | } | ||
| 510 | return CONFIG_UNKNOWN; | ||
| 511 | } | ||
| 512 | |||
| 441 | int parse_configfile(char *config_filename) { | 513 | int parse_configfile(char *config_filename) { |
| 442 | FILE *accesslist_filehandle; | 514 | FILE *config_file_handle; |
| 443 | char inbuf[512]; | 515 | char inbuf[512]; |
| 444 | ot_ip6 tmpip; | 516 | ot_ip6 tmpip; |
| 445 | #if defined(WANT_RESTRICT_STATS) || defined(WANT_IP_FROM_PROXY) || defined(WANT_SYNC_LIVE) | 517 | #if defined(WANT_RESTRICT_STATS) || defined(WANT_IP_FROM_PROXY) || defined(WANT_SYNC_LIVE) |
| @@ -447,19 +519,19 @@ int parse_configfile(char *config_filename) { | |||
| 447 | #endif | 519 | #endif |
| 448 | int bound = 0; | 520 | int bound = 0; |
| 449 | 521 | ||
| 450 | accesslist_filehandle = fopen(config_filename, "r"); | 522 | config_file_handle = fopen(config_filename, "r"); |
| 451 | 523 | ||
| 452 | if (accesslist_filehandle == NULL) { | 524 | if (config_file_handle == NULL) { |
| 453 | fprintf(stderr, "Warning: Can't open config file: %s.", config_filename); | 525 | fprintf(stderr, "Warning: Can't open config file: %s.", config_filename); |
| 454 | return 0; | 526 | return 0; |
| 455 | } | 527 | } |
| 456 | 528 | ||
| 457 | while (fgets(inbuf, sizeof(inbuf), accesslist_filehandle)) { | 529 | while (fgets(inbuf, sizeof(inbuf), config_file_handle)) { |
| 458 | char *p = inbuf; | 530 | char *p = inbuf, *end; |
| 459 | size_t strl; | 531 | uint16_t tmpport = 6969; |
| 460 | 532 | ||
| 461 | /* Skip white spaces */ | 533 | /* Skip white spaces */ |
| 462 | while (isspace(*p)) | 534 | while (isspace((unsigned char)*p)) |
| 463 | ++p; | 535 | ++p; |
| 464 | 536 | ||
| 465 | /* Ignore comments and empty lines */ | 537 | /* Ignore comments and empty lines */ |
| @@ -467,87 +539,98 @@ int parse_configfile(char *config_filename) { | |||
| 467 | continue; | 539 | continue; |
| 468 | 540 | ||
| 469 | /* consume trailing new lines and spaces */ | 541 | /* consume trailing new lines and spaces */ |
| 470 | strl = strlen(p); | 542 | end = p + strlen(p); |
| 471 | while (strl && isspace(p[strl - 1])) | 543 | while (end > p && isspace((unsigned char)end[-1])) |
| 472 | p[--strl] = 0; | 544 | *--end = 0; |
| 473 | 545 | ||
| 474 | /* Scan for commands */ | 546 | /* Scan for commands */ |
| 475 | if (!byte_diff(p, 15, "tracker.rootdir") && isspace(p[15])) { | 547 | switch (parse_config_key(&p)) { |
| 476 | set_config_option(&g_serverdir, p + 16); | 548 | case CONFIG_TRACKER_ROOTDIR: |
| 477 | } else if (!byte_diff(p, 12, "tracker.user") && isspace(p[12])) { | 549 | set_config_option(&g_serverdir, p); |
| 478 | set_config_option(&g_serveruser, p + 13); | 550 | break; |
| 479 | } else if (!byte_diff(p, 14, "listen.tcp_udp") && isspace(p[14])) { | 551 | case CONFIG_TRACKER_USER: |
| 480 | uint16_t tmpport = 6969; | 552 | set_config_option(&g_serveruser, p); |
| 481 | if (!scan_ip6_port(p + 15, tmpip, &tmpport)) | 553 | break; |
| 482 | goto parse_error; | 554 | case CONFIG_LISTEN_TCP_UDP: |
| 483 | ot_try_bind(tmpip, tmpport, FLAG_TCP); | 555 | if (!scan_ip6_port(p, tmpip, &tmpport)) |
| 484 | ++bound; | 556 | goto parse_error; |
| 485 | ot_try_bind(tmpip, tmpport, FLAG_UDP); | 557 | ot_try_bind(tmpip, tmpport, FLAG_TCP); |
| 486 | ++bound; | 558 | ++bound; |
| 487 | } else if (!byte_diff(p, 10, "listen.tcp") && isspace(p[10])) { | 559 | ot_try_bind(tmpip, tmpport, FLAG_UDP); |
| 488 | uint16_t tmpport = 6969; | 560 | ++bound; |
| 489 | if (!scan_ip6_port(p + 11, tmpip, &tmpport)) | 561 | break; |
| 490 | goto parse_error; | 562 | case CONFIG_LISTEN_TCP: |
| 491 | ot_try_bind(tmpip, tmpport, FLAG_TCP); | 563 | if (!scan_ip6_port(p, tmpip, &tmpport)) |
| 492 | ++bound; | 564 | goto parse_error; |
| 493 | } else if (!byte_diff(p, 10, "listen.udp") && isspace(p[10])) { | 565 | ot_try_bind(tmpip, tmpport, FLAG_TCP); |
| 494 | uint16_t tmpport = 6969; | 566 | ++bound; |
| 495 | if (!scan_ip6_port(p + 11, tmpip, &tmpport)) | 567 | break; |
| 496 | goto parse_error; | 568 | case CONFIG_LISTEN_UDP: |
| 497 | ot_try_bind(tmpip, tmpport, FLAG_UDP); | 569 | if (!scan_ip6_port(p, tmpip, &tmpport)) |
| 498 | ++bound; | 570 | goto parse_error; |
| 499 | } else if (!byte_diff(p, 18, "listen.udp.workers") && isspace(p[18])) { | 571 | ot_try_bind(tmpip, tmpport, FLAG_UDP); |
| 500 | char *value = p + 18; | 572 | ++bound; |
| 501 | while (isspace(*value)) | 573 | case CONFIG_LISTEN_UDP_WORKERS: |
| 502 | ++value; | 574 | scan_uint(p, &g_udp_workers); |
| 503 | scan_uint(value, &g_udp_workers); | 575 | break; |
| 504 | #ifdef WANT_ACCESSLIST_WHITE | 576 | #ifdef WANT_ACCESSLIST_WHITE |
| 505 | } else if (!byte_diff(p, 16, "access.whitelist") && isspace(p[16])) { | 577 | case CONFIG_ACCESS_WHITELIST: |
| 506 | set_config_option(&g_accesslist_filename, p + 17); | 578 | set_config_option(&g_accesslist_filename, p); |
| 579 | break; | ||
| 507 | #elif defined(WANT_ACCESSLIST_BLACK) | 580 | #elif defined(WANT_ACCESSLIST_BLACK) |
| 508 | } else if (!byte_diff(p, 16, "access.blacklist") && isspace(p[16])) { | 581 | case CONFIG_ACCESS_BLACKLIST: |
| 509 | set_config_option(&g_accesslist_filename, p + 17); | 582 | set_config_option(&g_accesslist_filename, p); |
| 583 | break; | ||
| 510 | #endif | 584 | #endif |
| 511 | #ifdef WANT_DYNAMIC_ACCESSLIST | 585 | #ifdef WANT_DYNAMIC_ACCESSLIST |
| 512 | } else if (!byte_diff(p, 15, "access.fifo_add") && isspace(p[15])) { | 586 | case CONFIG_ACCESS_FIFO_ADD: |
| 513 | set_config_option(&g_accesslist_pipe_add, p + 16); | 587 | set_config_option(&g_accesslist_pipe_add, p); |
| 514 | } else if (!byte_diff(p, 18, "access.fifo_delete") && isspace(p[18])) { | 588 | break; |
| 515 | set_config_option(&g_accesslist_pipe_delete, p + 19); | 589 | case CONFIG_ACCESS_FIFO_DELETE: |
| 590 | set_config_option(&g_accesslist_pipe_delete, p); | ||
| 591 | break; | ||
| 516 | #endif | 592 | #endif |
| 517 | #ifdef WANT_RESTRICT_STATS | 593 | #ifdef WANT_RESTRICT_STATS |
| 518 | } else if (!byte_diff(p, 12, "access.stats") && isspace(p[12])) { | 594 | case CONFIG_ACCESS_STATS: |
| 519 | if (!scan_ip6_net(p + 13, &tmpnet)) | 595 | if (!scan_ip6_net(p, &tmpnet)) |
| 520 | goto parse_error; | 596 | goto parse_error; |
| 521 | accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_STAT); | 597 | accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_STAT); |
| 598 | break; | ||
| 522 | #endif | 599 | #endif |
| 523 | } else if (!byte_diff(p, 17, "access.stats_path") && isspace(p[17])) { | 600 | case CONFIG_ACCESS_STATS_PATH: |
| 524 | set_config_option(&g_stats_path, p + 18); | 601 | set_config_option(&g_stats_path, p); |
| 602 | break; | ||
| 525 | #ifdef WANT_IP_FROM_PROXY | 603 | #ifdef WANT_IP_FROM_PROXY |
| 526 | } else if (!byte_diff(p, 12, "access.proxy") && isspace(p[12])) { | 604 | case CONFIG_ACCESS_PROXY: |
| 527 | if (!scan_ip6_net(p + 13, &tmpnet)) | 605 | if (!scan_ip6_net(p, &tmpnet)) |
| 528 | goto parse_error; | 606 | goto parse_error; |
| 529 | accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_PROXY); | 607 | accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_PROXY); |
| 608 | break; | ||
| 530 | #endif | 609 | #endif |
| 531 | } else if (!byte_diff(p, 20, "tracker.redirect_url") && isspace(p[20])) { | 610 | case CONFIG_TRACKER_REDIRECT_URL: |
| 532 | set_config_option(&g_redirecturl, p + 21); | 611 | set_config_option(&g_redirecturl, p); |
| 612 | break; | ||
| 533 | #ifdef WANT_SYNC_LIVE | 613 | #ifdef WANT_SYNC_LIVE |
| 534 | } else if (!byte_diff(p, 24, "livesync.cluster.node_ip") && isspace(p[24])) { | 614 | case CONFIG_LIVESYNC_CLUSTER_NODE_IP: |
| 535 | if (!scan_ip6_net(p + 25, &tmpnet)) | 615 | if (!scan_ip6_net(p, &tmpnet)) |
| 536 | goto parse_error; | 616 | goto parse_error; |
| 537 | accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_LIVESYNC); | 617 | accesslist_bless_net(&tmpnet, OT_PERMISSION_MAY_LIVESYNC); |
| 538 | } else if (!byte_diff(p, 23, "livesync.cluster.listen") && isspace(p[23])) { | 618 | break; |
| 539 | uint16_t tmpport = LIVESYNC_PORT; | 619 | case CONFIG_LIVESYNC_CLUSTER_LISTEN: |
| 540 | if (!scan_ip6_port(p + 24, tmpip, &tmpport)) | 620 | tmpport = LIVESYNC_PORT; |
| 541 | goto parse_error; | 621 | if (!scan_ip6_port(p, tmpip, &tmpport)) |
| 542 | livesync_bind_mcast(tmpip, tmpport); | 622 | goto parse_error; |
| 623 | livesync_bind_mcast(tmpip, tmpport); | ||
| 624 | break; | ||
| 543 | #endif | 625 | #endif |
| 544 | } else | 626 | default: |
| 545 | fprintf(stderr, "Unhandled line in config file: %s\n", inbuf); | 627 | fprintf(stderr, "Unhandled line in config file: %s\n", inbuf); |
| 546 | continue; | 628 | break; |
| 547 | parse_error: | 629 | parse_error: |
| 548 | fprintf(stderr, "Parse error in config file: %s\n", inbuf); | 630 | fprintf(stderr, "Parse error in config file: %s\n", inbuf); |
| 631 | } | ||
| 549 | } | 632 | } |
| 550 | fclose(accesslist_filehandle); | 633 | fclose(config_file_handle); |
| 551 | return bound; | 634 | return bound; |
| 552 | } | 635 | } |
| 553 | 636 | ||
diff --git a/ot_accesslist.c b/ot_accesslist.c index 8e9afdc..2827494 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c | |||
| @@ -17,7 +17,6 @@ | |||
| 17 | #endif | 17 | #endif |
| 18 | 18 | ||
| 19 | /* Libowfat */ | 19 | /* Libowfat */ |
| 20 | #include "byte.h" | ||
| 21 | #include "fmt.h" | 20 | #include "fmt.h" |
| 22 | #include "ip6.h" | 21 | #include "ip6.h" |
| 23 | #include "mmap.h" | 22 | #include "mmap.h" |
diff --git a/ot_fullscrape.c b/ot_fullscrape.c index b800431..3e36e8e 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c | |||
| @@ -10,7 +10,6 @@ | |||
| 10 | #include <pthread.h> | 10 | #include <pthread.h> |
| 11 | #include <stdio.h> | 11 | #include <stdio.h> |
| 12 | #include <string.h> | 12 | #include <string.h> |
| 13 | #include <sys/param.h> | ||
| 14 | #ifdef WANT_COMPRESSION_GZIP | 13 | #ifdef WANT_COMPRESSION_GZIP |
| 15 | #include <zlib.h> | 14 | #include <zlib.h> |
| 16 | #endif | 15 | #endif |
| @@ -20,7 +19,6 @@ | |||
| 20 | 19 | ||
| 21 | 20 | ||
| 22 | /* Libowfat */ | 21 | /* Libowfat */ |
| 23 | #include "byte.h" | ||
| 24 | #include "io.h" | 22 | #include "io.h" |
| 25 | #include "textcode.h" | 23 | #include "textcode.h" |
| 26 | 24 | ||
| @@ -180,12 +178,15 @@ static void fullscrape_make(int taskid, ot_tasktype mode) { | |||
| 180 | 178 | ||
| 181 | if (mutex_workqueue_pushchunked(taskid, &iovector)) { | 179 | if (mutex_workqueue_pushchunked(taskid, &iovector)) { |
| 182 | free(iovector.iov_base); | 180 | free(iovector.iov_base); |
| 183 | return mutex_bucket_unlock(bucket, 0); | 181 | mutex_bucket_unlock(bucket, 0); |
| 182 | return; | ||
| 184 | } | 183 | } |
| 185 | /* Allocate a fresh output buffer */ | 184 | /* Allocate a fresh output buffer */ |
| 186 | r = iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE); | 185 | r = iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE); |
| 187 | if (!r) | 186 | if (!r) { |
| 188 | return mutex_bucket_unlock(bucket, 0); | 187 | mutex_bucket_unlock(bucket, 0); |
| 188 | return; | ||
| 189 | } | ||
| 189 | 190 | ||
| 190 | /* re points to low watermark */ | 191 | /* re points to low watermark */ |
| 191 | re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN; | 192 | re = r + OT_SCRAPE_CHUNK_SIZE - OT_SCRAPE_MAXENTRYLEN; |
| @@ -222,7 +223,7 @@ static void fullscrape_make_gzip(int taskid, ot_tasktype mode) { | |||
| 222 | if (!iovector.iov_base) | 223 | if (!iovector.iov_base) |
| 223 | return; | 224 | return; |
| 224 | 225 | ||
| 225 | byte_zero(&strm, sizeof(strm)); | 226 | memset(&strm, 0, sizeof(strm)); |
| 226 | strm.next_out = (uint8_t *)iovector.iov_base; | 227 | strm.next_out = (uint8_t *)iovector.iov_base; |
| 227 | strm.avail_out = OT_SCRAPE_CHUNK_SIZE; | 228 | strm.avail_out = OT_SCRAPE_CHUNK_SIZE; |
| 228 | if (deflateInit2(&strm, 7, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY) != Z_OK) | 229 | if (deflateInit2(&strm, 7, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY) != Z_OK) |
| @@ -257,14 +258,16 @@ static void fullscrape_make_gzip(int taskid, ot_tasktype mode) { | |||
| 257 | 258 | ||
| 258 | if (mutex_workqueue_pushchunked(taskid, &iovector)) { | 259 | if (mutex_workqueue_pushchunked(taskid, &iovector)) { |
| 259 | free(iovector.iov_base); | 260 | free(iovector.iov_base); |
| 260 | return mutex_bucket_unlock(bucket, 0); | 261 | mutex_bucket_unlock(bucket, 0); |
| 262 | return; | ||
| 261 | } | 263 | } |
| 262 | /* Allocate a fresh output buffer */ | 264 | /* Allocate a fresh output buffer */ |
| 263 | iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE); | 265 | iovector.iov_base = malloc(OT_SCRAPE_CHUNK_SIZE); |
| 264 | if (!iovector.iov_base) { | 266 | if (!iovector.iov_base) { |
| 265 | fprintf(stderr, "Out of memory trying to claim ouput buffer\n"); | 267 | fprintf(stderr, "Out of memory trying to claim ouput buffer\n"); |
| 266 | deflateEnd(&strm); | 268 | deflateEnd(&strm); |
| 267 | return mutex_bucket_unlock(bucket, 0); | 269 | mutex_bucket_unlock(bucket, 0); |
| 270 | return; | ||
| 268 | } | 271 | } |
| 269 | strm.next_out = (uint8_t *)iovector.iov_base; | 272 | strm.next_out = (uint8_t *)iovector.iov_base; |
| 270 | strm.avail_out = OT_SCRAPE_CHUNK_SIZE; | 273 | strm.avail_out = OT_SCRAPE_CHUNK_SIZE; |
| @@ -15,7 +15,6 @@ | |||
| 15 | 15 | ||
| 16 | /* Libowfat */ | 16 | /* Libowfat */ |
| 17 | #include "array.h" | 17 | #include "array.h" |
| 18 | #include "byte.h" | ||
| 19 | #include "case.h" | 18 | #include "case.h" |
| 20 | #include "iob.h" | 19 | #include "iob.h" |
| 21 | #include "ip6.h" | 20 | #include "ip6.h" |
| @@ -5,9 +5,7 @@ | |||
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <stdlib.h> | 7 | #include <stdlib.h> |
| 8 | #include <sys/types.h> | ||
| 9 | #include <sys/uio.h> | 8 | #include <sys/uio.h> |
| 10 | #include <unistd.h> | ||
| 11 | 9 | ||
| 12 | /* Libowfat */ | 10 | /* Libowfat */ |
| 13 | 11 | ||
diff --git a/ot_livesync.c b/ot_livesync.c index f620592..b876ef8 100644 --- a/ot_livesync.c +++ b/ot_livesync.c | |||
| @@ -4,15 +4,14 @@ | |||
| 4 | $id$ */ | 4 | $id$ */ |
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <arpa/inet.h> | ||
| 7 | #include <pthread.h> | 8 | #include <pthread.h> |
| 8 | #include <stdlib.h> | 9 | #include <stdlib.h> |
| 9 | #include <string.h> | 10 | #include <string.h> |
| 10 | #include <sys/types.h> | 11 | #include <sys/types.h> |
| 11 | #include <sys/uio.h> | ||
| 12 | #include <unistd.h> | 12 | #include <unistd.h> |
| 13 | 13 | ||
| 14 | /* Libowfat */ | 14 | /* Libowfat */ |
| 15 | #include "byte.h" | ||
| 16 | #include "ip6.h" | 15 | #include "ip6.h" |
| 17 | #include "ndelay.h" | 16 | #include "ndelay.h" |
| 18 | #include "socket.h" | 17 | #include "socket.h" |
| @@ -57,7 +56,9 @@ static sync_buffer g_v6_buf; | |||
| 57 | static sync_buffer g_v4_buf; | 56 | static sync_buffer g_v4_buf; |
| 58 | 57 | ||
| 59 | static pthread_t thread_id; | 58 | static pthread_t thread_id; |
| 60 | void livesync_init() { | 59 | void livesync_init(void) { |
| 60 | uint32_t packet_type6 = htonl(OT_SYNC_PEER6); | ||
| 61 | uint32_t packet_type4 = htonl(OT_SYNC_PEER4); | ||
| 61 | 62 | ||
| 62 | if (g_socket_in == -1) | 63 | if (g_socket_in == -1) |
| 63 | exerr("No socket address for live sync specified."); | 64 | exerr("No socket address for live sync specified."); |
| @@ -66,8 +67,8 @@ void livesync_init() { | |||
| 66 | memcpy(g_v6_buf.data, &g_tracker_id, sizeof(g_tracker_id)); | 67 | memcpy(g_v6_buf.data, &g_tracker_id, sizeof(g_tracker_id)); |
| 67 | memcpy(g_v4_buf.data, &g_tracker_id, sizeof(g_tracker_id)); | 68 | memcpy(g_v4_buf.data, &g_tracker_id, sizeof(g_tracker_id)); |
| 68 | 69 | ||
| 69 | uint32_pack_big((char *)g_v6_buf.data + sizeof(g_tracker_id), OT_SYNC_PEER6); | 70 | memcpy(g_v6_buf.data + sizeof(g_tracker_id), &packet_type6, sizeof(packet_type6)); |
| 70 | uint32_pack_big((char *)g_v4_buf.data + sizeof(g_tracker_id), OT_SYNC_PEER4); | 71 | memcpy(g_v4_buf.data + sizeof(g_tracker_id), &packet_type4, sizeof(packet_type4)); |
| 71 | 72 | ||
| 72 | g_v6_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); | 73 | g_v6_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); |
| 73 | g_v4_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); | 74 | g_v4_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); |
| @@ -78,7 +79,7 @@ void livesync_init() { | |||
| 78 | pthread_create(&thread_id, NULL, livesync_worker, NULL); | 79 | pthread_create(&thread_id, NULL, livesync_worker, NULL); |
| 79 | } | 80 | } |
| 80 | 81 | ||
| 81 | void livesync_deinit() { | 82 | void livesync_deinit(void) { |
| 82 | if (g_socket_in != -1) | 83 | if (g_socket_in != -1) |
| 83 | close(g_socket_in); | 84 | close(g_socket_in); |
| 84 | if (g_socket_out != -1) | 85 | if (g_socket_out != -1) |
| @@ -167,7 +168,7 @@ static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) | |||
| 167 | /* Tickle the live sync module from time to time, so no events get | 168 | /* Tickle the live sync module from time to time, so no events get |
| 168 | stuck when there's not enough traffic to fill udp packets fast | 169 | stuck when there's not enough traffic to fill udp packets fast |
| 169 | enough */ | 170 | enough */ |
| 170 | void livesync_ticker() { | 171 | void livesync_ticker(void) { |
| 171 | /* livesync_issue_peersync sets g_next_packet_time */ | 172 | /* livesync_issue_peersync sets g_next_packet_time */ |
| 172 | pthread_mutex_lock(&g_outbuf_mutex); | 173 | pthread_mutex_lock(&g_outbuf_mutex); |
| 173 | if (g_now_seconds > g_v6_buf.next_packet_time && g_v6_buf.fill > sizeof(g_tracker_id) + sizeof(uint32_t)) | 174 | if (g_now_seconds > g_v6_buf.next_packet_time && g_v6_buf.fill > sizeof(g_tracker_id) + sizeof(uint32_t)) |
| @@ -216,6 +217,8 @@ static void *livesync_worker(void *args) { | |||
| 216 | memcpy(in_ip, V4mappedprefix, sizeof(V4mappedprefix)); | 217 | memcpy(in_ip, V4mappedprefix, sizeof(V4mappedprefix)); |
| 217 | 218 | ||
| 218 | while (1) { | 219 | while (1) { |
| 220 | uint32_t packet_type; | ||
| 221 | |||
| 219 | ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); | 222 | ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); |
| 220 | 223 | ||
| 221 | /* Expect at least tracker id and packet type */ | 224 | /* Expect at least tracker id and packet type */ |
| @@ -228,7 +231,9 @@ static void *livesync_worker(void *args) { | |||
| 228 | continue; | 231 | continue; |
| 229 | } | 232 | } |
| 230 | 233 | ||
| 231 | switch (uint32_read_big(sizeof(g_tracker_id) + (char *)ws.inbuf)) { | 234 | memcpy(&packet_type, ws.inbuf + sizeof(g_tracker_id), sizeof(packet_type)); |
| 235 | |||
| 236 | switch (ntohl(packet_type)) { | ||
| 232 | case OT_SYNC_PEER6: | 237 | case OT_SYNC_PEER6: |
| 233 | livesync_handle_peersync(&ws, OT_PEER_SIZE6); | 238 | livesync_handle_peersync(&ws, OT_PEER_SIZE6); |
| 234 | break; | 239 | break; |
diff --git a/ot_livesync.h b/ot_livesync.h index cb28774..ed710f0 100644 --- a/ot_livesync.h +++ b/ot_livesync.h | |||
| @@ -47,8 +47,8 @@ | |||
| 47 | 47 | ||
| 48 | #define LIVESYNC_PORT 9696 | 48 | #define LIVESYNC_PORT 9696 |
| 49 | 49 | ||
| 50 | void livesync_init(); | 50 | void livesync_init(void); |
| 51 | void livesync_deinit(); | 51 | void livesync_deinit(void); |
| 52 | 52 | ||
| 53 | /* Join multicast group for listening and create sending socket */ | 53 | /* Join multicast group for listening and create sending socket */ |
| 54 | void livesync_bind_mcast(char *ip, uint16_t port); | 54 | void livesync_bind_mcast(char *ip, uint16_t port); |
| @@ -59,7 +59,7 @@ void livesync_tell(struct ot_workstruct *ws); | |||
| 59 | /* Tickle the live sync module from time to time, so no events get | 59 | /* Tickle the live sync module from time to time, so no events get |
| 60 | stuck when there's not enough traffic to fill udp packets fast | 60 | stuck when there's not enough traffic to fill udp packets fast |
| 61 | enough */ | 61 | enough */ |
| 62 | void livesync_ticker(); | 62 | void livesync_ticker(void); |
| 63 | 63 | ||
| 64 | /* Handle an incoming live sync packet */ | 64 | /* Handle an incoming live sync packet */ |
| 65 | void handle_livesync(const int64 sock); | 65 | void handle_livesync(const int64 sock); |
| @@ -4,16 +4,15 @@ | |||
| 4 | $id$ */ | 4 | $id$ */ |
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <arpa/inet.h> | ||
| 7 | #include <pthread.h> | 8 | #include <pthread.h> |
| 8 | #include <stdio.h> | 9 | #include <stdio.h> |
| 9 | #include <stdlib.h> | 10 | #include <stdlib.h> |
| 10 | #include <sys/mman.h> | 11 | #include <string.h> |
| 11 | #include <sys/uio.h> | 12 | #include <sys/uio.h> |
| 12 | 13 | ||
| 13 | /* Libowfat */ | 14 | /* Libowfat */ |
| 14 | #include "byte.h" | ||
| 15 | #include "io.h" | 15 | #include "io.h" |
| 16 | #include "uint32.h" | ||
| 17 | 16 | ||
| 18 | /* Opentracker */ | 17 | /* Opentracker */ |
| 19 | #include "ot_iovec.h" | 18 | #include "ot_iovec.h" |
| @@ -37,7 +36,15 @@ ot_vector *mutex_bucket_lock(int bucket) { | |||
| 37 | return all_torrents + bucket; | 36 | return all_torrents + bucket; |
| 38 | } | 37 | } |
| 39 | 38 | ||
| 40 | ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(uint32_read_big((const char *)hash) >> OT_BUCKET_COUNT_SHIFT); } | 39 | /* Torrents are spread over buckets by the first four bytes of their info_hash, |
| 40 | which may sit at any alignment, hence the memcpy */ | ||
| 41 | static int bucket_from_hash(ot_hash const hash) { | ||
| 42 | uint32_t bucket; | ||
| 43 | memcpy(&bucket, hash, sizeof(bucket)); | ||
| 44 | return ntohl(bucket) >> OT_BUCKET_COUNT_SHIFT; | ||
| 45 | } | ||
| 46 | |||
| 47 | ot_vector *mutex_bucket_lock_by_hash(ot_hash const hash) { return mutex_bucket_lock(bucket_from_hash(hash)); } | ||
| 41 | 48 | ||
| 42 | void mutex_bucket_unlock(int bucket, int delta_torrentcount) { | 49 | void mutex_bucket_unlock(int bucket, int delta_torrentcount) { |
| 43 | pthread_mutex_unlock(bucket_mutex + bucket); | 50 | pthread_mutex_unlock(bucket_mutex + bucket); |
| @@ -45,7 +52,7 @@ void mutex_bucket_unlock(int bucket, int delta_torrentcount) { | |||
| 45 | } | 52 | } |
| 46 | 53 | ||
| 47 | void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) { | 54 | void mutex_bucket_unlock_by_hash(ot_hash const hash, int delta_torrentcount) { |
| 48 | mutex_bucket_unlock(uint32_read_big((char *)hash) >> OT_BUCKET_COUNT_SHIFT, delta_torrentcount); | 55 | mutex_bucket_unlock(bucket_from_hash(hash), delta_torrentcount); |
| 49 | } | 56 | } |
| 50 | 57 | ||
| 51 | size_t mutex_get_torrent_count(void) { return g_torrent_count; } | 58 | size_t mutex_get_torrent_count(void) { return g_torrent_count; } |
| @@ -259,7 +266,7 @@ void mutex_init(void) { | |||
| 259 | pthread_cond_init(&tasklist_being_filled, NULL); | 266 | pthread_cond_init(&tasklist_being_filled, NULL); |
| 260 | for (i = 0; i < OT_BUCKET_COUNT; ++i) | 267 | for (i = 0; i < OT_BUCKET_COUNT; ++i) |
| 261 | pthread_mutex_init(bucket_mutex + i, NULL); | 268 | pthread_mutex_init(bucket_mutex + i, NULL); |
| 262 | byte_zero(all_torrents, sizeof(all_torrents)); | 269 | memset(all_torrents, 0, sizeof(all_torrents)); |
| 263 | } | 270 | } |
| 264 | 271 | ||
| 265 | void mutex_deinit(void) { | 272 | void mutex_deinit(void) { |
| @@ -268,5 +275,5 @@ void mutex_deinit(void) { | |||
| 268 | pthread_mutex_destroy(bucket_mutex + i); | 275 | pthread_mutex_destroy(bucket_mutex + i); |
| 269 | pthread_mutex_destroy(&tasklist_mutex); | 276 | pthread_mutex_destroy(&tasklist_mutex); |
| 270 | pthread_cond_destroy(&tasklist_being_filled); | 277 | pthread_cond_destroy(&tasklist_being_filled); |
| 271 | byte_zero(all_torrents, sizeof(all_torrents)); | 278 | memset(all_torrents, 0, sizeof(all_torrents)); |
| 272 | } | 279 | } |
| @@ -4,13 +4,11 @@ | |||
| 4 | $id$ */ | 4 | $id$ */ |
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <arpa/inet.h> | ||
| 8 | #include <inttypes.h> | 7 | #include <inttypes.h> |
| 9 | #include <pthread.h> | 8 | #include <pthread.h> |
| 10 | #include <stdio.h> | 9 | #include <stdio.h> |
| 11 | #include <stdlib.h> | 10 | #include <stdlib.h> |
| 12 | #include <string.h> | 11 | #include <string.h> |
| 13 | #include <sys/mman.h> | ||
| 14 | #include <sys/types.h> | 12 | #include <sys/types.h> |
| 15 | #include <sys/uio.h> | 13 | #include <sys/uio.h> |
| 16 | #include <unistd.h> | 14 | #include <unistd.h> |
| @@ -19,7 +17,6 @@ | |||
| 19 | #endif | 17 | #endif |
| 20 | 18 | ||
| 21 | /* Libowfat */ | 19 | /* Libowfat */ |
| 22 | #include "byte.h" | ||
| 23 | #include "io.h" | 20 | #include "io.h" |
| 24 | #include "ip4.h" | 21 | #include "ip4.h" |
| 25 | #include "ip6.h" | 22 | #include "ip6.h" |
| @@ -339,9 +336,9 @@ size_t stats_top_txt(char *reply, size_t amount) { | |||
| 339 | if (amount > 100) | 336 | if (amount > 100) |
| 340 | amount = 100; | 337 | amount = 100; |
| 341 | 338 | ||
| 342 | byte_zero(top100s, sizeof(top100s)); | 339 | memset(top100s, 0, sizeof(top100s)); |
| 343 | byte_zero(top100c, sizeof(top100c)); | 340 | memset(top100c, 0, sizeof(top100c)); |
| 344 | byte_zero(top100l, sizeof(top100l)); | 341 | memset(top100l, 0, sizeof(top100l)); |
| 345 | 342 | ||
| 346 | /* Iterate over complete torrent list */ | 343 | /* Iterate over complete torrent list */ |
| 347 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { | 344 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { |
| @@ -7,12 +7,10 @@ | |||
| 7 | #include <pthread.h> | 7 | #include <pthread.h> |
| 8 | #include <stdio.h> | 8 | #include <stdio.h> |
| 9 | #include <string.h> | 9 | #include <string.h> |
| 10 | #include <sys/mman.h> | ||
| 11 | #include <sys/types.h> | 10 | #include <sys/types.h> |
| 12 | #include <sys/uio.h> | 11 | #include <sys/uio.h> |
| 13 | 12 | ||
| 14 | /* Libowfat */ | 13 | /* Libowfat */ |
| 15 | #include "byte.h" | ||
| 16 | #include "io.h" | 14 | #include "io.h" |
| 17 | #include "scan.h" | 15 | #include "scan.h" |
| 18 | 16 | ||
| @@ -36,15 +34,15 @@ int add_changeset_to_tracker(uint8_t *data, size_t len) { | |||
| 36 | uint8_t *end = data + len; | 34 | uint8_t *end = data + len; |
| 37 | unsigned long peer_count; | 35 | unsigned long peer_count; |
| 38 | 36 | ||
| 39 | /* We do know, that the string is \n terminated, so it cant | 37 | /* We do know, that the string is \n terminated, so it can't |
| 40 | overflow */ | 38 | overflow */ |
| 41 | if (byte_diff(data, 8, "d4:syncd")) | 39 | if (!memcmp(data, "d4:syncd", 8)) |
| 42 | return -1; | 40 | return -1; |
| 43 | data += 8; | 41 | data += 8; |
| 44 | 42 | ||
| 45 | while (1) { | 43 | while (1) { |
| 46 | if (byte_diff(data, 3, "20:")) { | 44 | if (!memcmp(data, "20:", 3)) { |
| 47 | if (byte_diff(data, 2, "ee")) | 45 | if (!memcmp(data, "ee", 2)) |
| 48 | return -1; | 46 | return -1; |
| 49 | return 0; | 47 | return 0; |
| 50 | } | 48 | } |
diff --git a/ot_vector.c b/ot_vector.c index d068999..e3c3d3f 100644 --- a/ot_vector.c +++ b/ot_vector.c | |||
| @@ -13,10 +13,6 @@ | |||
| 13 | /* Opentracker */ | 13 | /* Opentracker */ |
| 14 | #include "trackerlogic.h" | 14 | #include "trackerlogic.h" |
| 15 | 15 | ||
| 16 | /* Libowfat */ | ||
| 17 | #include "uint16.h" | ||
| 18 | #include "uint32.h" | ||
| 19 | |||
| 20 | static int vector_compare_peer6(const void *peer1, const void *peer2) { return memcmp(peer1, peer2, OT_PEER_COMPARE_SIZE6); } | 16 | static int vector_compare_peer6(const void *peer1, const void *peer2) { return memcmp(peer1, peer2, OT_PEER_COMPARE_SIZE6); } |
| 21 | static int vector_compare_peer4(const void *peer1, const void *peer2) { return memcmp(peer1, peer2, OT_PEER_COMPARE_SIZE4); } | 17 | static int vector_compare_peer4(const void *peer1, const void *peer2) { return memcmp(peer1, peer2, OT_PEER_COMPARE_SIZE4); } |
| 22 | 18 | ||
| @@ -222,8 +218,10 @@ void vector_redistribute_buckets(ot_peerlist *peer_list, size_t peer_size) { | |||
| 222 | for (bucket = 0; bucket < num_buckets_new; ++bucket) { | 218 | for (bucket = 0; bucket < num_buckets_new; ++bucket) { |
| 223 | bucket_list_new[bucket].space = bucket_size_new; | 219 | bucket_list_new[bucket].space = bucket_size_new; |
| 224 | bucket_list_new[bucket].data = malloc(bucket_size_new * peer_size); | 220 | bucket_list_new[bucket].data = malloc(bucket_size_new * peer_size); |
| 225 | if (!bucket_list_new[bucket].data) | 221 | if (!bucket_list_new[bucket].data) { |
| 226 | return vector_clean_list(bucket_list_new, num_buckets_new); | 222 | vector_clean_list(bucket_list_new, num_buckets_new); |
| 223 | return; | ||
| 224 | } | ||
| 227 | } | 225 | } |
| 228 | 226 | ||
| 229 | /* Now sort them into the correct bucket */ | 227 | /* Now sort them into the correct bucket */ |
| @@ -236,8 +234,10 @@ void vector_redistribute_buckets(ot_peerlist *peer_list, size_t peer_size) { | |||
| 236 | bucket_dest += vector_hash_peer(peers_old, OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size), num_buckets_new); | 234 | bucket_dest += vector_hash_peer(peers_old, OT_PEER_COMPARE_SIZE_FROM_PEER_SIZE(peer_size), num_buckets_new); |
| 237 | if (bucket_dest->size + 1 > bucket_dest->space) { | 235 | if (bucket_dest->size + 1 > bucket_dest->space) { |
| 238 | void *tmp = realloc(bucket_dest->data, peer_size * OT_VECTOR_GROW_RATIO * bucket_dest->space); | 236 | void *tmp = realloc(bucket_dest->data, peer_size * OT_VECTOR_GROW_RATIO * bucket_dest->space); |
| 239 | if (!tmp) | 237 | if (!tmp) { |
| 240 | return vector_clean_list(bucket_list_new, num_buckets_new); | 238 | vector_clean_list(bucket_list_new, num_buckets_new); |
| 239 | return; | ||
| 240 | } | ||
| 241 | bucket_dest->data = tmp; | 241 | bucket_dest->data = tmp; |
| 242 | bucket_dest->space *= OT_VECTOR_GROW_RATIO; | 242 | bucket_dest->space *= OT_VECTOR_GROW_RATIO; |
| 243 | } | 243 | } |
| @@ -8,8 +8,6 @@ | |||
| 8 | #include <ctype.h> | 8 | #include <ctype.h> |
| 9 | #include <errno.h> | 9 | #include <errno.h> |
| 10 | #include <pthread.h> | 10 | #include <pthread.h> |
| 11 | #include <pwd.h> | ||
| 12 | #include <signal.h> | ||
| 13 | #include <stdint.h> | 11 | #include <stdint.h> |
| 14 | #include <stdio.h> | 12 | #include <stdio.h> |
| 15 | #include <stdlib.h> | 13 | #include <stdlib.h> |
| @@ -18,7 +16,6 @@ | |||
| 18 | #include <unistd.h> | 16 | #include <unistd.h> |
| 19 | 17 | ||
| 20 | /* Libowfat */ | 18 | /* Libowfat */ |
| 21 | #include "byte.h" | ||
| 22 | #include "io.h" | 19 | #include "io.h" |
| 23 | #include "iob.h" | 20 | #include "iob.h" |
| 24 | #include "ip6.h" | 21 | #include "ip6.h" |
| @@ -142,8 +139,8 @@ size_t add_peer_to_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) | |||
| 142 | return -1; | 139 | return -1; |
| 143 | } | 140 | } |
| 144 | 141 | ||
| 145 | byte_zero(torrent->peer_list6, sizeof(ot_peerlist)); | 142 | memset(torrent->peer_list6, 0, sizeof(ot_peerlist)); |
| 146 | byte_zero(torrent->peer_list4, sizeof(ot_peerlist)); | 143 | memset(torrent->peer_list4, 0, sizeof(ot_peerlist)); |
| 147 | } | 144 | } |
| 148 | 145 | ||
| 149 | peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; | 146 | peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; |
| @@ -473,13 +470,15 @@ static void handle_write(int64 peersocket) { | |||
| 473 | } | 470 | } |
| 474 | 471 | ||
| 475 | static void server_mainloop() { | 472 | static void server_mainloop() { |
| 476 | int64 sock; | 473 | int64 sock; |
| 474 | uint32_t packet_type; | ||
| 477 | 475 | ||
| 478 | /* inlined livesync_init() */ | 476 | /* inlined livesync_init() */ |
| 479 | memset(g_peerbuffer_start, 0, sizeof(g_peerbuffer_start)); | 477 | memset(g_peerbuffer_start, 0, sizeof(g_peerbuffer_start)); |
| 480 | g_peerbuffer_pos = g_peerbuffer_start; | 478 | g_peerbuffer_pos = g_peerbuffer_start; |
| 481 | memcpy(g_peerbuffer_pos, &g_tracker_id, sizeof(g_tracker_id)); | 479 | memcpy(g_peerbuffer_pos, &g_tracker_id, sizeof(g_tracker_id)); |
| 482 | uint32_pack_big((char *)g_peerbuffer_pos + sizeof(g_tracker_id), OT_SYNC_PEER); | 480 | packet_type = htonl(OT_SYNC_PEER); |
| 481 | memcpy(g_peerbuffer_pos + sizeof(g_tracker_id), &packet_type, sizeof(packet_type)); | ||
| 483 | g_peerbuffer_pos += sizeof(g_tracker_id) + sizeof(uint32_t); | 482 | g_peerbuffer_pos += sizeof(g_tracker_id) + sizeof(uint32_t); |
| 484 | g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; | 483 | g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; |
| 485 | 484 | ||
| @@ -871,6 +870,7 @@ static void *livesync_worker(void *args) { | |||
| 871 | while (1) { | 870 | while (1) { |
| 872 | ot_ip6 in_ip; | 871 | ot_ip6 in_ip; |
| 873 | uint16_t in_port; | 872 | uint16_t in_port; |
| 873 | uint32_t packet_type; | ||
| 874 | size_t datalen = socket_recv4(g_socket_in, (char *)g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); | 874 | size_t datalen = socket_recv4(g_socket_in, (char *)g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); |
| 875 | 875 | ||
| 876 | /* Expect at least tracker id and packet type */ | 876 | /* Expect at least tracker id and packet type */ |
| @@ -880,7 +880,9 @@ static void *livesync_worker(void *args) { | |||
| 880 | /* drop packet coming from ourselves */ | 880 | /* drop packet coming from ourselves */ |
| 881 | continue; | 881 | continue; |
| 882 | } | 882 | } |
| 883 | switch (uint32_read_big((char *)g_inbuffer + sizeof(g_tracker_id))) { | 883 | memcpy(&packet_type, g_inbuffer + sizeof(g_tracker_id), sizeof(packet_type)); |
| 884 | |||
| 885 | switch (ntohl(packet_type)) { | ||
| 884 | case OT_SYNC_PEER4: | 886 | case OT_SYNC_PEER4: |
| 885 | livesync_handle_peersync(datalen, OT_PEER_SIZE4); | 887 | livesync_handle_peersync(datalen, OT_PEER_SIZE4); |
| 886 | break; | 888 | break; |
| @@ -888,7 +890,7 @@ static void *livesync_worker(void *args) { | |||
| 888 | livesync_handle_peersync(datalen, OT_PEER_SIZE6); | 890 | livesync_handle_peersync(datalen, OT_PEER_SIZE6); |
| 889 | break; | 891 | break; |
| 890 | default: | 892 | default: |
| 891 | // fprintf( stderr, "Received an unknown live sync packet type %u.\n", uint32_read_big( sizeof( g_tracker_id ) + (char*)g_inbuffer ) ); | 893 | // fprintf( stderr, "Received an unknown live sync packet type %u.\n", ntohl( packet_type ) ); |
| 892 | break; | 894 | break; |
| 893 | } | 895 | } |
| 894 | } | 896 | } |
diff --git a/scan_urlencoded_query.c b/scan_urlencoded_query.c index 38d544a..50aced3 100644 --- a/scan_urlencoded_query.c +++ b/scan_urlencoded_query.c | |||
| @@ -6,9 +6,6 @@ | |||
| 6 | /* Opentracker */ | 6 | /* Opentracker */ |
| 7 | #include "scan_urlencoded_query.h" | 7 | #include "scan_urlencoded_query.h" |
| 8 | 8 | ||
| 9 | /* Libwofat */ | ||
| 10 | #include "scan.h" | ||
| 11 | |||
| 12 | /* System */ | 9 | /* System */ |
| 13 | #include <string.h> | 10 | #include <string.h> |
| 14 | 11 | ||
diff --git a/trackerlogic.c b/trackerlogic.c index 7c6b7e1..59e9643 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
| @@ -5,16 +5,12 @@ | |||
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <arpa/inet.h> | 7 | #include <arpa/inet.h> |
| 8 | #include <errno.h> | ||
| 9 | #include <stdint.h> | 8 | #include <stdint.h> |
| 10 | #include <stdio.h> | 9 | #include <stdio.h> |
| 11 | #include <stdlib.h> | 10 | #include <stdlib.h> |
| 12 | #include <string.h> | 11 | #include <string.h> |
| 13 | #include <unistd.h> | ||
| 14 | 12 | ||
| 15 | /* Libowfat */ | 13 | /* Libowfat */ |
| 16 | #include "array.h" | ||
| 17 | #include "byte.h" | ||
| 18 | #include "io.h" | 14 | #include "io.h" |
| 19 | #include "iob.h" | 15 | #include "iob.h" |
| 20 | #include "ip6.h" | 16 | #include "ip6.h" |
| @@ -49,30 +45,35 @@ void add_torrent_from_saved_state(ot_hash const hash, ot_time base, size_t down_ | |||
| 49 | ot_torrent *torrent; | 45 | ot_torrent *torrent; |
| 50 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); | 46 | ot_vector *torrents_list = mutex_bucket_lock_by_hash(hash); |
| 51 | 47 | ||
| 52 | if (!accesslist_hashisvalid(hash)) | 48 | if (!accesslist_hashisvalid(hash)) { |
| 53 | return mutex_bucket_unlock_by_hash(hash, 0); | 49 | mutex_bucket_unlock_by_hash(hash, 0); |
| 50 | return; | ||
| 51 | } | ||
| 54 | 52 | ||
| 55 | torrent = vector_find_or_insert(torrents_list, (void *)hash, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); | 53 | torrent = vector_find_or_insert(torrents_list, (void *)hash, sizeof(ot_torrent), OT_HASH_COMPARE_SIZE, &exactmatch); |
| 56 | if (!torrent || exactmatch) | 54 | if (!torrent || exactmatch) { |
| 57 | return mutex_bucket_unlock_by_hash(hash, 0); | 55 | mutex_bucket_unlock_by_hash(hash, 0); |
| 56 | return; | ||
| 57 | } | ||
| 58 | 58 | ||
| 59 | /* Create a new torrent entry, then */ | 59 | /* Create a new torrent entry, then */ |
| 60 | byte_zero(torrent, sizeof(ot_torrent)); | 60 | memset(torrent, 0, sizeof(ot_torrent)); |
| 61 | memcpy(torrent->hash, hash, sizeof(ot_hash)); | 61 | memcpy(torrent->hash, hash, sizeof(ot_hash)); |
| 62 | 62 | ||
| 63 | if (!(torrent->peer_list6 = malloc(sizeof(ot_peerlist))) || !(torrent->peer_list4 = malloc(sizeof(ot_peerlist)))) { | 63 | if (!(torrent->peer_list6 = malloc(sizeof(ot_peerlist))) || !(torrent->peer_list4 = malloc(sizeof(ot_peerlist)))) { |
| 64 | vector_remove_torrent(torrents_list, torrent); | 64 | vector_remove_torrent(torrents_list, torrent); |
| 65 | return mutex_bucket_unlock_by_hash(hash, 0); | 65 | mutex_bucket_unlock_by_hash(hash, 0); |
| 66 | return; | ||
| 66 | } | 67 | } |
| 67 | 68 | ||
| 68 | byte_zero(torrent->peer_list6, sizeof(ot_peerlist)); | 69 | memset(torrent->peer_list6, 0, sizeof(ot_peerlist)); |
| 69 | byte_zero(torrent->peer_list4, sizeof(ot_peerlist)); | 70 | memset(torrent->peer_list4, 0, sizeof(ot_peerlist)); |
| 70 | torrent->peer_list6->base = base; | 71 | torrent->peer_list6->base = base; |
| 71 | torrent->peer_list4->base = base; | 72 | torrent->peer_list4->base = base; |
| 72 | torrent->peer_list6->down_count = down_count; | 73 | torrent->peer_list6->down_count = down_count; |
| 73 | torrent->peer_list4->down_count = down_count; | 74 | torrent->peer_list4->down_count = down_count; |
| 74 | 75 | ||
| 75 | return mutex_bucket_unlock_by_hash(hash, 1); | 76 | mutex_bucket_unlock_by_hash(hash, 1); |
| 76 | } | 77 | } |
| 77 | 78 | ||
| 78 | size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount) { | 79 | size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstruct *ws, size_t amount) { |
| @@ -102,7 +103,7 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 102 | 103 | ||
| 103 | if (!exactmatch) { | 104 | if (!exactmatch) { |
| 104 | /* Create a new torrent entry, then */ | 105 | /* Create a new torrent entry, then */ |
| 105 | byte_zero(torrent, sizeof(ot_torrent)); | 106 | memset(torrent, 0, sizeof(ot_torrent)); |
| 106 | memcpy(torrent->hash, *ws->hash, sizeof(ot_hash)); | 107 | memcpy(torrent->hash, *ws->hash, sizeof(ot_hash)); |
| 107 | 108 | ||
| 108 | if (!(torrent->peer_list6 = malloc(sizeof(ot_peerlist))) || !(torrent->peer_list4 = malloc(sizeof(ot_peerlist)))) { | 109 | if (!(torrent->peer_list6 = malloc(sizeof(ot_peerlist))) || !(torrent->peer_list4 = malloc(sizeof(ot_peerlist)))) { |
| @@ -111,8 +112,8 @@ size_t add_peer_to_torrent_and_return_peers(PROTO_FLAG proto, struct ot_workstru | |||
| 111 | return 0; | 112 | return 0; |
| 112 | } | 113 | } |
| 113 | 114 | ||
| 114 | byte_zero(torrent->peer_list6, sizeof(ot_peerlist)); | 115 | memset(torrent->peer_list6, 0, sizeof(ot_peerlist)); |
| 115 | byte_zero(torrent->peer_list4, sizeof(ot_peerlist)); | 116 | memset(torrent->peer_list4, 0, sizeof(ot_peerlist)); |
| 116 | delta_torrentcount = 1; | 117 | delta_torrentcount = 1; |
| 117 | } else | 118 | } else |
| 118 | clean_single_torrent(torrent); | 119 | clean_single_torrent(torrent); |
