summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c2
-rw-r--r--ot_accesslist.c2
-rw-r--r--ot_clean.c2
-rw-r--r--ot_fullscrape.c6
-rw-r--r--ot_http.c12
-rw-r--r--ot_livesync.c11
-rw-r--r--ot_udp.c12
-rw-r--r--ot_vector.c5
-rw-r--r--trackerlogic.c52
-rw-r--r--trackerlogic.h17
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 ) {
165 io_wantread( i ); 165 io_wantread( i );
166 166
167 memset( h, 0, sizeof( struct http_data ) ); 167 memset( h, 0, sizeof( struct http_data ) );
168 memmove( h->ip, ip, sizeof( ip ) ); 168 WRITE32(h->ip,0,READ32(ip,0));
169 169
170 stats_issue_event( EVENT_ACCEPT, FLAG_TCP, ntohl(*(uint32_t*)ip)); 170 stats_issue_event( EVENT_ACCEPT, FLAG_TCP, ntohl(*(uint32_t*)ip));
171 171
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;
105int accesslist_blessip( char *ip, ot_permissions permissions ) { 105int accesslist_blessip( char *ip, ot_permissions permissions ) {
106 if( g_adminip_count >= OT_ADMINIP_MAX ) 106 if( g_adminip_count >= OT_ADMINIP_MAX )
107 return -1; 107 return -1;
108 memmove( g_adminip_addresses + g_adminip_count, ip, 4 ); 108 WRITE32(g_adminip_addresses + g_adminip_count,0,READ32(ip,0));
109 g_adminip_permissions[ g_adminip_count++ ] = permissions; 109 g_adminip_permissions[ g_adminip_count++ ] = permissions;
110#ifdef _DEBUG 110#ifdef _DEBUG
111 uint8_t *_ip = (uint8_t*)ip; 111 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
35 OT_PEERTIME( peers ) = timediff; 35 OT_PEERTIME( peers ) = timediff;
36 *(uint64_t*)(insert_point++) = *(uint64_t*)(peers++); 36 *(uint64_t*)(insert_point++) = *(uint64_t*)(peers++);
37 } else 37 } else
38 if( OT_FLAG( peers++ ) & PEER_FLAG_SEEDING ) 38 if( OT_PEERFLAG( peers++ ) & PEER_FLAG_SEEDING )
39 (*removed_seeders)++; 39 (*removed_seeders)++;
40 40
41 return peers - insert_point; 41 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,
113} 113}
114 114
115static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) { 115static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tasktype mode ) {
116 int bucket; 116 int bucket,i;
117 char *r, *re; 117 char *r, *re;
118#ifdef WANT_COMPRESSION_GZIP 118#ifdef WANT_COMPRESSION_GZIP
119 char compress_buffer[OT_SCRAPE_MAXENTRYLEN]; 119 char compress_buffer[OT_SCRAPE_MAXENTRYLEN];
@@ -163,7 +163,7 @@ static void fullscrape_make( int *iovec_entries, struct iovec **iovector, ot_tas
163 163
164 /* push hash as bencoded string */ 164 /* push hash as bencoded string */
165 *r++='2'; *r++='0'; *r++=':'; 165 *r++='2'; *r++='0'; *r++=':';
166 memmove( r, hash, 20 ); r+=20; 166 for(i=0;i<20;i+=4) WRITE32(r+=4,0,READ32(hash,i));
167 167
168 /* push rest of the scrape string */ 168 /* push rest of the scrape string */
169 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 ); 169 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
174 r += sprintf( r, ":%zd:%zd\n", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count ); 174 r += sprintf( r, ":%zd:%zd\n", peer_list->seed_count, peer_list->peer_count-peer_list->seed_count );
175 break; 175 break;
176 case TASK_FULLSCRAPE_TPB_BINARY: 176 case TASK_FULLSCRAPE_TPB_BINARY:
177 memmove( r, hash, 20 ); r+=20; 177 for(i=0;i<20;i+=4) WRITE32(r+=4,0,READ32(hash,i));
178 *(uint32_t*)r++ = htonl( (uint32_t)peer_list->seed_count ); 178 *(uint32_t*)r++ = htonl( (uint32_t)peer_list->seed_count );
179 *(uint32_t*)r++ = htonl( (uint32_t)( peer_list->peer_count-peer_list->seed_count) ); 179 *(uint32_t*)r++ = htonl( (uint32_t)( peer_list->peer_count-peer_list->seed_count) );
180 break; 180 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 ) {
399 399
400 OT_SETIP( &peer, ((struct http_data*)io_getcookie( client_socket ) )->ip ); 400 OT_SETIP( &peer, ((struct http_data*)io_getcookie( client_socket ) )->ip );
401 OT_SETPORT( &peer, &port ); 401 OT_SETPORT( &peer, &port );
402 OT_FLAG( &peer ) = 0; 402 OT_PEERFLAG( &peer ) = 0;
403 numwant = 50; 403 numwant = 50;
404 scanon = 1; 404 scanon = 1;
405 405
@@ -427,7 +427,7 @@ static ssize_t http_handle_announce( const int64 client_socket, char *data ) {
427 } else if( !byte_diff( data, 4, "left" ) ) { 427 } else if( !byte_diff( data, 4, "left" ) ) {
428 if( ( len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM; 428 if( ( len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ) ) <= 0 ) HTTPERROR_400_PARAM;
429 if( scan_fixed_int( data, len, &tmp ) ) tmp = 0; 429 if( scan_fixed_int( data, len, &tmp ) ) tmp = 0;
430 if( !tmp ) OT_FLAG( &peer ) |= PEER_FLAG_SEEDING; 430 if( !tmp ) OT_PEERFLAG( &peer ) |= PEER_FLAG_SEEDING;
431 } else 431 } else
432 scan_urlencoded_skipvalue( &c ); 432 scan_urlencoded_skipvalue( &c );
433 break; 433 break;
@@ -438,10 +438,10 @@ static ssize_t http_handle_announce( const int64 client_socket, char *data ) {
438 case -1: 438 case -1:
439 HTTPERROR_400_PARAM; 439 HTTPERROR_400_PARAM;
440 case 7: 440 case 7:
441 if( !byte_diff( data, 7, "stopped" ) ) OT_FLAG( &peer ) |= PEER_FLAG_STOPPED; 441 if( !byte_diff( data, 7, "stopped" ) ) OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED;
442 break; 442 break;
443 case 9: 443 case 9:
444 if( !byte_diff( data, 9, "completed" ) ) OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED; 444 if( !byte_diff( data, 9, "completed" ) ) OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED;
445 default: /* Fall through intended */ 445 default: /* Fall through intended */
446 break; 446 break;
447 } 447 }
@@ -481,7 +481,7 @@ static ssize_t http_handle_announce( const int64 client_socket, char *data ) {
481 if( !hash ) 481 if( !hash )
482 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" ); 482 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" );
483 483
484 if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) 484 if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED )
485 len = remove_peer_from_torrent( hash, &peer, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, FLAG_TCP ); 485 len = remove_peer_from_torrent( hash, &peer, SUCCESS_HTTP_HEADER_LENGTH + static_outbuf, FLAG_TCP );
486 else { 486 else {
487 torrent = add_peer_to_torrent( hash, &peer WANT_SYNC_PARAM( 0 ) ); 487 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_
498#ifdef _DEBUG_HTTPERROR 498#ifdef _DEBUG_HTTPERROR
499 if( recv_length >= sizeof( debug_request ) ) 499 if( recv_length >= sizeof( debug_request ) )
500 recv_length = sizeof( debug_request) - 1; 500 recv_length = sizeof( debug_request) - 1;
501 memcpy( debug_request, recv_header, recv_length ); 501 memmove( debug_request, recv_header, recv_length );
502 debug_request[ recv_length ] = 0; 502 debug_request[ recv_length ] = 0;
503#endif 503#endif
504 504
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( ) {
94 94
95/* Inform live sync about whats going on. */ 95/* Inform live sync about whats going on. */
96void livesync_tell( ot_hash * const info_hash, const ot_peer * const peer ) { 96void livesync_tell( ot_hash * const info_hash, const ot_peer * const peer ) {
97 memmove( livesync_outbuffer_pos , info_hash, sizeof(ot_hash)); 97 int i;
98 memmove( livesync_outbuffer_pos + sizeof(ot_hash), peer, sizeof(ot_peer)); 98 for(i=0;i<20;i+=4) WRITE32(livesync_outbuffer_pos+=4,0,READ32(info_hash,i));
99 99 WRITE32(livesync_outbuffer_pos+=4,0,READ32(peer,0));
100 livesync_outbuffer_pos += sizeof(ot_hash) + sizeof(ot_peer); 100 WRITE32(livesync_outbuffer_pos+=4,0,READ32(peer,4));
101
101 if( livesync_outbuffer_pos >= livesync_outbuffer_highwater ) 102 if( livesync_outbuffer_pos >= livesync_outbuffer_highwater )
102 livesync_issuepacket(); 103 livesync_issuepacket();
103} 104}
@@ -146,7 +147,7 @@ static void * livesync_worker( void * args ) {
146 ot_peer *peer = (ot_peer*)(livesync_inbuffer + off + sizeof(ot_hash)); 147 ot_peer *peer = (ot_peer*)(livesync_inbuffer + off + sizeof(ot_hash));
147 ot_hash *hash = (ot_hash*)(livesync_inbuffer + off); 148 ot_hash *hash = (ot_hash*)(livesync_inbuffer + off);
148 149
149 if( OT_FLAG(peer) & PEER_FLAG_STOPPED ) 150 if( OT_PEERFLAG(peer) & PEER_FLAG_STOPPED )
150 remove_peer_from_torrent(hash, peer, NULL, FLAG_MCA); 151 remove_peer_from_torrent(hash, peer, NULL, FLAG_MCA);
151 else 152 else
152 add_peer_to_torrent( hash, peer WANT_SYNC_PARAM(1)); 153 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 ) {
27 (void)remoteip; 27 (void)remoteip;
28 28
29 /* Use a static secret for now */ 29 /* Use a static secret for now */
30 memcpy( connid, g_static_connid, 8 ); 30 memmove( connid, g_static_connid, 8 );
31} 31}
32 32
33static int udp_test_connectionid( const uint32_t * const connid, const char * remoteip ) { 33static int udp_test_connectionid( const uint32_t * const connid, const char * remoteip ) {
@@ -94,21 +94,21 @@ void handle_udp4( int64 serversocket ) {
94 94
95 OT_SETIP( &peer, remoteip ); 95 OT_SETIP( &peer, remoteip );
96 OT_SETPORT( &peer, &port ); 96 OT_SETPORT( &peer, &port );
97 OT_FLAG( &peer ) = 0; 97 OT_PEERFLAG( &peer ) = 0;
98 98
99 switch( event ) { 99 switch( event ) {
100 case 1: OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED; break; 100 case 1: OT_PEERFLAG( &peer ) |= PEER_FLAG_COMPLETED; break;
101 case 3: OT_FLAG( &peer ) |= PEER_FLAG_STOPPED; break; 101 case 3: OT_PEERFLAG( &peer ) |= PEER_FLAG_STOPPED; break;
102 default: break; 102 default: break;
103 } 103 }
104 104
105 if( !left ) 105 if( !left )
106 OT_FLAG( &peer ) |= PEER_FLAG_SEEDING; 106 OT_PEERFLAG( &peer ) |= PEER_FLAG_SEEDING;
107 107
108 outpacket[0] = htonl( 1 ); /* announce action */ 108 outpacket[0] = htonl( 1 ); /* announce action */
109 outpacket[1] = inpacket[12/4]; 109 outpacket[1] = inpacket[12/4];
110 110
111 if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */ 111 if( OT_PEERFLAG( &peer ) & PEER_FLAG_STOPPED ) /* Peer is gone. */
112 r = remove_peer_from_torrent( hash, &peer, static_outbuf, FLAG_UDP ); 112 r = remove_peer_from_torrent( hash, &peer, static_outbuf, FLAG_UDP );
113 else { 113 else {
114 torrent = add_peer_to_torrent( hash, &peer WANT_SYNC_PARAM( 0 ) ); 114 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 @@
16#include "uint32.h" 16#include "uint32.h"
17#include "uint16.h" 17#include "uint16.h"
18 18
19#define READ16(addr,offs) ((int16_t)uint16_read((offs)+(uint8_t*)(addr)))
20#define READ32(addr,offs) ((int32_t)uint32_read((offs)+(uint8_t*)(addr)))
21
22static int vector_compare_peer(const void *peer1, const void *peer2 ) { 19static int vector_compare_peer(const void *peer1, const void *peer2 ) {
23 int32_t cmp = READ32(peer1,0) - READ32(peer2,0); 20 int32_t cmp = READ32(peer1,0) - READ32(peer2,0);
24 if (cmp == 0) cmp = READ16(peer1,4) - READ16(peer2,4); 21 if (cmp == 0) cmp = READ16(peer1,4) - READ16(peer2,4);
@@ -166,7 +163,7 @@ int vector_remove_peer( ot_vector *vector, ot_peer *peer ) {
166 match = binary_search_peer( peer, vector->data, vector->size, &exactmatch ); 163 match = binary_search_peer( peer, vector->data, vector->size, &exactmatch );
167 if( !exactmatch ) return 0; 164 if( !exactmatch ) return 0;
168 165
169 exactmatch = ( OT_FLAG( match ) & PEER_FLAG_SEEDING ) ? 2 : 1; 166 exactmatch = ( OT_PEERFLAG( match ) & PEER_FLAG_SEEDING ) ? 2 : 1;
170 memmove( match, match + 1, sizeof(ot_peer) * ( end - match - 1 ) ); 167 memmove( match, match + 1, sizeof(ot_peer) * ( end - match - 1 ) );
171 168
172 vector->size--; 169 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(
62 62
63 if( !exactmatch ) { 63 if( !exactmatch ) {
64 /* Create a new torrent entry, then */ 64 /* Create a new torrent entry, then */
65 memmove( &torrent->hash, hash, sizeof( ot_hash ) ); 65 int i; for(i=0;i<20;i+=4) WRITE32(&torrent->hash,i,READ32(hash,i));
66 66
67 if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { 67 if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) {
68 vector_remove_torrent( torrents_list, torrent ); 68 vector_remove_torrent( torrents_list, torrent );
69 mutex_bucket_unlock_by_hash( hash ); 69 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(
86 OT_PEERTIME( peer ) = 0; 86 OT_PEERTIME( peer ) = 0;
87 87
88 /* Sanitize flags: Whoever claims to have completed download, must be a seeder */ 88 /* Sanitize flags: Whoever claims to have completed download, must be a seeder */
89 if( ( OT_FLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) == PEER_FLAG_COMPLETED ) 89 if( ( OT_PEERFLAG( peer ) & ( PEER_FLAG_COMPLETED | PEER_FLAG_SEEDING ) ) == PEER_FLAG_COMPLETED )
90 OT_FLAG( peer ) ^= PEER_FLAG_COMPLETED; 90 OT_PEERFLAG( peer ) ^= PEER_FLAG_COMPLETED;
91 91
92 /* If we hadn't had a match create peer there */ 92 /* If we hadn't had a match create peer there */
93 if( !exactmatch ) { 93 if( !exactmatch ) {
@@ -96,13 +96,13 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
96 if( !from_sync ) 96 if( !from_sync )
97 livesync_tell( hash, peer ); 97 livesync_tell( hash, peer );
98 else 98 else
99 OT_FLAG( peer ) |= PEER_FLAG_FROM_SYNC; 99 OT_PEERFLAG( peer ) |= PEER_FLAG_FROM_SYNC;
100#endif 100#endif
101 101
102 torrent->peer_list->peer_count++; 102 torrent->peer_list->peer_count++;
103 if( OT_FLAG(peer) & PEER_FLAG_COMPLETED ) 103 if( OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED )
104 torrent->peer_list->down_count++; 104 torrent->peer_list->down_count++;
105 if( OT_FLAG(peer) & PEER_FLAG_SEEDING ) 105 if( OT_PEERFLAG(peer) & PEER_FLAG_SEEDING )
106 torrent->peer_list->seed_count++; 106 torrent->peer_list->seed_count++;
107 107
108 } else { 108 } else {
@@ -114,7 +114,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
114 int i; 114 int i;
115 for( i=0;i<20;++i)printf("%02X",(*hash)[i]); 115 for( i=0;i<20;++i)printf("%02X",(*hash)[i]);
116 if( g_this_peerid_data ) g_this_peerid_data[g_this_peerid_len] = 0; 116 if( g_this_peerid_data ) g_this_peerid_data[g_this_peerid_len] = 0;
117 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 : "-" ); 117 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 : "-" );
118 } 118 }
119#endif 119#endif
120 120
@@ -123,19 +123,19 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer WANT_SYNC_PARAM(
123 fresh "completed" reports */ 123 fresh "completed" reports */
124 if( !from_sync ) { 124 if( !from_sync ) {
125 if( OT_PEERTIME( peer_dest ) > OT_CLIENT_SYNC_RENEW_BOUNDARY || 125 if( OT_PEERTIME( peer_dest ) > OT_CLIENT_SYNC_RENEW_BOUNDARY ||
126 ( !(OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_FLAG(peer) & PEER_FLAG_COMPLETED ) ) ) 126 ( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) ) )
127 livesync_tell( hash, peer ); 127 livesync_tell( hash, peer );
128 } 128 }
129#endif 129#endif
130 130
131 if( (OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) 131 if( (OT_PEERFLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) )
132 torrent->peer_list->seed_count--; 132 torrent->peer_list->seed_count--;
133 if( !(OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_FLAG(peer) & PEER_FLAG_SEEDING ) ) 133 if( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_PEERFLAG(peer) & PEER_FLAG_SEEDING ) )
134 torrent->peer_list->seed_count++; 134 torrent->peer_list->seed_count++;
135 if( !(OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_FLAG(peer) & PEER_FLAG_COMPLETED ) ) 135 if( !(OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED ) && (OT_PEERFLAG(peer) & PEER_FLAG_COMPLETED ) )
136 torrent->peer_list->down_count++; 136 torrent->peer_list->down_count++;
137 if( OT_FLAG(peer_dest) & PEER_FLAG_COMPLETED ) 137 if( OT_PEERFLAG(peer_dest) & PEER_FLAG_COMPLETED )
138 OT_FLAG( peer ) |= PEER_FLAG_COMPLETED; 138 OT_PEERFLAG( peer ) |= PEER_FLAG_COMPLETED;
139 } 139 }
140 140
141 *(uint64_t*)(peer_dest) = *(uint64_t*)(peer); 141 *(uint64_t*)(peer_dest) = *(uint64_t*)(peer);
@@ -163,8 +163,10 @@ static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) {
163 for( bucket = 0; bucket<num_buckets; ++bucket ) { 163 for( bucket = 0; bucket<num_buckets; ++bucket ) {
164 ot_peer * peers = (ot_peer*)bucket_list[bucket].data; 164 ot_peer * peers = (ot_peer*)bucket_list[bucket].data;
165 size_t peer_count = bucket_list[bucket].size; 165 size_t peer_count = bucket_list[bucket].size;
166 while( peer_count-- ) 166 while( peer_count-- ) {
167 memmove( r+=6, peers++, 6 ); 167 WRITE32(r+=4,0,READ32(peers,0));
168 WRITE16(r+=2,0,READ16(peers++,4));
169 }
168 } 170 }
169 171
170 return r - reply; 172 return r - reply;
@@ -194,6 +196,8 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
194 bucket_offset = random() % peer_list->peer_count; 196 bucket_offset = random() % peer_list->peer_count;
195 197
196 while( amount-- ) { 198 while( amount-- ) {
199 ot_peer * peer;
200
197 /* This is the aliased, non shifted range, next value may fall into */ 201 /* This is the aliased, non shifted range, next value may fall into */
198 unsigned int diff = ( ( ( amount + 1 ) * shifted_step ) >> shift ) - 202 unsigned int diff = ( ( ( amount + 1 ) * shifted_step ) >> shift ) -
199 ( ( amount * shifted_step ) >> shift ); 203 ( ( amount * shifted_step ) >> shift );
@@ -203,9 +207,9 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha
203 bucket_offset -= bucket_list[bucket_index].size; 207 bucket_offset -= bucket_list[bucket_index].size;
204 bucket_index = ( bucket_index + 1 ) % num_buckets; 208 bucket_index = ( bucket_index + 1 ) % num_buckets;
205 } 209 }
206 210 peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset;
207 memmove( r, ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset, 6 ); 211 WRITE32(r+=4,0,READ32(peer,0));
208 r += 6; 212 WRITE16(r+=2,0,READ16(peer,4));
209 } 213 }
210 return r - reply; 214 return r - reply;
211} 215}
@@ -288,9 +292,11 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash_list, int amount, char *repl
288 if( clean_single_torrent( torrent ) ) { 292 if( clean_single_torrent( torrent ) ) {
289 vector_remove_torrent( torrents_list, torrent ); 293 vector_remove_torrent( torrents_list, torrent );
290 } else { 294 } else {
291 memmove( r, "20:", 3 ); memmove( r+3, hash, 20 ); 295 int j;
292 r += sprintf( r+23, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", 296 *r++='2';*r++='0';*r++=':';
293 torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count ) + 23; 297 for(j=0;j<20;j+=4) WRITE32(r+=4,0,READ32(hash,i));
298 r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee",
299 torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count );
294 } 300 }
295 } 301 }
296 mutex_bucket_unlock_by_hash( hash ); 302 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
310 316
311#ifdef WANT_SYNC_LIVE 317#ifdef WANT_SYNC_LIVE
312 if( proto != FLAG_MCA ) { 318 if( proto != FLAG_MCA ) {
313 OT_FLAG( peer ) |= PEER_FLAG_STOPPED; 319 OT_PEERFLAG( peer ) |= PEER_FLAG_STOPPED;
314 livesync_tell( hash, peer ); 320 livesync_tell( hash, peer );
315 } 321 }
316#endif 322#endif
diff --git a/trackerlogic.h b/trackerlogic.h
index 81bd97a..1705928 100644
--- a/trackerlogic.h
+++ b/trackerlogic.h
@@ -11,6 +11,15 @@
11#include <time.h> 11#include <time.h>
12#include <stdint.h> 12#include <stdint.h>
13 13
14/* Libowfat */
15#include <uint16.h>
16#include <uint32.h>
17
18#define READ16(addr,offs) ((int16_t)uint16_read((offs)+(uint8_t*)(addr)))
19#define READ32(addr,offs) ((int32_t)uint32_read((offs)+(uint8_t*)(addr)))
20#define WRITE16(addr,offs,val) uint16_pack((offs)+(uint8_t*)(addr),(val))
21#define WRITE32(addr,offs,val) uint32_pack((offs)+(uint8_t*)(addr),(val))
22
14typedef uint8_t ot_hash[20]; 23typedef uint8_t ot_hash[20];
15typedef time_t ot_time; 24typedef time_t ot_time;
16 25
@@ -55,10 +64,10 @@ static const uint8_t PEER_FLAG_STOPPED = 0x20;
55static const uint8_t PEER_FLAG_FROM_SYNC = 0x10; 64static const uint8_t PEER_FLAG_FROM_SYNC = 0x10;
56static const uint8_t PEER_FLAG_LEECHING = 0x00; 65static const uint8_t PEER_FLAG_LEECHING = 0x00;
57 66
58#define OT_SETIP( peer, ip ) memmove((peer),(ip),4); 67#define OT_SETIP(peer,ip) WRITE32((peer),0,READ32((ip),0))
59#define OT_SETPORT( peer, port ) memmove(((uint8_t*)peer)+4,(port),2); 68#define OT_SETPORT(peer,port) WRITE16((peer),4,READ16((port),0))
60#define OT_FLAG(peer) (((uint8_t*)(peer))[6]) 69#define OT_PEERFLAG(peer) (((uint8_t*)(peer))[6])
61#define OT_PEERTIME(peer) (((uint8_t*)(peer))[7]) 70#define OT_PEERTIME(peer) (((uint8_t*)(peer))[7])
62 71
63#define OT_PEER_COMPARE_SIZE ((size_t)6) 72#define OT_PEER_COMPARE_SIZE ((size_t)6)
64#define OT_HASH_COMPARE_SIZE (sizeof(ot_hash)) 73#define OT_HASH_COMPARE_SIZE (sizeof(ot_hash))