1 | /* |
---|
2 | * This file Copyright (C) 2007-2008 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 7121 2008-11-16 21:15:37Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <string.h> /* memcpy, memcmp, strstr */ |
---|
16 | #include <stdlib.h> /* qsort */ |
---|
17 | #include <limits.h> /* INT_MAX */ |
---|
18 | |
---|
19 | #include <event.h> |
---|
20 | |
---|
21 | #include "transmission.h" |
---|
22 | #include "blocklist.h" |
---|
23 | #include "clients.h" |
---|
24 | #include "completion.h" |
---|
25 | #include "crypto.h" |
---|
26 | #include "handshake.h" |
---|
27 | #include "inout.h" /* tr_ioTestPiece */ |
---|
28 | #include "net.h" |
---|
29 | #include "peer-io.h" |
---|
30 | #include "peer-mgr.h" |
---|
31 | #include "peer-mgr-private.h" |
---|
32 | #include "peer-msgs.h" |
---|
33 | #include "ptrarray.h" |
---|
34 | #include "ratecontrol.h" |
---|
35 | #include "stats.h" /* tr_statsAddUploaded, tr_statsAddDownloaded */ |
---|
36 | #include "torrent.h" |
---|
37 | #include "trevent.h" |
---|
38 | #include "utils.h" |
---|
39 | #include "webseed.h" |
---|
40 | |
---|
41 | enum |
---|
42 | { |
---|
43 | /* how frequently to change which peers are choked */ |
---|
44 | RECHOKE_PERIOD_MSEC = ( 10 * 1000 ), |
---|
45 | |
---|
46 | /* minimum interval for refilling peers' request lists */ |
---|
47 | REFILL_PERIOD_MSEC = 333, |
---|
48 | |
---|
49 | /* when many peers are available, keep idle ones this long */ |
---|
50 | MIN_UPLOAD_IDLE_SECS = ( 60 * 3 ), |
---|
51 | |
---|
52 | /* when few peers are available, keep idle ones this long */ |
---|
53 | MAX_UPLOAD_IDLE_SECS = ( 60 * 10 ), |
---|
54 | |
---|
55 | /* how frequently to decide which peers live and die */ |
---|
56 | RECONNECT_PERIOD_MSEC = ( 2 * 1000 ), |
---|
57 | |
---|
58 | /* how frequently to reallocate bandwidth */ |
---|
59 | BANDWIDTH_PERIOD_MSEC = 250, |
---|
60 | |
---|
61 | /* max # of peers to ask fer per torrent per reconnect pulse */ |
---|
62 | MAX_RECONNECTIONS_PER_PULSE = 2, |
---|
63 | |
---|
64 | /* max number of peers to ask for per second overall. |
---|
65 | * this throttle is to avoid overloading the router */ |
---|
66 | MAX_CONNECTIONS_PER_SECOND = 4, |
---|
67 | |
---|
68 | /* number of unchoked peers per torrent. |
---|
69 | * FIXME: this probably ought to be configurable */ |
---|
70 | MAX_UNCHOKED_PEERS = 12, |
---|
71 | |
---|
72 | /* number of bad pieces a peer is allowed to send before we ban them */ |
---|
73 | MAX_BAD_PIECES_PER_PEER = 3, |
---|
74 | |
---|
75 | /* use for bitwise operations w/peer_atom.myflags */ |
---|
76 | MYFLAG_BANNED = 1, |
---|
77 | |
---|
78 | /* unreachable for now... but not banned. |
---|
79 | * if they try to connect to us it's okay */ |
---|
80 | MYFLAG_UNREACHABLE = 2, |
---|
81 | |
---|
82 | /* the minimum we'll wait before attempting to reconnect to a peer */ |
---|
83 | MINIMUM_RECONNECT_INTERVAL_SECS = 5 |
---|
84 | }; |
---|
85 | |
---|
86 | |
---|
87 | /** |
---|
88 | *** |
---|
89 | **/ |
---|
90 | |
---|
91 | /* We keep one of these for every peer we know about, whether |
---|
92 | * it's connected or not, so the struct must be small. |
---|
93 | * When our current connections underperform, we dip back |
---|
94 | * into this list for new ones. */ |
---|
95 | struct peer_atom |
---|
96 | { |
---|
97 | uint8_t from; |
---|
98 | uint8_t flags; /* these match the added_f flags */ |
---|
99 | uint8_t myflags; /* flags that aren't defined in added_f */ |
---|
100 | uint16_t port; |
---|
101 | uint16_t numFails; |
---|
102 | struct in_addr addr; |
---|
103 | time_t time; /* when the peer's connection status last changed */ |
---|
104 | time_t piece_data_time; |
---|
105 | }; |
---|
106 | |
---|
107 | typedef struct |
---|
108 | { |
---|
109 | unsigned int isRunning : 1; |
---|
110 | |
---|
111 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
112 | int * pendingRequestCount; |
---|
113 | tr_ptrArray * outgoingHandshakes; /* tr_handshake */ |
---|
114 | tr_ptrArray * pool; /* struct peer_atom */ |
---|
115 | tr_ptrArray * peers; /* tr_peer */ |
---|
116 | tr_ptrArray * webseeds; /* tr_webseed */ |
---|
117 | tr_timer * reconnectTimer; |
---|
118 | tr_timer * rechokeTimer; |
---|
119 | tr_timer * refillTimer; |
---|
120 | tr_torrent * tor; |
---|
121 | tr_peer * optimistic; /* the optimistic peer, or NULL if none */ |
---|
122 | |
---|
123 | struct tr_peerMgr * manager; |
---|
124 | } |
---|
125 | Torrent; |
---|
126 | |
---|
127 | struct tr_peerMgr |
---|
128 | { |
---|
129 | tr_session * session; |
---|
130 | tr_ptrArray * torrents; /* Torrent */ |
---|
131 | tr_ptrArray * incomingHandshakes; /* tr_handshake */ |
---|
132 | tr_timer * bandwidthTimer; |
---|
133 | tr_ratecontrol * globalPoolRawSpeed[2]; |
---|
134 | }; |
---|
135 | |
---|
136 | #define tordbg( t, ... ) \ |
---|
137 | do { \ |
---|
138 | if( tr_deepLoggingIsActive( ) ) \ |
---|
139 | tr_deepLog( __FILE__, __LINE__, t->tor->info.name, __VA_ARGS__ ); \ |
---|
140 | } while( 0 ) |
---|
141 | |
---|
142 | #define dbgmsg( ... ) \ |
---|
143 | do { \ |
---|
144 | if( tr_deepLoggingIsActive( ) ) \ |
---|
145 | tr_deepLog( __FILE__, __LINE__, NULL, __VA_ARGS__ ); \ |
---|
146 | } while( 0 ) |
---|
147 | |
---|
148 | /** |
---|
149 | *** |
---|
150 | **/ |
---|
151 | |
---|
152 | static void |
---|
153 | managerLock( const struct tr_peerMgr * manager ) |
---|
154 | { |
---|
155 | tr_globalLock( manager->session ); |
---|
156 | } |
---|
157 | |
---|
158 | static void |
---|
159 | managerUnlock( const struct tr_peerMgr * manager ) |
---|
160 | { |
---|
161 | tr_globalUnlock( manager->session ); |
---|
162 | } |
---|
163 | |
---|
164 | static void |
---|
165 | torrentLock( Torrent * torrent ) |
---|
166 | { |
---|
167 | managerLock( torrent->manager ); |
---|
168 | } |
---|
169 | |
---|
170 | static void |
---|
171 | torrentUnlock( Torrent * torrent ) |
---|
172 | { |
---|
173 | managerUnlock( torrent->manager ); |
---|
174 | } |
---|
175 | |
---|
176 | static int |
---|
177 | torrentIsLocked( const Torrent * t ) |
---|
178 | { |
---|
179 | return tr_globalIsLocked( t->manager->session ); |
---|
180 | } |
---|
181 | |
---|
182 | /** |
---|
183 | *** |
---|
184 | **/ |
---|
185 | |
---|
186 | static int |
---|
187 | compareAddresses( const struct in_addr * a, |
---|
188 | const struct in_addr * b ) |
---|
189 | { |
---|
190 | if( a->s_addr != b->s_addr ) |
---|
191 | return a->s_addr < b->s_addr ? -1 : 1; |
---|
192 | |
---|
193 | return 0; |
---|
194 | } |
---|
195 | |
---|
196 | static int |
---|
197 | handshakeCompareToAddr( const void * va, |
---|
198 | const void * vb ) |
---|
199 | { |
---|
200 | const tr_handshake * a = va; |
---|
201 | |
---|
202 | return compareAddresses( tr_handshakeGetAddr( a, NULL ), vb ); |
---|
203 | } |
---|
204 | |
---|
205 | static int |
---|
206 | handshakeCompare( const void * a, |
---|
207 | const void * b ) |
---|
208 | { |
---|
209 | return handshakeCompareToAddr( a, tr_handshakeGetAddr( b, NULL ) ); |
---|
210 | } |
---|
211 | |
---|
212 | static tr_handshake* |
---|
213 | getExistingHandshake( tr_ptrArray * handshakes, |
---|
214 | const struct in_addr * in_addr ) |
---|
215 | { |
---|
216 | return tr_ptrArrayFindSorted( handshakes, |
---|
217 | in_addr, |
---|
218 | handshakeCompareToAddr ); |
---|
219 | } |
---|
220 | |
---|
221 | static int |
---|
222 | comparePeerAtomToAddress( const void * va, |
---|
223 | const void * vb ) |
---|
224 | { |
---|
225 | const struct peer_atom * a = va; |
---|
226 | |
---|
227 | return compareAddresses( &a->addr, vb ); |
---|
228 | } |
---|
229 | |
---|
230 | static int |
---|
231 | comparePeerAtoms( const void * va, |
---|
232 | const void * vb ) |
---|
233 | { |
---|
234 | const struct peer_atom * b = vb; |
---|
235 | |
---|
236 | return comparePeerAtomToAddress( va, &b->addr ); |
---|
237 | } |
---|
238 | |
---|
239 | /** |
---|
240 | *** |
---|
241 | **/ |
---|
242 | |
---|
243 | static int |
---|
244 | torrentCompare( const void * va, |
---|
245 | const void * vb ) |
---|
246 | { |
---|
247 | const Torrent * a = va; |
---|
248 | const Torrent * b = vb; |
---|
249 | |
---|
250 | return memcmp( a->hash, b->hash, SHA_DIGEST_LENGTH ); |
---|
251 | } |
---|
252 | |
---|
253 | static int |
---|
254 | torrentCompareToHash( const void * va, |
---|
255 | const void * vb ) |
---|
256 | { |
---|
257 | const Torrent * a = va; |
---|
258 | const uint8_t * b_hash = vb; |
---|
259 | |
---|
260 | return memcmp( a->hash, b_hash, SHA_DIGEST_LENGTH ); |
---|
261 | } |
---|
262 | |
---|
263 | static Torrent* |
---|
264 | getExistingTorrent( tr_peerMgr * manager, |
---|
265 | const uint8_t * hash ) |
---|
266 | { |
---|
267 | return (Torrent*) tr_ptrArrayFindSorted( manager->torrents, |
---|
268 | hash, |
---|
269 | torrentCompareToHash ); |
---|
270 | } |
---|
271 | |
---|
272 | static int |
---|
273 | peerCompare( const void * va, |
---|
274 | const void * vb ) |
---|
275 | { |
---|
276 | const tr_peer * a = va; |
---|
277 | const tr_peer * b = vb; |
---|
278 | |
---|
279 | return compareAddresses( &a->in_addr, &b->in_addr ); |
---|
280 | } |
---|
281 | |
---|
282 | static int |
---|
283 | peerCompareToAddr( const void * va, |
---|
284 | const void * vb ) |
---|
285 | { |
---|
286 | const tr_peer * a = va; |
---|
287 | |
---|
288 | return compareAddresses( &a->in_addr, vb ); |
---|
289 | } |
---|
290 | |
---|
291 | static tr_peer* |
---|
292 | getExistingPeer( Torrent * torrent, |
---|
293 | const struct in_addr * in_addr ) |
---|
294 | { |
---|
295 | assert( torrentIsLocked( torrent ) ); |
---|
296 | assert( in_addr ); |
---|
297 | |
---|
298 | return tr_ptrArrayFindSorted( torrent->peers, |
---|
299 | in_addr, |
---|
300 | peerCompareToAddr ); |
---|
301 | } |
---|
302 | |
---|
303 | static struct peer_atom* |
---|
304 | getExistingAtom( const Torrent * t, |
---|
305 | const struct in_addr * addr ) |
---|
306 | { |
---|
307 | assert( torrentIsLocked( t ) ); |
---|
308 | return tr_ptrArrayFindSorted( t->pool, addr, comparePeerAtomToAddress ); |
---|
309 | } |
---|
310 | |
---|
311 | static int |
---|
312 | peerIsInUse( const Torrent * ct, |
---|
313 | const struct in_addr * addr ) |
---|
314 | { |
---|
315 | Torrent * t = (Torrent*) ct; |
---|
316 | |
---|
317 | assert( torrentIsLocked ( t ) ); |
---|
318 | |
---|
319 | return getExistingPeer( t, addr ) |
---|
320 | || getExistingHandshake( t->outgoingHandshakes, addr ) |
---|
321 | || getExistingHandshake( t->manager->incomingHandshakes, addr ); |
---|
322 | } |
---|
323 | |
---|
324 | static tr_peer* |
---|
325 | peerConstructor( const struct in_addr * in_addr ) |
---|
326 | { |
---|
327 | tr_peer * p; |
---|
328 | |
---|
329 | p = tr_new0( tr_peer, 1 ); |
---|
330 | memcpy( &p->in_addr, in_addr, sizeof( struct in_addr ) ); |
---|
331 | p->pieceSpeed[TR_CLIENT_TO_PEER] = tr_rcInit( ); |
---|
332 | p->pieceSpeed[TR_PEER_TO_CLIENT] = tr_rcInit( ); |
---|
333 | return p; |
---|
334 | } |
---|
335 | |
---|
336 | static tr_peer* |
---|
337 | getPeer( Torrent * torrent, |
---|
338 | const struct in_addr * in_addr ) |
---|
339 | { |
---|
340 | tr_peer * peer; |
---|
341 | |
---|
342 | assert( torrentIsLocked( torrent ) ); |
---|
343 | |
---|
344 | peer = getExistingPeer( torrent, in_addr ); |
---|
345 | |
---|
346 | if( peer == NULL ) |
---|
347 | { |
---|
348 | peer = peerConstructor( in_addr ); |
---|
349 | tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare ); |
---|
350 | } |
---|
351 | |
---|
352 | return peer; |
---|
353 | } |
---|
354 | |
---|
355 | static void |
---|
356 | peerDestructor( tr_peer * peer ) |
---|
357 | { |
---|
358 | assert( peer ); |
---|
359 | assert( peer->msgs ); |
---|
360 | |
---|
361 | tr_peerMsgsUnsubscribe( peer->msgs, peer->msgsTag ); |
---|
362 | tr_peerMsgsFree( peer->msgs ); |
---|
363 | |
---|
364 | tr_peerIoFree( peer->io ); |
---|
365 | |
---|
366 | tr_bitfieldFree( peer->have ); |
---|
367 | tr_bitfieldFree( peer->blame ); |
---|
368 | tr_free( peer->client ); |
---|
369 | |
---|
370 | tr_rcClose( peer->pieceSpeed[TR_CLIENT_TO_PEER] ); |
---|
371 | tr_rcClose( peer->pieceSpeed[TR_PEER_TO_CLIENT] ); |
---|
372 | tr_free( peer ); |
---|
373 | } |
---|
374 | |
---|
375 | static void |
---|
376 | removePeer( Torrent * t, |
---|
377 | tr_peer * peer ) |
---|
378 | { |
---|
379 | tr_peer * removed; |
---|
380 | struct peer_atom * atom; |
---|
381 | |
---|
382 | assert( torrentIsLocked( t ) ); |
---|
383 | |
---|
384 | atom = getExistingAtom( t, &peer->in_addr ); |
---|
385 | assert( atom ); |
---|
386 | atom->time = time( NULL ); |
---|
387 | |
---|
388 | removed = tr_ptrArrayRemoveSorted( t->peers, peer, peerCompare ); |
---|
389 | assert( removed == peer ); |
---|
390 | peerDestructor( removed ); |
---|
391 | } |
---|
392 | |
---|
393 | static void |
---|
394 | removeAllPeers( Torrent * t ) |
---|
395 | { |
---|
396 | while( !tr_ptrArrayEmpty( t->peers ) ) |
---|
397 | removePeer( t, tr_ptrArrayNth( t->peers, 0 ) ); |
---|
398 | } |
---|
399 | |
---|
400 | static void |
---|
401 | torrentDestructor( void * vt ) |
---|
402 | { |
---|
403 | Torrent * t = vt; |
---|
404 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
405 | |
---|
406 | assert( t ); |
---|
407 | assert( !t->isRunning ); |
---|
408 | assert( t->peers ); |
---|
409 | assert( torrentIsLocked( t ) ); |
---|
410 | assert( tr_ptrArrayEmpty( t->outgoingHandshakes ) ); |
---|
411 | assert( tr_ptrArrayEmpty( t->peers ) ); |
---|
412 | |
---|
413 | memcpy( hash, t->hash, SHA_DIGEST_LENGTH ); |
---|
414 | |
---|
415 | tr_timerFree( &t->reconnectTimer ); |
---|
416 | tr_timerFree( &t->rechokeTimer ); |
---|
417 | tr_timerFree( &t->refillTimer ); |
---|
418 | |
---|
419 | tr_ptrArrayFree( t->webseeds, (PtrArrayForeachFunc)tr_webseedFree ); |
---|
420 | tr_ptrArrayFree( t->pool, (PtrArrayForeachFunc)tr_free ); |
---|
421 | tr_ptrArrayFree( t->outgoingHandshakes, NULL ); |
---|
422 | tr_ptrArrayFree( t->peers, NULL ); |
---|
423 | |
---|
424 | tr_free( t->pendingRequestCount ); |
---|
425 | tr_free( t ); |
---|
426 | } |
---|
427 | |
---|
428 | static void peerCallbackFunc( void * vpeer, |
---|
429 | void * vevent, |
---|
430 | void * vt ); |
---|
431 | |
---|
432 | static Torrent* |
---|
433 | torrentConstructor( tr_peerMgr * manager, |
---|
434 | tr_torrent * tor ) |
---|
435 | { |
---|
436 | int i; |
---|
437 | Torrent * t; |
---|
438 | |
---|
439 | t = tr_new0( Torrent, 1 ); |
---|
440 | t->manager = manager; |
---|
441 | t->tor = tor; |
---|
442 | t->pool = tr_ptrArrayNew( ); |
---|
443 | t->peers = tr_ptrArrayNew( ); |
---|
444 | t->webseeds = tr_ptrArrayNew( ); |
---|
445 | t->outgoingHandshakes = tr_ptrArrayNew( ); |
---|
446 | memcpy( t->hash, tor->info.hash, SHA_DIGEST_LENGTH ); |
---|
447 | |
---|
448 | for( i = 0; i < tor->info.webseedCount; ++i ) |
---|
449 | { |
---|
450 | tr_webseed * w = |
---|
451 | tr_webseedNew( tor, tor->info.webseeds[i], peerCallbackFunc, |
---|
452 | t ); |
---|
453 | tr_ptrArrayAppend( t->webseeds, w ); |
---|
454 | } |
---|
455 | |
---|
456 | return t; |
---|
457 | } |
---|
458 | |
---|
459 | /** |
---|
460 | * For explanation, see http://www.bittorrent.org/fast_extensions.html |
---|
461 | * Also see the "test-allowed-set" unit test |
---|
462 | * |
---|
463 | * @param k number of pieces in set |
---|
464 | * @param sz number of pieces in the torrent |
---|
465 | * @param infohash torrent's SHA1 hash |
---|
466 | * @param ip peer's address |
---|
467 | */ |
---|
468 | struct tr_bitfield * |
---|
469 | tr_peerMgrGenerateAllowedSet( |
---|
470 | const uint32_t k, |
---|
471 | const uint32_t sz, |
---|
472 | const uint8_t * |
---|
473 | infohash, |
---|
474 | const struct in_addr * ip ) |
---|
475 | { |
---|
476 | uint8_t w[SHA_DIGEST_LENGTH + 4]; |
---|
477 | uint8_t x[SHA_DIGEST_LENGTH]; |
---|
478 | tr_bitfield * a; |
---|
479 | uint32_t a_size; |
---|
480 | |
---|
481 | *(uint32_t*)w = ntohl( htonl( ip->s_addr ) & 0xffffff00 ); /* (1) */ |
---|
482 | memcpy( w + 4, infohash, SHA_DIGEST_LENGTH ); /* (2) */ |
---|
483 | tr_sha1( x, w, sizeof( w ), NULL ); /* (3) */ |
---|
484 | |
---|
485 | a = tr_bitfieldNew( sz ); |
---|
486 | a_size = 0; |
---|
487 | |
---|
488 | while( a_size < k ) |
---|
489 | { |
---|
490 | int i; |
---|
491 | for( i = 0; i < 5 && a_size < k; ++i ) /* (4) */ |
---|
492 | { |
---|
493 | uint32_t j = i * 4; /* (5) */ |
---|
494 | uint32_t y = ntohl( *( uint32_t* )( x + j ) ); /* (6) */ |
---|
495 | uint32_t index = y % sz; /* (7) */ |
---|
496 | if( !tr_bitfieldHas( a, index ) ) /* (8) */ |
---|
497 | { |
---|
498 | tr_bitfieldAdd( a, index ); /* (9) */ |
---|
499 | ++a_size; |
---|
500 | } |
---|
501 | } |
---|
502 | tr_sha1( x, x, sizeof( x ), NULL ); /* (3) */ |
---|
503 | } |
---|
504 | |
---|
505 | return a; |
---|
506 | } |
---|
507 | |
---|
508 | static int bandwidthPulse( void * vmgr ); |
---|
509 | |
---|
510 | |
---|
511 | tr_peerMgr* |
---|
512 | tr_peerMgrNew( tr_session * session ) |
---|
513 | { |
---|
514 | tr_peerMgr * m = tr_new0( tr_peerMgr, 1 ); |
---|
515 | |
---|
516 | m->session = session; |
---|
517 | m->torrents = tr_ptrArrayNew( ); |
---|
518 | m->incomingHandshakes = tr_ptrArrayNew( ); |
---|
519 | m->globalPoolRawSpeed[TR_CLIENT_TO_PEER] = tr_rcInit( ); |
---|
520 | m->globalPoolRawSpeed[TR_PEER_TO_CLIENT] = tr_rcInit( ); |
---|
521 | m->bandwidthTimer = tr_timerNew( session, bandwidthPulse, m, BANDWIDTH_PERIOD_MSEC ); |
---|
522 | return m; |
---|
523 | } |
---|
524 | |
---|
525 | void |
---|
526 | tr_peerMgrFree( tr_peerMgr * manager ) |
---|
527 | { |
---|
528 | managerLock( manager ); |
---|
529 | |
---|
530 | tr_timerFree( &manager->bandwidthTimer ); |
---|
531 | tr_rcClose( manager->globalPoolRawSpeed[TR_CLIENT_TO_PEER] ); |
---|
532 | tr_rcClose( manager->globalPoolRawSpeed[TR_PEER_TO_CLIENT] ); |
---|
533 | |
---|
534 | /* free the handshakes. Abort invokes handshakeDoneCB(), which removes |
---|
535 | * the item from manager->handshakes, so this is a little roundabout... */ |
---|
536 | while( !tr_ptrArrayEmpty( manager->incomingHandshakes ) ) |
---|
537 | tr_handshakeAbort( tr_ptrArrayNth( manager->incomingHandshakes, 0 ) ); |
---|
538 | |
---|
539 | tr_ptrArrayFree( manager->incomingHandshakes, NULL ); |
---|
540 | |
---|
541 | /* free the torrents. */ |
---|
542 | tr_ptrArrayFree( manager->torrents, torrentDestructor ); |
---|
543 | |
---|
544 | managerUnlock( manager ); |
---|
545 | tr_free( manager ); |
---|
546 | } |
---|
547 | |
---|
548 | static tr_peer** |
---|
549 | getConnectedPeers( Torrent * t, |
---|
550 | int * setmeCount ) |
---|
551 | { |
---|
552 | int i, peerCount, connectionCount; |
---|
553 | tr_peer **peers; |
---|
554 | tr_peer **ret; |
---|
555 | |
---|
556 | assert( torrentIsLocked( t ) ); |
---|
557 | |
---|
558 | peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
559 | ret = tr_new( tr_peer *, peerCount ); |
---|
560 | |
---|
561 | for( i = connectionCount = 0; i < peerCount; ++i ) |
---|
562 | if( peers[i]->msgs ) |
---|
563 | ret[connectionCount++] = peers[i]; |
---|
564 | |
---|
565 | *setmeCount = connectionCount; |
---|
566 | return ret; |
---|
567 | } |
---|
568 | |
---|
569 | static int |
---|
570 | clientIsDownloadingFrom( const tr_peer * peer ) |
---|
571 | { |
---|
572 | return peer->clientIsInterested && !peer->clientIsChoked; |
---|
573 | } |
---|
574 | |
---|
575 | static int |
---|
576 | clientIsUploadingTo( const tr_peer * peer ) |
---|
577 | { |
---|
578 | return peer->peerIsInterested && !peer->peerIsChoked; |
---|
579 | } |
---|
580 | |
---|
581 | /*** |
---|
582 | **** |
---|
583 | ***/ |
---|
584 | |
---|
585 | int |
---|
586 | tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr, |
---|
587 | const uint8_t * torrentHash, |
---|
588 | const struct in_addr * addr ) |
---|
589 | { |
---|
590 | int isSeed = FALSE; |
---|
591 | const Torrent * t = NULL; |
---|
592 | const struct peer_atom * atom = NULL; |
---|
593 | |
---|
594 | t = getExistingTorrent( (tr_peerMgr*)mgr, torrentHash ); |
---|
595 | if( t ) |
---|
596 | atom = getExistingAtom( t, addr ); |
---|
597 | if( atom ) |
---|
598 | isSeed = ( atom->flags & ADDED_F_SEED_FLAG ) != 0; |
---|
599 | |
---|
600 | return isSeed; |
---|
601 | } |
---|
602 | |
---|
603 | /**** |
---|
604 | ***** |
---|
605 | ***** REFILL |
---|
606 | ***** |
---|
607 | ****/ |
---|
608 | |
---|
609 | static void |
---|
610 | assertValidPiece( Torrent * t, tr_piece_index_t piece ) |
---|
611 | { |
---|
612 | assert( t ); |
---|
613 | assert( t->tor ); |
---|
614 | assert( piece < t->tor->info.pieceCount ); |
---|
615 | } |
---|
616 | |
---|
617 | static int |
---|
618 | getPieceRequests( Torrent * t, tr_piece_index_t piece ) |
---|
619 | { |
---|
620 | assertValidPiece( t, piece ); |
---|
621 | |
---|
622 | return t->pendingRequestCount ? t->pendingRequestCount[piece] : 0; |
---|
623 | } |
---|
624 | |
---|
625 | static void |
---|
626 | incrementPieceRequests( Torrent * t, tr_piece_index_t piece ) |
---|
627 | { |
---|
628 | assertValidPiece( t, piece ); |
---|
629 | |
---|
630 | if( t->pendingRequestCount == NULL ) |
---|
631 | t->pendingRequestCount = tr_new0( int, t->tor->info.pieceCount ); |
---|
632 | t->pendingRequestCount[piece]++; |
---|
633 | } |
---|
634 | |
---|
635 | static void |
---|
636 | decrementPieceRequests( Torrent * t, tr_piece_index_t piece ) |
---|
637 | { |
---|
638 | assertValidPiece( t, piece ); |
---|
639 | |
---|
640 | if( t->pendingRequestCount ) |
---|
641 | t->pendingRequestCount[piece]--; |
---|
642 | } |
---|
643 | |
---|
644 | struct tr_refill_piece |
---|
645 | { |
---|
646 | tr_priority_t priority; |
---|
647 | uint32_t piece; |
---|
648 | uint32_t peerCount; |
---|
649 | int random; |
---|
650 | int pendingRequestCount; |
---|
651 | int missingBlockCount; |
---|
652 | }; |
---|
653 | |
---|
654 | static int |
---|
655 | compareRefillPiece( const void * aIn, |
---|
656 | const void * bIn ) |
---|
657 | { |
---|
658 | const struct tr_refill_piece * a = aIn; |
---|
659 | const struct tr_refill_piece * b = bIn; |
---|
660 | |
---|
661 | /* if one piece has a higher priority, it goes first */ |
---|
662 | if( a->priority != b->priority ) |
---|
663 | return a->priority > b->priority ? -1 : 1; |
---|
664 | |
---|
665 | /* have a per-priority endgame */ |
---|
666 | if( a->pendingRequestCount != b->pendingRequestCount ) |
---|
667 | return a->pendingRequestCount < b->pendingRequestCount ? -1 : 1; |
---|
668 | |
---|
669 | /* fewer missing pieces goes first */ |
---|
670 | if( a->missingBlockCount != b->missingBlockCount ) |
---|
671 | return a->missingBlockCount < b->missingBlockCount ? -1 : 1; |
---|
672 | |
---|
673 | /* otherwise if one has fewer peers, it goes first */ |
---|
674 | if( a->peerCount != b->peerCount ) |
---|
675 | return a->peerCount < b->peerCount ? -1 : 1; |
---|
676 | |
---|
677 | /* otherwise go with our random seed */ |
---|
678 | if( a->random != b->random ) |
---|
679 | return a->random < b->random ? -1 : 1; |
---|
680 | |
---|
681 | return 0; |
---|
682 | } |
---|
683 | |
---|
684 | static tr_piece_index_t * |
---|
685 | getPreferredPieces( Torrent * t, |
---|
686 | tr_piece_index_t * pieceCount ) |
---|
687 | { |
---|
688 | const tr_torrent * tor = t->tor; |
---|
689 | const tr_info * inf = &tor->info; |
---|
690 | tr_piece_index_t i; |
---|
691 | tr_piece_index_t poolSize = 0; |
---|
692 | tr_piece_index_t * pool = tr_new( tr_piece_index_t , inf->pieceCount ); |
---|
693 | int peerCount; |
---|
694 | tr_peer** peers; |
---|
695 | |
---|
696 | assert( torrentIsLocked( t ) ); |
---|
697 | |
---|
698 | peers = getConnectedPeers( t, &peerCount ); |
---|
699 | |
---|
700 | /* make a list of the pieces that we want but don't have */ |
---|
701 | for( i = 0; i < inf->pieceCount; ++i ) |
---|
702 | if( !tor->info.pieces[i].dnd |
---|
703 | && !tr_cpPieceIsComplete( tor->completion, i ) ) |
---|
704 | pool[poolSize++] = i; |
---|
705 | |
---|
706 | /* sort the pool by which to request next */ |
---|
707 | if( poolSize > 1 ) |
---|
708 | { |
---|
709 | tr_piece_index_t j; |
---|
710 | struct tr_refill_piece * p = tr_new( struct tr_refill_piece, poolSize ); |
---|
711 | |
---|
712 | for( j = 0; j < poolSize; ++j ) |
---|
713 | { |
---|
714 | int k; |
---|
715 | const tr_piece_index_t piece = pool[j]; |
---|
716 | struct tr_refill_piece * setme = p + j; |
---|
717 | |
---|
718 | setme->piece = piece; |
---|
719 | setme->priority = inf->pieces[piece].priority; |
---|
720 | setme->peerCount = 0; |
---|
721 | setme->random = tr_cryptoWeakRandInt( INT_MAX ); |
---|
722 | setme->pendingRequestCount = getPieceRequests( t, piece ); |
---|
723 | setme->missingBlockCount |
---|
724 | = tr_cpMissingBlocksInPiece( tor->completion, piece ); |
---|
725 | |
---|
726 | for( k = 0; k < peerCount; ++k ) |
---|
727 | { |
---|
728 | const tr_peer * peer = peers[k]; |
---|
729 | if( peer->peerIsInterested |
---|
730 | && !peer->clientIsChoked |
---|
731 | && tr_bitfieldHas( peer->have, piece ) ) |
---|
732 | ++setme->peerCount; |
---|
733 | } |
---|
734 | } |
---|
735 | |
---|
736 | qsort( p, poolSize, sizeof( struct tr_refill_piece ), |
---|
737 | compareRefillPiece ); |
---|
738 | |
---|
739 | for( j = 0; j < poolSize; ++j ) |
---|
740 | pool[j] = p[j].piece; |
---|
741 | |
---|
742 | tr_free( p ); |
---|
743 | } |
---|
744 | |
---|
745 | tr_free( peers ); |
---|
746 | |
---|
747 | *pieceCount = poolSize; |
---|
748 | return pool; |
---|
749 | } |
---|
750 | |
---|
751 | struct tr_blockIterator |
---|
752 | { |
---|
753 | Torrent * t; |
---|
754 | tr_block_index_t blockIndex, blockCount, *blocks; |
---|
755 | tr_piece_index_t pieceIndex, pieceCount, *pieces; |
---|
756 | }; |
---|
757 | |
---|
758 | static struct tr_blockIterator* |
---|
759 | blockIteratorNew( Torrent * t ) |
---|
760 | { |
---|
761 | struct tr_blockIterator * i = tr_new0( struct tr_blockIterator, 1 ); |
---|
762 | i->t = t; |
---|
763 | i->pieces = getPreferredPieces( t, &i->pieceCount ); |
---|
764 | i->blocks = tr_new0( tr_block_index_t, t->tor->blockCount ); |
---|
765 | return i; |
---|
766 | } |
---|
767 | |
---|
768 | static int |
---|
769 | blockIteratorNext( struct tr_blockIterator * i, tr_block_index_t * setme ) |
---|
770 | { |
---|
771 | int found; |
---|
772 | Torrent * t = i->t; |
---|
773 | tr_torrent * tor = t->tor; |
---|
774 | |
---|
775 | while( ( i->blockIndex == i->blockCount ) |
---|
776 | && ( i->pieceIndex < i->pieceCount ) ) |
---|
777 | { |
---|
778 | const tr_piece_index_t index = i->pieces[i->pieceIndex++]; |
---|
779 | const tr_block_index_t b = tr_torPieceFirstBlock( tor, index ); |
---|
780 | const tr_block_index_t e = b + tr_torPieceCountBlocks( tor, index ); |
---|
781 | tr_block_index_t block; |
---|
782 | |
---|
783 | assert( index < tor->info.pieceCount ); |
---|
784 | |
---|
785 | i->blockCount = 0; |
---|
786 | i->blockIndex = 0; |
---|
787 | for( block=b; block!=e; ++block ) |
---|
788 | if( !tr_cpBlockIsComplete( tor->completion, block ) ) |
---|
789 | i->blocks[i->blockCount++] = block; |
---|
790 | } |
---|
791 | |
---|
792 | if(( found = ( i->blockIndex < i->blockCount ))) |
---|
793 | *setme = i->blocks[i->blockIndex++]; |
---|
794 | |
---|
795 | return found; |
---|
796 | } |
---|
797 | |
---|
798 | static void |
---|
799 | blockIteratorFree( struct tr_blockIterator * i ) |
---|
800 | { |
---|
801 | tr_free( i->blocks ); |
---|
802 | tr_free( i->pieces ); |
---|
803 | tr_free( i ); |
---|
804 | } |
---|
805 | |
---|
806 | static tr_peer** |
---|
807 | getPeersUploadingToClient( Torrent * t, |
---|
808 | int * setmeCount ) |
---|
809 | { |
---|
810 | int j; |
---|
811 | int peerCount = 0; |
---|
812 | int retCount = 0; |
---|
813 | tr_peer ** peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
814 | tr_peer ** ret = tr_new( tr_peer *, peerCount ); |
---|
815 | |
---|
816 | j = 0; /* this is a temporary test to make sure we walk through all the peers */ |
---|
817 | if( peerCount ) |
---|
818 | { |
---|
819 | /* Get a list of peers we're downloading from. |
---|
820 | Pick a different starting point each time so all peers |
---|
821 | get a chance at being the first in line */ |
---|
822 | const int fencepost = tr_cryptoWeakRandInt( peerCount ); |
---|
823 | int i = fencepost; |
---|
824 | do { |
---|
825 | if( clientIsDownloadingFrom( peers[i] ) ) |
---|
826 | ret[retCount++] = peers[i]; |
---|
827 | i = ( i + 1 ) % peerCount; |
---|
828 | ++j; |
---|
829 | } while( i != fencepost ); |
---|
830 | } |
---|
831 | assert( j == peerCount ); |
---|
832 | *setmeCount = retCount; |
---|
833 | return ret; |
---|
834 | } |
---|
835 | |
---|
836 | static uint32_t |
---|
837 | getBlockOffsetInPiece( const tr_torrent * tor, uint64_t b ) |
---|
838 | { |
---|
839 | const uint64_t piecePos = tor->info.pieceSize * tr_torBlockPiece( tor, b ); |
---|
840 | const uint64_t blockPos = tor->blockSize * b; |
---|
841 | assert( blockPos >= piecePos ); |
---|
842 | return (uint32_t)( blockPos - piecePos ); |
---|
843 | } |
---|
844 | |
---|
845 | static int |
---|
846 | refillPulse( void * vtorrent ) |
---|
847 | { |
---|
848 | tr_block_index_t block; |
---|
849 | int peerCount; |
---|
850 | int webseedCount; |
---|
851 | tr_peer ** peers; |
---|
852 | tr_webseed ** webseeds; |
---|
853 | struct tr_blockIterator * blockIterator; |
---|
854 | Torrent * t = vtorrent; |
---|
855 | tr_torrent * tor = t->tor; |
---|
856 | |
---|
857 | if( !t->isRunning ) |
---|
858 | return TRUE; |
---|
859 | if( tr_torrentIsSeed( t->tor ) ) |
---|
860 | return TRUE; |
---|
861 | |
---|
862 | torrentLock( t ); |
---|
863 | tordbg( t, "Refilling Request Buffers..." ); |
---|
864 | |
---|
865 | blockIterator = blockIteratorNew( t ); |
---|
866 | peers = getPeersUploadingToClient( t, &peerCount ); |
---|
867 | webseedCount = tr_ptrArraySize( t->webseeds ); |
---|
868 | webseeds = tr_memdup( tr_ptrArrayBase( t->webseeds ), |
---|
869 | webseedCount * sizeof( tr_webseed* ) ); |
---|
870 | |
---|
871 | while( ( webseedCount || peerCount ) |
---|
872 | && blockIteratorNext( blockIterator, &block ) ) |
---|
873 | { |
---|
874 | int j; |
---|
875 | int handled = FALSE; |
---|
876 | |
---|
877 | const tr_piece_index_t index = tr_torBlockPiece( tor, block ); |
---|
878 | const uint32_t offset = getBlockOffsetInPiece( tor, block ); |
---|
879 | const uint32_t length = tr_torBlockCountBytes( tor, block ); |
---|
880 | |
---|
881 | /* find a peer who can ask for this block */ |
---|
882 | for( j=0; !handled && j<peerCount; ) |
---|
883 | { |
---|
884 | const int val = tr_peerMsgsAddRequest( peers[j]->msgs, |
---|
885 | index, offset, length ); |
---|
886 | switch( val ) |
---|
887 | { |
---|
888 | case TR_ADDREQ_FULL: |
---|
889 | case TR_ADDREQ_CLIENT_CHOKED: |
---|
890 | peers[j] = peers[--peerCount]; |
---|
891 | break; |
---|
892 | |
---|
893 | case TR_ADDREQ_MISSING: |
---|
894 | case TR_ADDREQ_DUPLICATE: |
---|
895 | ++j; |
---|
896 | break; |
---|
897 | |
---|
898 | case TR_ADDREQ_OK: |
---|
899 | incrementPieceRequests( t, index ); |
---|
900 | handled = TRUE; |
---|
901 | break; |
---|
902 | |
---|
903 | default: |
---|
904 | assert( 0 && "unhandled value" ); |
---|
905 | break; |
---|
906 | } |
---|
907 | } |
---|
908 | |
---|
909 | /* maybe one of the webseeds can do it */ |
---|
910 | for( j=0; !handled && j<webseedCount; ) |
---|
911 | { |
---|
912 | const tr_addreq_t val = tr_webseedAddRequest( webseeds[j], |
---|
913 | index, offset, length ); |
---|
914 | switch( val ) |
---|
915 | { |
---|
916 | case TR_ADDREQ_FULL: |
---|
917 | webseeds[j] = webseeds[--webseedCount]; |
---|
918 | break; |
---|
919 | |
---|
920 | case TR_ADDREQ_OK: |
---|
921 | incrementPieceRequests( t, index ); |
---|
922 | handled = TRUE; |
---|
923 | break; |
---|
924 | |
---|
925 | default: |
---|
926 | assert( 0 && "unhandled value" ); |
---|
927 | break; |
---|
928 | } |
---|
929 | } |
---|
930 | } |
---|
931 | |
---|
932 | /* cleanup */ |
---|
933 | blockIteratorFree( blockIterator ); |
---|
934 | tr_free( webseeds ); |
---|
935 | tr_free( peers ); |
---|
936 | |
---|
937 | t->refillTimer = NULL; |
---|
938 | torrentUnlock( t ); |
---|
939 | return FALSE; |
---|
940 | } |
---|
941 | |
---|
942 | static void |
---|
943 | broadcastGotBlock( Torrent * t, uint32_t index, uint32_t offset, uint32_t length ) |
---|
944 | { |
---|
945 | int i, size; |
---|
946 | tr_peer ** peers; |
---|
947 | |
---|
948 | assert( torrentIsLocked( t ) ); |
---|
949 | |
---|
950 | peers = getConnectedPeers( t, &size ); |
---|
951 | for( i=0; i<size; ++i ) |
---|
952 | tr_peerMsgsCancel( peers[i]->msgs, index, offset, length ); |
---|
953 | tr_free( peers ); |
---|
954 | } |
---|
955 | |
---|
956 | static void |
---|
957 | addStrike( Torrent * t, |
---|
958 | tr_peer * peer ) |
---|
959 | { |
---|
960 | tordbg( t, "increasing peer %s strike count to %d", |
---|
961 | tr_peerIoAddrStr( &peer->in_addr, |
---|
962 | peer->port ), peer->strikes + 1 ); |
---|
963 | |
---|
964 | if( ++peer->strikes >= MAX_BAD_PIECES_PER_PEER ) |
---|
965 | { |
---|
966 | struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
967 | atom->myflags |= MYFLAG_BANNED; |
---|
968 | peer->doPurge = 1; |
---|
969 | tordbg( t, "banning peer %s", |
---|
970 | tr_peerIoAddrStr( &atom->addr, atom->port ) ); |
---|
971 | } |
---|
972 | } |
---|
973 | |
---|
974 | static void |
---|
975 | gotBadPiece( Torrent * t, |
---|
976 | tr_piece_index_t pieceIndex ) |
---|
977 | { |
---|
978 | tr_torrent * tor = t->tor; |
---|
979 | const uint32_t byteCount = tr_torPieceCountBytes( tor, pieceIndex ); |
---|
980 | |
---|
981 | tor->corruptCur += byteCount; |
---|
982 | tor->downloadedCur -= MIN( tor->downloadedCur, byteCount ); |
---|
983 | } |
---|
984 | |
---|
985 | static void |
---|
986 | refillSoon( Torrent * t ) |
---|
987 | { |
---|
988 | if( t->refillTimer == NULL ) |
---|
989 | t->refillTimer = tr_timerNew( t->manager->session, |
---|
990 | refillPulse, t, |
---|
991 | REFILL_PERIOD_MSEC ); |
---|
992 | } |
---|
993 | |
---|
994 | static void |
---|
995 | peerCallbackFunc( void * vpeer, |
---|
996 | void * vevent, |
---|
997 | void * vt ) |
---|
998 | { |
---|
999 | tr_peer * peer = vpeer; /* may be NULL if peer is a webseed */ |
---|
1000 | Torrent * t = (Torrent *) vt; |
---|
1001 | const tr_peer_event * e = vevent; |
---|
1002 | |
---|
1003 | torrentLock( t ); |
---|
1004 | |
---|
1005 | switch( e->eventType ) |
---|
1006 | { |
---|
1007 | case TR_PEER_NEED_REQ: |
---|
1008 | refillSoon( t ); |
---|
1009 | break; |
---|
1010 | |
---|
1011 | case TR_PEER_CANCEL: |
---|
1012 | decrementPieceRequests( t, e->pieceIndex ); |
---|
1013 | break; |
---|
1014 | |
---|
1015 | case TR_PEER_PEER_GOT_DATA: |
---|
1016 | { |
---|
1017 | const time_t now = time( NULL ); |
---|
1018 | tr_torrent * tor = t->tor; |
---|
1019 | tor->activityDate = now; |
---|
1020 | tor->uploadedCur += e->length; |
---|
1021 | if( peer ) |
---|
1022 | tr_rcTransferred ( peer->pieceSpeed[TR_CLIENT_TO_PEER], e->length ); |
---|
1023 | tr_rcTransferred ( tor->pieceSpeed[TR_CLIENT_TO_PEER], e->length ); |
---|
1024 | tr_rcTransferred ( tor->session->pieceSpeed[TR_CLIENT_TO_PEER], e->length ); |
---|
1025 | tr_statsAddUploaded( tor->session, e->length ); |
---|
1026 | if( peer ) |
---|
1027 | { |
---|
1028 | struct peer_atom * a = getExistingAtom( t, &peer->in_addr ); |
---|
1029 | a->piece_data_time = now; |
---|
1030 | } |
---|
1031 | break; |
---|
1032 | } |
---|
1033 | |
---|
1034 | case TR_PEER_CLIENT_GOT_DATA: |
---|
1035 | { |
---|
1036 | const time_t now = time( NULL ); |
---|
1037 | tr_torrent * tor = t->tor; |
---|
1038 | tor->activityDate = now; |
---|
1039 | tr_statsAddDownloaded( tor->session, e->length ); |
---|
1040 | if( peer ) |
---|
1041 | tr_rcTransferred ( peer->pieceSpeed[TR_PEER_TO_CLIENT], e->length ); |
---|
1042 | tr_rcTransferred ( tor->pieceSpeed[TR_PEER_TO_CLIENT], e->length ); |
---|
1043 | tr_rcTransferred ( tor->session->pieceSpeed[TR_PEER_TO_CLIENT], e->length ); |
---|
1044 | /* only add this to downloadedCur if we got it from a peer -- |
---|
1045 | * webseeds shouldn't count against our ratio. As one tracker |
---|
1046 | * admin put it, "Those pieces are downloaded directly from the |
---|
1047 | * content distributor, not the peers, it is the tracker's job |
---|
1048 | * to manage the swarms, not the web server and does not fit |
---|
1049 | * into the jurisdiction of the tracker." */ |
---|
1050 | if( peer ) |
---|
1051 | tor->downloadedCur += e->length; |
---|
1052 | if( peer ) { |
---|
1053 | struct peer_atom * a = getExistingAtom( t, &peer->in_addr ); |
---|
1054 | a->piece_data_time = now; |
---|
1055 | } |
---|
1056 | break; |
---|
1057 | } |
---|
1058 | |
---|
1059 | case TR_PEER_PEER_PROGRESS: |
---|
1060 | { |
---|
1061 | if( peer ) |
---|
1062 | { |
---|
1063 | struct peer_atom * atom = getExistingAtom( t, |
---|
1064 | &peer->in_addr ); |
---|
1065 | const int peerIsSeed = e->progress >= 1.0; |
---|
1066 | if( peerIsSeed ) |
---|
1067 | { |
---|
1068 | tordbg( t, "marking peer %s as a seed", |
---|
1069 | tr_peerIoAddrStr( &atom->addr, |
---|
1070 | atom->port ) ); |
---|
1071 | atom->flags |= ADDED_F_SEED_FLAG; |
---|
1072 | } |
---|
1073 | else |
---|
1074 | { |
---|
1075 | tordbg( t, "marking peer %s as a non-seed", |
---|
1076 | tr_peerIoAddrStr( &atom->addr, |
---|
1077 | atom->port ) ); |
---|
1078 | atom->flags &= ~ADDED_F_SEED_FLAG; |
---|
1079 | } |
---|
1080 | } |
---|
1081 | break; |
---|
1082 | } |
---|
1083 | |
---|
1084 | case TR_PEER_CLIENT_GOT_BLOCK: |
---|
1085 | { |
---|
1086 | tr_torrent * tor = t->tor; |
---|
1087 | |
---|
1088 | tr_block_index_t block = _tr_block( tor, e->pieceIndex, |
---|
1089 | e->offset ); |
---|
1090 | |
---|
1091 | tr_cpBlockAdd( tor->completion, block ); |
---|
1092 | decrementPieceRequests( t, e->pieceIndex ); |
---|
1093 | |
---|
1094 | broadcastGotBlock( t, e->pieceIndex, e->offset, e->length ); |
---|
1095 | |
---|
1096 | if( tr_cpPieceIsComplete( tor->completion, e->pieceIndex ) ) |
---|
1097 | { |
---|
1098 | const tr_piece_index_t p = e->pieceIndex; |
---|
1099 | const int ok = tr_ioTestPiece( tor, p ); |
---|
1100 | |
---|
1101 | if( !ok ) |
---|
1102 | { |
---|
1103 | tr_torerr( tor, |
---|
1104 | _( "Piece %lu, which was just downloaded, failed its checksum test" ), |
---|
1105 | (unsigned long)p ); |
---|
1106 | } |
---|
1107 | |
---|
1108 | tr_torrentSetHasPiece( tor, p, ok ); |
---|
1109 | tr_torrentSetPieceChecked( tor, p, TRUE ); |
---|
1110 | tr_peerMgrSetBlame( tor->session->peerMgr, tor->info.hash, p, ok ); |
---|
1111 | |
---|
1112 | if( !ok ) |
---|
1113 | gotBadPiece( t, p ); |
---|
1114 | else |
---|
1115 | { |
---|
1116 | int i, peerCount; |
---|
1117 | tr_peer ** peers = getConnectedPeers( t, &peerCount ); |
---|
1118 | for( i = 0; i < peerCount; ++i ) |
---|
1119 | tr_peerMsgsHave( peers[i]->msgs, p ); |
---|
1120 | tr_free( peers ); |
---|
1121 | } |
---|
1122 | |
---|
1123 | tr_torrentRecheckCompleteness( tor ); |
---|
1124 | } |
---|
1125 | break; |
---|
1126 | } |
---|
1127 | |
---|
1128 | case TR_PEER_ERROR: |
---|
1129 | if( e->err == EINVAL ) |
---|
1130 | { |
---|
1131 | addStrike( t, peer ); |
---|
1132 | peer->doPurge = 1; |
---|
1133 | } |
---|
1134 | else if( ( e->err == ERANGE ) |
---|
1135 | || ( e->err == EMSGSIZE ) |
---|
1136 | || ( e->err == ENOTCONN ) ) |
---|
1137 | { |
---|
1138 | /* some protocol error from the peer */ |
---|
1139 | peer->doPurge = 1; |
---|
1140 | } |
---|
1141 | else /* a local error, such as an IO error */ |
---|
1142 | { |
---|
1143 | t->tor->error = e->err; |
---|
1144 | tr_strlcpy( t->tor->errorString, |
---|
1145 | tr_strerror( t->tor->error ), |
---|
1146 | sizeof( t->tor->errorString ) ); |
---|
1147 | tr_torrentStop( t->tor ); |
---|
1148 | } |
---|
1149 | break; |
---|
1150 | |
---|
1151 | default: |
---|
1152 | assert( 0 ); |
---|
1153 | } |
---|
1154 | |
---|
1155 | torrentUnlock( t ); |
---|
1156 | } |
---|
1157 | |
---|
1158 | static void |
---|
1159 | ensureAtomExists( Torrent * t, |
---|
1160 | const struct in_addr * addr, |
---|
1161 | uint16_t port, |
---|
1162 | uint8_t flags, |
---|
1163 | uint8_t from ) |
---|
1164 | { |
---|
1165 | if( getExistingAtom( t, addr ) == NULL ) |
---|
1166 | { |
---|
1167 | struct peer_atom * a; |
---|
1168 | a = tr_new0( struct peer_atom, 1 ); |
---|
1169 | a->addr = *addr; |
---|
1170 | a->port = port; |
---|
1171 | a->flags = flags; |
---|
1172 | a->from = from; |
---|
1173 | tordbg( t, "got a new atom: %s", |
---|
1174 | tr_peerIoAddrStr( &a->addr, a->port ) ); |
---|
1175 | tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms ); |
---|
1176 | } |
---|
1177 | } |
---|
1178 | |
---|
1179 | static int |
---|
1180 | getMaxPeerCount( const tr_torrent * tor ) |
---|
1181 | { |
---|
1182 | return tor->maxConnectedPeers; |
---|
1183 | } |
---|
1184 | |
---|
1185 | static int |
---|
1186 | getPeerCount( const Torrent * t ) |
---|
1187 | { |
---|
1188 | return tr_ptrArraySize( t->peers ) + tr_ptrArraySize( |
---|
1189 | t->outgoingHandshakes ); |
---|
1190 | } |
---|
1191 | |
---|
1192 | /* FIXME: this is kind of a mess. */ |
---|
1193 | static int |
---|
1194 | myHandshakeDoneCB( tr_handshake * handshake, |
---|
1195 | tr_peerIo * io, |
---|
1196 | int isConnected, |
---|
1197 | const uint8_t * peer_id, |
---|
1198 | void * vmanager ) |
---|
1199 | { |
---|
1200 | int ok = isConnected; |
---|
1201 | int success = FALSE; |
---|
1202 | uint16_t port; |
---|
1203 | const struct in_addr * addr; |
---|
1204 | tr_peerMgr * manager = (tr_peerMgr*) vmanager; |
---|
1205 | Torrent * t; |
---|
1206 | tr_handshake * ours; |
---|
1207 | |
---|
1208 | assert( io ); |
---|
1209 | assert( isConnected == 0 || isConnected == 1 ); |
---|
1210 | |
---|
1211 | t = tr_peerIoHasTorrentHash( io ) |
---|
1212 | ? getExistingTorrent( manager, tr_peerIoGetTorrentHash( io ) ) |
---|
1213 | : NULL; |
---|
1214 | |
---|
1215 | if( tr_peerIoIsIncoming ( io ) ) |
---|
1216 | ours = tr_ptrArrayRemoveSorted( manager->incomingHandshakes, |
---|
1217 | handshake, handshakeCompare ); |
---|
1218 | else if( t ) |
---|
1219 | ours = tr_ptrArrayRemoveSorted( t->outgoingHandshakes, |
---|
1220 | handshake, handshakeCompare ); |
---|
1221 | else |
---|
1222 | ours = handshake; |
---|
1223 | |
---|
1224 | assert( ours ); |
---|
1225 | assert( ours == handshake ); |
---|
1226 | |
---|
1227 | if( t ) |
---|
1228 | torrentLock( t ); |
---|
1229 | |
---|
1230 | addr = tr_peerIoGetAddress( io, &port ); |
---|
1231 | |
---|
1232 | if( !ok || !t || !t->isRunning ) |
---|
1233 | { |
---|
1234 | if( t ) |
---|
1235 | { |
---|
1236 | struct peer_atom * atom = getExistingAtom( t, addr ); |
---|
1237 | if( atom ) |
---|
1238 | ++atom->numFails; |
---|
1239 | } |
---|
1240 | |
---|
1241 | tr_peerIoFree( io ); |
---|
1242 | } |
---|
1243 | else /* looking good */ |
---|
1244 | { |
---|
1245 | struct peer_atom * atom; |
---|
1246 | ensureAtomExists( t, addr, port, 0, TR_PEER_FROM_INCOMING ); |
---|
1247 | atom = getExistingAtom( t, addr ); |
---|
1248 | atom->time = time( NULL ); |
---|
1249 | |
---|
1250 | if( atom->myflags & MYFLAG_BANNED ) |
---|
1251 | { |
---|
1252 | tordbg( t, "banned peer %s tried to reconnect", |
---|
1253 | tr_peerIoAddrStr( &atom->addr, |
---|
1254 | atom->port ) ); |
---|
1255 | tr_peerIoFree( io ); |
---|
1256 | } |
---|
1257 | else if( tr_peerIoIsIncoming( io ) |
---|
1258 | && ( getPeerCount( t ) >= getMaxPeerCount( t->tor ) ) ) |
---|
1259 | |
---|
1260 | { |
---|
1261 | tr_peerIoFree( io ); |
---|
1262 | } |
---|
1263 | else |
---|
1264 | { |
---|
1265 | tr_peer * peer = getExistingPeer( t, addr ); |
---|
1266 | |
---|
1267 | if( peer ) /* we already have this peer */ |
---|
1268 | { |
---|
1269 | tr_peerIoFree( io ); |
---|
1270 | } |
---|
1271 | else |
---|
1272 | { |
---|
1273 | peer = getPeer( t, addr ); |
---|
1274 | tr_free( peer->client ); |
---|
1275 | |
---|
1276 | if( !peer_id ) |
---|
1277 | peer->client = NULL; |
---|
1278 | else |
---|
1279 | { |
---|
1280 | char client[128]; |
---|
1281 | tr_clientForId( client, sizeof( client ), peer_id ); |
---|
1282 | peer->client = tr_strdup( client ); |
---|
1283 | } |
---|
1284 | peer->port = port; |
---|
1285 | peer->io = io; |
---|
1286 | peer->msgs = |
---|
1287 | tr_peerMsgsNew( t->tor, peer, peerCallbackFunc, t, |
---|
1288 | &peer->msgsTag ); |
---|
1289 | |
---|
1290 | success = TRUE; |
---|
1291 | } |
---|
1292 | } |
---|
1293 | } |
---|
1294 | |
---|
1295 | if( t ) |
---|
1296 | torrentUnlock( t ); |
---|
1297 | |
---|
1298 | return success; |
---|
1299 | } |
---|
1300 | |
---|
1301 | void |
---|
1302 | tr_peerMgrAddIncoming( tr_peerMgr * manager, |
---|
1303 | struct in_addr * addr, |
---|
1304 | uint16_t port, |
---|
1305 | int socket ) |
---|
1306 | { |
---|
1307 | managerLock( manager ); |
---|
1308 | |
---|
1309 | if( tr_sessionIsAddressBlocked( manager->session, addr ) ) |
---|
1310 | { |
---|
1311 | tr_dbg( "Banned IP address \"%s\" tried to connect to us", |
---|
1312 | inet_ntoa( *addr ) ); |
---|
1313 | tr_netClose( socket ); |
---|
1314 | } |
---|
1315 | else if( getExistingHandshake( manager->incomingHandshakes, addr ) ) |
---|
1316 | { |
---|
1317 | tr_netClose( socket ); |
---|
1318 | } |
---|
1319 | else /* we don't have a connetion to them yet... */ |
---|
1320 | { |
---|
1321 | tr_peerIo * io; |
---|
1322 | tr_handshake * handshake; |
---|
1323 | |
---|
1324 | io = tr_peerIoNewIncoming( manager->session, addr, port, socket ); |
---|
1325 | |
---|
1326 | handshake = tr_handshakeNew( io, |
---|
1327 | manager->session->encryptionMode, |
---|
1328 | myHandshakeDoneCB, |
---|
1329 | manager ); |
---|
1330 | |
---|
1331 | tr_ptrArrayInsertSorted( manager->incomingHandshakes, handshake, |
---|
1332 | handshakeCompare ); |
---|
1333 | } |
---|
1334 | |
---|
1335 | managerUnlock( manager ); |
---|
1336 | } |
---|
1337 | |
---|
1338 | void |
---|
1339 | tr_peerMgrAddPex( tr_peerMgr * manager, |
---|
1340 | const uint8_t * torrentHash, |
---|
1341 | uint8_t from, |
---|
1342 | const tr_pex * pex ) |
---|
1343 | { |
---|
1344 | Torrent * t; |
---|
1345 | |
---|
1346 | managerLock( manager ); |
---|
1347 | |
---|
1348 | t = getExistingTorrent( manager, torrentHash ); |
---|
1349 | if( !tr_sessionIsAddressBlocked( t->manager->session, &pex->in_addr ) ) |
---|
1350 | ensureAtomExists( t, &pex->in_addr, pex->port, pex->flags, from ); |
---|
1351 | |
---|
1352 | managerUnlock( manager ); |
---|
1353 | } |
---|
1354 | |
---|
1355 | tr_pex * |
---|
1356 | tr_peerMgrCompactToPex( const void * compact, |
---|
1357 | size_t compactLen, |
---|
1358 | const uint8_t * added_f, |
---|
1359 | size_t added_f_len, |
---|
1360 | size_t * pexCount ) |
---|
1361 | { |
---|
1362 | size_t i; |
---|
1363 | size_t n = compactLen / 6; |
---|
1364 | const uint8_t * walk = compact; |
---|
1365 | tr_pex * pex = tr_new0( tr_pex, n ); |
---|
1366 | |
---|
1367 | for( i = 0; i < n; ++i ) |
---|
1368 | { |
---|
1369 | memcpy( &pex[i].in_addr, walk, 4 ); walk += 4; |
---|
1370 | memcpy( &pex[i].port, walk, 2 ); walk += 2; |
---|
1371 | if( added_f && ( n == added_f_len ) ) |
---|
1372 | pex[i].flags = added_f[i]; |
---|
1373 | } |
---|
1374 | |
---|
1375 | *pexCount = n; |
---|
1376 | return pex; |
---|
1377 | } |
---|
1378 | |
---|
1379 | /** |
---|
1380 | *** |
---|
1381 | **/ |
---|
1382 | |
---|
1383 | void |
---|
1384 | tr_peerMgrSetBlame( tr_peerMgr * manager, |
---|
1385 | const uint8_t * torrentHash, |
---|
1386 | tr_piece_index_t pieceIndex, |
---|
1387 | int success ) |
---|
1388 | { |
---|
1389 | if( !success ) |
---|
1390 | { |
---|
1391 | int peerCount, i; |
---|
1392 | Torrent * t = getExistingTorrent( manager, torrentHash ); |
---|
1393 | tr_peer ** peers; |
---|
1394 | |
---|
1395 | assert( torrentIsLocked( t ) ); |
---|
1396 | |
---|
1397 | peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1398 | for( i = 0; i < peerCount; ++i ) |
---|
1399 | { |
---|
1400 | tr_peer * peer = peers[i]; |
---|
1401 | if( tr_bitfieldHas( peer->blame, pieceIndex ) ) |
---|
1402 | { |
---|
1403 | tordbg( |
---|
1404 | t, |
---|
1405 | "peer %s contributed to corrupt piece (%d); now has %d strikes", |
---|
1406 | tr_peerIoAddrStr( &peer->in_addr, peer->port ), |
---|
1407 | pieceIndex, (int)peer->strikes + 1 ); |
---|
1408 | addStrike( t, peer ); |
---|
1409 | } |
---|
1410 | } |
---|
1411 | } |
---|
1412 | } |
---|
1413 | |
---|
1414 | int |
---|
1415 | tr_pexCompare( const void * va, |
---|
1416 | const void * vb ) |
---|
1417 | { |
---|
1418 | const tr_pex * a = va; |
---|
1419 | const tr_pex * b = vb; |
---|
1420 | int i = |
---|
1421 | memcmp( &a->in_addr, &b->in_addr, sizeof( struct in_addr ) ); |
---|
1422 | |
---|
1423 | if( i ) return i; |
---|
1424 | if( a->port < b->port ) return -1; |
---|
1425 | if( a->port > b->port ) return 1; |
---|
1426 | return 0; |
---|
1427 | } |
---|
1428 | |
---|
1429 | int tr_pexCompare( const void * a, |
---|
1430 | const void * b ); |
---|
1431 | |
---|
1432 | static int |
---|
1433 | peerPrefersCrypto( const tr_peer * peer ) |
---|
1434 | { |
---|
1435 | if( peer->encryption_preference == ENCRYPTION_PREFERENCE_YES ) |
---|
1436 | return TRUE; |
---|
1437 | |
---|
1438 | if( peer->encryption_preference == ENCRYPTION_PREFERENCE_NO ) |
---|
1439 | return FALSE; |
---|
1440 | |
---|
1441 | return tr_peerIoIsEncrypted( peer->io ); |
---|
1442 | } |
---|
1443 | |
---|
1444 | int |
---|
1445 | tr_peerMgrGetPeers( tr_peerMgr * manager, |
---|
1446 | const uint8_t * torrentHash, |
---|
1447 | tr_pex ** setme_pex ) |
---|
1448 | { |
---|
1449 | int peerCount = 0; |
---|
1450 | const Torrent * t; |
---|
1451 | |
---|
1452 | managerLock( manager ); |
---|
1453 | |
---|
1454 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1455 | if( !t ) |
---|
1456 | { |
---|
1457 | *setme_pex = NULL; |
---|
1458 | } |
---|
1459 | else |
---|
1460 | { |
---|
1461 | int i; |
---|
1462 | const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1463 | tr_pex * pex = tr_new( tr_pex, peerCount ); |
---|
1464 | tr_pex * walk = pex; |
---|
1465 | |
---|
1466 | for( i = 0; i < peerCount; ++i, ++walk ) |
---|
1467 | { |
---|
1468 | const tr_peer * peer = peers[i]; |
---|
1469 | walk->in_addr = peer->in_addr; |
---|
1470 | walk->port = peer->port; |
---|
1471 | walk->flags = 0; |
---|
1472 | if( peerPrefersCrypto( peer ) ) walk->flags |= ADDED_F_ENCRYPTION_FLAG; |
---|
1473 | if( peer->progress >= 1.0 ) walk->flags |= ADDED_F_SEED_FLAG; |
---|
1474 | } |
---|
1475 | |
---|
1476 | assert( ( walk - pex ) == peerCount ); |
---|
1477 | qsort( pex, peerCount, sizeof( tr_pex ), tr_pexCompare ); |
---|
1478 | *setme_pex = pex; |
---|
1479 | } |
---|
1480 | |
---|
1481 | managerUnlock( manager ); |
---|
1482 | return peerCount; |
---|
1483 | } |
---|
1484 | |
---|
1485 | static int reconnectPulse( void * vtorrent ); |
---|
1486 | |
---|
1487 | static int rechokePulse( void * vtorrent ); |
---|
1488 | |
---|
1489 | void |
---|
1490 | tr_peerMgrStartTorrent( tr_peerMgr * manager, |
---|
1491 | const uint8_t * torrentHash ) |
---|
1492 | { |
---|
1493 | Torrent * t; |
---|
1494 | |
---|
1495 | managerLock( manager ); |
---|
1496 | |
---|
1497 | t = getExistingTorrent( manager, torrentHash ); |
---|
1498 | |
---|
1499 | assert( t ); |
---|
1500 | assert( ( t->isRunning != 0 ) == ( t->reconnectTimer != NULL ) ); |
---|
1501 | assert( ( t->isRunning != 0 ) == ( t->rechokeTimer != NULL ) ); |
---|
1502 | |
---|
1503 | if( !t->isRunning ) |
---|
1504 | { |
---|
1505 | t->isRunning = 1; |
---|
1506 | |
---|
1507 | t->reconnectTimer = tr_timerNew( t->manager->session, |
---|
1508 | reconnectPulse, t, |
---|
1509 | RECONNECT_PERIOD_MSEC ); |
---|
1510 | |
---|
1511 | t->rechokeTimer = tr_timerNew( t->manager->session, |
---|
1512 | rechokePulse, t, |
---|
1513 | RECHOKE_PERIOD_MSEC ); |
---|
1514 | |
---|
1515 | reconnectPulse( t ); |
---|
1516 | |
---|
1517 | rechokePulse( t ); |
---|
1518 | |
---|
1519 | if( !tr_ptrArrayEmpty( t->webseeds ) ) |
---|
1520 | refillSoon( t ); |
---|
1521 | } |
---|
1522 | |
---|
1523 | managerUnlock( manager ); |
---|
1524 | } |
---|
1525 | |
---|
1526 | static void |
---|
1527 | stopTorrent( Torrent * t ) |
---|
1528 | { |
---|
1529 | assert( torrentIsLocked( t ) ); |
---|
1530 | |
---|
1531 | t->isRunning = 0; |
---|
1532 | tr_timerFree( &t->rechokeTimer ); |
---|
1533 | tr_timerFree( &t->reconnectTimer ); |
---|
1534 | |
---|
1535 | /* disconnect the peers. */ |
---|
1536 | tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)peerDestructor ); |
---|
1537 | tr_ptrArrayClear( t->peers ); |
---|
1538 | |
---|
1539 | /* disconnect the handshakes. handshakeAbort calls handshakeDoneCB(), |
---|
1540 | * which removes the handshake from t->outgoingHandshakes... */ |
---|
1541 | while( !tr_ptrArrayEmpty( t->outgoingHandshakes ) ) |
---|
1542 | tr_handshakeAbort( tr_ptrArrayNth( t->outgoingHandshakes, 0 ) ); |
---|
1543 | } |
---|
1544 | |
---|
1545 | void |
---|
1546 | tr_peerMgrStopTorrent( tr_peerMgr * manager, |
---|
1547 | const uint8_t * torrentHash ) |
---|
1548 | { |
---|
1549 | managerLock( manager ); |
---|
1550 | |
---|
1551 | stopTorrent( getExistingTorrent( manager, torrentHash ) ); |
---|
1552 | |
---|
1553 | managerUnlock( manager ); |
---|
1554 | } |
---|
1555 | |
---|
1556 | void |
---|
1557 | tr_peerMgrAddTorrent( tr_peerMgr * manager, |
---|
1558 | tr_torrent * tor ) |
---|
1559 | { |
---|
1560 | Torrent * t; |
---|
1561 | |
---|
1562 | managerLock( manager ); |
---|
1563 | |
---|
1564 | assert( tor ); |
---|
1565 | assert( getExistingTorrent( manager, tor->info.hash ) == NULL ); |
---|
1566 | |
---|
1567 | t = torrentConstructor( manager, tor ); |
---|
1568 | tr_ptrArrayInsertSorted( manager->torrents, t, torrentCompare ); |
---|
1569 | |
---|
1570 | managerUnlock( manager ); |
---|
1571 | } |
---|
1572 | |
---|
1573 | void |
---|
1574 | tr_peerMgrRemoveTorrent( tr_peerMgr * manager, |
---|
1575 | const uint8_t * torrentHash ) |
---|
1576 | { |
---|
1577 | Torrent * t; |
---|
1578 | |
---|
1579 | managerLock( manager ); |
---|
1580 | |
---|
1581 | t = getExistingTorrent( manager, torrentHash ); |
---|
1582 | assert( t ); |
---|
1583 | stopTorrent( t ); |
---|
1584 | tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare ); |
---|
1585 | torrentDestructor( t ); |
---|
1586 | |
---|
1587 | managerUnlock( manager ); |
---|
1588 | } |
---|
1589 | |
---|
1590 | void |
---|
1591 | tr_peerMgrTorrentAvailability( const tr_peerMgr * manager, |
---|
1592 | const uint8_t * torrentHash, |
---|
1593 | int8_t * tab, |
---|
1594 | unsigned int tabCount ) |
---|
1595 | { |
---|
1596 | tr_piece_index_t i; |
---|
1597 | const Torrent * t; |
---|
1598 | const tr_torrent * tor; |
---|
1599 | float interval; |
---|
1600 | int isComplete; |
---|
1601 | int peerCount; |
---|
1602 | const tr_peer ** peers; |
---|
1603 | |
---|
1604 | managerLock( manager ); |
---|
1605 | |
---|
1606 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1607 | tor = t->tor; |
---|
1608 | interval = tor->info.pieceCount / (float)tabCount; |
---|
1609 | isComplete = tor |
---|
1610 | && ( tr_cpGetStatus ( tor->completion ) == TR_CP_COMPLETE ); |
---|
1611 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1612 | |
---|
1613 | memset( tab, 0, tabCount ); |
---|
1614 | |
---|
1615 | for( i = 0; tor && i < tabCount; ++i ) |
---|
1616 | { |
---|
1617 | const int piece = i * interval; |
---|
1618 | |
---|
1619 | if( isComplete || tr_cpPieceIsComplete( tor->completion, piece ) ) |
---|
1620 | tab[i] = -1; |
---|
1621 | else if( peerCount ) |
---|
1622 | { |
---|
1623 | int j; |
---|
1624 | for( j = 0; j < peerCount; ++j ) |
---|
1625 | if( tr_bitfieldHas( peers[j]->have, i ) ) |
---|
1626 | ++tab[i]; |
---|
1627 | } |
---|
1628 | } |
---|
1629 | |
---|
1630 | managerUnlock( manager ); |
---|
1631 | } |
---|
1632 | |
---|
1633 | /* Returns the pieces that are available from peers */ |
---|
1634 | tr_bitfield* |
---|
1635 | tr_peerMgrGetAvailable( const tr_peerMgr * manager, |
---|
1636 | const uint8_t * torrentHash ) |
---|
1637 | { |
---|
1638 | int i, size; |
---|
1639 | Torrent * t; |
---|
1640 | tr_peer ** peers; |
---|
1641 | tr_bitfield * pieces; |
---|
1642 | |
---|
1643 | managerLock( manager ); |
---|
1644 | |
---|
1645 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1646 | pieces = tr_bitfieldNew( t->tor->info.pieceCount ); |
---|
1647 | peers = getConnectedPeers( t, &size ); |
---|
1648 | for( i = 0; i < size; ++i ) |
---|
1649 | tr_bitfieldOr( pieces, peers[i]->have ); |
---|
1650 | |
---|
1651 | managerUnlock( manager ); |
---|
1652 | tr_free( peers ); |
---|
1653 | return pieces; |
---|
1654 | } |
---|
1655 | |
---|
1656 | int |
---|
1657 | tr_peerMgrHasConnections( const tr_peerMgr * manager, |
---|
1658 | const uint8_t * torrentHash ) |
---|
1659 | { |
---|
1660 | int ret; |
---|
1661 | const Torrent * t; |
---|
1662 | |
---|
1663 | managerLock( manager ); |
---|
1664 | |
---|
1665 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1666 | ret = t && ( !tr_ptrArrayEmpty( t->peers ) |
---|
1667 | || !tr_ptrArrayEmpty( t->webseeds ) ); |
---|
1668 | |
---|
1669 | managerUnlock( manager ); |
---|
1670 | return ret; |
---|
1671 | } |
---|
1672 | |
---|
1673 | void |
---|
1674 | tr_peerMgrTorrentStats( const tr_peerMgr * manager, |
---|
1675 | const uint8_t * torrentHash, |
---|
1676 | int * setmePeersKnown, |
---|
1677 | int * setmePeersConnected, |
---|
1678 | int * setmeSeedsConnected, |
---|
1679 | int * setmeWebseedsSendingToUs, |
---|
1680 | int * setmePeersSendingToUs, |
---|
1681 | int * setmePeersGettingFromUs, |
---|
1682 | int * setmePeersFrom ) |
---|
1683 | { |
---|
1684 | int i, size; |
---|
1685 | const Torrent * t; |
---|
1686 | const tr_peer ** peers; |
---|
1687 | const tr_webseed ** webseeds; |
---|
1688 | |
---|
1689 | managerLock( manager ); |
---|
1690 | |
---|
1691 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1692 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size ); |
---|
1693 | |
---|
1694 | *setmePeersKnown = tr_ptrArraySize( t->pool ); |
---|
1695 | *setmePeersConnected = 0; |
---|
1696 | *setmeSeedsConnected = 0; |
---|
1697 | *setmePeersGettingFromUs = 0; |
---|
1698 | *setmePeersSendingToUs = 0; |
---|
1699 | *setmeWebseedsSendingToUs = 0; |
---|
1700 | |
---|
1701 | for( i = 0; i < TR_PEER_FROM__MAX; ++i ) |
---|
1702 | setmePeersFrom[i] = 0; |
---|
1703 | |
---|
1704 | for( i = 0; i < size; ++i ) |
---|
1705 | { |
---|
1706 | const tr_peer * peer = peers[i]; |
---|
1707 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1708 | |
---|
1709 | if( peer->io == NULL ) /* not connected */ |
---|
1710 | continue; |
---|
1711 | |
---|
1712 | ++ * setmePeersConnected; |
---|
1713 | |
---|
1714 | ++setmePeersFrom[atom->from]; |
---|
1715 | |
---|
1716 | if( clientIsDownloadingFrom( peer ) ) |
---|
1717 | ++ * setmePeersSendingToUs; |
---|
1718 | |
---|
1719 | if( clientIsUploadingTo( peer ) ) |
---|
1720 | ++ * setmePeersGettingFromUs; |
---|
1721 | |
---|
1722 | if( atom->flags & ADDED_F_SEED_FLAG ) |
---|
1723 | ++ * setmeSeedsConnected; |
---|
1724 | } |
---|
1725 | |
---|
1726 | webseeds = (const tr_webseed **) tr_ptrArrayPeek( t->webseeds, &size ); |
---|
1727 | for( i = 0; i < size; ++i ) |
---|
1728 | { |
---|
1729 | if( tr_webseedIsActive( webseeds[i] ) ) |
---|
1730 | ++ * setmeWebseedsSendingToUs; |
---|
1731 | } |
---|
1732 | |
---|
1733 | managerUnlock( manager ); |
---|
1734 | } |
---|
1735 | |
---|
1736 | float* |
---|
1737 | tr_peerMgrWebSpeeds( const tr_peerMgr * manager, |
---|
1738 | const uint8_t * torrentHash ) |
---|
1739 | { |
---|
1740 | const Torrent * t; |
---|
1741 | const tr_webseed ** webseeds; |
---|
1742 | int i; |
---|
1743 | int webseedCount; |
---|
1744 | float * ret; |
---|
1745 | |
---|
1746 | assert( manager ); |
---|
1747 | managerLock( manager ); |
---|
1748 | |
---|
1749 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1750 | webseeds = (const tr_webseed**) tr_ptrArrayPeek( t->webseeds, |
---|
1751 | &webseedCount ); |
---|
1752 | assert( webseedCount == t->tor->info.webseedCount ); |
---|
1753 | ret = tr_new0( float, webseedCount ); |
---|
1754 | |
---|
1755 | for( i = 0; i < webseedCount; ++i ) |
---|
1756 | if( !tr_webseedGetSpeed( webseeds[i], &ret[i] ) ) |
---|
1757 | ret[i] = -1.0; |
---|
1758 | |
---|
1759 | managerUnlock( manager ); |
---|
1760 | return ret; |
---|
1761 | } |
---|
1762 | |
---|
1763 | double |
---|
1764 | tr_peerGetPieceSpeed( const tr_peer * peer, |
---|
1765 | tr_direction direction ) |
---|
1766 | { |
---|
1767 | assert( peer ); |
---|
1768 | assert( direction==TR_CLIENT_TO_PEER || direction==TR_PEER_TO_CLIENT ); |
---|
1769 | |
---|
1770 | return tr_rcRate( peer->pieceSpeed[direction] ); |
---|
1771 | } |
---|
1772 | |
---|
1773 | |
---|
1774 | struct tr_peer_stat * |
---|
1775 | tr_peerMgrPeerStats( const tr_peerMgr * manager, |
---|
1776 | const uint8_t * torrentHash, |
---|
1777 | int * setmeCount UNUSED ) |
---|
1778 | { |
---|
1779 | int i, size; |
---|
1780 | const Torrent * t; |
---|
1781 | tr_peer ** peers; |
---|
1782 | tr_peer_stat * ret; |
---|
1783 | |
---|
1784 | assert( manager ); |
---|
1785 | managerLock( manager ); |
---|
1786 | |
---|
1787 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1788 | peers = getConnectedPeers( (Torrent*)t, &size ); |
---|
1789 | ret = tr_new0( tr_peer_stat, size ); |
---|
1790 | |
---|
1791 | for( i = 0; i < size; ++i ) |
---|
1792 | { |
---|
1793 | char * pch; |
---|
1794 | const tr_peer * peer = peers[i]; |
---|
1795 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1796 | tr_peer_stat * stat = ret + i; |
---|
1797 | |
---|
1798 | tr_netNtop( &peer->in_addr, stat->addr, sizeof( stat->addr ) ); |
---|
1799 | tr_strlcpy( stat->client, ( peer->client ? peer->client : "" ), |
---|
1800 | sizeof( stat->client ) ); |
---|
1801 | stat->port = peer->port; |
---|
1802 | stat->from = atom->from; |
---|
1803 | stat->progress = peer->progress; |
---|
1804 | stat->isEncrypted = tr_peerIoIsEncrypted( peer->io ) ? 1 : 0; |
---|
1805 | stat->rateToPeer = tr_peerGetPieceSpeed( peer, TR_CLIENT_TO_PEER ); |
---|
1806 | stat->rateToClient = tr_peerGetPieceSpeed( peer, TR_PEER_TO_CLIENT ); |
---|
1807 | stat->peerIsChoked = peer->peerIsChoked; |
---|
1808 | stat->peerIsInterested = peer->peerIsInterested; |
---|
1809 | stat->clientIsChoked = peer->clientIsChoked; |
---|
1810 | stat->clientIsInterested = peer->clientIsInterested; |
---|
1811 | stat->isIncoming = tr_peerIoIsIncoming( peer->io ); |
---|
1812 | stat->isDownloadingFrom = clientIsDownloadingFrom( peer ); |
---|
1813 | stat->isUploadingTo = clientIsUploadingTo( peer ); |
---|
1814 | |
---|
1815 | pch = stat->flagStr; |
---|
1816 | if( t->optimistic == peer ) *pch++ = 'O'; |
---|
1817 | if( stat->isDownloadingFrom ) *pch++ = 'D'; |
---|
1818 | else if( stat->clientIsInterested ) *pch++ = 'd'; |
---|
1819 | if( stat->isUploadingTo ) *pch++ = 'U'; |
---|
1820 | else if( stat->peerIsInterested ) *pch++ = 'u'; |
---|
1821 | if( !stat->clientIsChoked && !stat->clientIsInterested ) *pch++ = |
---|
1822 | 'K'; |
---|
1823 | if( !stat->peerIsChoked && !stat->peerIsInterested ) *pch++ = '?'; |
---|
1824 | if( stat->isEncrypted ) *pch++ = 'E'; |
---|
1825 | if( stat->from == TR_PEER_FROM_PEX ) *pch++ = 'X'; |
---|
1826 | if( stat->isIncoming ) *pch++ = 'I'; |
---|
1827 | *pch = '\0'; |
---|
1828 | } |
---|
1829 | |
---|
1830 | *setmeCount = size; |
---|
1831 | tr_free( peers ); |
---|
1832 | |
---|
1833 | managerUnlock( manager ); |
---|
1834 | return ret; |
---|
1835 | } |
---|
1836 | |
---|
1837 | /** |
---|
1838 | *** |
---|
1839 | **/ |
---|
1840 | |
---|
1841 | struct ChokeData |
---|
1842 | { |
---|
1843 | unsigned int doUnchoke : 1; |
---|
1844 | unsigned int isInterested : 1; |
---|
1845 | unsigned int isChoked : 1; |
---|
1846 | int rate; |
---|
1847 | tr_peer * peer; |
---|
1848 | }; |
---|
1849 | |
---|
1850 | static int |
---|
1851 | compareChoke( const void * va, |
---|
1852 | const void * vb ) |
---|
1853 | { |
---|
1854 | const struct ChokeData * a = va; |
---|
1855 | const struct ChokeData * b = vb; |
---|
1856 | |
---|
1857 | if( a->rate != b->rate ) /* prefer higher overall speeds */ |
---|
1858 | return a->rate > b->rate ? -1 : 1; |
---|
1859 | |
---|
1860 | if( a->isChoked != b->isChoked ) /* prefer unchoked */ |
---|
1861 | return a->isChoked ? 1 : -1; |
---|
1862 | |
---|
1863 | return 0; |
---|
1864 | } |
---|
1865 | |
---|
1866 | static int |
---|
1867 | isNew( const tr_peer * peer ) |
---|
1868 | { |
---|
1869 | return peer && peer->io && tr_peerIoGetAge( peer->io ) < 45; |
---|
1870 | } |
---|
1871 | |
---|
1872 | static int |
---|
1873 | isSame( const tr_peer * peer ) |
---|
1874 | { |
---|
1875 | return peer && peer->client && strstr( peer->client, "Transmission" ); |
---|
1876 | } |
---|
1877 | |
---|
1878 | /** |
---|
1879 | *** |
---|
1880 | **/ |
---|
1881 | |
---|
1882 | static void |
---|
1883 | rechoke( Torrent * t ) |
---|
1884 | { |
---|
1885 | int i, peerCount, size, unchokedInterested; |
---|
1886 | tr_peer ** peers = getConnectedPeers( t, &peerCount ); |
---|
1887 | struct ChokeData * choke = tr_new0( struct ChokeData, peerCount ); |
---|
1888 | const int chokeAll = !tr_torrentIsPieceTransferAllowed( t->tor, TR_CLIENT_TO_PEER ); |
---|
1889 | |
---|
1890 | assert( torrentIsLocked( t ) ); |
---|
1891 | |
---|
1892 | /* sort the peers by preference and rate */ |
---|
1893 | for( i = 0, size = 0; i < peerCount; ++i ) |
---|
1894 | { |
---|
1895 | tr_peer * peer = peers[i]; |
---|
1896 | if( peer->progress >= 1.0 ) /* choke all seeds */ |
---|
1897 | tr_peerMsgsSetChoke( peer->msgs, TRUE ); |
---|
1898 | else if( chokeAll ) |
---|
1899 | tr_peerMsgsSetChoke( peer->msgs, TRUE ); |
---|
1900 | else { |
---|
1901 | struct ChokeData * n = &choke[size++]; |
---|
1902 | n->peer = peer; |
---|
1903 | n->isInterested = peer->peerIsInterested; |
---|
1904 | n->isChoked = peer->peerIsChoked; |
---|
1905 | n->rate = (int)(tr_peerGetPieceSpeed( peer, TR_CLIENT_TO_PEER ) |
---|
1906 | + tr_peerGetPieceSpeed( peer, TR_PEER_TO_CLIENT ) ); |
---|
1907 | } |
---|
1908 | } |
---|
1909 | |
---|
1910 | qsort( choke, size, sizeof( struct ChokeData ), compareChoke ); |
---|
1911 | |
---|
1912 | /** |
---|
1913 | * Reciprocation and number of uploads capping is managed by unchoking |
---|
1914 | * the N peers which have the best upload rate and are interested. |
---|
1915 | * This maximizes the client's download rate. These N peers are |
---|
1916 | * referred to as downloaders, because they are interested in downloading |
---|
1917 | * from the client. |
---|
1918 | * |
---|
1919 | * Peers which have a better upload rate (as compared to the downloaders) |
---|
1920 | * but aren't interested get unchoked. If they become interested, the |
---|
1921 | * downloader with the worst upload rate gets choked. If a client has |
---|
1922 | * a complete file, it uses its upload rate rather than its download |
---|
1923 | * rate to decide which peers to unchoke. |
---|
1924 | */ |
---|
1925 | unchokedInterested = 0; |
---|
1926 | for( i = 0; i < size && unchokedInterested < MAX_UNCHOKED_PEERS; ++i ) |
---|
1927 | { |
---|
1928 | choke[i].doUnchoke = 1; |
---|
1929 | if( choke[i].isInterested ) |
---|
1930 | ++unchokedInterested; |
---|
1931 | } |
---|
1932 | |
---|
1933 | /* optimistic unchoke */ |
---|
1934 | if( i < size ) |
---|
1935 | { |
---|
1936 | int n; |
---|
1937 | struct ChokeData * c; |
---|
1938 | tr_ptrArray * randPool = tr_ptrArrayNew( ); |
---|
1939 | |
---|
1940 | for( ; i < size; ++i ) |
---|
1941 | { |
---|
1942 | if( choke[i].isInterested ) |
---|
1943 | { |
---|
1944 | const tr_peer * peer = choke[i].peer; |
---|
1945 | int x = 1, y; |
---|
1946 | if( isNew( peer ) ) x *= 3; |
---|
1947 | if( isSame( peer ) ) x *= 3; |
---|
1948 | for( y = 0; y < x; ++y ) |
---|
1949 | tr_ptrArrayAppend( randPool, &choke[i] ); |
---|
1950 | } |
---|
1951 | } |
---|
1952 | |
---|
1953 | if( ( n = tr_ptrArraySize( randPool ) ) ) |
---|
1954 | { |
---|
1955 | c = tr_ptrArrayNth( randPool, tr_cryptoWeakRandInt( n ) ); |
---|
1956 | c->doUnchoke = 1; |
---|
1957 | t->optimistic = c->peer; |
---|
1958 | } |
---|
1959 | |
---|
1960 | tr_ptrArrayFree( randPool, NULL ); |
---|
1961 | } |
---|
1962 | |
---|
1963 | for( i = 0; i < size; ++i ) |
---|
1964 | tr_peerMsgsSetChoke( choke[i].peer->msgs, !choke[i].doUnchoke ); |
---|
1965 | |
---|
1966 | /* cleanup */ |
---|
1967 | tr_free( choke ); |
---|
1968 | tr_free( peers ); |
---|
1969 | } |
---|
1970 | |
---|
1971 | static int |
---|
1972 | rechokePulse( void * vtorrent ) |
---|
1973 | { |
---|
1974 | Torrent * t = vtorrent; |
---|
1975 | |
---|
1976 | torrentLock( t ); |
---|
1977 | rechoke( t ); |
---|
1978 | torrentUnlock( t ); |
---|
1979 | return TRUE; |
---|
1980 | } |
---|
1981 | |
---|
1982 | /*** |
---|
1983 | **** |
---|
1984 | **** Life and Death |
---|
1985 | **** |
---|
1986 | ***/ |
---|
1987 | |
---|
1988 | static int |
---|
1989 | shouldPeerBeClosed( const Torrent * t, |
---|
1990 | const tr_peer * peer, |
---|
1991 | int peerCount ) |
---|
1992 | { |
---|
1993 | const tr_torrent * tor = t->tor; |
---|
1994 | const time_t now = time( NULL ); |
---|
1995 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1996 | |
---|
1997 | /* if it's marked for purging, close it */ |
---|
1998 | if( peer->doPurge ) |
---|
1999 | { |
---|
2000 | tordbg( t, "purging peer %s because its doPurge flag is set", |
---|
2001 | tr_peerIoAddrStr( &atom->addr, |
---|
2002 | atom->port ) ); |
---|
2003 | return TRUE; |
---|
2004 | } |
---|
2005 | |
---|
2006 | /* if we're seeding and the peer has everything we have, |
---|
2007 | * and enough time has passed for a pex exchange, then disconnect */ |
---|
2008 | if( tr_torrentIsSeed( tor ) ) |
---|
2009 | { |
---|
2010 | int peerHasEverything; |
---|
2011 | if( atom->flags & ADDED_F_SEED_FLAG ) |
---|
2012 | peerHasEverything = TRUE; |
---|
2013 | else if( peer->progress < tr_cpPercentDone( tor->completion ) ) |
---|
2014 | peerHasEverything = FALSE; |
---|
2015 | else |
---|
2016 | { |
---|
2017 | tr_bitfield * tmp = |
---|
2018 | tr_bitfieldDup( tr_cpPieceBitfield( tor->completion ) ); |
---|
2019 | tr_bitfieldDifference( tmp, peer->have ); |
---|
2020 | peerHasEverything = tr_bitfieldCountTrueBits( tmp ) == 0; |
---|
2021 | tr_bitfieldFree( tmp ); |
---|
2022 | } |
---|
2023 | if( peerHasEverything |
---|
2024 | && ( !tr_torrentAllowsPex( tor ) || ( now - atom->time >= 30 ) ) ) |
---|
2025 | { |
---|
2026 | tordbg( t, "purging peer %s because we're both seeds", |
---|
2027 | tr_peerIoAddrStr( &atom->addr, |
---|
2028 | atom->port ) ); |
---|
2029 | return TRUE; |
---|
2030 | } |
---|
2031 | } |
---|
2032 | |
---|
2033 | /* disconnect if it's been too long since piece data has been transferred. |
---|
2034 | * this is on a sliding scale based on number of available peers... */ |
---|
2035 | { |
---|
2036 | const int relaxStrictnessIfFewerThanN = |
---|
2037 | (int)( ( getMaxPeerCount( tor ) * 0.9 ) + 0.5 ); |
---|
2038 | /* if we have >= relaxIfFewerThan, strictness is 100%. |
---|
2039 | * if we have zero connections, strictness is 0% */ |
---|
2040 | const float strictness = peerCount >= relaxStrictnessIfFewerThanN |
---|
2041 | ? 1.0 |
---|
2042 | : peerCount / |
---|
2043 | (float)relaxStrictnessIfFewerThanN; |
---|
2044 | const int lo = MIN_UPLOAD_IDLE_SECS; |
---|
2045 | const int hi = MAX_UPLOAD_IDLE_SECS; |
---|
2046 | const int limit = lo + ( ( hi - lo ) * strictness ); |
---|
2047 | const time_t then = peer->pieceDataActivityDate; |
---|
2048 | const int idleTime = then ? ( now - then ) : 0; |
---|
2049 | if( idleTime > limit ) |
---|
2050 | { |
---|
2051 | tordbg( |
---|
2052 | t, |
---|
2053 | "purging peer %s because it's been %d secs since we shared anything", |
---|
2054 | tr_peerIoAddrStr( &atom->addr, atom->port ), idleTime ); |
---|
2055 | return TRUE; |
---|
2056 | } |
---|
2057 | } |
---|
2058 | |
---|
2059 | return FALSE; |
---|
2060 | } |
---|
2061 | |
---|
2062 | static tr_peer ** |
---|
2063 | getPeersToClose( Torrent * t, |
---|
2064 | int * setmeSize ) |
---|
2065 | { |
---|
2066 | int i, peerCount, outsize; |
---|
2067 | tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, |
---|
2068 | &peerCount ); |
---|
2069 | struct tr_peer ** ret = tr_new( tr_peer *, peerCount ); |
---|
2070 | |
---|
2071 | assert( torrentIsLocked( t ) ); |
---|
2072 | |
---|
2073 | for( i = outsize = 0; i < peerCount; ++i ) |
---|
2074 | if( shouldPeerBeClosed( t, peers[i], peerCount ) ) |
---|
2075 | ret[outsize++] = peers[i]; |
---|
2076 | |
---|
2077 | *setmeSize = outsize; |
---|
2078 | return ret; |
---|
2079 | } |
---|
2080 | |
---|
2081 | static int |
---|
2082 | compareCandidates( const void * va, |
---|
2083 | const void * vb ) |
---|
2084 | { |
---|
2085 | const struct peer_atom * a = *(const struct peer_atom**) va; |
---|
2086 | const struct peer_atom * b = *(const struct peer_atom**) vb; |
---|
2087 | |
---|
2088 | /* <Charles> Here we would probably want to try reconnecting to |
---|
2089 | * peers that had most recently given us data. Lots of users have |
---|
2090 | * trouble with resets due to their routers and/or ISPs. This way we |
---|
2091 | * can quickly recover from an unwanted reset. So we sort |
---|
2092 | * piece_data_time in descending order. |
---|
2093 | */ |
---|
2094 | |
---|
2095 | if( a->piece_data_time != b->piece_data_time ) |
---|
2096 | return a->piece_data_time < b->piece_data_time ? 1 : -1; |
---|
2097 | |
---|
2098 | if( a->numFails != b->numFails ) |
---|
2099 | return a->numFails < b->numFails ? -1 : 1; |
---|
2100 | |
---|
2101 | if( a->time != b->time ) |
---|
2102 | return a->time < b->time ? -1 : 1; |
---|
2103 | |
---|
2104 | return 0; |
---|
2105 | } |
---|
2106 | |
---|
2107 | static int |
---|
2108 | getReconnectIntervalSecs( const struct peer_atom * atom ) |
---|
2109 | { |
---|
2110 | int sec; |
---|
2111 | const time_t now = time( NULL ); |
---|
2112 | |
---|
2113 | /* if we were recently connected to this peer and transferring piece |
---|
2114 | * data, try to reconnect to them sooner rather that later -- we don't |
---|
2115 | * want network troubles to get in the way of a good peer. */ |
---|
2116 | if( ( now - atom->piece_data_time ) <= |
---|
2117 | ( MINIMUM_RECONNECT_INTERVAL_SECS * 2 ) ) |
---|
2118 | sec = MINIMUM_RECONNECT_INTERVAL_SECS; |
---|
2119 | |
---|
2120 | /* don't allow reconnects more often than our minimum */ |
---|
2121 | else if( ( now - atom->time ) < MINIMUM_RECONNECT_INTERVAL_SECS ) |
---|
2122 | sec = MINIMUM_RECONNECT_INTERVAL_SECS; |
---|
2123 | |
---|
2124 | /* otherwise, the interval depends on how many times we've tried |
---|
2125 | * and failed to connect to the peer */ |
---|
2126 | else switch( atom->numFails ) |
---|
2127 | { |
---|
2128 | case 0: |
---|
2129 | sec = 0; break; |
---|
2130 | |
---|
2131 | case 1: |
---|
2132 | sec = 5; break; |
---|
2133 | |
---|
2134 | case 2: |
---|
2135 | sec = 2 * 60; break; |
---|
2136 | |
---|
2137 | case 3: |
---|
2138 | sec = 15 * 60; break; |
---|
2139 | |
---|
2140 | case 4: |
---|
2141 | sec = 30 * 60; break; |
---|
2142 | |
---|
2143 | case 5: |
---|
2144 | sec = 60 * 60; break; |
---|
2145 | |
---|
2146 | default: |
---|
2147 | sec = 120 * 60; break; |
---|
2148 | } |
---|
2149 | |
---|
2150 | return sec; |
---|
2151 | } |
---|
2152 | |
---|
2153 | static struct peer_atom ** |
---|
2154 | getPeerCandidates( Torrent * t, |
---|
2155 | int * setmeSize ) |
---|
2156 | { |
---|
2157 | int i, atomCount, retCount; |
---|
2158 | struct peer_atom ** atoms; |
---|
2159 | struct peer_atom ** ret; |
---|
2160 | const time_t now = time( NULL ); |
---|
2161 | const int seed = tr_torrentIsSeed( t->tor ); |
---|
2162 | |
---|
2163 | assert( torrentIsLocked( t ) ); |
---|
2164 | |
---|
2165 | atoms = (struct peer_atom**) tr_ptrArrayPeek( t->pool, &atomCount ); |
---|
2166 | ret = tr_new( struct peer_atom*, atomCount ); |
---|
2167 | for( i = retCount = 0; i < atomCount; ++i ) |
---|
2168 | { |
---|
2169 | int interval; |
---|
2170 | struct peer_atom * atom = atoms[i]; |
---|
2171 | |
---|
2172 | /* peer fed us too much bad data ... we only keep it around |
---|
2173 | * now to weed it out in case someone sends it to us via pex */ |
---|
2174 | if( atom->myflags & MYFLAG_BANNED ) |
---|
2175 | continue; |
---|
2176 | |
---|
2177 | /* peer was unconnectable before, so we're not going to keep trying. |
---|
2178 | * this is needs a separate flag from `banned', since if they try |
---|
2179 | * to connect to us later, we'll let them in */ |
---|
2180 | if( atom->myflags & MYFLAG_UNREACHABLE ) |
---|
2181 | continue; |
---|
2182 | |
---|
2183 | /* we don't need two connections to the same peer... */ |
---|
2184 | if( peerIsInUse( t, &atom->addr ) ) |
---|
2185 | continue; |
---|
2186 | |
---|
2187 | /* no need to connect if we're both seeds... */ |
---|
2188 | if( seed && ( atom->flags & ADDED_F_SEED_FLAG ) ) |
---|
2189 | continue; |
---|
2190 | |
---|
2191 | /* don't reconnect too often */ |
---|
2192 | interval = getReconnectIntervalSecs( atom ); |
---|
2193 | if( ( now - atom->time ) < interval ) |
---|
2194 | { |
---|
2195 | tordbg( |
---|
2196 | t, |
---|
2197 | "RECONNECT peer %d (%s) is in its grace period of %d seconds..", |
---|
2198 | i, tr_peerIoAddrStr( &atom->addr, |
---|
2199 | atom->port ), interval ); |
---|
2200 | continue; |
---|
2201 | } |
---|
2202 | |
---|
2203 | /* Don't connect to peers in our blocklist */ |
---|
2204 | if( tr_sessionIsAddressBlocked( t->manager->session, &atom->addr ) ) |
---|
2205 | continue; |
---|
2206 | |
---|
2207 | ret[retCount++] = atom; |
---|
2208 | } |
---|
2209 | |
---|
2210 | qsort( ret, retCount, sizeof( struct peer_atom* ), compareCandidates ); |
---|
2211 | *setmeSize = retCount; |
---|
2212 | return ret; |
---|
2213 | } |
---|
2214 | |
---|
2215 | static int |
---|
2216 | reconnectPulse( void * vtorrent ) |
---|
2217 | { |
---|
2218 | Torrent * t = vtorrent; |
---|
2219 | static time_t prevTime = 0; |
---|
2220 | static int newConnectionsThisSecond = 0; |
---|
2221 | time_t now; |
---|
2222 | |
---|
2223 | torrentLock( t ); |
---|
2224 | |
---|
2225 | now = time( NULL ); |
---|
2226 | if( prevTime != now ) |
---|
2227 | { |
---|
2228 | prevTime = now; |
---|
2229 | newConnectionsThisSecond = 0; |
---|
2230 | } |
---|
2231 | |
---|
2232 | if( !t->isRunning ) |
---|
2233 | { |
---|
2234 | removeAllPeers( t ); |
---|
2235 | } |
---|
2236 | else |
---|
2237 | { |
---|
2238 | int i, nCandidates, nBad; |
---|
2239 | struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates ); |
---|
2240 | struct tr_peer ** connections = getPeersToClose( t, &nBad ); |
---|
2241 | |
---|
2242 | if( nBad || nCandidates ) |
---|
2243 | tordbg( |
---|
2244 | t, "reconnect pulse for [%s]: %d bad connections, " |
---|
2245 | "%d connection candidates, %d atoms, max per pulse is %d", |
---|
2246 | t->tor->info.name, nBad, nCandidates, |
---|
2247 | tr_ptrArraySize( t->pool ), |
---|
2248 | (int)MAX_RECONNECTIONS_PER_PULSE ); |
---|
2249 | |
---|
2250 | /* disconnect some peers. |
---|
2251 | if we transferred piece data, then they might be good peers, |
---|
2252 | so reset their `numFails' weight to zero. otherwise we connected |
---|
2253 | to them fruitlessly, so mark it as another fail */ |
---|
2254 | for( i = 0; i < nBad; ++i ) |
---|
2255 | { |
---|
2256 | tr_peer * peer = connections[i]; |
---|
2257 | struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
2258 | if( peer->pieceDataActivityDate ) |
---|
2259 | atom->numFails = 0; |
---|
2260 | else |
---|
2261 | ++atom->numFails; |
---|
2262 | tordbg( t, "removing bad peer %s", |
---|
2263 | tr_peerIoGetAddrStr( peer->io ) ); |
---|
2264 | removePeer( t, peer ); |
---|
2265 | } |
---|
2266 | |
---|
2267 | /* add some new ones */ |
---|
2268 | for( i = 0; ( i < nCandidates ) |
---|
2269 | && ( i < MAX_RECONNECTIONS_PER_PULSE ) |
---|
2270 | && ( getPeerCount( t ) < getMaxPeerCount( t->tor ) ) |
---|
2271 | && ( newConnectionsThisSecond < MAX_CONNECTIONS_PER_SECOND ); |
---|
2272 | ++i ) |
---|
2273 | { |
---|
2274 | tr_peerMgr * mgr = t->manager; |
---|
2275 | struct peer_atom * atom = candidates[i]; |
---|
2276 | tr_peerIo * io; |
---|
2277 | |
---|
2278 | tordbg( t, "Starting an OUTGOING connection with %s", |
---|
2279 | tr_peerIoAddrStr( &atom->addr, atom->port ) ); |
---|
2280 | |
---|
2281 | io = |
---|
2282 | tr_peerIoNewOutgoing( mgr->session, &atom->addr, atom->port, |
---|
2283 | t->hash ); |
---|
2284 | if( io == NULL ) |
---|
2285 | { |
---|
2286 | atom->myflags |= MYFLAG_UNREACHABLE; |
---|
2287 | } |
---|
2288 | else |
---|
2289 | { |
---|
2290 | tr_handshake * handshake = tr_handshakeNew( |
---|
2291 | io, |
---|
2292 | mgr->session-> |
---|
2293 | encryptionMode, |
---|
2294 | myHandshakeDoneCB, |
---|
2295 | mgr ); |
---|
2296 | |
---|
2297 | assert( tr_peerIoGetTorrentHash( io ) ); |
---|
2298 | |
---|
2299 | ++newConnectionsThisSecond; |
---|
2300 | |
---|
2301 | tr_ptrArrayInsertSorted( t->outgoingHandshakes, handshake, |
---|
2302 | handshakeCompare ); |
---|
2303 | } |
---|
2304 | |
---|
2305 | atom->time = time( NULL ); |
---|
2306 | } |
---|
2307 | |
---|
2308 | /* cleanup */ |
---|
2309 | tr_free( connections ); |
---|
2310 | tr_free( candidates ); |
---|
2311 | } |
---|
2312 | |
---|
2313 | torrentUnlock( t ); |
---|
2314 | return TRUE; |
---|
2315 | } |
---|
2316 | |
---|
2317 | /**** |
---|
2318 | ***** |
---|
2319 | ***** BANDWIDTH ALLOCATION |
---|
2320 | ***** |
---|
2321 | ****/ |
---|
2322 | |
---|
2323 | static double |
---|
2324 | allocateHowMuch( double desired_average_kb_per_sec, |
---|
2325 | const tr_ratecontrol * ratecontrol ) |
---|
2326 | { |
---|
2327 | const int pulses_per_history = TR_RATECONTROL_HISTORY_MSEC / BANDWIDTH_PERIOD_MSEC; |
---|
2328 | const double seconds_per_pulse = BANDWIDTH_PERIOD_MSEC / 1000.0; |
---|
2329 | const double baseline_bytes_per_pulse = desired_average_kb_per_sec * 1024.0 * seconds_per_pulse; |
---|
2330 | const double min = baseline_bytes_per_pulse * 0.80; |
---|
2331 | const double max = baseline_bytes_per_pulse * 1.10; |
---|
2332 | const double current_bytes_per_pulse = tr_rcRate( ratecontrol ) * 1024.0 * seconds_per_pulse; |
---|
2333 | const double next_pulse_bytes = baseline_bytes_per_pulse * ( pulses_per_history + 1 ) |
---|
2334 | - ( current_bytes_per_pulse * pulses_per_history ); |
---|
2335 | double clamped; |
---|
2336 | |
---|
2337 | /* clamp the return value to lessen oscillation */ |
---|
2338 | clamped = next_pulse_bytes; |
---|
2339 | clamped = MAX( clamped, min ); |
---|
2340 | clamped = MIN( clamped, max ); |
---|
2341 | |
---|
2342 | #if 0 |
---|
2343 | fprintf( stderr, "desiredAvgKB is %5.2f, rate is %5.2f, allocating %5.2f (%5.2f)\n", |
---|
2344 | desired_average_kb_per_sec, |
---|
2345 | tr_rcRate( ratecontrol ), |
---|
2346 | clamped/1024.0, |
---|
2347 | next_pulse_bytes/1024.0 ); |
---|
2348 | #endif |
---|
2349 | |
---|
2350 | return clamped; |
---|
2351 | } |
---|
2352 | |
---|
2353 | /** |
---|
2354 | * Distributes a fixed amount of bandwidth among a set of peers. |
---|
2355 | * |
---|
2356 | * @param peerArray peers whose client-to-peer bandwidth will be set |
---|
2357 | * @param direction whether to allocate upload or download bandwidth |
---|
2358 | * @param history recent bandwidth history for these peers |
---|
2359 | * @param desiredAvgKB overall bandwidth goal for this set of peers |
---|
2360 | */ |
---|
2361 | static void |
---|
2362 | setPeerBandwidth( tr_ptrArray * peerArray, |
---|
2363 | const tr_direction direction, |
---|
2364 | const tr_ratecontrol * ratecontrol, |
---|
2365 | double desiredAvgKB ) |
---|
2366 | { |
---|
2367 | const int peerCount = tr_ptrArraySize( peerArray ); |
---|
2368 | const double bytes = allocateHowMuch( desiredAvgKB, ratecontrol ); |
---|
2369 | const double welfareBytes = MIN( 2048, bytes * 0.2 ); |
---|
2370 | const double meritBytes = MAX( 0, bytes - welfareBytes ); |
---|
2371 | tr_peer ** peers = (tr_peer**) tr_ptrArrayBase( peerArray ); |
---|
2372 | tr_peer ** candidates = tr_new( tr_peer *, peerCount ); |
---|
2373 | int i; |
---|
2374 | int candidateCount; |
---|
2375 | double welfare; |
---|
2376 | size_t bytesUsed; |
---|
2377 | |
---|
2378 | assert( meritBytes >= 0.0 ); |
---|
2379 | assert( welfareBytes >= 0.0 ); |
---|
2380 | assert( direction == TR_UP || direction == TR_DOWN ); |
---|
2381 | |
---|
2382 | for( i = candidateCount = 0; i < peerCount; ++i ) |
---|
2383 | if( tr_peerIoWantsBandwidth( peers[i]->io, direction ) ) |
---|
2384 | candidates[candidateCount++] = peers[i]; |
---|
2385 | else |
---|
2386 | tr_peerIoSetBandwidth( peers[i]->io, direction, 0 ); |
---|
2387 | |
---|
2388 | for( i = bytesUsed = 0; i < candidateCount; ++i ) |
---|
2389 | bytesUsed += tr_peerIoGetBandwidthUsed( candidates[i]->io, |
---|
2390 | direction ); |
---|
2391 | |
---|
2392 | welfare = welfareBytes / candidateCount; |
---|
2393 | |
---|
2394 | for( i = 0; i < candidateCount; ++i ) |
---|
2395 | { |
---|
2396 | tr_peer * peer = candidates[i]; |
---|
2397 | const double merit = bytesUsed |
---|
2398 | ? ( meritBytes * |
---|
2399 | tr_peerIoGetBandwidthUsed( peer->io, |
---|
2400 | direction ) ) / |
---|
2401 | bytesUsed |
---|
2402 | : ( meritBytes / candidateCount ); |
---|
2403 | tr_peerIoSetBandwidth( peer->io, direction, merit + welfare ); |
---|
2404 | } |
---|
2405 | |
---|
2406 | /* cleanup */ |
---|
2407 | tr_free( candidates ); |
---|
2408 | } |
---|
2409 | |
---|
2410 | static size_t |
---|
2411 | countHandshakeBandwidth( tr_ptrArray * handshakes, |
---|
2412 | tr_direction direction ) |
---|
2413 | { |
---|
2414 | const int n = tr_ptrArraySize( handshakes ); |
---|
2415 | int i; |
---|
2416 | size_t total; |
---|
2417 | |
---|
2418 | for( i = total = 0; i < n; ++i ) |
---|
2419 | { |
---|
2420 | tr_peerIo * io = tr_handshakeGetIO( tr_ptrArrayNth( handshakes, i ) ); |
---|
2421 | total += tr_peerIoGetBandwidthUsed( io, direction ); |
---|
2422 | } |
---|
2423 | return total; |
---|
2424 | } |
---|
2425 | |
---|
2426 | static size_t |
---|
2427 | countPeerBandwidth( tr_ptrArray * peers, |
---|
2428 | tr_direction direction ) |
---|
2429 | { |
---|
2430 | const int n = tr_ptrArraySize( peers ); |
---|
2431 | int i; |
---|
2432 | size_t total; |
---|
2433 | |
---|
2434 | for( i = total = 0; i < n; ++i ) |
---|
2435 | { |
---|
2436 | tr_peer * peer = tr_ptrArrayNth( peers, i ); |
---|
2437 | total += tr_peerIoGetBandwidthUsed( peer->io, direction ); |
---|
2438 | } |
---|
2439 | return total; |
---|
2440 | } |
---|
2441 | |
---|
2442 | static void |
---|
2443 | givePeersUnlimitedBandwidth( tr_ptrArray * peers, |
---|
2444 | tr_direction direction ) |
---|
2445 | { |
---|
2446 | const int n = tr_ptrArraySize( peers ); |
---|
2447 | int i; |
---|
2448 | |
---|
2449 | for( i = 0; i < n; ++i ) |
---|
2450 | { |
---|
2451 | tr_peer * peer = tr_ptrArrayNth( peers, i ); |
---|
2452 | tr_peerIoSetBandwidthUnlimited( peer->io, direction ); |
---|
2453 | } |
---|
2454 | } |
---|
2455 | |
---|
2456 | static void |
---|
2457 | pumpAllPeers( tr_peerMgr * mgr ) |
---|
2458 | { |
---|
2459 | const int torrentCount = tr_ptrArraySize( mgr->torrents ); |
---|
2460 | int i, j; |
---|
2461 | |
---|
2462 | for( i = 0; i < torrentCount; ++i ) |
---|
2463 | { |
---|
2464 | Torrent * t = tr_ptrArrayNth( mgr->torrents, i ); |
---|
2465 | for( j = 0; j < tr_ptrArraySize( t->peers ); ++j ) |
---|
2466 | { |
---|
2467 | tr_peer * peer = tr_ptrArrayNth( t->peers, j ); |
---|
2468 | tr_peerMsgsPulse( peer->msgs ); |
---|
2469 | } |
---|
2470 | } |
---|
2471 | } |
---|
2472 | |
---|
2473 | /** |
---|
2474 | * Allocate bandwidth for each peer connection. |
---|
2475 | * |
---|
2476 | * @param mgr the peer manager |
---|
2477 | * @param direction whether to allocate upload or download bandwidth |
---|
2478 | * @return the amount of directional bandwidth used since the last pulse. |
---|
2479 | */ |
---|
2480 | static double |
---|
2481 | allocateBandwidth( tr_peerMgr * mgr, |
---|
2482 | tr_direction direction ) |
---|
2483 | { |
---|
2484 | tr_session * session = mgr->session; |
---|
2485 | const int torrentCount = tr_ptrArraySize( mgr->torrents ); |
---|
2486 | Torrent ** torrents = (Torrent **) tr_ptrArrayBase( mgr->torrents ); |
---|
2487 | tr_ptrArray * globalPool = tr_ptrArrayNew( ); |
---|
2488 | double allBytesUsed = 0; |
---|
2489 | size_t poolBytesUsed = 0; |
---|
2490 | int i; |
---|
2491 | |
---|
2492 | assert( mgr ); |
---|
2493 | assert( direction == TR_UP || direction == TR_DOWN ); |
---|
2494 | |
---|
2495 | /* before allocating bandwidth, pump the connected peers */ |
---|
2496 | pumpAllPeers( mgr ); |
---|
2497 | |
---|
2498 | for( i=0; i<torrentCount; ++i ) |
---|
2499 | { |
---|
2500 | Torrent * t = torrents[i]; |
---|
2501 | size_t used; |
---|
2502 | tr_speedlimit speedMode; |
---|
2503 | |
---|
2504 | /* no point in allocating bandwidth for stopped torrents */ |
---|
2505 | if( tr_torrentGetActivity( t->tor ) == TR_STATUS_STOPPED ) |
---|
2506 | continue; |
---|
2507 | |
---|
2508 | used = countPeerBandwidth( t->peers, direction ); |
---|
2509 | countHandshakeBandwidth( t->outgoingHandshakes, direction ); |
---|
2510 | |
---|
2511 | /* remember this torrent's bytes used */ |
---|
2512 | tr_rcTransferred( t->tor->rawSpeed[direction], used ); |
---|
2513 | |
---|
2514 | /* add this torrent's bandwidth use to allBytesUsed */ |
---|
2515 | allBytesUsed += used; |
---|
2516 | |
---|
2517 | /* if piece data is disallowed, don't bother limiting bandwidth -- |
---|
2518 | * we won't be asking for, or sending out, any pieces */ |
---|
2519 | if( !tr_torrentIsPieceTransferAllowed( t->tor, direction ) ) |
---|
2520 | speedMode = TR_SPEEDLIMIT_UNLIMITED; |
---|
2521 | else |
---|
2522 | speedMode = tr_torrentGetSpeedMode( t->tor, direction ); |
---|
2523 | |
---|
2524 | /* process the torrent's peers based on its speed mode */ |
---|
2525 | switch( speedMode ) |
---|
2526 | { |
---|
2527 | case TR_SPEEDLIMIT_UNLIMITED: |
---|
2528 | givePeersUnlimitedBandwidth( t->peers, direction ); |
---|
2529 | break; |
---|
2530 | |
---|
2531 | case TR_SPEEDLIMIT_SINGLE: |
---|
2532 | setPeerBandwidth( t->peers, direction, |
---|
2533 | t->tor->rawSpeed[direction], |
---|
2534 | tr_torrentGetSpeedLimit( t->tor, direction ) ); |
---|
2535 | break; |
---|
2536 | |
---|
2537 | case TR_SPEEDLIMIT_GLOBAL: |
---|
2538 | { |
---|
2539 | int i; |
---|
2540 | const int n = tr_ptrArraySize( t->peers ); |
---|
2541 | for( i = 0; i < n; ++i ) |
---|
2542 | tr_ptrArrayAppend( globalPool, |
---|
2543 | tr_ptrArrayNth( t->peers, i ) ); |
---|
2544 | poolBytesUsed += used; |
---|
2545 | break; |
---|
2546 | } |
---|
2547 | } |
---|
2548 | } |
---|
2549 | |
---|
2550 | /* add incoming handshakes to the global pool */ |
---|
2551 | i = countHandshakeBandwidth( mgr->incomingHandshakes, direction ); |
---|
2552 | allBytesUsed += i; |
---|
2553 | poolBytesUsed += i; |
---|
2554 | |
---|
2555 | tr_rcTransferred( mgr->globalPoolRawSpeed[direction], poolBytesUsed ); |
---|
2556 | |
---|
2557 | /* handle the global pool's connections */ |
---|
2558 | if( !tr_sessionIsSpeedLimitEnabled( session, direction ) ) |
---|
2559 | givePeersUnlimitedBandwidth( globalPool, direction ); |
---|
2560 | else |
---|
2561 | setPeerBandwidth( globalPool, direction, |
---|
2562 | mgr->globalPoolRawSpeed[direction], |
---|
2563 | tr_sessionGetSpeedLimit( session, direction ) ); |
---|
2564 | |
---|
2565 | /* now that we've allocated bandwidth, pump all the connected peers */ |
---|
2566 | pumpAllPeers( mgr ); |
---|
2567 | |
---|
2568 | /* cleanup */ |
---|
2569 | tr_ptrArrayFree( globalPool, NULL ); |
---|
2570 | return allBytesUsed; |
---|
2571 | } |
---|
2572 | |
---|
2573 | static int |
---|
2574 | bandwidthPulse( void * vmgr ) |
---|
2575 | { |
---|
2576 | tr_peerMgr * mgr = vmgr; |
---|
2577 | int i; |
---|
2578 | |
---|
2579 | managerLock( mgr ); |
---|
2580 | |
---|
2581 | /* allocate the upload and download bandwidth */ |
---|
2582 | for( i = 0; i < 2; ++i ) |
---|
2583 | allocateBandwidth( mgr, i ); |
---|
2584 | |
---|
2585 | managerUnlock( mgr ); |
---|
2586 | return TRUE; |
---|
2587 | } |
---|
2588 | |
---|