From 08c71627839a9134367db354c8255f7a720e1ff3 Mon Sep 17 00:00:00 2001 From: erdgeist <> Date: Sat, 6 Dec 2008 18:46:00 +0000 Subject: Renamed OT_FLAG to OT_PEERFLAG to make code easier to read Introduced READ16/32 and WRITE16/32 makros to abstract loading/storing from unaligned addresses away on cpu's that can actually load/store everywhere Removed all unnecessary memmoves, especially where it only moved 6 bytes in inner loop. I replaced them with WRITE16/32(READ16/32()) makros --- opentracker.c | 2 +- ot_accesslist.c | 2 +- ot_clean.c | 2 +- ot_fullscrape.c | 6 +++--- ot_http.c | 12 ++++++------ ot_livesync.c | 11 ++++++----- ot_udp.c | 12 ++++++------ ot_vector.c | 5 +---- trackerlogic.c | 52 +++++++++++++++++++++++++++++----------------------- trackerlogic.h | 17 +++++++++++++---- 10 files changed, 67 insertions(+), 54 deletions(-) diff --git a/opentracker.c b/opentracker.c index f8587c9..d506085 100644 --- a/opentracker.c +++ b/opentracker.c @@ -165,7 +165,7 @@ static void handle_accept( const int64 serversocket ) { io_wantread( i ); memset( h, 0, sizeof( struct http_data ) ); - memmove( h->ip, ip, sizeof( ip ) ); + WRITE32(h->ip,0,READ32(ip,0)); stats_issue_event( EVENT_ACCEPT, FLAG_TCP, ntohl(*(uint32_t*)ip)); diff --git a/ot_accesslist.c b/ot_accesslist.c index d306aeb..f4c21c5 100644 --- a/ot_accesslist.c +++ b/ot_accesslist.c @@ -105,7 +105,7 @@ static unsigned int g_adminip_count = 0; int accesslist_blessip( char *ip, ot_permissions permissions ) { if( g_adminip_count >= OT_ADMINIP_MAX ) return -1; - memmove( g_adminip_addresses + g_adminip_count, ip, 4 ); + WRITE32(g_adminip_addresses + g_adminip_count,0,READ32(ip,0)); g_adminip_permissions[ g_adminip_count++ ] = permissions; #ifdef _DEBUG uint8_t *_ip = (uint8_t*)ip; diff --git a/ot_clean.c b/ot_clean.c index 5204be6..2173c27 100644 --- a/ot_clean.c +++ b/ot_clean.c @@ -35,7 +35,7 @@ static ssize_t clean_single_bucket( ot_peer *peers, size_t peer_count, time_t ti OT_PEERTIME( peers ) = timediff; *(uint64_t*)(insert_point++) = *(uint64_t*)(peers++); } else - if( OT_FLAG( peers++ ) & PEER_FLAG_SEEDING ) + if( OT_PEERFLAG( peers++ ) & PEER_FLAG_SEEDING ) (*removed_seeders)++; return peers - insert_point; diff --git a/ot_fullscrape.c b/ot_fullscrape.c index dfad640..5f61afd 100644 --- a/ot_fullscrape.c +++ b/ot_fullscrape.c @@ -113,7 +113,7 @@ static int fullscrape_increase( int *iovec_entries, struct iovec **iovector, } static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) { - int bucket; + int bucket,i; char *r, *re; #ifdef WANT_COMPRESSION_GZIP char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; @@ -163,7 +163,7 @@ static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tas /* push hash as bencoded string */ *r++='2'; *r++='0'; *r++=':'; - memmove( r, hash, 20 ); r+=20; + for(i=0;i<20;i+=4) WRITE32(r+=4,0,READ32(hash,i)); /* push rest of the scrape string */ r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", peer_list->seed_count, peer_list->down_count, peer_list->peer_count-peer_list->seed_count ); @@ -174,7 +174,7 @@ static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tas r += sprintf( r, ":%zd:%zd\n", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count ); break; case TASK_FULLSCRAPE_TPB_BINARY: - memmove( r, hash, 20 ); r+=20; + for(i=0;i<20;i+=4) WRITE32(r+=4,0,READ32(hash,i)); *(uint32_t*)r++ = htonl( (uint32_t)peer_list->seed_count ); *(uint32_t*)r++ = htonl( (uint32_t)( peer_list->peer_count-peer_list->seed_count) ); break; diff --git a/ot_http.c b/ot_http.c index aa4b3b3..d66fc80 100644 --- a/ot_http.c +++ b/ot_http.c @@ -399,7 +399,7 @@ static ssize_t http_handle_announce( const int64 client_socket, char *data ) { OT_SETIP( &peer, ((struct http_data*)io_getcookie( client_socket ) )->ip ); OT_SETPORT( &peer, &port ); - OT_FLAG( &peer ) = 0; + OT_PEERFLAG( &peer ) = 0; numwant = 50; scanon = 1; @@ -427,7 +427,7 @@ static ssize_t http_handle_announce( const int64 client_socket, char *data ) { } else if( !byte_diff( data, 4, "left" ) ) { if( ( len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM; if( scan_fixed_int( data, len, &tmp ) ) tmp = 0; - if( !tmp ) OT_FLAG( &peer ) |= PEER_FLAG_SEEDING; + if( !tmp ) OT_PEERFLAG( &peer ) |= PEER_FLAG_SEEDING; } else scan_urlencoded_skipvalue( &c ); break; @@ -438,10 +438,10 @@ static ssize_t http_handle_announce( const int64 client_socket, char *data ) { case -1: HTTPERROR_400_PARAM; case 7: - if( !byte_diff( data, 7, "stopped" ) ) OT_FLAG( &peer ) |= PEER_FLAG_STOPPED; + if( !byte_diff( data, 7, "stopped" ) ) OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED; break; case 9: - if( !byte_diff( data, 9, "completed" ) ) OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED; + if( !byte_diff( data, 9, "completed" ) ) OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED; default: /* Fall through intended */ break; } @@ -481,7 +481,7 @@ static ssize_t http_handle_announce( const int64 client_socket, char *data ) { if( !hash ) return sprintf( static_outbuf + SUCCESS_HTTP_HEADER_LENGTH, "d14:failure reason80:Your client forgot to send your torrent's info_hash. Please upgrade your client.e" ); - if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) + if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) len = remove_peer_from_torrent( hash, &peer, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, FLAG_TCP ); else { torrent = add_peer_to_torrent( hash, &peer WANT_SYNC_PARAM( 0 ) ); @@ -498,7 +498,7 @@ ssize_t http_handle_request( const int64 client_socket, char *data, size_t recv_ #ifdef _DEBUG_HTTPERROR if( recv_length >= sizeof( debug_request ) ) recv_length = sizeof( debug_request) - 1; - memcpy( debug_request, recv_header, recv_length ); + memmove( debug_request, recv_header, recv_length ); debug_request[ recv_length ] = 0; #endif diff --git a/ot_livesync.c b/ot_livesync.c index 1b1efe0..b447e83 100644 --- a/ot_livesync.c +++ b/ot_livesync.c @@ -94,10 +94,11 @@ static void livesync_issuepacket( ) { /* Inform live sync about whats going on. */ void livesync_tell( ot_hash * const info_hash, const ot_peer * const peer ) { - memmove( livesync_outbuffer_pos , info_hash, sizeof(ot_hash)); - memmove( livesync_outbuffer_pos + sizeof(ot_hash), peer, sizeof(ot_peer)); - - livesync_outbuffer_pos += sizeof(ot_hash) + sizeof(ot_peer); + int i; + for(i=0;i<20;i+=4) WRITE32(livesync_outbuffer_pos+=4,0,READ32(info_hash,i)); + WRITE32(livesync_outbuffer_pos+=4,0,READ32(peer,0)); + WRITE32(livesync_outbuffer_pos+=4,0,READ32(peer,4)); + if( livesync_outbuffer_pos >= livesync_outbuffer_highwater ) livesync_issuepacket(); } @@ -146,7 +147,7 @@ static void * livesync_worker( void * args ) { ot_peer *peer = (ot_peer*)(livesync_inbuffer + off + sizeof(ot_hash)); ot_hash *hash = (ot_hash*)(livesync_inbuffer + off); - if( OT_FLAG(peer) & PEER_FLAG_STOPPED ) + if( OT_PEERFLAG(peer) & PEER_FLAG_STOPPED ) remove_peer_from_torrent(hash, peer, NULL, FLAG_MCA); else add_peer_to_torrent( hash, peer WANT_SYNC_PARAM(1)); diff --git a/ot_udp.c b/ot_udp.c index 764281c..8f55963 100644 --- a/ot_udp.c +++ b/ot_udp.c @@ -27,7 +27,7 @@ static void udp_make_connectionid( uint32_t * connid, const char * remoteip ) { (void)remoteip; /* Use a static secret for now */ - memcpy( connid, g_static_connid, 8 ); + memmove( connid, g_static_connid, 8 ); } static int udp_test_connectionid( const uint32_t * const connid, const char * remoteip ) { @@ -94,21 +94,21 @@ void handle_udp4( int64 serversocket ) { OT_SETIP( &peer, remoteip ); OT_SETPORT( &peer, &port ); - OT_FLAG( &peer ) = 0; + OT_PEERFLAG( &peer ) = 0; switch( event ) { - case 1: OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED; break; - case 3: OT_FLAG( &peer ) |= PEER_FLAG_STOPPED; break; + case 1: OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED; break; + case 3: OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED; break; default: break; } if( !left ) - OT_FLAG( &peer ) |= PEER_FLAG_SEEDING; + OT_PEERFLAG( &peer ) |= PEER_FLAG_SEEDING; outpacket[0] = htonl( 1 ); /* announce action */ outpacket[1] = inpacket[12/4]; - if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */ + if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */ r = remove_peer_from_torrent( hash, &peer, static_outbuf, FLAG_UDP ); else { torrent = add_peer_to_torrent( hash, &peer WANT_SYNC_PARAM( 0 ) ); diff --git a/ot_vector.c b/ot_vector.c index 7d2a1a9..154d1c8 100644 --- a/ot_vector.c +++ b/ot_vector.c @@ -16,9 +16,6 @@ #include "uint32.h" #include "uint16.h" -#define READ16(addr,offs) ((int16_t)uint16_read((offs)+(uint8_t*)(addr))) -#define READ32(addr,offs) ((int32_t)uint32_read((offs)+(uint8_t*)(addr))) - static int vector_compare_peer(const void *peer1, const void *peer2 ) { int32_t cmp = READ32(peer1,0) - READ32(peer2,0); if (cmp == 0) cmp = READ16(peer1,4) - READ16(peer2,4); @@ -166,7 +163,7 @@ int vector_remove_peer( ot_vector *vector, ot_peer *peer ) { match = binary_search_peer( peer, vector->data, vector->size, &exactmatch ); if( !exactmatch ) return 0; - exactmatch = ( OT_FLAG( match ) & PEER_FLAG_SEEDING ) ? 2 : 1; + exactmatch = ( OT_PEERFLAG( match ) & PEER_FLAG_SEEDING ) ? 2 : 1; memmove( match, match + 1, sizeof(ot_peer) * ( end - match - 1 ) ); vector->size--; diff --git a/trackerlogic.c b/trackerlogic.c index 4c6b5b0..d7fca69 100644 --- a/trackerlogic.c +++ b/trackerlogic.c @@ -62,8 +62,8 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM( if( !exactmatch ) { /* Create a new torrent entry, then */ - memmove( &torrent->hash, hash, sizeof( ot_hash ) ); - + int i; for(i=0;i<20;i+=4) WRITE32(&torrent->hash,i,READ32(hash,i)); + if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { vector_remove_torrent( torrents_list, torrent ); mutex_bucket_unlock_by_hash( hash ); @@ -86,8 +86,8 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM( OT_PEERTIME( peer ) = 0; /* Sanitize flags: Whoever claims to have completed download, must be a seeder */ - if( ( OT_FLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) == PEER_FLAG_COMPLETED ) - OT_FLAG( peer ) ^= PEER_FLAG_COMPLETED; + if( ( OT_PEERFLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) == PEER_FLAG_COMPLETED ) + OT_PEERFLAG( peer ) ^= PEER_FLAG_COMPLETED; /* If we hadn't had a match create peer there */ if( !exactmatch ) { @@ -96,13 +96,13 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM( if( !from_sync ) livesync_tell( hash, peer ); else - OT_FLAG( peer ) |= PEER_FLAG_FROM_SYNC; + OT_PEERFLAG( peer ) |= PEER_FLAG_FROM_SYNC; #endif torrent->peer_list->peer_count++; - if( OT_FLAG(peer) & PEER_FLAG_COMPLETED ) + if( OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) torrent->peer_list->down_count++; - if( OT_FLAG(peer) & PEER_FLAG_SEEDING ) + if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) torrent->peer_list->seed_count++; } else { @@ -114,7 +114,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM( int i; for( i=0;i<20;++i)printf("%02X",(*hash)[i]); if( g_this_peerid_data ) g_this_peerid_data[g_this_peerid_len] = 0; - printf( " %d.%d.%d.%d:%d\t%d %02X %s\n", _ip[0], _ip[1], _ip[2], _ip[3], OT_PEERTIME( peer_dest ), *(uint16_t*)( ((char*)peer_dest)+4 ), OT_FLAG(peer_dest), g_this_peerid_data ? g_this_peerid_data : "-" ); + printf( " %d.%d.%d.%d:%d\t%d %02X %s\n", _ip[0], _ip[1], _ip[2], _ip[3], OT_PEERTIME( peer_dest ), *(uint16_t*)( ((char*)peer_dest)+4 ), OT_PEERFLAG(peer_dest), g_this_peerid_data ? g_this_peerid_data : "-" ); } #endif @@ -123,19 +123,19 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM( fresh "completed" reports */ if( !from_sync ) { if( OT_PEERTIME( peer_dest ) > OT_CLIENT_SYNC_RENEW_BOUNDARY || - ( !(OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_FLAG(peer) & PEER_FLAG_COMPLETED ) ) ) + ( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) ) ) livesync_tell( hash, peer ); } #endif - if( (OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) + if( (OT_PEERFLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) ) torrent->peer_list->seed_count--; - if( !(OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) + if( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) ) torrent->peer_list->seed_count++; - if( !(OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_FLAG(peer) & PEER_FLAG_COMPLETED ) ) + if( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) ) torrent->peer_list->down_count++; - if( OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) - OT_FLAG( peer ) |= PEER_FLAG_COMPLETED; + if( OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED ) + OT_PEERFLAG( peer ) |= PEER_FLAG_COMPLETED; } *(uint64_t*)(peer_dest) = *(uint64_t*)(peer); @@ -163,8 +163,10 @@ static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) { for( bucket = 0; bucketpeer_count; while( amount-- ) { + ot_peer * peer; + /* This is the aliased, non shifted range, next value may fall into */ unsigned int diff = ( ( ( amount + 1 ) * shifted_step ) >> shift ) - ( ( amount * shifted_step ) >> shift ); @@ -203,9 +207,9 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha bucket_offset -= bucket_list[bucket_index].size; bucket_index = ( bucket_index + 1 ) % num_buckets; } - - memmove( r, ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset, 6 ); - r += 6; + peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset; + WRITE32(r+=4,0,READ32(peer,0)); + WRITE16(r+=2,0,READ16(peer,4)); } return r - reply; } @@ -288,9 +292,11 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash_list, int amount, char *repl if( clean_single_torrent( torrent ) ) { vector_remove_torrent( torrents_list, torrent ); } else { - memmove( r, "20:", 3 ); memmove( r+3, hash, 20 ); - r += sprintf( r+23, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", - torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count ) + 23; + int j; + *r++='2';*r++='0';*r++=':'; + for(j=0;j<20;j+=4) WRITE32(r+=4,0,READ32(hash,i)); + r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", + torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count ); } } mutex_bucket_unlock_by_hash( hash ); @@ -310,7 +316,7 @@ size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, PROT #ifdef WANT_SYNC_LIVE if( proto != FLAG_MCA ) { - OT_FLAG( peer ) |= PEER_FLAG_STOPPED; + OT_PEERFLAG( peer ) |= PEER_FLAG_STOPPED; livesync_tell( hash, peer ); } #endif diff --git a/trackerlogic.h b/trackerlogic.h index 81bd97a..1705928 100644 --- a/trackerlogic.h +++ b/trackerlogic.h @@ -11,6 +11,15 @@ #include #include +/* Libowfat */ +#include +#include + +#define READ16(addr,offs) ((int16_t)uint16_read((offs)+(uint8_t*)(addr))) +#define READ32(addr,offs) ((int32_t)uint32_read((offs)+(uint8_t*)(addr))) +#define WRITE16(addr,offs,val) uint16_pack((offs)+(uint8_t*)(addr),(val)) +#define WRITE32(addr,offs,val) uint32_pack((offs)+(uint8_t*)(addr),(val)) + typedef uint8_t ot_hash[20]; typedef time_t ot_time; @@ -55,10 +64,10 @@ static const uint8_t PEER_FLAG_STOPPED = 0x20; static const uint8_t PEER_FLAG_FROM_SYNC = 0x10; static const uint8_t PEER_FLAG_LEECHING = 0x00; -#define OT_SETIP( peer, ip ) memmove((peer),(ip),4); -#define OT_SETPORT( peer, port ) memmove(((uint8_t*)peer)+4,(port),2); -#define OT_FLAG(peer) (((uint8_t*)(peer))[6]) -#define OT_PEERTIME(peer) (((uint8_t*)(peer))[7]) +#define OT_SETIP(peer,ip) WRITE32((peer),0,READ32((ip),0)) +#define OT_SETPORT(peer,port) WRITE16((peer),4,READ16((port),0)) +#define OT_PEERFLAG(peer) (((uint8_t*)(peer))[6]) +#define OT_PEERTIME(peer) (((uint8_t*)(peer))[7]) #define OT_PEER_COMPARE_SIZE ((size_t)6) #define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) -- cgit v1.2.3