diff options
Diffstat (limited to 'proxy.c')
-rw-r--r-- | proxy.c | 53 |
1 files changed, 33 insertions, 20 deletions
@@ -380,22 +380,27 @@ static void handle_read( int64 peersocket ) { | |||
380 | 380 | ||
381 | /* The new connection is good, send our tracker_id on incoming connections */ | 381 | /* The new connection is good, send our tracker_id on incoming connections */ |
382 | if( peer->state == FLAG_CONNECTING ) | 382 | if( peer->state == FLAG_CONNECTING ) |
383 | io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ); | 383 | if( io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ) != sizeof( g_tracker_id ) ) |
384 | goto close_socket; | ||
384 | 385 | ||
385 | peer->tracker_id = tracker_id; | 386 | peer->tracker_id = tracker_id; |
386 | PROXYPEER_SETCONNECTED( peer->state ); | 387 | PROXYPEER_SETCONNECTED( peer->state ); |
387 | 388 | ||
388 | fprintf( stderr, "Incoming connection successful.\n" ); | 389 | if( peer->state & FLAG_OUTGOING ) |
390 | fprintf( stderr, "succeeded.\n" ); | ||
391 | else | ||
392 | fprintf( stderr, "Incoming connection successful.\n" ); | ||
389 | 393 | ||
390 | break; | 394 | break; |
391 | close_socket: | 395 | close_socket: |
396 | fprintf( stderr, "Handshake incomplete, closing socket\n" ); | ||
392 | io_close( peersocket ); | 397 | io_close( peersocket ); |
393 | reset_info_block( peer ); | 398 | reset_info_block( peer ); |
394 | break; | 399 | break; |
395 | case FLAG_CONNECTED: | 400 | case FLAG_CONNECTED: |
396 | /* Here we acutally expect data from peer | 401 | /* Here we acutally expect data from peer |
397 | indata_length should be less than 20+256*7 bytes, for incomplete torrent entries */ | 402 | indata_length should be less than 20+256*7 bytes, for incomplete torrent entries */ |
398 | datalen = io_tryread( peersocket, (void*)(peer->indata + peer->indata_length), sizeof( peer->indata ) - peer->indata_length ); | 403 | datalen = io_tryread( peersocket, (void*)(peer->indata + peer->indata_length), sizeof( peer->indata ) - peer->indata_length ); |
399 | if( !datalen || datalen < -1 ) { | 404 | if( !datalen || datalen < -1 ) { |
400 | fprintf( stderr, "Connection closed by remote peer.\n" ); | 405 | fprintf( stderr, "Connection closed by remote peer.\n" ); |
401 | io_close( peersocket ); | 406 | io_close( peersocket ); |
@@ -432,12 +437,15 @@ static void handle_write( int64 peersocket ) { | |||
432 | break; | 437 | break; |
433 | } | 438 | } |
434 | 439 | ||
435 | io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ); | 440 | if( io_trywrite( peersocket, (void*)&g_tracker_id, sizeof( g_tracker_id ) ) == sizeof( g_tracker_id ) ) { |
436 | PROXYPEER_SETWAITTRACKERID( peer->state ); | 441 | PROXYPEER_SETWAITTRACKERID( peer->state ); |
437 | fprintf( stderr, " succeeded.\n" ); | 442 | io_dontwantwrite( peersocket ); |
438 | 443 | io_wantread( peersocket ); | |
439 | io_dontwantwrite( peersocket ); | 444 | } else { |
440 | io_wantread( peersocket ); | 445 | fprintf( stderr, "Handshake incomplete, closing socket\n" ); |
446 | io_close( peersocket ); | ||
447 | reset_info_block( peer ); | ||
448 | } | ||
441 | break; | 449 | break; |
442 | case FLAG_CONNECTED: | 450 | case FLAG_CONNECTED: |
443 | switch( iob_send( peersocket, &peer->outdata ) ) { | 451 | switch( iob_send( peersocket, &peer->outdata ) ) { |
@@ -612,7 +620,7 @@ static void * streamsync_worker( void * args ) { | |||
612 | } | 620 | } |
613 | 621 | ||
614 | /* Maximal memory requirement: max 3 blocks, max torrents * 20 + max peers * 7 */ | 622 | /* Maximal memory requirement: max 3 blocks, max torrents * 20 + max peers * 7 */ |
615 | mem = 3 * ( 4 + 1 + 1 + 2 ) + ( count_one + count_two ) * 19 + count_def * ( 19 + 8 ) + | 623 | mem = 3 * ( 4 + 1 + 1 + 2 ) + ( count_one + count_two ) * ( 19 + 1 ) + count_def * ( 19 + 8 ) + |
616 | ( count_one + 2 * count_two + count_peers ) * 7; | 624 | ( count_one + 2 * count_two + count_peers ) * 7; |
617 | 625 | ||
618 | fprintf( stderr, "Mem: %zd\n", mem ); | 626 | fprintf( stderr, "Mem: %zd\n", mem ); |
@@ -630,7 +638,7 @@ static void * streamsync_worker( void * args ) { | |||
630 | ptr_a[7] = count_one & 255; | 638 | ptr_a[7] = count_one & 255; |
631 | ptr_a += 8; | 639 | ptr_a += 8; |
632 | } else | 640 | } else |
633 | count_def += count_one; | 641 | count_def += count_one; |
634 | 642 | ||
635 | if( count_two > 8 || !count_def ) { | 643 | if( count_two > 8 || !count_def ) { |
636 | mem_b = 4 + 1 + 1 + 2 + count_two * ( 19 + 14 ); | 644 | mem_b = 4 + 1 + 1 + 2 + count_two * ( 19 + 14 ); |
@@ -642,7 +650,7 @@ static void * streamsync_worker( void * args ) { | |||
642 | ptr_b[7] = count_two & 255; | 650 | ptr_b[7] = count_two & 255; |
643 | ptr_b += 8; | 651 | ptr_b += 8; |
644 | } else | 652 | } else |
645 | count_def += count_two; | 653 | count_def += count_two; |
646 | 654 | ||
647 | if( count_def ) { | 655 | if( count_def ) { |
648 | memcpy( ptr_c, &g_tracker_id, sizeof(g_tracker_id)); /* Offset 0: the tracker ID */ | 656 | memcpy( ptr_c, &g_tracker_id, sizeof(g_tracker_id)); /* Offset 0: the tracker ID */ |
@@ -740,22 +748,23 @@ void livesync_ticker( ) { | |||
740 | } | 748 | } |
741 | 749 | ||
742 | static void livesync_proxytell( uint8_t prefix, uint8_t *info_hash, uint8_t *peer ) { | 750 | static void livesync_proxytell( uint8_t prefix, uint8_t *info_hash, uint8_t *peer ) { |
743 | unsigned int i; | 751 | // unsigned int i; |
744 | 752 | ||
745 | *g_peerbuffer_pos = prefix; | 753 | *g_peerbuffer_pos = prefix; |
746 | memcpy( g_peerbuffer_pos + 1, info_hash, sizeof(ot_hash) - 1 ); | 754 | memcpy( g_peerbuffer_pos + 1, info_hash, sizeof(ot_hash) - 1 ); |
747 | memcpy( g_peerbuffer_pos + sizeof(ot_hash), peer, sizeof(ot_peer) - 1 ); | 755 | memcpy( g_peerbuffer_pos + sizeof(ot_hash), peer, sizeof(ot_peer) - 1 ); |
748 | 756 | ||
757 | #if 0 | ||
749 | /* Dump info_hash */ | 758 | /* Dump info_hash */ |
750 | for( i=0; i<sizeof(ot_hash); ++i ) | 759 | for( i=0; i<sizeof(ot_hash); ++i ) |
751 | printf( "%02X", g_peerbuffer_pos[i] ); | 760 | printf( "%02X", g_peerbuffer_pos[i] ); |
752 | putchar( ':' ); | 761 | putchar( ':' ); |
753 | 762 | #endif | |
754 | g_peerbuffer_pos += sizeof(ot_hash); | 763 | g_peerbuffer_pos += sizeof(ot_hash); |
755 | 764 | #if 0 | |
756 | 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], | 765 | 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], |
757 | g_peerbuffer_pos[4] | ( g_peerbuffer_pos[5] << 8 ), g_peerbuffer_pos[6], g_peerbuffer_pos[7] ); | 766 | g_peerbuffer_pos[4] | ( g_peerbuffer_pos[5] << 8 ), g_peerbuffer_pos[6], g_peerbuffer_pos[7] ); |
758 | 767 | #endif | |
759 | g_peerbuffer_pos += sizeof(ot_peer); | 768 | g_peerbuffer_pos += sizeof(ot_peer); |
760 | 769 | ||
761 | if( g_peerbuffer_pos >= g_peerbuffer_highwater ) | 770 | if( g_peerbuffer_pos >= g_peerbuffer_highwater ) |
@@ -794,10 +803,14 @@ printf( "type: %hhu, prefix: %02X, torrentcount: %zd\n", peer->packet_type, peer | |||
794 | do peers |= ( 0x7f & *data ) << ( 7 * shift ); | 803 | do peers |= ( 0x7f & *data ) << ( 7 * shift ); |
795 | while ( *(data++) & 0x80 && shift++ < 6 ); | 804 | while ( *(data++) & 0x80 && shift++ < 6 ); |
796 | } | 805 | } |
797 | 806 | #if 0 | |
807 | printf( "peers: %zd\n", peers ); | ||
808 | #endif | ||
798 | /* Ensure enough data being read to hold all peers */ | 809 | /* Ensure enough data being read to hold all peers */ |
799 | if( data + (OT_IP_SIZE + 3) * peers > dataend ) break; | 810 | if( data + (OT_IP_SIZE + 3) * peers > dataend ) { |
800 | 811 | data = hash; | |
812 | break; | ||
813 | } | ||
801 | while( peers-- ) { | 814 | while( peers-- ) { |
802 | livesync_proxytell( peer->packet_tprefix, hash, data ); | 815 | livesync_proxytell( peer->packet_tprefix, hash, data ); |
803 | data += OT_IP_SIZE + 3; | 816 | data += OT_IP_SIZE + 3; |