diff options
Diffstat (limited to 'proxy.c')
-rw-r--r-- | proxy.c | 25 |
1 files changed, 15 insertions, 10 deletions
@@ -59,7 +59,7 @@ int g_self_pipe[2]; | |||
59 | /* So after each bucket wait 1 / OT_BUCKET_COUNT intervals */ | 59 | /* So after each bucket wait 1 / OT_BUCKET_COUNT intervals */ |
60 | #define OT_SYNC_SLEEP ( ( ( OT_SYNC_INTERVAL_MINUTES ) * 60 * 1000000 ) / ( OT_BUCKET_COUNT ) ) | 60 | #define OT_SYNC_SLEEP ( ( ( OT_SYNC_INTERVAL_MINUTES ) * 60 * 1000000 ) / ( OT_BUCKET_COUNT ) ) |
61 | 61 | ||
62 | enum { OT_SYNC_PEER }; | 62 | enum { OT_SYNC_PEER = 0xbeef }; |
63 | enum { FLAG_SERVERSOCKET = 1 }; | 63 | enum { FLAG_SERVERSOCKET = 1 }; |
64 | 64 | ||
65 | /* For incoming packets */ | 65 | /* For incoming packets */ |
@@ -195,8 +195,8 @@ void free_peerlist( ot_peerlist *peer_list ) { | |||
195 | static void livesync_handle_peersync( ssize_t datalen ) { | 195 | static void livesync_handle_peersync( ssize_t datalen ) { |
196 | int off = sizeof( g_tracker_id ) + sizeof( uint32_t ); | 196 | int off = sizeof( g_tracker_id ) + sizeof( uint32_t ); |
197 | 197 | ||
198 | /* Now basic sanity checks have been done on the live sync packet | 198 | fprintf( stderr, "." ); |
199 | We might add more testing and logging. */ | 199 | |
200 | while( off + (ssize_t)sizeof( ot_hash ) + (ssize_t)sizeof( ot_peer ) <= datalen ) { | 200 | while( off + (ssize_t)sizeof( ot_hash ) + (ssize_t)sizeof( ot_peer ) <= datalen ) { |
201 | ot_peer *peer = (ot_peer*)(g_inbuffer + off + sizeof(ot_hash)); | 201 | ot_peer *peer = (ot_peer*)(g_inbuffer + off + sizeof(ot_hash)); |
202 | ot_hash *hash = (ot_hash*)(g_inbuffer + off); | 202 | ot_hash *hash = (ot_hash*)(g_inbuffer + off); |
@@ -227,6 +227,7 @@ enum { | |||
227 | }; | 227 | }; |
228 | 228 | ||
229 | #define PROXYPEER_NEEDSCONNECT(flag) ((flag)==FLAG_OUTGOING) | 229 | #define PROXYPEER_NEEDSCONNECT(flag) ((flag)==FLAG_OUTGOING) |
230 | #define PROXYPEER_ISCONNECTED(flag) (((flag)&FLAG_MASK)==FLAG_CONNECTED) | ||
230 | #define PROXYPEER_SETDISCONNECTED(flag) (flag)=(((flag)&FLAG_OUTGOING)|FLAG_DISCONNECTED) | 231 | #define PROXYPEER_SETDISCONNECTED(flag) (flag)=(((flag)&FLAG_OUTGOING)|FLAG_DISCONNECTED) |
231 | #define PROXYPEER_SETCONNECTING(flag) (flag)=(((flag)&FLAG_OUTGOING)|FLAG_CONNECTING) | 232 | #define PROXYPEER_SETCONNECTING(flag) (flag)=(((flag)&FLAG_OUTGOING)|FLAG_CONNECTING) |
232 | #define PROXYPEER_SETWAITTRACKERID(flag) (flag)=(((flag)&FLAG_OUTGOING)|FLAG_WAITTRACKERID) | 233 | #define PROXYPEER_SETWAITTRACKERID(flag) (flag)=(((flag)&FLAG_OUTGOING)|FLAG_WAITTRACKERID) |
@@ -439,16 +440,13 @@ static void handle_write( int64 peersocket ) { | |||
439 | case FLAG_CONNECTED: | 440 | case FLAG_CONNECTED: |
440 | switch( iob_send( peersocket, &peer->outdata ) ) { | 441 | switch( iob_send( peersocket, &peer->outdata ) ) { |
441 | case 0: /* all data sent */ | 442 | case 0: /* all data sent */ |
442 | fprintf( stderr, "EMPTY\n" ); | ||
443 | io_dontwantwrite( peersocket ); | 443 | io_dontwantwrite( peersocket ); |
444 | break; | 444 | break; |
445 | case -3: /* an error occured */ | 445 | case -3: /* an error occured */ |
446 | fprintf( stderr, "ERROR\n" ); | ||
447 | io_close( peersocket ); | 446 | io_close( peersocket ); |
448 | reset_info_block( peer ); | 447 | reset_info_block( peer ); |
449 | break; | 448 | break; |
450 | default: /* Normal operation or eagain */ | 449 | default: /* Normal operation or eagain */ |
451 | fprintf( stderr, "EGAIN\n" ); | ||
452 | break; | 450 | break; |
453 | } | 451 | } |
454 | break; | 452 | break; |
@@ -594,7 +592,9 @@ static void * streamsync_worker( void * args ) { | |||
594 | ot_vector *torrents_list = mutex_bucket_lock( bucket ); | 592 | ot_vector *torrents_list = mutex_bucket_lock( bucket ); |
595 | size_t tor_offset, count_def = 0, count_one = 0, count_two = 0, count_peers = 0; | 593 | size_t tor_offset, count_def = 0, count_one = 0, count_two = 0, count_peers = 0; |
596 | size_t mem, mem_a = 0, mem_b = 0; | 594 | size_t mem, mem_a = 0, mem_b = 0; |
597 | uint8_t *ptr, *ptr_a, *ptr_b, *ptr_c; | 595 | uint8_t *ptr = 0, *ptr_a, *ptr_b, *ptr_c; |
596 | |||
597 | if( !torrents_list->size ) goto unlock_continue; | ||
598 | 598 | ||
599 | /* For each torrent in this bucket.. */ | 599 | /* For each torrent in this bucket.. */ |
600 | for( tor_offset=0; tor_offset<torrents_list->size; ++tor_offset ) { | 600 | for( tor_offset=0; tor_offset<torrents_list->size; ++tor_offset ) { |
@@ -614,6 +614,8 @@ static void * streamsync_worker( void * args ) { | |||
614 | mem = 3 * ( 4 + 1 + 1 + 2 ) + ( count_one + count_two ) * 19 + count_def * 20 + | 614 | mem = 3 * ( 4 + 1 + 1 + 2 ) + ( count_one + count_two ) * 19 + count_def * 20 + |
615 | ( count_one + 2 * count_two + count_peers ) * 7; | 615 | ( count_one + 2 * count_two + count_peers ) * 7; |
616 | 616 | ||
617 | fprintf( stderr, "Mem: %d\n", mem ); | ||
618 | |||
617 | ptr = ptr_a = ptr_b = ptr_c = malloc( mem ); | 619 | ptr = ptr_a = ptr_b = ptr_c = malloc( mem ); |
618 | if( !ptr ) goto unlock_continue; | 620 | if( !ptr ) goto unlock_continue; |
619 | 621 | ||
@@ -697,11 +699,14 @@ unlock_continue: | |||
697 | mem = ptr_c - ptr; | 699 | mem = ptr_c - ptr; |
698 | 700 | ||
699 | for( i=0; i<g_connection_count; ++i ) { | 701 | for( i=0; i<g_connection_count; ++i ) { |
700 | if( g_connections[i].fd != -1 ) { | 702 | if( PROXYPEER_ISCONNECTED(g_connections[i].state) ) { |
701 | void *tmp = malloc( mem ); | 703 | void *tmp = malloc( mem ); |
702 | if( tmp ) | 704 | if( tmp ) { |
705 | memcpy( tmp, ptr, mem ); | ||
703 | if( !iob_addbuf_free( &g_connections[i].outdata, tmp, mem ) ) | 706 | if( !iob_addbuf_free( &g_connections[i].outdata, tmp, mem ) ) |
704 | free( tmp ); | 707 | free( tmp ); |
708 | io_wantwrite( g_connections[i].fd ); | ||
709 | } | ||
705 | } | 710 | } |
706 | } | 711 | } |
707 | 712 | ||
@@ -793,7 +798,7 @@ static void * livesync_worker( void * args ) { | |||
793 | livesync_handle_peersync( datalen ); | 798 | livesync_handle_peersync( datalen ); |
794 | break; | 799 | break; |
795 | default: | 800 | default: |
796 | fprintf( stderr, "Received an unknown live sync packet type %u.\n", uint32_read_big( sizeof( g_tracker_id ) + (char*)g_inbuffer ) ); | 801 | // fprintf( stderr, "Received an unknown live sync packet type %u.\n", uint32_read_big( sizeof( g_tracker_id ) + (char*)g_inbuffer ) ); |
797 | break; | 802 | break; |
798 | } | 803 | } |
799 | } | 804 | } |