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