diff options
Diffstat (limited to 'trackerlogic.c')
| -rw-r--r-- | trackerlogic.c | 47 |
1 files changed, 15 insertions, 32 deletions
diff --git a/trackerlogic.c b/trackerlogic.c index 38be9f7..5eff5c0 100644 --- a/trackerlogic.c +++ b/trackerlogic.c | |||
| @@ -41,12 +41,7 @@ void free_peerlist( ot_peerlist *peer_list ) { | |||
| 41 | free( peer_list ); | 41 | free( peer_list ); |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | #ifdef _DEBUG_PEERID | 44 | size_t add_peer_to_torrent_and_return_peers( ot_hash hash, ot_peer *peer, PROTO_FLAG proto, size_t amount, char * reply ) { |
| 45 | extern size_t g_this_peerid_len; | ||
| 46 | extern char *g_this_peerid_data; | ||
| 47 | #endif | ||
| 48 | |||
| 49 | size_t add_peer_to_torrent_and_return_peers( ot_hash *hash, ot_peer *peer, PROTO_FLAG proto, size_t amount, char * reply ) { | ||
| 50 | int exactmatch, delta_torrentcount = 0; | 45 | int exactmatch, delta_torrentcount = 0; |
| 51 | size_t reply_size; | 46 | size_t reply_size; |
| 52 | ot_torrent *torrent; | 47 | ot_torrent *torrent; |
| @@ -66,7 +61,7 @@ size_t add_peer_to_torrent_and_return_peers( ot_hash *hash, ot_peer *peer, PROTO | |||
| 66 | 61 | ||
| 67 | if( !exactmatch ) { | 62 | if( !exactmatch ) { |
| 68 | /* Create a new torrent entry, then */ | 63 | /* Create a new torrent entry, then */ |
| 69 | int i; for(i=0;i<20;i+=4) WRITE32(&torrent->hash,i,READ32(hash,i)); | 64 | memcpy( torrent->hash, hash, sizeof(ot_hash) ); |
| 70 | 65 | ||
| 71 | if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { | 66 | if( !( torrent->peer_list = malloc( sizeof (ot_peerlist) ) ) ) { |
| 72 | vector_remove_torrent( torrents_list, torrent ); | 67 | vector_remove_torrent( torrents_list, torrent ); |
| @@ -114,16 +109,6 @@ size_t add_peer_to_torrent_and_return_peers( ot_hash *hash, ot_peer *peer, PROTO | |||
| 114 | } else { | 109 | } else { |
| 115 | stats_issue_event( EVENT_RENEW, 0, OT_PEERTIME( peer_dest ) ); | 110 | stats_issue_event( EVENT_RENEW, 0, OT_PEERTIME( peer_dest ) ); |
| 116 | 111 | ||
| 117 | #ifdef _DEBUG_PEERID | ||
| 118 | if( OT_PEERTIME( peer_dest ) < 2 ) { | ||
| 119 | uint8_t *_ip = (uint8_t*)peer_dest; | ||
| 120 | int i; | ||
| 121 | for( i=0;i<20;++i)printf("%02X",(*hash)[i]); | ||
| 122 | if( g_this_peerid_data ) g_this_peerid_data[g_this_peerid_len] = 0; | ||
| 123 | 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 : "-" ); | ||
| 124 | } | ||
| 125 | #endif | ||
| 126 | |||
| 127 | #ifdef WANT_SYNC_LIVE | 112 | #ifdef WANT_SYNC_LIVE |
| 128 | /* Won't live sync peers that come back too fast. Only exception: | 113 | /* Won't live sync peers that come back too fast. Only exception: |
| 129 | fresh "completed" reports */ | 114 | fresh "completed" reports */ |
| @@ -144,7 +129,7 @@ size_t add_peer_to_torrent_and_return_peers( ot_hash *hash, ot_peer *peer, PROTO | |||
| 144 | OT_PEERFLAG( peer ) |= PEER_FLAG_COMPLETED; | 129 | OT_PEERFLAG( peer ) |= PEER_FLAG_COMPLETED; |
| 145 | } | 130 | } |
| 146 | 131 | ||
| 147 | *(uint64_t*)(peer_dest) = *(uint64_t*)(peer); | 132 | *peer_dest = *peer; |
| 148 | #ifdef WANT_SYNC | 133 | #ifdef WANT_SYNC |
| 149 | if( proto == FLAG_MCA ) { | 134 | if( proto == FLAG_MCA ) { |
| 150 | mutex_bucket_unlock_by_hash( hash, delta_torrentcount ); | 135 | mutex_bucket_unlock_by_hash( hash, delta_torrentcount ); |
| @@ -153,7 +138,7 @@ size_t add_peer_to_torrent_and_return_peers( ot_hash *hash, ot_peer *peer, PROTO | |||
| 153 | #endif | 138 | #endif |
| 154 | 139 | ||
| 155 | reply_size = return_peers_for_torrent( torrent, amount, reply, proto ); | 140 | reply_size = return_peers_for_torrent( torrent, amount, reply, proto ); |
| 156 | mutex_bucket_unlock_by_hash( &torrent->hash, delta_torrentcount ); | 141 | mutex_bucket_unlock_by_hash( torrent->hash, delta_torrentcount ); |
| 157 | return reply_size; | 142 | return reply_size; |
| 158 | } | 143 | } |
| 159 | 144 | ||
| @@ -171,9 +156,9 @@ static size_t return_peers_all( ot_peerlist *peer_list, char *reply ) { | |||
| 171 | ot_peer * peers = (ot_peer*)bucket_list[bucket].data; | 156 | ot_peer * peers = (ot_peer*)bucket_list[bucket].data; |
| 172 | size_t peer_count = bucket_list[bucket].size; | 157 | size_t peer_count = bucket_list[bucket].size; |
| 173 | while( peer_count-- ) { | 158 | while( peer_count-- ) { |
| 174 | WRITE32(r,0,READ32(peers,0)); | 159 | memcpy(r,peers,OT_PEER_COMPARE_SIZE); |
| 175 | WRITE16(r,4,READ16(peers++,4)); | 160 | peers+=sizeof(ot_peer); |
| 176 | r+=6; | 161 | r+=OT_PEER_COMPARE_SIZE; |
| 177 | } | 162 | } |
| 178 | } | 163 | } |
| 179 | 164 | ||
| @@ -216,9 +201,8 @@ static size_t return_peers_selection( ot_peerlist *peer_list, size_t amount, cha | |||
| 216 | bucket_index = ( bucket_index + 1 ) % num_buckets; | 201 | bucket_index = ( bucket_index + 1 ) % num_buckets; |
| 217 | } | 202 | } |
| 218 | peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset; | 203 | peer = ((ot_peer*)bucket_list[bucket_index].data) + bucket_offset; |
| 219 | WRITE32(r,0,READ32(peer,0)); | 204 | memcpy(r,peer,OT_PEER_COMPARE_SIZE); |
| 220 | WRITE16(r,4,READ16(peer,4)); | 205 | r+=OT_PEER_COMPARE_SIZE; |
| 221 | r+=6; | ||
| 222 | } | 206 | } |
| 223 | return r - reply; | 207 | return r - reply; |
| 224 | } | 208 | } |
| @@ -236,7 +220,7 @@ size_t return_peers_for_torrent( ot_torrent *torrent, size_t amount, char *reply | |||
| 236 | 220 | ||
| 237 | if( proto == FLAG_TCP ) { | 221 | if( proto == FLAG_TCP ) { |
| 238 | int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; | 222 | int erval = OT_CLIENT_REQUEST_INTERVAL_RANDOM; |
| 239 | r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zde8:intervali%ie12:min intervali%ie5:peers%zd:", peer_list->seed_count, peer_list->down_count, peer_list->peer_count-peer_list->seed_count, erval, erval/2, 6*amount ); | 223 | r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zde8:intervali%ie12:min intervali%ie5:peers" PEERS6 "%zd:", peer_list->seed_count, peer_list->down_count, peer_list->peer_count-peer_list->seed_count, erval, erval/2, OT_PEER_COMPARE_SIZE*amount ); |
| 240 | } else { | 224 | } else { |
| 241 | *(uint32_t*)(r+0) = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); | 225 | *(uint32_t*)(r+0) = htonl( OT_CLIENT_REQUEST_INTERVAL_RANDOM ); |
| 242 | *(uint32_t*)(r+4) = htonl( peer_list->peer_count ); | 226 | *(uint32_t*)(r+4) = htonl( peer_list->peer_count ); |
| @@ -258,7 +242,7 @@ size_t return_peers_for_torrent( ot_torrent *torrent, size_t amount, char *reply | |||
| 258 | } | 242 | } |
| 259 | 243 | ||
| 260 | /* Fetches scrape info for a specific torrent */ | 244 | /* Fetches scrape info for a specific torrent */ |
| 261 | size_t return_udp_scrape_for_torrent( ot_hash *hash, char *reply ) { | 245 | size_t return_udp_scrape_for_torrent( ot_hash hash, char *reply ) { |
| 262 | int exactmatch, delta_torrentcount = 0; | 246 | int exactmatch, delta_torrentcount = 0; |
| 263 | ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ); | 247 | ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ); |
| 264 | ot_torrent *torrent = binary_search( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); | 248 | ot_torrent *torrent = binary_search( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); |
| @@ -292,7 +276,7 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash_list, int amount, char *repl | |||
| 292 | 276 | ||
| 293 | for( i=0; i<amount; ++i ) { | 277 | for( i=0; i<amount; ++i ) { |
| 294 | ot_hash *hash = hash_list + i; | 278 | ot_hash *hash = hash_list + i; |
| 295 | ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ); | 279 | ot_vector *torrents_list = mutex_bucket_lock_by_hash( *hash ); |
| 296 | ot_torrent *torrent = binary_search( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); | 280 | ot_torrent *torrent = binary_search( hash, torrents_list->data, torrents_list->size, sizeof( ot_torrent ), OT_HASH_COMPARE_SIZE, &exactmatch ); |
| 297 | 281 | ||
| 298 | if( exactmatch ) { | 282 | if( exactmatch ) { |
| @@ -300,14 +284,13 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash_list, int amount, char *repl | |||
| 300 | vector_remove_torrent( torrents_list, torrent ); | 284 | vector_remove_torrent( torrents_list, torrent ); |
| 301 | delta_torrentcount = -1; | 285 | delta_torrentcount = -1; |
| 302 | } else { | 286 | } else { |
| 303 | int j; | ||
| 304 | *r++='2';*r++='0';*r++=':'; | 287 | *r++='2';*r++='0';*r++=':'; |
| 305 | for(j=0;j<20;j+=4) WRITE32(r,j,READ32(hash,j)); r += 20; | 288 | memcpy( r, hash, sizeof(ot_hash) ); r+=sizeof(ot_hash); |
| 306 | r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", | 289 | r += sprintf( r, "d8:completei%zde10:downloadedi%zde10:incompletei%zdee", |
| 307 | torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count ); | 290 | torrent->peer_list->seed_count, torrent->peer_list->down_count, torrent->peer_list->peer_count-torrent->peer_list->seed_count ); |
| 308 | } | 291 | } |
| 309 | } | 292 | } |
| 310 | mutex_bucket_unlock_by_hash( hash, delta_torrentcount ); | 293 | mutex_bucket_unlock_by_hash( *hash, delta_torrentcount ); |
| 311 | } | 294 | } |
| 312 | 295 | ||
| 313 | *r++ = 'e'; *r++ = 'e'; | 296 | *r++ = 'e'; *r++ = 'e'; |
| @@ -315,7 +298,7 @@ size_t return_tcp_scrape_for_torrent( ot_hash *hash_list, int amount, char *repl | |||
| 315 | } | 298 | } |
| 316 | 299 | ||
| 317 | static ot_peerlist dummy_list; | 300 | static ot_peerlist dummy_list; |
| 318 | size_t remove_peer_from_torrent( ot_hash *hash, ot_peer *peer, char *reply, PROTO_FLAG proto ) { | 301 | size_t remove_peer_from_torrent( ot_hash hash, ot_peer *peer, char *reply, PROTO_FLAG proto ) { |
| 319 | int exactmatch; | 302 | int exactmatch; |
| 320 | size_t reply_size = 0; | 303 | size_t reply_size = 0; |
| 321 | ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ); | 304 | ot_vector *torrents_list = mutex_bucket_lock_by_hash( hash ); |
