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