Address reconnecting issues

dynamic-accesslists
erdgeist 14 years ago
parent 3e30fa32aa
commit 4b3189eb44

@ -380,15 +380,20 @@ static void handle_read( int64 peersocket ) {
/* The new connection is good, send our tracker_id on incoming connections */ /* The new connection is good, send our tracker_id on incoming connections */
if( peer->state == FLAG_CONNECTING ) if( peer->state == FLAG_CONNECTING )
io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ); if( io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ) != sizeof( g_tracker_id ) )
goto close_socket;
peer->tracker_id = tracker_id; peer->tracker_id = tracker_id;
PROXYPEER_SETCONNECTED( peer->state ); PROXYPEER_SETCONNECTED( peer->state );
fprintf( stderr, "Incoming connection successful.\n" ); if( peer->state & FLAG_OUTGOING )
fprintf( stderr, "succeeded.\n" );
else
fprintf( stderr, "Incoming connection successful.\n" );
break; break;
close_socket: close_socket:
fprintf( stderr, "Handshake incomplete, closing socket\n" );
io_close( peersocket ); io_close( peersocket );
reset_info_block( peer ); reset_info_block( peer );
break; break;
@ -432,12 +437,15 @@ static void handle_write( int64 peersocket ) {
break; break;
} }
io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ); if( io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ) == sizeof( g_tracker_id ) ) {
PROXYPEER_SETWAITTRACKERID( peer->state ); PROXYPEER_SETWAITTRACKERID( peer->state );
fprintf( stderr, " succeeded.\n" ); io_dontwantwrite( peersocket );
io_wantread( peersocket );
io_dontwantwrite( peersocket ); } else {
io_wantread( peersocket ); fprintf( stderr, "Handshake incomplete, closing socket\n" );
io_close( peersocket );
reset_info_block( peer );
}
break; break;
case FLAG_CONNECTED: case FLAG_CONNECTED:
switch( iob_send( peersocket, &peer->outdata ) ) { switch( iob_send( peersocket, &peer->outdata ) ) {
@ -612,7 +620,7 @@ static void * streamsync_worker( void * args ) {
} }
/* Maximal memory requirement: max 3 blocks, max torrents * 20 + max peers * 7 */ /* Maximal memory requirement: max 3 blocks, max torrents * 20 + max peers * 7 */
mem = 3 * ( 4 + 1 + 1 + 2 ) + ( count_one + count_two ) * 19 + count_def * ( 19 + 8 ) + mem = 3 * ( 4 + 1 + 1 + 2 ) + ( count_one + count_two ) * ( 19 + 1 ) + count_def * ( 19 + 8 ) +
( count_one + 2 * count_two + count_peers ) * 7; ( count_one + 2 * count_two + count_peers ) * 7;
fprintf( stderr, "Mem: %zd\n", mem ); fprintf( stderr, "Mem: %zd\n", mem );
@ -630,7 +638,7 @@ static void * streamsync_worker( void * args ) {
ptr_a[7] = count_one & 255; ptr_a[7] = count_one & 255;
ptr_a += 8; ptr_a += 8;
} else } else
count_def += count_one; count_def += count_one;
if( count_two > 8 || !count_def ) { if( count_two > 8 || !count_def ) {
mem_b = 4 + 1 + 1 + 2 + count_two * ( 19 + 14 ); mem_b = 4 + 1 + 1 + 2 + count_two * ( 19 + 14 );
@ -642,7 +650,7 @@ static void * streamsync_worker( void * args ) {
ptr_b[7] = count_two & 255; ptr_b[7] = count_two & 255;
ptr_b += 8; ptr_b += 8;
} else } else
count_def += count_two; count_def += count_two;
if( count_def ) { if( count_def ) {
memcpy( ptr_c, &g_tracker_id, sizeof(g_tracker_id)); /* Offset 0: the tracker ID */ memcpy( ptr_c, &g_tracker_id, sizeof(g_tracker_id)); /* Offset 0: the tracker ID */
@ -740,22 +748,23 @@ void livesync_ticker( ) {
} }
static void livesync_proxytell( uint8_t prefix, uint8_t *info_hash, uint8_t *peer ) { static void livesync_proxytell( uint8_t prefix, uint8_t *info_hash, uint8_t *peer ) {
unsigned int i; // unsigned int i;
*g_peerbuffer_pos = prefix; *g_peerbuffer_pos = prefix;
memcpy( g_peerbuffer_pos + 1, info_hash, sizeof(ot_hash) - 1 ); memcpy( g_peerbuffer_pos + 1, info_hash, sizeof(ot_hash) - 1 );
memcpy( g_peerbuffer_pos + sizeof(ot_hash), peer, sizeof(ot_peer) - 1 ); memcpy( g_peerbuffer_pos + sizeof(ot_hash), peer, sizeof(ot_peer) - 1 );
#if 0
/* Dump info_hash */ /* Dump info_hash */
for( i=0; i<sizeof(ot_hash); ++i ) for( i=0; i<sizeof(ot_hash); ++i )
printf( "%02X", g_peerbuffer_pos[i] ); printf( "%02X", g_peerbuffer_pos[i] );
putchar( ':' ); putchar( ':' );
#endif
g_peerbuffer_pos += sizeof(ot_hash); g_peerbuffer_pos += sizeof(ot_hash);
#if 0
printf( "%hhu.%hhu.%hhu.%hhu:%hu (%02X %02X)\n", g_peerbuffer_pos[0], g_peerbuffer_pos[1], g_peerbuffer_pos[2], g_peerbuffer_pos[3], printf( "%hhu.%hhu.%hhu.%hhu:%hu (%02X %02X)\n", g_peerbuffer_pos[0], g_peerbuffer_pos[1], g_peerbuffer_pos[2], g_peerbuffer_pos[3],
g_peerbuffer_pos[4] | ( g_peerbuffer_pos[5] << 8 ), g_peerbuffer_pos[6], g_peerbuffer_pos[7] ); g_peerbuffer_pos[4] | ( g_peerbuffer_pos[5] << 8 ), g_peerbuffer_pos[6], g_peerbuffer_pos[7] );
#endif
g_peerbuffer_pos += sizeof(ot_peer); g_peerbuffer_pos += sizeof(ot_peer);
if( g_peerbuffer_pos >= g_peerbuffer_highwater ) if( g_peerbuffer_pos >= g_peerbuffer_highwater )
@ -794,10 +803,14 @@ printf( "type: %hhu, prefix: %02X, torrentcount: %zd\n", peer->packet_type, peer
do peers |= ( 0x7f & *data ) << ( 7 * shift ); do peers |= ( 0x7f & *data ) << ( 7 * shift );
while ( *(data++) & 0x80 && shift++ < 6 ); while ( *(data++) & 0x80 && shift++ < 6 );
} }
#if 0
printf( "peers: %zd\n", peers );
#endif
/* Ensure enough data being read to hold all peers */ /* Ensure enough data being read to hold all peers */
if( data + (OT_IP_SIZE + 3) * peers > dataend ) break; if( data + (OT_IP_SIZE + 3) * peers > dataend ) {
data = hash;
break;
}
while( peers-- ) { while( peers-- ) {
livesync_proxytell( peer->packet_tprefix, hash, data ); livesync_proxytell( peer->packet_tprefix, hash, data );
data += OT_IP_SIZE + 3; data += OT_IP_SIZE + 3;

Loading…
Cancel
Save