summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--opentracker.c18
-rw-r--r--trackerlogic.c14
-rw-r--r--trackerlogic.h7
3 files changed, 21 insertions, 18 deletions
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)
114 ot_torrent *torrent; 114 ot_torrent *torrent;
115 ot_hash *hash = NULL; 115 ot_hash *hash = NULL;
116 int numwant, tmp, scanon; 116 int numwant, tmp, scanon;
117 unsigned short port = htons(6881);
117 size_t reply_size = 0; 118 size_t reply_size = 0;
118 119
119 array_cat0(&h->r); 120 array_cat0(&h->r);
@@ -187,8 +188,9 @@ e400:
187 if( byte_diff(data,8,"announce")) 188 if( byte_diff(data,8,"announce"))
188 goto e404; 189 goto e404;
189 190
190 peer.ip = h->ip; 191 OT_SETIP( &peer, &h->ip);
191 peer.port_flags = 6881 << 16; 192 OT_SETPORT( &peer, &port );
193 OT_FLAG( &peer ) = 0;
192 numwant = 50; 194 numwant = 50;
193 scanon = 1; 195 scanon = 1;
194 196
@@ -202,12 +204,12 @@ e400:
202 case 4: 204 case 4:
203 if(!byte_diff(data,4,"port")) { 205 if(!byte_diff(data,4,"port")) {
204 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 206 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
205 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || (tmp > 65536) ) goto e404; 207 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) || ( tmp > 0xffff ) ) goto e404;
206 peer.port_flags = ( tmp << 16 ) | ( peer.port_flags & 0xffff ); 208 port = htons( tmp ); OT_SETPORT ( &peer, &port );
207 } else if(!byte_diff(data,4,"left")) { 209 } else if(!byte_diff(data,4,"left")) {
208 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE ); 210 size_t len = scan_urlencoded_query( &c, data = c, SCAN_SEARCHPATH_VALUE );
209 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) goto e404; 211 if( ( len <= 0 ) || scan_fixed_int( data, len, &tmp ) ) goto e404;
210 if( !tmp ) peer.port_flags |= PEER_FLAG_SEEDING; 212 if( !tmp ) OT_FLAG( &peer ) |= PEER_FLAG_SEEDING;
211 } else 213 } else
212 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE ); 214 scan_urlencoded_query( &c, NULL, SCAN_SEARCHPATH_VALUE );
213 break; 215 break;
@@ -218,10 +220,10 @@ e400:
218 case -1: 220 case -1:
219 goto e404; 221 goto e404;
220 case 7: 222 case 7:
221 if(!byte_diff(data,7,"stopped")) peer.port_flags |= PEER_FLAG_STOPPED; 223 if(!byte_diff(data,7,"stopped")) OT_FLAG( &peer ) |= PEER_FLAG_STOPPED;
222 break; 224 break;
223 case 9: 225 case 9:
224 if(!byte_diff(data,9,"complete")) peer.port_flags |= PEER_FLAG_COMPLETED; 226 if(!byte_diff(data,9,"complete")) OT_FLAG( &peer ) |= PEER_FLAG_COMPLETED;
225 default: // Fall through intended 227 default: // Fall through intended
226 break; 228 break;
227 } 229 }
@@ -263,7 +265,7 @@ e400:
263 /* Scanned whole query string */ 265 /* Scanned whole query string */
264 if( !hash ) goto e404; 266 if( !hash ) goto e404;
265 267
266 if( peer.port_flags & PEER_FLAG_STOPPED ) { 268 if( OT_FLAG( &peer ) & PEER_FLAG_STOPPED ) {
267 remove_peer_from_torrent( hash, &peer ); 269 remove_peer_from_torrent( hash, &peer );
268 reply = strdup( "d15:warning message4:Okaye" ); reply_size = 26; 270 reply = strdup( "d15:warning message4:Okaye" ); reply_size = 26;
269 } else { 271 } else {
diff --git a/trackerlogic.c b/trackerlogic.c
index e9b9b2e..7b3c5db 100644
--- a/trackerlogic.c
+++ b/trackerlogic.c
@@ -18,9 +18,7 @@
18// Helper functions for binary_find 18// Helper functions for binary_find
19// 19//
20int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); } 20int compare_hash( const void *hash1, const void *hash2 ) { return memcmp( hash1, hash2, sizeof( ot_hash )); }
21int compare_ip_port( const void *peer1, const void *peer2 ) { 21int compare_ip_port( const void *peer1, const void *peer2 ) { return memcmp( peer1, peer2, 6 ); }
22if( ((ot_peer*)peer1)->ip != ((ot_peer*)peer2)->ip ) return ((ot_peer*)peer1)->ip - ((ot_peer*)peer2)->ip;
23return ((ot_peer*)peer1)->port_flags - ((ot_peer*)peer2)->port_flags; }
24 22
25static void *binary_search( const void *key, const void *base, 23static void *binary_search( const void *key, const void *base,
26 unsigned long member_count, const unsigned long member_size, 24 unsigned long member_count, const unsigned long member_size,
@@ -82,7 +80,7 @@ static int vector_remove_peer( ot_vector *vector, ot_peer *peer ) {
82 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), compare_ip_port, &exactmatch ); 80 match = BINARY_FIND( peer, vector->data, vector->size, sizeof( ot_peer ), compare_ip_port, &exactmatch );
83 81
84 if( !exactmatch ) return 0; 82 if( !exactmatch ) return 0;
85 exactmatch = match->port_flags & PEER_FLAG_SEEDING ? 2 : 1; 83 exactmatch = OT_FLAG( match ) & PEER_FLAG_SEEDING ? 2 : 1;
86 MEMMOVE( match, match + 1, ((ot_peer*)vector->data) + vector->size - match - 1 ); 84 MEMMOVE( match, match + 1, ((ot_peer*)vector->data) + vector->size - match - 1 );
87 vector->size--; 85 vector->size--;
88 return exactmatch; 86 return exactmatch;
@@ -164,7 +162,7 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
164 if( !exactmatch ) { 162 if( !exactmatch ) {
165 int i; 163 int i;
166 MEMMOVE( peer_dest, peer, sizeof( ot_peer ) ); 164 MEMMOVE( peer_dest, peer, sizeof( ot_peer ) );
167 if( peer->port_flags & PEER_FLAG_SEEDING ) 165 if( OT_FLAG(peer) & PEER_FLAG_SEEDING )
168 torrent->peer_list->seed_count[0]++; 166 torrent->peer_list->seed_count[0]++;
169 for( i=1; i<OT_POOLS_COUNT; ++i ) { 167 for( i=1; i<OT_POOLS_COUNT; ++i ) {
170 switch( vector_remove_peer( &torrent->peer_list->peers[i], peer ) ) { 168 switch( vector_remove_peer( &torrent->peer_list->peers[i], peer ) ) {
@@ -174,12 +172,12 @@ ot_torrent *add_peer_to_torrent( ot_hash *hash, ot_peer *peer ) {
174 } 172 }
175 } 173 }
176 } else { 174 } else {
177 if( (peer_dest->port_flags & PEER_FLAG_SEEDING ) && !(peer->port_flags & PEER_FLAG_SEEDING ) ) 175 if( (OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && !(OT_FLAG(peer) & PEER_FLAG_SEEDING ) )
178 torrent->peer_list->seed_count[0]--; 176 torrent->peer_list->seed_count[0]--;
179 if( !(peer_dest->port_flags & PEER_FLAG_SEEDING ) && (peer->port_flags & PEER_FLAG_SEEDING ) ) 177 if( !(OT_FLAG(peer_dest) & PEER_FLAG_SEEDING ) && (OT_FLAG(peer) & PEER_FLAG_SEEDING ) )
180 torrent->peer_list->seed_count[0]++; 178 torrent->peer_list->seed_count[0]++;
181 } 179 }
182 if( peer->port_flags & PEER_FLAG_COMPLETED ) 180 if( OT_FLAG(peer) & PEER_FLAG_COMPLETED )
183 torrent->peer_list->downloaded++; 181 torrent->peer_list->downloaded++;
184 182
185 return torrent; 183 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 {
39} ot_vector; 39} ot_vector;
40 40
41typedef struct { 41typedef struct {
42 ot_ip ip; 42 ot_byte data[8];
43 ot_dword port_flags;
44} ot_peer; 43} ot_peer;
45static const ot_byte PEER_FLAG_SEEDING = 0x80; 44static const ot_byte PEER_FLAG_SEEDING = 0x80;
46static const ot_byte PEER_FLAG_COMPLETED = 0x40; 45static const ot_byte PEER_FLAG_COMPLETED = 0x40;
47static const ot_byte PEER_FLAG_STOPPED = 0x20; 46static const ot_byte PEER_FLAG_STOPPED = 0x20;
48 47
48#define OT_SETIP( peer, ip ) MEMMOVE((peer),(ip),4);
49#define OT_SETPORT( peer, port ) MEMMOVE((peer),(port),2);
50#define OT_FLAG(peer) (((ot_byte*)(peer))[6])
51
49typedef struct { 52typedef struct {
50 ot_time base; 53 ot_time base;
51 unsigned long seed_count[ OT_POOLS_COUNT ]; 54 unsigned long seed_count[ OT_POOLS_COUNT ];