1 | /* |
---|
2 | * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: peer-mgr.c 3618 2007-10-28 19:42:46Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <string.h> /* memcpy, memcmp */ |
---|
15 | #include <stdlib.h> /* qsort */ |
---|
16 | #include <stdio.h> /* printf */ |
---|
17 | #include <limits.h> /* INT_MAX */ |
---|
18 | |
---|
19 | #include <libgen.h> /* basename */ |
---|
20 | #include <sys/types.h> /* event.h needs this */ |
---|
21 | #include <arpa/inet.h> /* inet_ntoa */ |
---|
22 | |
---|
23 | #include <event.h> |
---|
24 | |
---|
25 | #include "transmission.h" |
---|
26 | #include "clients.h" |
---|
27 | #include "completion.h" |
---|
28 | #include "crypto.h" |
---|
29 | #include "handshake.h" |
---|
30 | #include "net.h" |
---|
31 | #include "peer-io.h" |
---|
32 | #include "peer-mgr.h" |
---|
33 | #include "peer-mgr-private.h" |
---|
34 | #include "peer-msgs.h" |
---|
35 | #include "platform.h" |
---|
36 | #include "ptrarray.h" |
---|
37 | #include "ratecontrol.h" |
---|
38 | #include "shared.h" |
---|
39 | #include "trevent.h" |
---|
40 | #include "utils.h" |
---|
41 | |
---|
42 | enum |
---|
43 | { |
---|
44 | /* how frequently to change which peers are choked */ |
---|
45 | RECHOKE_PERIOD_MSEC = (1000), |
---|
46 | |
---|
47 | /* how frequently to decide which peers live and die */ |
---|
48 | RECONNECT_PERIOD_MSEC = (5 * 1000), |
---|
49 | |
---|
50 | /* how frequently to refill peers' request lists */ |
---|
51 | REFILL_PERIOD_MSEC = 666, |
---|
52 | |
---|
53 | /* don't change a peer's choke status more often than this */ |
---|
54 | MIN_CHOKE_PERIOD_SEC = 10, |
---|
55 | |
---|
56 | /* following the BT spec, we consider ourselves `snubbed' if |
---|
57 | * we're we don't get piece data from a peer in this long */ |
---|
58 | SNUBBED_SEC = 60, |
---|
59 | |
---|
60 | /* arbitrary */ |
---|
61 | MAX_CONNECTED_PEERS_PER_TORRENT = 60, |
---|
62 | |
---|
63 | /* when many peers are available, keep idle ones this long */ |
---|
64 | MIN_UPLOAD_IDLE_SECS = 60, |
---|
65 | |
---|
66 | /* when few peers are available, keep idle ones this long */ |
---|
67 | MAX_UPLOAD_IDLE_SECS = 240, |
---|
68 | |
---|
69 | /* how many peers to unchoke per-torrent. */ |
---|
70 | /* FIXME: make this user-configurable? */ |
---|
71 | NUM_UNCHOKED_PEERS_PER_TORRENT = 12, /* arbitrary */ |
---|
72 | |
---|
73 | /* set this too high and there will be a lot of churn. |
---|
74 | * set it too low and you'll get peers too slowly */ |
---|
75 | MAX_RECONNECTIONS_PER_PULSE = 10, |
---|
76 | |
---|
77 | /* corresponds to ut_pex's added.f flags */ |
---|
78 | ADDED_F_ENCRYPTION_FLAG = 1, |
---|
79 | /* corresponds to ut_pex's added.f flags */ |
---|
80 | ADDED_F_SEED_FLAG = 2, |
---|
81 | |
---|
82 | /* number of bad pieces a peer is allowed to send before we ban them */ |
---|
83 | MAX_BAD_PIECES_PER_PEER = 3, |
---|
84 | /* use for bitwise operations w/peer_atom.myflags */ |
---|
85 | MYFLAG_BANNED = 1 |
---|
86 | }; |
---|
87 | |
---|
88 | |
---|
89 | /** |
---|
90 | *** |
---|
91 | **/ |
---|
92 | |
---|
93 | /* We keep one of these for every peer we know about, whether |
---|
94 | * it's connected or not, so the struct must be small. |
---|
95 | * When our current connections underperform, we dip back |
---|
96 | * into this list for new ones. */ |
---|
97 | struct peer_atom |
---|
98 | { |
---|
99 | uint8_t from; |
---|
100 | uint8_t flags; /* these match the added_f flags */ |
---|
101 | uint8_t myflags; /* flags that aren't defined in added_f */ |
---|
102 | uint16_t port; |
---|
103 | uint16_t numFails; |
---|
104 | struct in_addr addr; |
---|
105 | time_t time; |
---|
106 | }; |
---|
107 | |
---|
108 | typedef struct |
---|
109 | { |
---|
110 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
111 | tr_ptrArray * outgoingHandshakes; /* tr_handshake */ |
---|
112 | tr_ptrArray * pool; /* struct peer_atom */ |
---|
113 | tr_ptrArray * peers; /* tr_peer */ |
---|
114 | tr_timer * reconnectTimer; |
---|
115 | tr_timer * rechokeTimer; |
---|
116 | tr_timer * refillTimer; |
---|
117 | tr_torrent * tor; |
---|
118 | tr_bitfield * requested; |
---|
119 | |
---|
120 | unsigned int isRunning : 1; |
---|
121 | |
---|
122 | struct tr_peerMgr * manager; |
---|
123 | } |
---|
124 | Torrent; |
---|
125 | |
---|
126 | struct tr_peerMgr |
---|
127 | { |
---|
128 | tr_handle * handle; |
---|
129 | tr_ptrArray * torrents; /* Torrent */ |
---|
130 | tr_ptrArray * incomingHandshakes; /* tr_handshake */ |
---|
131 | }; |
---|
132 | |
---|
133 | /** |
---|
134 | *** |
---|
135 | **/ |
---|
136 | |
---|
137 | static void |
---|
138 | myDebug( const char * file, int line, const Torrent * t, const char * fmt, ... ) |
---|
139 | { |
---|
140 | FILE * fp = tr_getLog( ); |
---|
141 | if( fp != NULL ) |
---|
142 | { |
---|
143 | va_list args; |
---|
144 | char timestr[64]; |
---|
145 | struct evbuffer * buf = evbuffer_new( ); |
---|
146 | char * myfile = tr_strdup( file ); |
---|
147 | |
---|
148 | evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) ); |
---|
149 | if( t != NULL ) |
---|
150 | evbuffer_add_printf( buf, "%s ", t->tor->info.name ); |
---|
151 | va_start( args, fmt ); |
---|
152 | evbuffer_add_vprintf( buf, fmt, args ); |
---|
153 | va_end( args ); |
---|
154 | evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line ); |
---|
155 | fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp ); |
---|
156 | |
---|
157 | tr_free( myfile ); |
---|
158 | evbuffer_free( buf ); |
---|
159 | } |
---|
160 | } |
---|
161 | |
---|
162 | #define tordbg(t, fmt...) myDebug(__FILE__, __LINE__, t, ##fmt ) |
---|
163 | |
---|
164 | /** |
---|
165 | *** |
---|
166 | **/ |
---|
167 | |
---|
168 | static void |
---|
169 | managerLock( struct tr_peerMgr * manager ) |
---|
170 | { |
---|
171 | tr_globalLock( manager->handle ); |
---|
172 | } |
---|
173 | static void |
---|
174 | managerUnlock( struct tr_peerMgr * manager ) |
---|
175 | { |
---|
176 | tr_globalUnlock( manager->handle ); |
---|
177 | } |
---|
178 | static void |
---|
179 | torrentLock( Torrent * torrent ) |
---|
180 | { |
---|
181 | managerLock( torrent->manager ); |
---|
182 | } |
---|
183 | static void |
---|
184 | torrentUnlock( Torrent * torrent ) |
---|
185 | { |
---|
186 | managerUnlock( torrent->manager ); |
---|
187 | } |
---|
188 | static int |
---|
189 | torrentIsLocked( const Torrent * t ) |
---|
190 | { |
---|
191 | return tr_globalIsLocked( t->manager->handle ); |
---|
192 | } |
---|
193 | |
---|
194 | /** |
---|
195 | *** |
---|
196 | **/ |
---|
197 | |
---|
198 | static int |
---|
199 | compareAddresses( const struct in_addr * a, const struct in_addr * b ) |
---|
200 | { |
---|
201 | return tr_compareUint32( a->s_addr, b->s_addr ); |
---|
202 | } |
---|
203 | |
---|
204 | static int |
---|
205 | handshakeCompareToAddr( const void * va, const void * vb ) |
---|
206 | { |
---|
207 | const tr_handshake * a = va; |
---|
208 | return compareAddresses( tr_handshakeGetAddr( a, NULL ), vb ); |
---|
209 | } |
---|
210 | |
---|
211 | static int |
---|
212 | handshakeCompare( const void * a, const void * b ) |
---|
213 | { |
---|
214 | return handshakeCompareToAddr( a, tr_handshakeGetAddr( b, NULL ) ); |
---|
215 | } |
---|
216 | |
---|
217 | static tr_handshake* |
---|
218 | getExistingHandshake( tr_ptrArray * handshakes, const struct in_addr * in_addr ) |
---|
219 | { |
---|
220 | return tr_ptrArrayFindSorted( handshakes, |
---|
221 | in_addr, |
---|
222 | handshakeCompareToAddr ); |
---|
223 | } |
---|
224 | |
---|
225 | static int |
---|
226 | comparePeerAtomToAddress( const void * va, const void * vb ) |
---|
227 | { |
---|
228 | const struct peer_atom * a = va; |
---|
229 | return compareAddresses( &a->addr, vb ); |
---|
230 | } |
---|
231 | |
---|
232 | static int |
---|
233 | comparePeerAtoms( const void * va, const void * vb ) |
---|
234 | { |
---|
235 | const struct peer_atom * b = vb; |
---|
236 | return comparePeerAtomToAddress( va, &b->addr ); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | *** |
---|
241 | **/ |
---|
242 | |
---|
243 | static int |
---|
244 | torrentCompare( const void * va, const void * vb ) |
---|
245 | { |
---|
246 | const Torrent * a = va; |
---|
247 | const Torrent * b = vb; |
---|
248 | return memcmp( a->hash, b->hash, SHA_DIGEST_LENGTH ); |
---|
249 | } |
---|
250 | |
---|
251 | static int |
---|
252 | torrentCompareToHash( const void * va, const void * vb ) |
---|
253 | { |
---|
254 | const Torrent * a = va; |
---|
255 | const uint8_t * b_hash = vb; |
---|
256 | return memcmp( a->hash, b_hash, SHA_DIGEST_LENGTH ); |
---|
257 | } |
---|
258 | |
---|
259 | static Torrent* |
---|
260 | getExistingTorrent( tr_peerMgr * manager, const uint8_t * hash ) |
---|
261 | { |
---|
262 | return (Torrent*) tr_ptrArrayFindSorted( manager->torrents, |
---|
263 | hash, |
---|
264 | torrentCompareToHash ); |
---|
265 | } |
---|
266 | |
---|
267 | static int |
---|
268 | peerCompare( const void * va, const void * vb ) |
---|
269 | { |
---|
270 | const tr_peer * a = va; |
---|
271 | const tr_peer * b = vb; |
---|
272 | return compareAddresses( &a->in_addr, &b->in_addr ); |
---|
273 | } |
---|
274 | |
---|
275 | static int |
---|
276 | peerCompareToAddr( const void * va, const void * vb ) |
---|
277 | { |
---|
278 | const tr_peer * a = va; |
---|
279 | return compareAddresses( &a->in_addr, vb ); |
---|
280 | } |
---|
281 | |
---|
282 | static tr_peer* |
---|
283 | getExistingPeer( Torrent * torrent, const struct in_addr * in_addr ) |
---|
284 | { |
---|
285 | assert( torrentIsLocked( torrent ) ); |
---|
286 | assert( in_addr != NULL ); |
---|
287 | |
---|
288 | return (tr_peer*) tr_ptrArrayFindSorted( torrent->peers, |
---|
289 | in_addr, |
---|
290 | peerCompareToAddr ); |
---|
291 | } |
---|
292 | |
---|
293 | static struct peer_atom* |
---|
294 | getExistingAtom( const Torrent * t, const struct in_addr * addr ) |
---|
295 | { |
---|
296 | assert( torrentIsLocked( t ) ); |
---|
297 | return tr_ptrArrayFindSorted( t->pool, addr, comparePeerAtomToAddress ); |
---|
298 | } |
---|
299 | |
---|
300 | static int |
---|
301 | peerIsInUse( const Torrent * ct, const struct in_addr * addr ) |
---|
302 | { |
---|
303 | Torrent * t = (Torrent*) ct; |
---|
304 | |
---|
305 | assert( torrentIsLocked ( t ) ); |
---|
306 | |
---|
307 | return getExistingPeer( t, addr ) |
---|
308 | || getExistingHandshake( t->outgoingHandshakes, addr ) |
---|
309 | || getExistingHandshake( t->manager->incomingHandshakes, addr ); |
---|
310 | } |
---|
311 | |
---|
312 | static tr_peer* |
---|
313 | peerConstructor( const struct in_addr * in_addr ) |
---|
314 | { |
---|
315 | tr_peer * p; |
---|
316 | p = tr_new0( tr_peer, 1 ); |
---|
317 | p->rcToClient = tr_rcInit( ); |
---|
318 | p->rcToPeer = tr_rcInit( ); |
---|
319 | memcpy( &p->in_addr, in_addr, sizeof(struct in_addr) ); |
---|
320 | return p; |
---|
321 | } |
---|
322 | |
---|
323 | static tr_peer* |
---|
324 | getPeer( Torrent * torrent, const struct in_addr * in_addr ) |
---|
325 | { |
---|
326 | tr_peer * peer; |
---|
327 | |
---|
328 | assert( torrentIsLocked( torrent ) ); |
---|
329 | |
---|
330 | peer = getExistingPeer( torrent, in_addr ); |
---|
331 | |
---|
332 | if( peer == NULL ) { |
---|
333 | peer = peerConstructor( in_addr ); |
---|
334 | tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare ); |
---|
335 | } |
---|
336 | |
---|
337 | return peer; |
---|
338 | } |
---|
339 | |
---|
340 | static void |
---|
341 | peerDestructor( tr_peer * peer ) |
---|
342 | { |
---|
343 | assert( peer != NULL ); |
---|
344 | assert( peer->msgs != NULL ); |
---|
345 | |
---|
346 | tr_peerMsgsUnsubscribe( peer->msgs, peer->msgsTag ); |
---|
347 | tr_peerMsgsFree( peer->msgs ); |
---|
348 | |
---|
349 | tr_peerIoFree( peer->io ); |
---|
350 | |
---|
351 | tr_bitfieldFree( peer->have ); |
---|
352 | tr_bitfieldFree( peer->blame ); |
---|
353 | tr_rcClose( peer->rcToClient ); |
---|
354 | tr_rcClose( peer->rcToPeer ); |
---|
355 | tr_free( peer->client ); |
---|
356 | tr_free( peer ); |
---|
357 | } |
---|
358 | |
---|
359 | static void |
---|
360 | removePeer( Torrent * t, tr_peer * peer ) |
---|
361 | { |
---|
362 | tr_peer * removed; |
---|
363 | struct peer_atom * atom; |
---|
364 | |
---|
365 | assert( torrentIsLocked( t ) ); |
---|
366 | |
---|
367 | atom = getExistingAtom( t, &peer->in_addr ); |
---|
368 | assert( atom != NULL ); |
---|
369 | atom->time = time( NULL ); |
---|
370 | |
---|
371 | removed = tr_ptrArrayRemoveSorted ( t->peers, peer, peerCompare ); |
---|
372 | assert( removed == peer ); |
---|
373 | peerDestructor( removed ); |
---|
374 | } |
---|
375 | |
---|
376 | static void |
---|
377 | removeAllPeers( Torrent * t ) |
---|
378 | { |
---|
379 | while( !tr_ptrArrayEmpty( t->peers ) ) |
---|
380 | removePeer( t, tr_ptrArrayNth( t->peers, 0 ) ); |
---|
381 | } |
---|
382 | |
---|
383 | static void |
---|
384 | torrentDestructor( Torrent * t ) |
---|
385 | { |
---|
386 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
387 | |
---|
388 | assert( t != NULL ); |
---|
389 | assert( !t->isRunning ); |
---|
390 | assert( t->peers != NULL ); |
---|
391 | assert( torrentIsLocked( t ) ); |
---|
392 | assert( tr_ptrArrayEmpty( t->outgoingHandshakes ) ); |
---|
393 | assert( tr_ptrArrayEmpty( t->peers ) ); |
---|
394 | |
---|
395 | memcpy( hash, t->hash, SHA_DIGEST_LENGTH ); |
---|
396 | |
---|
397 | tr_timerFree( &t->reconnectTimer ); |
---|
398 | tr_timerFree( &t->rechokeTimer ); |
---|
399 | tr_timerFree( &t->refillTimer ); |
---|
400 | |
---|
401 | tr_bitfieldFree( t->requested ); |
---|
402 | tr_ptrArrayFree( t->pool, (PtrArrayForeachFunc)tr_free ); |
---|
403 | tr_ptrArrayFree( t->outgoingHandshakes, NULL ); |
---|
404 | tr_ptrArrayFree( t->peers, NULL ); |
---|
405 | |
---|
406 | tr_free( t ); |
---|
407 | } |
---|
408 | |
---|
409 | static Torrent* |
---|
410 | torrentConstructor( tr_peerMgr * manager, tr_torrent * tor ) |
---|
411 | { |
---|
412 | Torrent * t; |
---|
413 | |
---|
414 | t = tr_new0( Torrent, 1 ); |
---|
415 | t->manager = manager; |
---|
416 | t->tor = tor; |
---|
417 | t->pool = tr_ptrArrayNew( ); |
---|
418 | t->peers = tr_ptrArrayNew( ); |
---|
419 | t->outgoingHandshakes = tr_ptrArrayNew( ); |
---|
420 | t->requested = tr_bitfieldNew( tor->blockCount ); |
---|
421 | memcpy( t->hash, tor->info.hash, SHA_DIGEST_LENGTH ); |
---|
422 | |
---|
423 | return t; |
---|
424 | } |
---|
425 | |
---|
426 | /** |
---|
427 | *** |
---|
428 | **/ |
---|
429 | |
---|
430 | struct tr_bitfield * |
---|
431 | tr_peerMgrGenerateAllowedSet( const uint32_t setCount, |
---|
432 | const uint32_t pieceCount, |
---|
433 | const uint8_t infohash[20], |
---|
434 | const struct in_addr * ip ) |
---|
435 | { |
---|
436 | /* This has been checked against the spec example implementation. Feeding it with : |
---|
437 | setCount = 9, pieceCount = 1313, infohash = Oxaa,0xaa,...0xaa, ip = 80.4.4.200 |
---|
438 | generate : |
---|
439 | 1059, 431, 808, 1217, 287, 376, 1188, 353, 508 |
---|
440 | but since we're storing in a bitfield, it won't be in this order... */ |
---|
441 | /* TODO : We should translate link-local IPv4 adresses to external IP, |
---|
442 | * so that being on same local network gives us the same allowed pieces */ |
---|
443 | |
---|
444 | uint8_t *seed; |
---|
445 | char buf[4]; |
---|
446 | uint32_t allowedPieceCount = 0; |
---|
447 | tr_bitfield_t * ret; |
---|
448 | |
---|
449 | printf( "%d piece allowed fast set for torrent with %d pieces and hex infohash\n", setCount, pieceCount ); |
---|
450 | printf( "%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x%x for node with IP %s:\n", |
---|
451 | infohash[0], infohash[1], infohash[2], infohash[3], infohash[4], infohash[5], infohash[6], infohash[7], infohash[8], infohash[9], |
---|
452 | infohash[10], infohash[11], infohash[12], infohash[13], infohash[14], infohash[15], infohash[16], infohash[7], infohash[18], infohash[19], |
---|
453 | inet_ntoa( *ip ) ); |
---|
454 | |
---|
455 | seed = malloc(4 + SHA_DIGEST_LENGTH); |
---|
456 | |
---|
457 | ret = tr_bitfieldNew( pieceCount ); |
---|
458 | |
---|
459 | /* We need a seed based on most significant bytes of peer address |
---|
460 | concatenated with torrent's infohash */ |
---|
461 | *(uint32_t*)buf = ntohl( htonl(ip->s_addr) & 0xffffff00 ); |
---|
462 | |
---|
463 | memcpy( seed, &buf, 4 ); |
---|
464 | memcpy( seed + 4, infohash, SHA_DIGEST_LENGTH ); |
---|
465 | |
---|
466 | tr_sha1( seed, seed, 4 + SHA_DIGEST_LENGTH, NULL ); |
---|
467 | |
---|
468 | while ( allowedPieceCount < setCount ) |
---|
469 | { |
---|
470 | int i; |
---|
471 | for ( i = 0 ; i < 5 && allowedPieceCount < setCount ; i++ ) |
---|
472 | { |
---|
473 | /* We generate indices from 4-byte chunks of the seed */ |
---|
474 | uint32_t j = i * 4; |
---|
475 | uint32_t y = ntohl( *(uint32_t*)(seed + j) ); |
---|
476 | uint32_t index = y % pieceCount; |
---|
477 | |
---|
478 | if ( !tr_bitfieldHas( ret, index ) ) |
---|
479 | { |
---|
480 | tr_bitfieldAdd( ret, index ); |
---|
481 | allowedPieceCount++; |
---|
482 | } |
---|
483 | } |
---|
484 | /* We randomize the seed, in case we need to iterate more */ |
---|
485 | tr_sha1( seed, seed, SHA_DIGEST_LENGTH, NULL ); |
---|
486 | } |
---|
487 | tr_free( seed ); |
---|
488 | |
---|
489 | return ret; |
---|
490 | } |
---|
491 | |
---|
492 | tr_peerMgr* |
---|
493 | tr_peerMgrNew( tr_handle * handle ) |
---|
494 | { |
---|
495 | tr_peerMgr * m = tr_new0( tr_peerMgr, 1 ); |
---|
496 | m->handle = handle; |
---|
497 | m->torrents = tr_ptrArrayNew( ); |
---|
498 | m->incomingHandshakes = tr_ptrArrayNew( ); |
---|
499 | return m; |
---|
500 | } |
---|
501 | |
---|
502 | void |
---|
503 | tr_peerMgrFree( tr_peerMgr * manager ) |
---|
504 | { |
---|
505 | managerLock( manager ); |
---|
506 | |
---|
507 | /* free the handshakes. Abort invokes handshakeDoneCB(), which removes |
---|
508 | * the item from manager->handshakes, so this is a little roundabout... */ |
---|
509 | while( !tr_ptrArrayEmpty( manager->incomingHandshakes ) ) |
---|
510 | tr_handshakeAbort( tr_ptrArrayNth( manager->incomingHandshakes, 0 ) ); |
---|
511 | tr_ptrArrayFree( manager->incomingHandshakes, NULL ); |
---|
512 | |
---|
513 | /* free the torrents. */ |
---|
514 | tr_ptrArrayFree( manager->torrents, (PtrArrayForeachFunc)torrentDestructor ); |
---|
515 | |
---|
516 | managerUnlock( manager ); |
---|
517 | tr_free( manager ); |
---|
518 | } |
---|
519 | |
---|
520 | static tr_peer** |
---|
521 | getConnectedPeers( Torrent * t, int * setmeCount ) |
---|
522 | { |
---|
523 | int i, peerCount, connectionCount; |
---|
524 | tr_peer **peers; |
---|
525 | tr_peer **ret; |
---|
526 | |
---|
527 | assert( torrentIsLocked( t ) ); |
---|
528 | |
---|
529 | peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
530 | ret = tr_new( tr_peer*, peerCount ); |
---|
531 | |
---|
532 | for( i=connectionCount=0; i<peerCount; ++i ) |
---|
533 | if( peers[i]->msgs != NULL ) |
---|
534 | ret[connectionCount++] = peers[i]; |
---|
535 | |
---|
536 | *setmeCount = connectionCount; |
---|
537 | return ret; |
---|
538 | } |
---|
539 | |
---|
540 | /*** |
---|
541 | **** Refill |
---|
542 | ***/ |
---|
543 | |
---|
544 | struct tr_refill_piece |
---|
545 | { |
---|
546 | tr_priority_t priority; |
---|
547 | uint16_t random; |
---|
548 | uint32_t piece; |
---|
549 | uint32_t peerCount; |
---|
550 | uint32_t fastAllowed; |
---|
551 | }; |
---|
552 | |
---|
553 | static int |
---|
554 | compareRefillPiece (const void * aIn, const void * bIn) |
---|
555 | { |
---|
556 | const struct tr_refill_piece * a = aIn; |
---|
557 | const struct tr_refill_piece * b = bIn; |
---|
558 | |
---|
559 | /* if one *might be* fastallowed to us, get it first... |
---|
560 | * I'm putting it on top so we prioritise those pieces at |
---|
561 | * startup, then we'll have them, and we'll be denied access |
---|
562 | * to them */ |
---|
563 | if (a->fastAllowed != b->fastAllowed) |
---|
564 | return a->fastAllowed < b->fastAllowed ? -1 : 1; |
---|
565 | |
---|
566 | /* if one piece has a higher priority, it goes first */ |
---|
567 | if (a->priority != b->priority) |
---|
568 | return a->priority > b->priority ? -1 : 1; |
---|
569 | |
---|
570 | /* otherwise if one has fewer peers, it goes first */ |
---|
571 | if (a->peerCount != b->peerCount) |
---|
572 | return a->peerCount < b->peerCount ? -1 : 1; |
---|
573 | |
---|
574 | /* otherwise go with our random seed */ |
---|
575 | return tr_compareUint16( a->random, b->random ); |
---|
576 | } |
---|
577 | |
---|
578 | static int |
---|
579 | isPieceInteresting( const tr_torrent * tor, |
---|
580 | int piece ) |
---|
581 | { |
---|
582 | if( tor->info.pieces[piece].dnd ) /* we don't want it */ |
---|
583 | return 0; |
---|
584 | |
---|
585 | if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we have it */ |
---|
586 | return 0; |
---|
587 | |
---|
588 | return 1; |
---|
589 | } |
---|
590 | |
---|
591 | static uint32_t* |
---|
592 | getPreferredPieces( Torrent * t, |
---|
593 | uint32_t * pieceCount ) |
---|
594 | { |
---|
595 | const tr_torrent * tor = t->tor; |
---|
596 | const tr_info * inf = &tor->info; |
---|
597 | int i; |
---|
598 | uint32_t poolSize = 0; |
---|
599 | uint32_t * pool = tr_new( uint32_t, inf->pieceCount ); |
---|
600 | int peerCount; |
---|
601 | tr_peer** peers; |
---|
602 | |
---|
603 | assert( torrentIsLocked( t ) ); |
---|
604 | |
---|
605 | peers = getConnectedPeers( t, &peerCount ); |
---|
606 | |
---|
607 | for( i=0; i<inf->pieceCount; ++i ) |
---|
608 | if( isPieceInteresting( tor, i ) ) |
---|
609 | pool[poolSize++] = i; |
---|
610 | |
---|
611 | /* sort the pool from most interesting to least... */ |
---|
612 | if( poolSize > 1 ) |
---|
613 | { |
---|
614 | uint32_t j; |
---|
615 | struct tr_refill_piece * p = tr_new( struct tr_refill_piece, poolSize ); |
---|
616 | |
---|
617 | for( j=0; j<poolSize; ++j ) |
---|
618 | { |
---|
619 | int k; |
---|
620 | const int piece = pool[j]; |
---|
621 | struct tr_refill_piece * setme = p + j; |
---|
622 | |
---|
623 | setme->piece = piece; |
---|
624 | setme->priority = inf->pieces[piece].priority; |
---|
625 | setme->peerCount = 0; |
---|
626 | setme->fastAllowed = 0; |
---|
627 | setme->random = tr_rand( UINT16_MAX ); |
---|
628 | |
---|
629 | for( k=0; k<peerCount; ++k ) { |
---|
630 | const tr_peer * peer = peers[k]; |
---|
631 | if( peer->peerIsInterested && !peer->clientIsChoked && tr_bitfieldHas( peer->have, piece ) ) |
---|
632 | ++setme->peerCount; |
---|
633 | /* The fast peer extension doesn't force a peer to actually HAVE a fast-allowed piece, |
---|
634 | but we're guaranteed to get the same pieces from different peers, |
---|
635 | so we'll build a list and pray one actually have this one */ |
---|
636 | setme->fastAllowed = tr_peerMsgIsPieceFastAllowed( peer->msgs, i); |
---|
637 | } |
---|
638 | } |
---|
639 | |
---|
640 | qsort (p, poolSize, sizeof(struct tr_refill_piece), compareRefillPiece); |
---|
641 | |
---|
642 | for( j=0; j<poolSize; ++j ) |
---|
643 | pool[j] = p[j].piece; |
---|
644 | |
---|
645 | tr_free( p ); |
---|
646 | } |
---|
647 | |
---|
648 | tr_free( peers ); |
---|
649 | |
---|
650 | *pieceCount = poolSize; |
---|
651 | return pool; |
---|
652 | } |
---|
653 | |
---|
654 | static uint64_t* |
---|
655 | getPreferredBlocks( Torrent * t, uint64_t * setmeCount ) |
---|
656 | { |
---|
657 | uint32_t i; |
---|
658 | uint32_t pieceCount; |
---|
659 | uint32_t * pieces; |
---|
660 | uint64_t *req, *unreq, *ret, *walk; |
---|
661 | int reqCount, unreqCount; |
---|
662 | const tr_torrent * tor = t->tor; |
---|
663 | |
---|
664 | assert( torrentIsLocked( t ) ); |
---|
665 | |
---|
666 | pieces = getPreferredPieces( t, &pieceCount ); |
---|
667 | |
---|
668 | req = tr_new( uint64_t, pieceCount * tor->blockCountInPiece ); |
---|
669 | reqCount = 0; |
---|
670 | unreq = tr_new( uint64_t, pieceCount * tor->blockCountInPiece ); |
---|
671 | unreqCount = 0; |
---|
672 | |
---|
673 | for( i=0; i<pieceCount; ++i ) { |
---|
674 | const uint32_t index = pieces[i]; |
---|
675 | const int begin = tr_torPieceFirstBlock( tor, index ); |
---|
676 | const int end = begin + tr_torPieceCountBlocks( tor, (int)index ); |
---|
677 | int block; |
---|
678 | for( block=begin; block<end; ++block ) |
---|
679 | if( tr_cpBlockIsComplete( tor->completion, block ) ) |
---|
680 | continue; |
---|
681 | else if( tr_bitfieldHas( t->requested, block ) ) |
---|
682 | req[reqCount++] = block; |
---|
683 | else |
---|
684 | unreq[unreqCount++] = block; |
---|
685 | } |
---|
686 | |
---|
687 | ret = walk = tr_new( uint64_t, unreqCount + reqCount ); |
---|
688 | memcpy( walk, unreq, sizeof(uint64_t) * unreqCount ); |
---|
689 | walk += unreqCount; |
---|
690 | memcpy( walk, req, sizeof(uint64_t) * reqCount ); |
---|
691 | walk += reqCount; |
---|
692 | assert( ( walk - ret ) == ( unreqCount + reqCount ) ); |
---|
693 | *setmeCount = walk - ret; |
---|
694 | |
---|
695 | tr_free( req ); |
---|
696 | tr_free( unreq ); |
---|
697 | tr_free( pieces ); |
---|
698 | |
---|
699 | return ret; |
---|
700 | } |
---|
701 | |
---|
702 | static int |
---|
703 | refillPulse( void * vtorrent ) |
---|
704 | { |
---|
705 | Torrent * t = vtorrent; |
---|
706 | tr_torrent * tor = t->tor; |
---|
707 | uint32_t i; |
---|
708 | int peerCount; |
---|
709 | tr_peer ** peers; |
---|
710 | uint64_t blockCount; |
---|
711 | uint64_t * blocks; |
---|
712 | |
---|
713 | if( !t->isRunning ) |
---|
714 | return TRUE; |
---|
715 | if( tr_torrentIsSeed( t->tor ) ) |
---|
716 | return TRUE; |
---|
717 | |
---|
718 | torrentLock( t ); |
---|
719 | tordbg( t, "Refilling Request Buffers..." ); |
---|
720 | |
---|
721 | blocks = getPreferredBlocks( t, &blockCount ); |
---|
722 | peers = getConnectedPeers( t, &peerCount ); |
---|
723 | |
---|
724 | for( i=0; peerCount && i<blockCount; ++i ) |
---|
725 | { |
---|
726 | const int block = blocks[i]; |
---|
727 | const uint32_t index = tr_torBlockPiece( tor, block ); |
---|
728 | const uint32_t begin = (block * tor->blockSize) - (index * tor->info.pieceSize); |
---|
729 | const uint32_t length = tr_torBlockCountBytes( tor, block ); |
---|
730 | int j; |
---|
731 | assert( _tr_block( tor, index, begin ) == block ); |
---|
732 | assert( begin < (uint32_t)tr_torPieceCountBytes( tor, (int)index ) ); |
---|
733 | assert( (begin + length) <= (uint32_t)tr_torPieceCountBytes( tor, (int)index ) ); |
---|
734 | |
---|
735 | |
---|
736 | /* find a peer who can ask for this block */ |
---|
737 | for( j=0; j<peerCount; ) |
---|
738 | { |
---|
739 | const int val = tr_peerMsgsAddRequest( peers[j]->msgs, index, begin, length ); |
---|
740 | switch( val ) |
---|
741 | { |
---|
742 | case TR_ADDREQ_FULL: |
---|
743 | case TR_ADDREQ_CLIENT_CHOKED: |
---|
744 | memmove( peers+j, peers+j+1, sizeof(tr_peer*)*(--peerCount-j) ); |
---|
745 | break; |
---|
746 | |
---|
747 | case TR_ADDREQ_MISSING: |
---|
748 | case TR_ADDREQ_DUPLICATE: |
---|
749 | ++j; |
---|
750 | break; |
---|
751 | |
---|
752 | case TR_ADDREQ_OK: |
---|
753 | tr_bitfieldAdd( t->requested, block ); |
---|
754 | j = peerCount; |
---|
755 | break; |
---|
756 | |
---|
757 | default: |
---|
758 | assert( 0 && "unhandled value" ); |
---|
759 | break; |
---|
760 | } |
---|
761 | } |
---|
762 | } |
---|
763 | |
---|
764 | /* cleanup */ |
---|
765 | tr_free( peers ); |
---|
766 | tr_free( blocks ); |
---|
767 | |
---|
768 | t->refillTimer = NULL; |
---|
769 | torrentUnlock( t ); |
---|
770 | return FALSE; |
---|
771 | } |
---|
772 | |
---|
773 | static void |
---|
774 | broadcastClientHave( Torrent * t, uint32_t index ) |
---|
775 | { |
---|
776 | int i, size; |
---|
777 | tr_peer ** peers; |
---|
778 | |
---|
779 | assert( torrentIsLocked( t ) ); |
---|
780 | |
---|
781 | peers = getConnectedPeers( t, &size ); |
---|
782 | for( i=0; i<size; ++i ) |
---|
783 | tr_peerMsgsHave( peers[i]->msgs, index ); |
---|
784 | tr_free( peers ); |
---|
785 | } |
---|
786 | |
---|
787 | static void |
---|
788 | broadcastGotBlock( Torrent * t, uint32_t index, uint32_t offset, uint32_t length ) |
---|
789 | { |
---|
790 | int i, size; |
---|
791 | tr_peer ** peers; |
---|
792 | |
---|
793 | assert( torrentIsLocked( t ) ); |
---|
794 | |
---|
795 | peers = getConnectedPeers( t, &size ); |
---|
796 | for( i=0; i<size; ++i ) |
---|
797 | tr_peerMsgsCancel( peers[i]->msgs, index, offset, length ); |
---|
798 | tr_free( peers ); |
---|
799 | } |
---|
800 | |
---|
801 | static void |
---|
802 | msgsCallbackFunc( void * vpeer, void * vevent, void * vt ) |
---|
803 | { |
---|
804 | tr_peer * peer = vpeer; |
---|
805 | Torrent * t = (Torrent *) vt; |
---|
806 | const tr_peermsgs_event * e = (const tr_peermsgs_event *) vevent; |
---|
807 | |
---|
808 | torrentLock( t ); |
---|
809 | |
---|
810 | switch( e->eventType ) |
---|
811 | { |
---|
812 | case TR_PEERMSG_NEED_REQ: |
---|
813 | if( t->refillTimer == NULL ) |
---|
814 | t->refillTimer = tr_timerNew( t->manager->handle, |
---|
815 | refillPulse, t, |
---|
816 | REFILL_PERIOD_MSEC ); |
---|
817 | break; |
---|
818 | |
---|
819 | case TR_PEERMSG_CANCEL: |
---|
820 | tr_bitfieldRem( t->requested, _tr_block( t->tor, e->pieceIndex, e->offset ) ); |
---|
821 | break; |
---|
822 | |
---|
823 | case TR_PEERMSG_CLIENT_HAVE: |
---|
824 | broadcastClientHave( t, e->pieceIndex ); |
---|
825 | tr_torrentRecheckCompleteness( t->tor ); |
---|
826 | break; |
---|
827 | |
---|
828 | case TR_PEERMSG_PEER_PROGRESS: { |
---|
829 | struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
830 | const int peerIsSeed = e->progress >= 1.0; |
---|
831 | if( peerIsSeed ) { |
---|
832 | tordbg( t, "marking peer %s as a seed", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
833 | atom->flags |= ADDED_F_SEED_FLAG; |
---|
834 | } else { |
---|
835 | tordbg( t, "marking peer %s as a non-seed", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
836 | atom->flags &= ~ADDED_F_SEED_FLAG; |
---|
837 | } break; |
---|
838 | } |
---|
839 | |
---|
840 | case TR_PEERMSG_CLIENT_BLOCK: |
---|
841 | broadcastGotBlock( t, e->pieceIndex, e->offset, e->length ); |
---|
842 | break; |
---|
843 | |
---|
844 | case TR_PEERMSG_GOT_ERROR: |
---|
845 | peer->doPurge = 1; |
---|
846 | break; |
---|
847 | |
---|
848 | default: |
---|
849 | assert(0); |
---|
850 | } |
---|
851 | |
---|
852 | torrentUnlock( t ); |
---|
853 | } |
---|
854 | |
---|
855 | static void |
---|
856 | ensureAtomExists( Torrent * t, const struct in_addr * addr, uint16_t port, uint8_t flags, uint8_t from ) |
---|
857 | { |
---|
858 | if( getExistingAtom( t, addr ) == NULL ) |
---|
859 | { |
---|
860 | struct peer_atom * a; |
---|
861 | a = tr_new0( struct peer_atom, 1 ); |
---|
862 | a->addr = *addr; |
---|
863 | a->port = port; |
---|
864 | a->flags = flags; |
---|
865 | a->from = from; |
---|
866 | tordbg( t, "got a new atom: %s", tr_peerIoAddrStr(&a->addr,a->port) ); |
---|
867 | tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms ); |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | /* FIXME: this is kind of a mess. */ |
---|
872 | static void |
---|
873 | myHandshakeDoneCB( tr_handshake * handshake, |
---|
874 | tr_peerIo * io, |
---|
875 | int isConnected, |
---|
876 | const uint8_t * peer_id, |
---|
877 | void * vmanager ) |
---|
878 | { |
---|
879 | int ok = isConnected; |
---|
880 | uint16_t port; |
---|
881 | const struct in_addr * addr; |
---|
882 | tr_peerMgr * manager = (tr_peerMgr*) vmanager; |
---|
883 | Torrent * t; |
---|
884 | tr_handshake * ours; |
---|
885 | |
---|
886 | assert( io != NULL ); |
---|
887 | assert( isConnected==0 || isConnected==1 ); |
---|
888 | |
---|
889 | t = tr_peerIoHasTorrentHash( io ) |
---|
890 | ? getExistingTorrent( manager, tr_peerIoGetTorrentHash( io ) ) |
---|
891 | : NULL; |
---|
892 | |
---|
893 | if( tr_peerIoIsIncoming ( io ) ) |
---|
894 | ours = tr_ptrArrayRemoveSorted( manager->incomingHandshakes, |
---|
895 | handshake, handshakeCompare ); |
---|
896 | else if( t != NULL ) |
---|
897 | ours = tr_ptrArrayRemoveSorted( t->outgoingHandshakes, |
---|
898 | handshake, handshakeCompare ); |
---|
899 | else |
---|
900 | ours = handshake; |
---|
901 | |
---|
902 | assert( ours != NULL ); |
---|
903 | assert( ours == handshake ); |
---|
904 | |
---|
905 | if( t != NULL ) |
---|
906 | torrentLock( t ); |
---|
907 | |
---|
908 | addr = tr_peerIoGetAddress( io, &port ); |
---|
909 | |
---|
910 | if( !ok || !t || !t->isRunning ) |
---|
911 | { |
---|
912 | tr_peerIoFree( io ); |
---|
913 | } |
---|
914 | else /* looking good */ |
---|
915 | { |
---|
916 | struct peer_atom * atom; |
---|
917 | ensureAtomExists( t, addr, port, 0, TR_PEER_FROM_INCOMING ); |
---|
918 | atom = getExistingAtom( t, addr ); |
---|
919 | |
---|
920 | if( atom->myflags & MYFLAG_BANNED ) |
---|
921 | { |
---|
922 | tordbg( t, "banned peer %s tried to reconnect", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
923 | tr_peerIoFree( io ); |
---|
924 | } |
---|
925 | else |
---|
926 | { |
---|
927 | tr_peer * peer = getExistingPeer( t, addr ); |
---|
928 | |
---|
929 | if( peer != NULL ) /* we already have this peer */ |
---|
930 | { |
---|
931 | tr_peerIoFree( io ); |
---|
932 | } |
---|
933 | else |
---|
934 | { |
---|
935 | peer = getPeer( t, addr ); |
---|
936 | tr_free( peer->client ); |
---|
937 | peer->client = peer_id ? tr_clientForId( peer_id ) : NULL; |
---|
938 | peer->port = port; |
---|
939 | peer->io = io; |
---|
940 | peer->msgs = tr_peerMsgsNew( t->tor, peer, msgsCallbackFunc, t, &peer->msgsTag ); |
---|
941 | atom->time = time( NULL ); |
---|
942 | } |
---|
943 | } |
---|
944 | } |
---|
945 | |
---|
946 | if( t != NULL ) |
---|
947 | torrentUnlock( t ); |
---|
948 | } |
---|
949 | |
---|
950 | void |
---|
951 | tr_peerMgrAddIncoming( tr_peerMgr * manager, |
---|
952 | struct in_addr * addr, |
---|
953 | uint16_t port, |
---|
954 | int socket ) |
---|
955 | { |
---|
956 | managerLock( manager ); |
---|
957 | |
---|
958 | if( getExistingHandshake( manager->incomingHandshakes, addr ) ) |
---|
959 | { |
---|
960 | tr_netClose( socket ); |
---|
961 | } |
---|
962 | else /* we don't have a connetion to them yet... */ |
---|
963 | { |
---|
964 | tr_peerIo * io; |
---|
965 | tr_handshake * handshake; |
---|
966 | |
---|
967 | tordbg( NULL, "Got an INCOMING connection with %s", tr_peerIoAddrStr( addr, port ) ); |
---|
968 | |
---|
969 | io = tr_peerIoNewIncoming( manager->handle, addr, port, socket ); |
---|
970 | |
---|
971 | handshake = tr_handshakeNew( io, |
---|
972 | manager->handle->encryptionMode, |
---|
973 | myHandshakeDoneCB, |
---|
974 | manager ); |
---|
975 | |
---|
976 | tr_ptrArrayInsertSorted( manager->incomingHandshakes, handshake, handshakeCompare ); |
---|
977 | } |
---|
978 | |
---|
979 | managerUnlock( manager ); |
---|
980 | } |
---|
981 | |
---|
982 | void |
---|
983 | tr_peerMgrAddPex( tr_peerMgr * manager, |
---|
984 | const uint8_t * torrentHash, |
---|
985 | uint8_t from, |
---|
986 | const tr_pex * pex, |
---|
987 | int pexCount ) |
---|
988 | { |
---|
989 | Torrent * t; |
---|
990 | const tr_pex * end; |
---|
991 | |
---|
992 | managerLock( manager ); |
---|
993 | |
---|
994 | t = getExistingTorrent( manager, torrentHash ); |
---|
995 | for( end=pex+pexCount; pex!=end; ++pex ) |
---|
996 | ensureAtomExists( t, &pex->in_addr, pex->port, pex->flags, from ); |
---|
997 | |
---|
998 | managerUnlock( manager ); |
---|
999 | } |
---|
1000 | |
---|
1001 | void |
---|
1002 | tr_peerMgrAddPeers( tr_peerMgr * manager, |
---|
1003 | const uint8_t * torrentHash, |
---|
1004 | uint8_t from, |
---|
1005 | const uint8_t * peerCompact, |
---|
1006 | int peerCount ) |
---|
1007 | { |
---|
1008 | int i; |
---|
1009 | const uint8_t * walk = peerCompact; |
---|
1010 | Torrent * t; |
---|
1011 | |
---|
1012 | managerLock( manager ); |
---|
1013 | |
---|
1014 | t = getExistingTorrent( manager, torrentHash ); |
---|
1015 | for( i=0; t!=NULL && i<peerCount; ++i ) |
---|
1016 | { |
---|
1017 | struct in_addr addr; |
---|
1018 | uint16_t port; |
---|
1019 | memcpy( &addr, walk, 4 ); walk += 4; |
---|
1020 | memcpy( &port, walk, 2 ); walk += 2; |
---|
1021 | ensureAtomExists( t, &addr, port, 0, from ); |
---|
1022 | } |
---|
1023 | |
---|
1024 | managerUnlock( manager ); |
---|
1025 | } |
---|
1026 | |
---|
1027 | /** |
---|
1028 | *** |
---|
1029 | **/ |
---|
1030 | |
---|
1031 | void |
---|
1032 | tr_peerMgrSetBlame( tr_peerMgr * manager, |
---|
1033 | const uint8_t * torrentHash, |
---|
1034 | int pieceIndex, |
---|
1035 | int success ) |
---|
1036 | { |
---|
1037 | if( !success ) |
---|
1038 | { |
---|
1039 | int peerCount, i; |
---|
1040 | Torrent * t = getExistingTorrent( manager, torrentHash ); |
---|
1041 | tr_peer ** peers; |
---|
1042 | |
---|
1043 | assert( torrentIsLocked( t ) ); |
---|
1044 | |
---|
1045 | peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1046 | for( i=0; i<peerCount; ++i ) |
---|
1047 | { |
---|
1048 | struct peer_atom * atom; |
---|
1049 | tr_peer * peer; |
---|
1050 | |
---|
1051 | peer = peers[i]; |
---|
1052 | if( !tr_bitfieldHas( peer->blame, pieceIndex ) ) |
---|
1053 | continue; |
---|
1054 | |
---|
1055 | ++peer->strikes; |
---|
1056 | tordbg( t, "peer %s contributed to corrupt piece (%d); now has %d strikes", |
---|
1057 | tr_peerIoAddrStr(&peer->in_addr,peer->port), |
---|
1058 | pieceIndex, (int)peer->strikes ); |
---|
1059 | if( peer->strikes < MAX_BAD_PIECES_PER_PEER ) |
---|
1060 | continue; |
---|
1061 | |
---|
1062 | atom = getExistingAtom( t, &peer->in_addr ); |
---|
1063 | atom->myflags |= MYFLAG_BANNED; |
---|
1064 | peer->doPurge = 1; |
---|
1065 | tordbg( t, "banning peer %s due to corrupt data", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1066 | } |
---|
1067 | } |
---|
1068 | } |
---|
1069 | |
---|
1070 | int |
---|
1071 | tr_pexCompare( const void * va, const void * vb ) |
---|
1072 | { |
---|
1073 | const tr_pex * a = (const tr_pex *) va; |
---|
1074 | const tr_pex * b = (const tr_pex *) vb; |
---|
1075 | int i = memcmp( &a->in_addr, &b->in_addr, sizeof(struct in_addr) ); |
---|
1076 | if( i ) return i; |
---|
1077 | if( a->port < b->port ) return -1; |
---|
1078 | if( a->port > b->port ) return 1; |
---|
1079 | return 0; |
---|
1080 | } |
---|
1081 | |
---|
1082 | int tr_pexCompare( const void * a, const void * b ); |
---|
1083 | |
---|
1084 | static int |
---|
1085 | peerPrefersCrypto( const tr_peer * peer ) |
---|
1086 | { |
---|
1087 | if( peer->encryption_preference == ENCRYPTION_PREFERENCE_YES ) |
---|
1088 | return TRUE; |
---|
1089 | |
---|
1090 | if( peer->encryption_preference == ENCRYPTION_PREFERENCE_NO ) |
---|
1091 | return FALSE; |
---|
1092 | |
---|
1093 | return tr_peerIoIsEncrypted( peer->io ); |
---|
1094 | }; |
---|
1095 | |
---|
1096 | int |
---|
1097 | tr_peerMgrGetPeers( tr_peerMgr * manager, |
---|
1098 | const uint8_t * torrentHash, |
---|
1099 | tr_pex ** setme_pex ) |
---|
1100 | { |
---|
1101 | const Torrent * t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1102 | int i, peerCount; |
---|
1103 | const tr_peer ** peers; |
---|
1104 | tr_pex * pex; |
---|
1105 | tr_pex * walk; |
---|
1106 | |
---|
1107 | torrentLock( (Torrent*)t ); |
---|
1108 | |
---|
1109 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1110 | pex = walk = tr_new( tr_pex, peerCount ); |
---|
1111 | |
---|
1112 | for( i=0; i<peerCount; ++i, ++walk ) |
---|
1113 | { |
---|
1114 | const tr_peer * peer = peers[i]; |
---|
1115 | |
---|
1116 | walk->in_addr = peer->in_addr; |
---|
1117 | |
---|
1118 | walk->port = peer->port; |
---|
1119 | |
---|
1120 | walk->flags = 0; |
---|
1121 | if( peerPrefersCrypto(peer) ) walk->flags |= ADDED_F_ENCRYPTION_FLAG; |
---|
1122 | if( peer->progress >= 1.0 ) walk->flags |= ADDED_F_SEED_FLAG; |
---|
1123 | } |
---|
1124 | |
---|
1125 | assert( ( walk - pex ) == peerCount ); |
---|
1126 | qsort( pex, peerCount, sizeof(tr_pex), tr_pexCompare ); |
---|
1127 | *setme_pex = pex; |
---|
1128 | |
---|
1129 | torrentUnlock( (Torrent*)t ); |
---|
1130 | |
---|
1131 | return peerCount; |
---|
1132 | } |
---|
1133 | |
---|
1134 | static int reconnectPulse( void * vtorrent ); |
---|
1135 | static int rechokePulse( void * vtorrent ); |
---|
1136 | |
---|
1137 | void |
---|
1138 | tr_peerMgrStartTorrent( tr_peerMgr * manager, |
---|
1139 | const uint8_t * torrentHash ) |
---|
1140 | { |
---|
1141 | Torrent * t; |
---|
1142 | |
---|
1143 | managerLock( manager ); |
---|
1144 | |
---|
1145 | t = getExistingTorrent( manager, torrentHash ); |
---|
1146 | |
---|
1147 | assert( t != NULL ); |
---|
1148 | assert( ( t->isRunning != 0 ) == ( t->reconnectTimer != NULL ) ); |
---|
1149 | assert( ( t->isRunning != 0 ) == ( t->rechokeTimer != NULL ) ); |
---|
1150 | |
---|
1151 | if( !t->isRunning ) |
---|
1152 | { |
---|
1153 | t->isRunning = 1; |
---|
1154 | |
---|
1155 | t->reconnectTimer = tr_timerNew( t->manager->handle, |
---|
1156 | reconnectPulse, t, |
---|
1157 | RECONNECT_PERIOD_MSEC ); |
---|
1158 | |
---|
1159 | t->rechokeTimer = tr_timerNew( t->manager->handle, |
---|
1160 | rechokePulse, t, |
---|
1161 | RECHOKE_PERIOD_MSEC ); |
---|
1162 | |
---|
1163 | reconnectPulse( t ); |
---|
1164 | |
---|
1165 | rechokePulse( t ); |
---|
1166 | } |
---|
1167 | |
---|
1168 | managerUnlock( manager ); |
---|
1169 | } |
---|
1170 | |
---|
1171 | static void |
---|
1172 | stopTorrent( Torrent * t ) |
---|
1173 | { |
---|
1174 | assert( torrentIsLocked( t ) ); |
---|
1175 | |
---|
1176 | t->isRunning = 0; |
---|
1177 | tr_timerFree( &t->rechokeTimer ); |
---|
1178 | tr_timerFree( &t->reconnectTimer ); |
---|
1179 | |
---|
1180 | /* disconnect the peers. */ |
---|
1181 | tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)peerDestructor ); |
---|
1182 | tr_ptrArrayClear( t->peers ); |
---|
1183 | |
---|
1184 | /* disconnect the handshakes. handshakeAbort calls handshakeDoneCB(), |
---|
1185 | * which removes the handshake from t->outgoingHandshakes... */ |
---|
1186 | while( !tr_ptrArrayEmpty( t->outgoingHandshakes ) ) |
---|
1187 | tr_handshakeAbort( tr_ptrArrayNth( t->outgoingHandshakes, 0 ) ); |
---|
1188 | } |
---|
1189 | void |
---|
1190 | tr_peerMgrStopTorrent( tr_peerMgr * manager, |
---|
1191 | const uint8_t * torrentHash) |
---|
1192 | { |
---|
1193 | managerLock( manager ); |
---|
1194 | |
---|
1195 | stopTorrent( getExistingTorrent( manager, torrentHash ) ); |
---|
1196 | |
---|
1197 | managerUnlock( manager ); |
---|
1198 | } |
---|
1199 | |
---|
1200 | void |
---|
1201 | tr_peerMgrAddTorrent( tr_peerMgr * manager, |
---|
1202 | tr_torrent * tor ) |
---|
1203 | { |
---|
1204 | Torrent * t; |
---|
1205 | |
---|
1206 | managerLock( manager ); |
---|
1207 | |
---|
1208 | assert( tor != NULL ); |
---|
1209 | assert( getExistingTorrent( manager, tor->info.hash ) == NULL ); |
---|
1210 | |
---|
1211 | t = torrentConstructor( manager, tor ); |
---|
1212 | tr_ptrArrayInsertSorted( manager->torrents, t, torrentCompare ); |
---|
1213 | |
---|
1214 | managerUnlock( manager ); |
---|
1215 | } |
---|
1216 | |
---|
1217 | void |
---|
1218 | tr_peerMgrRemoveTorrent( tr_peerMgr * manager, |
---|
1219 | const uint8_t * torrentHash ) |
---|
1220 | { |
---|
1221 | Torrent * t; |
---|
1222 | |
---|
1223 | managerLock( manager ); |
---|
1224 | |
---|
1225 | t = getExistingTorrent( manager, torrentHash ); |
---|
1226 | assert( t != NULL ); |
---|
1227 | stopTorrent( t ); |
---|
1228 | tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare ); |
---|
1229 | torrentDestructor( t ); |
---|
1230 | |
---|
1231 | managerUnlock( manager ); |
---|
1232 | } |
---|
1233 | |
---|
1234 | void |
---|
1235 | tr_peerMgrTorrentAvailability( const tr_peerMgr * manager, |
---|
1236 | const uint8_t * torrentHash, |
---|
1237 | int8_t * tab, |
---|
1238 | int tabCount ) |
---|
1239 | { |
---|
1240 | int i; |
---|
1241 | const Torrent * t; |
---|
1242 | const tr_torrent * tor; |
---|
1243 | float interval; |
---|
1244 | |
---|
1245 | managerLock( (tr_peerMgr*)manager ); |
---|
1246 | |
---|
1247 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1248 | tor = t->tor; |
---|
1249 | interval = tor->info.pieceCount / (float)tabCount; |
---|
1250 | |
---|
1251 | memset( tab, 0, tabCount ); |
---|
1252 | |
---|
1253 | for( i=0; i<tabCount; ++i ) |
---|
1254 | { |
---|
1255 | const int piece = i * interval; |
---|
1256 | |
---|
1257 | if( tor == NULL ) |
---|
1258 | tab[i] = 0; |
---|
1259 | else if( tr_cpPieceIsComplete( tor->completion, piece ) ) |
---|
1260 | tab[i] = -1; |
---|
1261 | else { |
---|
1262 | int j, peerCount; |
---|
1263 | const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1264 | for( j=0; j<peerCount; ++j ) |
---|
1265 | if( tr_bitfieldHas( peers[j]->have, i ) ) |
---|
1266 | ++tab[i]; |
---|
1267 | } |
---|
1268 | } |
---|
1269 | |
---|
1270 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1271 | } |
---|
1272 | |
---|
1273 | /* Returns the pieces that we and/or a connected peer has */ |
---|
1274 | tr_bitfield* |
---|
1275 | tr_peerMgrGetAvailable( const tr_peerMgr * manager, |
---|
1276 | const uint8_t * torrentHash ) |
---|
1277 | { |
---|
1278 | int i, size; |
---|
1279 | const Torrent * t; |
---|
1280 | const tr_peer ** peers; |
---|
1281 | tr_bitfield * pieces; |
---|
1282 | |
---|
1283 | managerLock( (tr_peerMgr*)manager ); |
---|
1284 | |
---|
1285 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1286 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size ); |
---|
1287 | pieces = tr_bitfieldDup( tr_cpPieceBitfield( t->tor->completion ) ); |
---|
1288 | for( i=0; i<size; ++i ) |
---|
1289 | if( peers[i]->io != NULL ) |
---|
1290 | tr_bitfieldAnd( pieces, peers[i]->have ); |
---|
1291 | |
---|
1292 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1293 | return pieces; |
---|
1294 | } |
---|
1295 | |
---|
1296 | int |
---|
1297 | tr_peerMgrHasConnections( const tr_peerMgr * manager, |
---|
1298 | const uint8_t * torrentHash ) |
---|
1299 | { |
---|
1300 | int ret; |
---|
1301 | const Torrent * t; |
---|
1302 | managerLock( (tr_peerMgr*)manager ); |
---|
1303 | |
---|
1304 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1305 | ret = t && tr_ptrArraySize( t->peers ); |
---|
1306 | |
---|
1307 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1308 | return ret; |
---|
1309 | } |
---|
1310 | |
---|
1311 | void |
---|
1312 | tr_peerMgrTorrentStats( const tr_peerMgr * manager, |
---|
1313 | const uint8_t * torrentHash, |
---|
1314 | int * setmePeersKnown, |
---|
1315 | int * setmePeersConnected, |
---|
1316 | int * setmePeersSendingToUs, |
---|
1317 | int * setmePeersGettingFromUs, |
---|
1318 | int * setmePeersFrom ) |
---|
1319 | { |
---|
1320 | int i, size; |
---|
1321 | const Torrent * t; |
---|
1322 | const tr_peer ** peers; |
---|
1323 | |
---|
1324 | managerLock( (tr_peerMgr*)manager ); |
---|
1325 | |
---|
1326 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1327 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size ); |
---|
1328 | |
---|
1329 | *setmePeersKnown = tr_ptrArraySize( t->pool ); |
---|
1330 | *setmePeersConnected = 0; |
---|
1331 | *setmePeersSendingToUs = 0; |
---|
1332 | *setmePeersGettingFromUs = 0; |
---|
1333 | |
---|
1334 | for( i=0; i<TR_PEER_FROM__MAX; ++i ) |
---|
1335 | setmePeersFrom[i] = 0; |
---|
1336 | |
---|
1337 | for( i=0; i<size; ++i ) |
---|
1338 | { |
---|
1339 | const tr_peer * peer = peers[i]; |
---|
1340 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1341 | |
---|
1342 | if( peer->io == NULL ) /* not connected */ |
---|
1343 | continue; |
---|
1344 | |
---|
1345 | ++*setmePeersConnected; |
---|
1346 | |
---|
1347 | ++setmePeersFrom[atom->from]; |
---|
1348 | |
---|
1349 | if( peer->rateToPeer > 0.01 ) |
---|
1350 | ++*setmePeersGettingFromUs; |
---|
1351 | |
---|
1352 | if( peer->rateToClient > 0.01 ) |
---|
1353 | ++*setmePeersSendingToUs; |
---|
1354 | } |
---|
1355 | |
---|
1356 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1357 | } |
---|
1358 | |
---|
1359 | struct tr_peer_stat * |
---|
1360 | tr_peerMgrPeerStats( const tr_peerMgr * manager, |
---|
1361 | const uint8_t * torrentHash, |
---|
1362 | int * setmeCount UNUSED ) |
---|
1363 | { |
---|
1364 | int i, size; |
---|
1365 | const Torrent * t; |
---|
1366 | tr_peer ** peers; |
---|
1367 | tr_peer_stat * ret; |
---|
1368 | |
---|
1369 | assert( manager != NULL ); |
---|
1370 | managerLock( (tr_peerMgr*)manager ); |
---|
1371 | |
---|
1372 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1373 | peers = getConnectedPeers( (Torrent*)t, &size ); |
---|
1374 | ret = tr_new0( tr_peer_stat, size ); |
---|
1375 | |
---|
1376 | for( i=0; i<size; ++i ) |
---|
1377 | { |
---|
1378 | const tr_peer * peer = peers[i]; |
---|
1379 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1380 | tr_peer_stat * stat = ret + i; |
---|
1381 | |
---|
1382 | tr_netNtop( &peer->in_addr, stat->addr, sizeof(stat->addr) ); |
---|
1383 | stat->port = peer->port; |
---|
1384 | stat->from = atom->from; |
---|
1385 | stat->client = tr_strdup( peer->client ? peer->client : "" ); |
---|
1386 | stat->progress = peer->progress; |
---|
1387 | stat->isEncrypted = tr_peerIoIsEncrypted( peer->io ) ? 1 : 0; |
---|
1388 | stat->uploadToRate = peer->rateToPeer; |
---|
1389 | stat->downloadFromRate = peer->rateToClient; |
---|
1390 | stat->isDownloading = stat->uploadToRate > 0.01; |
---|
1391 | stat->isUploading = stat->downloadFromRate > 0.01; |
---|
1392 | } |
---|
1393 | |
---|
1394 | *setmeCount = size; |
---|
1395 | tr_free( peers ); |
---|
1396 | |
---|
1397 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1398 | return ret; |
---|
1399 | } |
---|
1400 | |
---|
1401 | /** |
---|
1402 | *** |
---|
1403 | **/ |
---|
1404 | |
---|
1405 | struct ChokeData |
---|
1406 | { |
---|
1407 | tr_peer * peer; |
---|
1408 | float rate; |
---|
1409 | int randomKey; |
---|
1410 | int preferred; |
---|
1411 | int doUnchoke; |
---|
1412 | }; |
---|
1413 | |
---|
1414 | static int |
---|
1415 | compareChoke( const void * va, const void * vb ) |
---|
1416 | { |
---|
1417 | const struct ChokeData * a = va; |
---|
1418 | const struct ChokeData * b = vb; |
---|
1419 | |
---|
1420 | if( a->preferred != b->preferred ) |
---|
1421 | return a->preferred ? -1 : 1; |
---|
1422 | |
---|
1423 | if( a->preferred ) |
---|
1424 | { |
---|
1425 | if( a->rate > b->rate ) return -1; |
---|
1426 | if( a->rate < b->rate ) return 1; |
---|
1427 | return 0; |
---|
1428 | } |
---|
1429 | else |
---|
1430 | { |
---|
1431 | return a->randomKey - b->randomKey; |
---|
1432 | } |
---|
1433 | } |
---|
1434 | |
---|
1435 | static int |
---|
1436 | clientIsSnubbedBy( const tr_peer * peer ) |
---|
1437 | { |
---|
1438 | assert( peer != NULL ); |
---|
1439 | |
---|
1440 | return peer->peerSentPieceDataAt < (time(NULL) - SNUBBED_SEC); |
---|
1441 | } |
---|
1442 | |
---|
1443 | /** |
---|
1444 | *** |
---|
1445 | **/ |
---|
1446 | |
---|
1447 | static double |
---|
1448 | getWeightedThroughput( const tr_peer * peer ) |
---|
1449 | { |
---|
1450 | return ( 3 * peer->rateToPeer ) |
---|
1451 | + ( 1 * peer->rateToClient ); |
---|
1452 | } |
---|
1453 | |
---|
1454 | static void |
---|
1455 | rechoke( Torrent * t ) |
---|
1456 | { |
---|
1457 | int i, peerCount, size=0, unchoked=0; |
---|
1458 | const time_t fibrillationTime = time(NULL) - MIN_CHOKE_PERIOD_SEC; |
---|
1459 | tr_peer ** peers = getConnectedPeers( t, &peerCount ); |
---|
1460 | struct ChokeData * choke = tr_new0( struct ChokeData, peerCount ); |
---|
1461 | |
---|
1462 | assert( torrentIsLocked( t ) ); |
---|
1463 | |
---|
1464 | /* sort the peers by preference and rate */ |
---|
1465 | for( i=0; i<peerCount; ++i ) |
---|
1466 | { |
---|
1467 | tr_peer * peer = peers[i]; |
---|
1468 | struct ChokeData * node; |
---|
1469 | if( peer->chokeChangedAt > fibrillationTime ) |
---|
1470 | continue; |
---|
1471 | |
---|
1472 | node = &choke[size++]; |
---|
1473 | node->peer = peer; |
---|
1474 | node->preferred = peer->peerIsInterested && !clientIsSnubbedBy(peer); |
---|
1475 | node->randomKey = tr_rand( INT_MAX ); |
---|
1476 | node->rate = getWeightedThroughput( peer ); |
---|
1477 | } |
---|
1478 | |
---|
1479 | qsort( choke, size, sizeof(struct ChokeData), compareChoke ); |
---|
1480 | |
---|
1481 | for( i=0; i<size && i<NUM_UNCHOKED_PEERS_PER_TORRENT; ++i ) { |
---|
1482 | choke[i].doUnchoke = 1; |
---|
1483 | ++unchoked; |
---|
1484 | } |
---|
1485 | |
---|
1486 | for( ; i<size; ++i ) { |
---|
1487 | choke[i].doUnchoke = 1; |
---|
1488 | ++unchoked; |
---|
1489 | if( choke[i].peer->peerIsInterested ) |
---|
1490 | break; |
---|
1491 | } |
---|
1492 | |
---|
1493 | for( i=0; i<size; ++i ) |
---|
1494 | tr_peerMsgsSetChoke( choke[i].peer->msgs, !choke[i].doUnchoke ); |
---|
1495 | |
---|
1496 | /* cleanup */ |
---|
1497 | tr_free( choke ); |
---|
1498 | tr_free( peers ); |
---|
1499 | } |
---|
1500 | |
---|
1501 | static int |
---|
1502 | rechokePulse( void * vtorrent ) |
---|
1503 | { |
---|
1504 | Torrent * t = vtorrent; |
---|
1505 | torrentLock( t ); |
---|
1506 | rechoke( t ); |
---|
1507 | torrentUnlock( t ); |
---|
1508 | return TRUE; |
---|
1509 | } |
---|
1510 | |
---|
1511 | /*** |
---|
1512 | **** |
---|
1513 | **** Life and Death |
---|
1514 | **** |
---|
1515 | ***/ |
---|
1516 | |
---|
1517 | static int |
---|
1518 | shouldPeerBeClosed( const Torrent * t, const tr_peer * peer, int peerCount ) |
---|
1519 | { |
---|
1520 | const tr_torrent * tor = t->tor; |
---|
1521 | const time_t now = time( NULL ); |
---|
1522 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1523 | |
---|
1524 | /* if it's marked for purging, close it */ |
---|
1525 | if( peer->doPurge ) { |
---|
1526 | tordbg( t, "purging peer %s because its doPurge flag is set", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1527 | return TRUE; |
---|
1528 | } |
---|
1529 | |
---|
1530 | /* if we're both seeds and it's been long enough for a pex exchange, close it */ |
---|
1531 | if( 1 ) { |
---|
1532 | const int clientIsSeed = tr_torrentIsSeed( tor ); |
---|
1533 | const int peerIsSeed = atom->flags & ADDED_F_SEED_FLAG; |
---|
1534 | if( peerIsSeed && clientIsSeed && ( !tr_torrentIsPexEnabled(tor) || (now-atom->time>=30) ) ) { |
---|
1535 | tordbg( t, "purging peer %s because we're both seeds", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1536 | return TRUE; |
---|
1537 | } |
---|
1538 | } |
---|
1539 | |
---|
1540 | /* disconnect if it's been too long since piece data has been transferred. |
---|
1541 | * this is on a sliding scale based on number of available peers... */ |
---|
1542 | if( 1 ) { |
---|
1543 | const int relaxStrictnessIfFewerThanN = (int)((MAX_CONNECTED_PEERS_PER_TORRENT * 0.9) + 0.5); |
---|
1544 | /* if we have >= relaxIfFewerThan, strictness is 100%. |
---|
1545 | * if we have zero connections, strictness is 0% */ |
---|
1546 | const double strictness = peerCount >= relaxStrictnessIfFewerThanN |
---|
1547 | ? 1.0 |
---|
1548 | : peerCount / (double)relaxStrictnessIfFewerThanN; |
---|
1549 | const int lo = MIN_UPLOAD_IDLE_SECS; |
---|
1550 | const int hi = MAX_UPLOAD_IDLE_SECS; |
---|
1551 | const int limit = lo + ((hi-lo) * strictness); |
---|
1552 | const time_t then = peer->pieceDataActivityDate; |
---|
1553 | const int idleTime = then ? (now-then) : 0; |
---|
1554 | if( idleTime > limit ) { |
---|
1555 | tordbg( t, "purging peer %s because it's been %d secs since we shared anything", |
---|
1556 | tr_peerIoAddrStr(&atom->addr,atom->port), idleTime ); |
---|
1557 | return TRUE; |
---|
1558 | } |
---|
1559 | } |
---|
1560 | |
---|
1561 | return FALSE; |
---|
1562 | } |
---|
1563 | |
---|
1564 | static tr_peer ** |
---|
1565 | getPeersToClose( Torrent * t, int * setmeSize ) |
---|
1566 | { |
---|
1567 | int i, peerCount, outsize; |
---|
1568 | tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1569 | struct tr_peer ** ret = tr_new( tr_peer*, peerCount ); |
---|
1570 | |
---|
1571 | assert( torrentIsLocked( t ) ); |
---|
1572 | |
---|
1573 | for( i=outsize=0; i<peerCount; ++i ) |
---|
1574 | if( shouldPeerBeClosed( t, peers[i], peerCount ) ) |
---|
1575 | ret[outsize++] = peers[i]; |
---|
1576 | |
---|
1577 | *setmeSize = outsize; |
---|
1578 | return ret; |
---|
1579 | } |
---|
1580 | |
---|
1581 | static int |
---|
1582 | compareAtomByTime( const void * va, const void * vb ) |
---|
1583 | { |
---|
1584 | const struct peer_atom * a = * (const struct peer_atom**) va; |
---|
1585 | const struct peer_atom * b = * (const struct peer_atom**) vb; |
---|
1586 | if( a->time < b->time ) return -1; |
---|
1587 | if( a->time > b->time ) return 1; |
---|
1588 | return 0; |
---|
1589 | } |
---|
1590 | |
---|
1591 | static struct peer_atom ** |
---|
1592 | getPeerCandidates( Torrent * t, int * setmeSize ) |
---|
1593 | { |
---|
1594 | int i, atomCount, retCount; |
---|
1595 | struct peer_atom ** atoms; |
---|
1596 | struct peer_atom ** ret; |
---|
1597 | const time_t now = time( NULL ); |
---|
1598 | const int seed = tr_torrentIsSeed( t->tor ); |
---|
1599 | |
---|
1600 | assert( torrentIsLocked( t ) ); |
---|
1601 | |
---|
1602 | atoms = (struct peer_atom**) tr_ptrArrayPeek( t->pool, &atomCount ); |
---|
1603 | ret = tr_new( struct peer_atom*, atomCount ); |
---|
1604 | for( i=retCount=0; i<atomCount; ++i ) |
---|
1605 | { |
---|
1606 | int wait, minWait, maxWait; |
---|
1607 | struct peer_atom * atom = atoms[i]; |
---|
1608 | |
---|
1609 | /* peer fed us too much bad data ... we only keep it around |
---|
1610 | * now to weed it out in case someone sends it to us via pex */ |
---|
1611 | if( atom->myflags & MYFLAG_BANNED ) { |
---|
1612 | tordbg( t, "RECONNECT peer %d (%s) is banned...", |
---|
1613 | i, tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1614 | continue; |
---|
1615 | } |
---|
1616 | |
---|
1617 | /* we don't need two connections to the same peer... */ |
---|
1618 | if( peerIsInUse( t, &atom->addr ) ) { |
---|
1619 | tordbg( t, "RECONNECT peer %d (%s) is in use..", |
---|
1620 | i, tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1621 | continue; |
---|
1622 | } |
---|
1623 | |
---|
1624 | /* no need to connect if we're both seeds... */ |
---|
1625 | if( seed && (atom->flags & ADDED_F_SEED_FLAG) ) { |
---|
1626 | tordbg( t, "RECONNECT peer %d (%s) is a seed and so are we..", |
---|
1627 | i, tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1628 | continue; |
---|
1629 | } |
---|
1630 | |
---|
1631 | /* if we used this peer recently, give someone else a turn */ |
---|
1632 | minWait = 60; /* one minute */ |
---|
1633 | maxWait = (60 * 10); /* ten minutes */ |
---|
1634 | wait = atom->numFails * 15; /* add 15 secs to the wait interval for each consecutive failure*/ |
---|
1635 | if( wait < minWait ) wait = minWait; |
---|
1636 | if( wait > maxWait ) wait = maxWait; |
---|
1637 | if( ( now - atom->time ) < wait ) { |
---|
1638 | tordbg( t, "RECONNECT peer %d (%s) is in its grace period of %d seconds..", |
---|
1639 | i, tr_peerIoAddrStr(&atom->addr,atom->port), wait ); |
---|
1640 | continue; |
---|
1641 | } |
---|
1642 | |
---|
1643 | ret[retCount++] = atom; |
---|
1644 | } |
---|
1645 | |
---|
1646 | qsort( ret, retCount, sizeof(struct peer_atom*), compareAtomByTime ); |
---|
1647 | *setmeSize = retCount; |
---|
1648 | return ret; |
---|
1649 | } |
---|
1650 | |
---|
1651 | static int |
---|
1652 | reconnectPulse( void * vtorrent ) |
---|
1653 | { |
---|
1654 | Torrent * t = vtorrent; |
---|
1655 | |
---|
1656 | torrentLock( t ); |
---|
1657 | |
---|
1658 | if( !t->isRunning ) |
---|
1659 | { |
---|
1660 | removeAllPeers( t ); |
---|
1661 | } |
---|
1662 | else |
---|
1663 | { |
---|
1664 | int i, nCandidates, nBad, nAdd; |
---|
1665 | struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates ); |
---|
1666 | struct tr_peer ** connections = getPeersToClose( t, &nBad ); |
---|
1667 | const int peerCount = tr_ptrArraySize( t->peers ); |
---|
1668 | |
---|
1669 | if( nBad || nCandidates ) |
---|
1670 | tordbg( t, "reconnect pulse for [%s]: %d bad connections, " |
---|
1671 | "%d connection candidates, %d atoms, max per pulse is %d", |
---|
1672 | t->tor->info.name, nBad, nCandidates, |
---|
1673 | tr_ptrArraySize(t->pool), |
---|
1674 | (int)MAX_RECONNECTIONS_PER_PULSE ); |
---|
1675 | |
---|
1676 | /* disconnect some peers. |
---|
1677 | if we got transferred piece data, then they might be good peers, |
---|
1678 | so reset their `numFails' weight to zero. otherwise we connected |
---|
1679 | to them fruitlessly, so mark it as another fail */ |
---|
1680 | for( i=0; i<nBad; ++i ) { |
---|
1681 | tr_peer * peer = connections[i]; |
---|
1682 | struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1683 | if( peer->pieceDataActivityDate ) |
---|
1684 | atom->numFails = 0; |
---|
1685 | else |
---|
1686 | ++atom->numFails; |
---|
1687 | removePeer( t, peer ); |
---|
1688 | } |
---|
1689 | |
---|
1690 | /* add some new ones */ |
---|
1691 | nAdd = MAX_CONNECTED_PEERS_PER_TORRENT - peerCount; |
---|
1692 | for( i=0; i<nAdd && i<nCandidates && i<MAX_RECONNECTIONS_PER_PULSE; ++i ) |
---|
1693 | { |
---|
1694 | tr_peerMgr * mgr = t->manager; |
---|
1695 | struct peer_atom * atom = candidates[i]; |
---|
1696 | tr_peerIo * io; |
---|
1697 | tr_handshake * handshake; |
---|
1698 | |
---|
1699 | tordbg( t, "Starting an OUTGOING connection with %s", |
---|
1700 | tr_peerIoAddrStr( &atom->addr, atom->port ) ); |
---|
1701 | |
---|
1702 | io = tr_peerIoNewOutgoing( mgr->handle, &atom->addr, atom->port, t->hash ); |
---|
1703 | |
---|
1704 | handshake = tr_handshakeNew( io, |
---|
1705 | mgr->handle->encryptionMode, |
---|
1706 | myHandshakeDoneCB, |
---|
1707 | mgr ); |
---|
1708 | |
---|
1709 | assert( tr_peerIoGetTorrentHash( io ) != NULL ); |
---|
1710 | |
---|
1711 | tr_ptrArrayInsertSorted( t->outgoingHandshakes, handshake, handshakeCompare ); |
---|
1712 | |
---|
1713 | atom->time = time( NULL ); |
---|
1714 | } |
---|
1715 | |
---|
1716 | /* cleanup */ |
---|
1717 | tr_free( connections ); |
---|
1718 | tr_free( candidates ); |
---|
1719 | } |
---|
1720 | |
---|
1721 | torrentUnlock( t ); |
---|
1722 | return TRUE; |
---|
1723 | } |
---|