diff --git a/opentracker.c b/opentracker.c index 1139b9e..b20a4dc 100644 --- a/opentracker.c +++ b/opentracker.c @@ -114,6 +114,7 @@ void httpresponse(struct http_data* h,int64 s) ot_torrent *torrent; ot_hash *hash = NULL; int numwant, tmp, scanon; + unsigned short port = htons(6881); size_t reply_size = 0; array_cat0(&h->r); @@ -187,8 +188,9 @@ e400: if( byte_diff(data,8,"announce")) goto e404; - peer.ip = h->ip; - peer.port_flags = 6881 << 16; + OT_SETIP( &peer, &h->ip); + OT_SETPORT( &peer, &port ); + OT_FLAG( &peer ) = 0; numwant = 50; scanon = 1; @@ -202,12 +204,12 @@ e400: case 4: if(!byte_diff(data,4,"port")) { size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); - if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || (tmp > 65536) ) goto e404; - peer.port_flags = ( tmp << 16 ) | ( peer.port_flags & 0xffff ); + if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || ( tmp > 0xffff ) ) goto e404; + port = htons( tmp ); OT_SETPORT ( &peer, &port ); } else if(!byte_diff(data,4,"left")) { size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) goto e404; - if( !tmp ) peer.port_flags |= PEER_FLAG_SEEDING; + if( !tmp ) OT_FLAG( &peer ) |= PEER_FLAG_SEEDING; } else scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); break; @@ -218,10 +220,10 @@ e400: case -1: goto e404; case 7: - if(!byte_diff(data,7,"stopped")) peer.port_flags |= PEER_FLAG_STOPPED; + if(!byte_diff(data,7,"stopped")) OT_FLAG( &peer ) |= PEER_FLAG_STOPPED; break; case 9: - if(!byte_diff(data,9,"complete")) peer.port_flags |= PEER_FLAG_COMPLETED; + if(!byte_diff(data,9,"complete")) OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED; default: // Fall through intended break; } @@ -263,7 +265,7 @@ e400: /* Scanned whole query string */ if( !hash ) goto e404; - if( peer.port_flags & PEER_FLAG_STOPPED ) { + if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) { remove_peer_from_torrent( hash, &peer ); reply = strdup( "d15:warning message4:Okaye" ); reply_size = 26; } else { diff --git a/trackerlogic.c b/trackerlogic.c index e9b9b2e..7b3c5db 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -18,9 +18,7 @@ // Helper functions for binary_find // int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } -int compare_ip_port( const void *peer1, const void *peer2 ) { -if( ((ot_peer*)peer1)->ip != ((ot_peer*)peer2)->ip ) return ((ot_peer*)peer1)->ip - ((ot_peer*)peer2)->ip; -return ((ot_peer*)peer1)->port_flags - ((ot_peer*)peer2)->port_flags; } +int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( peer1, peer2, 6 ); } static void *binary_search( const void *key, const void *base, unsigned long member_count, const unsigned long member_size, @@ -82,7 +80,7 @@ static int vector_remove_peer( ot_vector *vector, ot_peer *peer ) { match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), compare_ip_port, &exactmatch ); if( !exactmatch ) return 0; - exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; + exactmatch = OT_FLAG( match ) & PEER_FLAG_SEEDING ? 2 : 1; MEMMOVE( match, match + 1, ((ot_peer*)vector->data) + vector->size - match - 1 ); vector->size--; return exactmatch; @@ -164,7 +162,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) { if( !exactmatch ) { int i; MEMMOVE( peer_dest, peer, sizeof( ot_peer ) ); - if( peer->port_flags & PEER_FLAG_SEEDING ) + if( OT_FLAG(peer) & PEER_FLAG_SEEDING ) torrent->peer_list->seed_count[0]++; for( i=1; ipeer_list->peers[i], peer ) ) { @@ -174,12 +172,12 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) { } } } else { - if( (peer_dest->port_flags & PEER_FLAG_SEEDING ) && !(peer->port_flags & PEER_FLAG_SEEDING ) ) + if( (OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) torrent->peer_list->seed_count[0]--; - if( !(peer_dest->port_flags & PEER_FLAG_SEEDING ) && (peer->port_flags & PEER_FLAG_SEEDING ) ) + if( !(OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) torrent->peer_list->seed_count[0]++; } - if( peer->port_flags & PEER_FLAG_COMPLETED ) + if( OT_FLAG(peer) & PEER_FLAG_COMPLETED ) torrent->peer_list->downloaded++; return torrent; diff --git a/trackerlogic.h b/trackerlogic.h index 44bd744..1bd7228 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -39,13 +39,16 @@ typedef struct { } ot_vector; typedef struct { - ot_ip ip; - ot_dword port_flags; + ot_byte data[8]; } ot_peer; static const ot_byte PEER_FLAG_SEEDING = 0x80; static const ot_byte PEER_FLAG_COMPLETED = 0x40; static const ot_byte PEER_FLAG_STOPPED = 0x20; +#define OT_SETIP( peer, ip ) MEMMOVE((peer),(ip),4); +#define OT_SETPORT( peer, port ) MEMMOVE((peer),(port),2); +#define OT_FLAG(peer) (((ot_byte*)(peer))[6]) + typedef struct { ot_time base; unsigned long seed_count[ OT_POOLS_COUNT ];