diff options
Diffstat (limited to 'proxy.c')
| -rw-r--r-- | proxy.c | 35 |
1 files changed, 17 insertions, 18 deletions
| @@ -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" |
| @@ -28,6 +25,7 @@ | |||
| 28 | 25 | ||
| 29 | /* Opentracker */ | 26 | /* Opentracker */ |
| 30 | #include "ot_mutex.h" | 27 | #include "ot_mutex.h" |
| 28 | #include "ot_random.h" | ||
| 31 | #include "ot_stats.h" | 29 | #include "ot_stats.h" |
| 32 | #include "ot_vector.h" | 30 | #include "ot_vector.h" |
| 33 | #include "trackerlogic.h" | 31 | #include "trackerlogic.h" |
| @@ -142,8 +140,8 @@ size_t add_peer_to_torrent_proxy(ot_hash hash, ot_peer *peer, size_t peer_size) | |||
| 142 | return -1; | 140 | return -1; |
| 143 | } | 141 | } |
| 144 | 142 | ||
| 145 | byte_zero(torrent->peer_list6, sizeof(ot_peerlist)); | 143 | memset(torrent->peer_list6, 0, sizeof(ot_peerlist)); |
| 146 | byte_zero(torrent->peer_list4, sizeof(ot_peerlist)); | 144 | memset(torrent->peer_list4, 0, sizeof(ot_peerlist)); |
| 147 | } | 145 | } |
| 148 | 146 | ||
| 149 | peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; | 147 | peer_list = peer_size == OT_PEER_SIZE6 ? torrent->peer_list6 : torrent->peer_list4; |
| @@ -276,12 +274,12 @@ void reset_info_block(proxy_peer *peer) { | |||
| 276 | * Multiple connections to/from the same ip are okay, if tracker_id doesn't match | 274 | * Multiple connections to/from the same ip are okay, if tracker_id doesn't match |
| 277 | * Reconnect attempts occur only twice a minute | 275 | * Reconnect attempts occur only twice a minute |
| 278 | */ | 276 | */ |
| 279 | static int g_connection_count; | 277 | static size_t g_connection_count; |
| 280 | static ot_time g_connection_reconn; | 278 | static ot_time g_connection_reconn; |
| 281 | static proxy_peer g_connections[MAX_PEERS]; | 279 | static proxy_peer g_connections[MAX_PEERS]; |
| 282 | 280 | ||
| 283 | static void handle_reconnects(void) { | 281 | static void handle_reconnects(void) { |
| 284 | int i; | 282 | size_t i; |
| 285 | for (i = 0; i < g_connection_count; ++i) | 283 | for (i = 0; i < g_connection_count; ++i) |
| 286 | if (PROXYPEER_NEEDSCONNECT(g_connections[i].state)) { | 284 | if (PROXYPEER_NEEDSCONNECT(g_connections[i].state)) { |
| 287 | int64 newfd = socket_tcp6(); | 285 | int64 newfd = socket_tcp6(); |
| @@ -473,13 +471,15 @@ static void handle_write(int64 peersocket) { | |||
| 473 | } | 471 | } |
| 474 | 472 | ||
| 475 | static void server_mainloop() { | 473 | static void server_mainloop() { |
| 476 | int64 sock; | 474 | int64 sock; |
| 475 | uint32_t packet_type; | ||
| 477 | 476 | ||
| 478 | /* inlined livesync_init() */ | 477 | /* inlined livesync_init() */ |
| 479 | memset(g_peerbuffer_start, 0, sizeof(g_peerbuffer_start)); | 478 | memset(g_peerbuffer_start, 0, sizeof(g_peerbuffer_start)); |
| 480 | g_peerbuffer_pos = g_peerbuffer_start; | 479 | g_peerbuffer_pos = g_peerbuffer_start; |
| 481 | memcpy(g_peerbuffer_pos, &g_tracker_id, sizeof(g_tracker_id)); | 480 | 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); | 481 | packet_type = htonl(OT_SYNC_PEER); |
| 482 | memcpy(g_peerbuffer_pos + sizeof(g_tracker_id), &packet_type, sizeof(packet_type)); | ||
| 483 | g_peerbuffer_pos += sizeof(g_tracker_id) + sizeof(uint32_t); | 483 | g_peerbuffer_pos += sizeof(g_tracker_id) + sizeof(uint32_t); |
| 484 | g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; | 484 | g_next_packet_time = time(NULL) + LIVESYNC_MAXDELAY; |
| 485 | 485 | ||
| @@ -564,12 +564,8 @@ int main(int argc, char **argv) { | |||
| 564 | uint16_t tmpport; | 564 | uint16_t tmpport; |
| 565 | int scanon = 1, lbound = 0, sbound = 0; | 565 | int scanon = 1, lbound = 0, sbound = 0; |
| 566 | 566 | ||
| 567 | srandom(time(NULL)); | 567 | random_init(); |
| 568 | #ifdef WANT_ARC4RANDOM | 568 | g_tracker_id = random_uint32(); |
| 569 | g_tracker_id = arc4random(); | ||
| 570 | #else | ||
| 571 | g_tracker_id = random(); | ||
| 572 | #endif | ||
| 573 | 569 | ||
| 574 | while (scanon) { | 570 | while (scanon) { |
| 575 | switch (getopt(argc, argv, ":l:c:L:h")) { | 571 | switch (getopt(argc, argv, ":l:c:L:h")) { |
| @@ -625,7 +621,7 @@ int main(int argc, char **argv) { | |||
| 625 | static void *streamsync_worker(void *args) { | 621 | static void *streamsync_worker(void *args) { |
| 626 | (void)args; | 622 | (void)args; |
| 627 | while (1) { | 623 | while (1) { |
| 628 | int bucket; | 624 | size_t bucket; |
| 629 | /* For each bucket... */ | 625 | /* For each bucket... */ |
| 630 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { | 626 | for (bucket = 0; bucket < OT_BUCKET_COUNT; ++bucket) { |
| 631 | /* Get exclusive access to that bucket */ | 627 | /* Get exclusive access to that bucket */ |
| @@ -871,6 +867,7 @@ static void *livesync_worker(void *args) { | |||
| 871 | while (1) { | 867 | while (1) { |
| 872 | ot_ip6 in_ip; | 868 | ot_ip6 in_ip; |
| 873 | uint16_t in_port; | 869 | uint16_t in_port; |
| 870 | 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); | 871 | size_t datalen = socket_recv4(g_socket_in, (char *)g_inbuffer, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); |
| 875 | 872 | ||
| 876 | /* Expect at least tracker id and packet type */ | 873 | /* Expect at least tracker id and packet type */ |
| @@ -880,7 +877,9 @@ static void *livesync_worker(void *args) { | |||
| 880 | /* drop packet coming from ourselves */ | 877 | /* drop packet coming from ourselves */ |
| 881 | continue; | 878 | continue; |
| 882 | } | 879 | } |
| 883 | switch (uint32_read_big((char *)g_inbuffer + sizeof(g_tracker_id))) { | 880 | memcpy(&packet_type, g_inbuffer + sizeof(g_tracker_id), sizeof(packet_type)); |
| 881 | |||
| 882 | switch (ntohl(packet_type)) { | ||
| 884 | case OT_SYNC_PEER4: | 883 | case OT_SYNC_PEER4: |
| 885 | livesync_handle_peersync(datalen, OT_PEER_SIZE4); | 884 | livesync_handle_peersync(datalen, OT_PEER_SIZE4); |
| 886 | break; | 885 | break; |
| @@ -888,7 +887,7 @@ static void *livesync_worker(void *args) { | |||
| 888 | livesync_handle_peersync(datalen, OT_PEER_SIZE6); | 887 | livesync_handle_peersync(datalen, OT_PEER_SIZE6); |
| 889 | break; | 888 | break; |
| 890 | default: | 889 | default: |
| 891 | // fprintf( stderr, "Received an unknown live sync packet type %u.\n", uint32_read_big( sizeof( g_tracker_id ) + (char*)g_inbuffer ) ); | 890 | // fprintf( stderr, "Received an unknown live sync packet type %u.\n", ntohl( packet_type ) ); |
| 892 | break; | 891 | break; |
| 893 | } | 892 | } |
| 894 | } | 893 | } |
