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 3361 2007-10-11 02:50:05Z 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 = 10, |
---|
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 *might be* fastallowed to us, get it first... |
---|
552 | * I'm putting it on top so we prioritise those pieces at |
---|
553 | * startup, then we'll have them, and we'll be denied access |
---|
554 | * to them */ |
---|
555 | if (a->fastAllowed != b->fastAllowed) |
---|
556 | return a->fastAllowed < b->fastAllowed ? -1 : 1; |
---|
557 | |
---|
558 | /* if one piece has a higher priority, it goes first */ |
---|
559 | if (a->priority != b->priority) |
---|
560 | return a->priority > b->priority ? -1 : 1; |
---|
561 | |
---|
562 | /* otherwise if one has fewer peers, it goes first */ |
---|
563 | if (a->peerCount != b->peerCount) |
---|
564 | return a->peerCount < b->peerCount ? -1 : 1; |
---|
565 | |
---|
566 | /* otherwise go with our random seed */ |
---|
567 | return tr_compareUint16( a->random, b->random ); |
---|
568 | } |
---|
569 | |
---|
570 | static int |
---|
571 | isPieceInteresting( const tr_torrent * tor, |
---|
572 | int piece ) |
---|
573 | { |
---|
574 | if( tor->info.pieces[piece].dnd ) /* we don't want it */ |
---|
575 | return 0; |
---|
576 | |
---|
577 | if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we have it */ |
---|
578 | return 0; |
---|
579 | |
---|
580 | return 1; |
---|
581 | } |
---|
582 | |
---|
583 | static uint32_t* |
---|
584 | getPreferredPieces( Torrent * t, |
---|
585 | uint32_t * pieceCount ) |
---|
586 | { |
---|
587 | const tr_torrent * tor = t->tor; |
---|
588 | const tr_info * inf = &tor->info; |
---|
589 | int i; |
---|
590 | uint32_t poolSize = 0; |
---|
591 | uint32_t * pool = tr_new( uint32_t, inf->pieceCount ); |
---|
592 | int peerCount; |
---|
593 | tr_peer** peers; |
---|
594 | |
---|
595 | assert( torrentIsLocked( t ) ); |
---|
596 | |
---|
597 | peers = getConnectedPeers( t, &peerCount ); |
---|
598 | |
---|
599 | for( i=0; i<inf->pieceCount; ++i ) |
---|
600 | if( isPieceInteresting( tor, i ) ) |
---|
601 | pool[poolSize++] = i; |
---|
602 | |
---|
603 | /* sort the pool from most interesting to least... */ |
---|
604 | if( poolSize > 1 ) |
---|
605 | { |
---|
606 | uint32_t j; |
---|
607 | struct tr_refill_piece * p = tr_new( struct tr_refill_piece, poolSize ); |
---|
608 | |
---|
609 | for( j=0; j<poolSize; ++j ) |
---|
610 | { |
---|
611 | int k; |
---|
612 | const int piece = pool[j]; |
---|
613 | struct tr_refill_piece * setme = p + j; |
---|
614 | |
---|
615 | setme->piece = piece; |
---|
616 | setme->priority = inf->pieces[piece].priority; |
---|
617 | setme->peerCount = 0; |
---|
618 | setme->fastAllowed = 0; |
---|
619 | setme->random = tr_rand( UINT16_MAX ); |
---|
620 | |
---|
621 | for( k=0; k<peerCount; ++k ) { |
---|
622 | const tr_peer * peer = peers[k]; |
---|
623 | if( peer->peerIsInterested && !peer->clientIsChoked && tr_bitfieldHas( peer->have, piece ) ) |
---|
624 | ++setme->peerCount; |
---|
625 | /* The fast peer extension doesn't force a peer to actually HAVE a fast-allowed piece, |
---|
626 | but we're guaranteed to get the same pieces from different peers, |
---|
627 | so we'll build a list and pray one actually have this one */ |
---|
628 | setme->fastAllowed = tr_peerMsgIsPieceFastAllowed( peer->msgs, i); |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | qsort (p, poolSize, sizeof(struct tr_refill_piece), compareRefillPiece); |
---|
633 | |
---|
634 | for( j=0; j<poolSize; ++j ) |
---|
635 | pool[j] = p[j].piece; |
---|
636 | |
---|
637 | tr_free( p ); |
---|
638 | } |
---|
639 | |
---|
640 | tr_free( peers ); |
---|
641 | |
---|
642 | *pieceCount = poolSize; |
---|
643 | return pool; |
---|
644 | } |
---|
645 | |
---|
646 | static uint64_t* |
---|
647 | getPreferredBlocks( Torrent * t, uint64_t * setmeCount ) |
---|
648 | { |
---|
649 | uint32_t i; |
---|
650 | uint32_t pieceCount; |
---|
651 | uint32_t * pieces; |
---|
652 | uint64_t *req, *unreq, *ret, *walk; |
---|
653 | int reqCount, unreqCount; |
---|
654 | const tr_torrent * tor = t->tor; |
---|
655 | |
---|
656 | assert( torrentIsLocked( t ) ); |
---|
657 | |
---|
658 | pieces = getPreferredPieces( t, &pieceCount ); |
---|
659 | |
---|
660 | req = tr_new( uint64_t, pieceCount * tor->blockCountInPiece ); |
---|
661 | reqCount = 0; |
---|
662 | unreq = tr_new( uint64_t, pieceCount * tor->blockCountInPiece ); |
---|
663 | unreqCount = 0; |
---|
664 | |
---|
665 | for( i=0; i<pieceCount; ++i ) { |
---|
666 | const uint32_t index = pieces[i]; |
---|
667 | const int begin = tr_torPieceFirstBlock( tor, index ); |
---|
668 | const int end = begin + tr_torPieceCountBlocks( tor, (int)index ); |
---|
669 | int block; |
---|
670 | for( block=begin; block<end; ++block ) |
---|
671 | if( tr_cpBlockIsComplete( tor->completion, block ) ) |
---|
672 | continue; |
---|
673 | else if( tr_bitfieldHas( t->requested, block ) ) |
---|
674 | req[reqCount++] = block; |
---|
675 | else |
---|
676 | unreq[unreqCount++] = block; |
---|
677 | } |
---|
678 | |
---|
679 | ret = walk = tr_new( uint64_t, unreqCount + reqCount ); |
---|
680 | memcpy( walk, unreq, sizeof(uint64_t) * unreqCount ); |
---|
681 | walk += unreqCount; |
---|
682 | memcpy( walk, req, sizeof(uint64_t) * reqCount ); |
---|
683 | walk += reqCount; |
---|
684 | assert( ( walk - ret ) == ( unreqCount + reqCount ) ); |
---|
685 | *setmeCount = walk - ret; |
---|
686 | |
---|
687 | tr_free( req ); |
---|
688 | tr_free( unreq ); |
---|
689 | tr_free( pieces ); |
---|
690 | |
---|
691 | return ret; |
---|
692 | } |
---|
693 | |
---|
694 | static int |
---|
695 | refillPulse( void * vtorrent ) |
---|
696 | { |
---|
697 | Torrent * t = vtorrent; |
---|
698 | tr_torrent * tor = t->tor; |
---|
699 | uint32_t i; |
---|
700 | int peerCount; |
---|
701 | tr_peer ** peers; |
---|
702 | uint64_t blockCount; |
---|
703 | uint64_t * blocks; |
---|
704 | |
---|
705 | if( !t->isRunning ) |
---|
706 | return TRUE; |
---|
707 | if( tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE ) |
---|
708 | return TRUE; |
---|
709 | |
---|
710 | torrentLock( t ); |
---|
711 | tordbg( t, "Refilling Request Buffers..." ); |
---|
712 | |
---|
713 | blocks = getPreferredBlocks( t, &blockCount ); |
---|
714 | peers = getConnectedPeers( t, &peerCount ); |
---|
715 | |
---|
716 | for( i=0; peerCount && i<blockCount; ++i ) |
---|
717 | { |
---|
718 | const int block = blocks[i]; |
---|
719 | const uint32_t index = tr_torBlockPiece( tor, block ); |
---|
720 | const uint32_t begin = (block * tor->blockSize) - (index * tor->info.pieceSize); |
---|
721 | const uint32_t length = tr_torBlockCountBytes( tor, block ); |
---|
722 | int j; |
---|
723 | assert( _tr_block( tor, index, begin ) == block ); |
---|
724 | assert( begin < (uint32_t)tr_torPieceCountBytes( tor, (int)index ) ); |
---|
725 | assert( (begin + length) <= (uint32_t)tr_torPieceCountBytes( tor, (int)index ) ); |
---|
726 | |
---|
727 | |
---|
728 | /* find a peer who can ask for this block */ |
---|
729 | for( j=0; j<peerCount; ) |
---|
730 | { |
---|
731 | const int val = tr_peerMsgsAddRequest( peers[j]->msgs, index, begin, length ); |
---|
732 | switch( val ) |
---|
733 | { |
---|
734 | case TR_ADDREQ_FULL: |
---|
735 | case TR_ADDREQ_CLIENT_CHOKED: |
---|
736 | memmove( peers+j, peers+j+1, sizeof(tr_peer*)*(--peerCount-j) ); |
---|
737 | break; |
---|
738 | |
---|
739 | case TR_ADDREQ_MISSING: |
---|
740 | case TR_ADDREQ_DUPLICATE: |
---|
741 | ++j; |
---|
742 | break; |
---|
743 | |
---|
744 | case TR_ADDREQ_OK: |
---|
745 | tr_bitfieldAdd( t->requested, block ); |
---|
746 | j = peerCount; |
---|
747 | break; |
---|
748 | |
---|
749 | default: |
---|
750 | assert( 0 && "unhandled value" ); |
---|
751 | break; |
---|
752 | } |
---|
753 | } |
---|
754 | } |
---|
755 | |
---|
756 | /* cleanup */ |
---|
757 | tr_free( peers ); |
---|
758 | tr_free( blocks ); |
---|
759 | |
---|
760 | t->refillTimer = NULL; |
---|
761 | torrentUnlock( t ); |
---|
762 | return FALSE; |
---|
763 | } |
---|
764 | |
---|
765 | static void |
---|
766 | broadcastClientHave( Torrent * t, uint32_t index ) |
---|
767 | { |
---|
768 | int i, size; |
---|
769 | tr_peer ** peers; |
---|
770 | |
---|
771 | assert( torrentIsLocked( t ) ); |
---|
772 | |
---|
773 | peers = getConnectedPeers( t, &size ); |
---|
774 | for( i=0; i<size; ++i ) |
---|
775 | tr_peerMsgsHave( peers[i]->msgs, index ); |
---|
776 | tr_free( peers ); |
---|
777 | } |
---|
778 | |
---|
779 | static void |
---|
780 | broadcastGotBlock( Torrent * t, uint32_t index, uint32_t offset, uint32_t length ) |
---|
781 | { |
---|
782 | int i, size; |
---|
783 | tr_peer ** peers; |
---|
784 | |
---|
785 | assert( torrentIsLocked( t ) ); |
---|
786 | |
---|
787 | peers = getConnectedPeers( t, &size ); |
---|
788 | for( i=0; i<size; ++i ) |
---|
789 | tr_peerMsgsCancel( peers[i]->msgs, index, offset, length ); |
---|
790 | tr_free( peers ); |
---|
791 | } |
---|
792 | |
---|
793 | static void |
---|
794 | msgsCallbackFunc( void * vpeer, void * vevent, void * vt ) |
---|
795 | { |
---|
796 | tr_peer * peer = vpeer; |
---|
797 | Torrent * t = (Torrent *) vt; |
---|
798 | const tr_peermsgs_event * e = (const tr_peermsgs_event *) vevent; |
---|
799 | |
---|
800 | torrentLock( t ); |
---|
801 | |
---|
802 | switch( e->eventType ) |
---|
803 | { |
---|
804 | case TR_PEERMSG_NEED_REQ: |
---|
805 | if( t->refillTimer == NULL ) |
---|
806 | t->refillTimer = tr_timerNew( t->manager->handle, |
---|
807 | refillPulse, t, |
---|
808 | REFILL_PERIOD_MSEC ); |
---|
809 | break; |
---|
810 | |
---|
811 | case TR_PEERMSG_CANCEL: |
---|
812 | tr_bitfieldRem( t->requested, _tr_block( t->tor, e->pieceIndex, e->offset ) ); |
---|
813 | break; |
---|
814 | |
---|
815 | case TR_PEERMSG_CLIENT_HAVE: |
---|
816 | broadcastClientHave( t, e->pieceIndex ); |
---|
817 | tr_torrentRecheckCompleteness( t->tor ); |
---|
818 | break; |
---|
819 | |
---|
820 | case TR_PEERMSG_PEER_PROGRESS: { |
---|
821 | struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
822 | const int peerIsSeed = e->progress >= 1.0; |
---|
823 | if( peerIsSeed ) { |
---|
824 | tordbg( t, "marking peer %s as a seed", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
825 | atom->flags |= ADDED_F_SEED_FLAG; |
---|
826 | } else { |
---|
827 | tordbg( t, "marking peer %s as a non-seed", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
828 | atom->flags &= ~ADDED_F_SEED_FLAG; |
---|
829 | } break; |
---|
830 | } |
---|
831 | |
---|
832 | case TR_PEERMSG_CLIENT_BLOCK: |
---|
833 | broadcastGotBlock( t, e->pieceIndex, e->offset, e->length ); |
---|
834 | break; |
---|
835 | |
---|
836 | case TR_PEERMSG_GOT_ERROR: |
---|
837 | peer->doPurge = 1; |
---|
838 | break; |
---|
839 | |
---|
840 | default: |
---|
841 | assert(0); |
---|
842 | } |
---|
843 | |
---|
844 | torrentUnlock( t ); |
---|
845 | } |
---|
846 | |
---|
847 | static void |
---|
848 | ensureAtomExists( Torrent * t, const struct in_addr * addr, uint16_t port, uint8_t flags, uint8_t from ) |
---|
849 | { |
---|
850 | if( !peerIsKnown( t, addr ) ) |
---|
851 | { |
---|
852 | struct peer_atom * a = tr_new0( struct peer_atom, 1 ); |
---|
853 | a->addr = *addr; |
---|
854 | a->port = port; |
---|
855 | a->flags = flags; |
---|
856 | a->from = from; |
---|
857 | tordbg( t, "got a new atom: %s", tr_peerIoAddrStr(&a->addr,a->port) ); |
---|
858 | tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms ); |
---|
859 | } |
---|
860 | } |
---|
861 | |
---|
862 | /* FIXME: this is kind of a mess. */ |
---|
863 | static void |
---|
864 | myHandshakeDoneCB( tr_handshake * handshake, |
---|
865 | tr_peerIo * io, |
---|
866 | int isConnected, |
---|
867 | const uint8_t * peer_id, |
---|
868 | void * vmanager ) |
---|
869 | { |
---|
870 | int ok = isConnected; |
---|
871 | uint16_t port; |
---|
872 | const struct in_addr * in_addr; |
---|
873 | tr_peerMgr * manager = (tr_peerMgr*) vmanager; |
---|
874 | Torrent * t; |
---|
875 | tr_handshake * ours; |
---|
876 | |
---|
877 | assert( io != NULL ); |
---|
878 | assert( isConnected==0 || isConnected==1 ); |
---|
879 | |
---|
880 | t = tr_peerIoHasTorrentHash( io ) |
---|
881 | ? getExistingTorrent( manager, tr_peerIoGetTorrentHash( io ) ) |
---|
882 | : NULL; |
---|
883 | |
---|
884 | if( tr_peerIoIsIncoming ( io ) ) |
---|
885 | ours = tr_ptrArrayRemoveSorted( manager->incomingHandshakes, |
---|
886 | handshake, handshakeCompare ); |
---|
887 | else if( t != NULL ) |
---|
888 | ours = tr_ptrArrayRemoveSorted( t->outgoingHandshakes, |
---|
889 | handshake, handshakeCompare ); |
---|
890 | else |
---|
891 | ours = handshake; |
---|
892 | |
---|
893 | assert( ours != NULL ); |
---|
894 | assert( ours == handshake ); |
---|
895 | |
---|
896 | if( t != NULL ) |
---|
897 | torrentLock( t ); |
---|
898 | |
---|
899 | in_addr = tr_peerIoGetAddress( io, &port ); |
---|
900 | |
---|
901 | if( !ok || !t || !t->isRunning ) |
---|
902 | { |
---|
903 | tr_peerIoFree( io ); |
---|
904 | } |
---|
905 | else /* looking good */ |
---|
906 | { |
---|
907 | uint16_t port; |
---|
908 | const struct in_addr * addr = tr_peerIoGetAddress( io, &port ); |
---|
909 | struct peer_atom * atom; |
---|
910 | ensureAtomExists( t, addr, port, 0, TR_PEER_FROM_INCOMING ); |
---|
911 | atom = getExistingAtom( t, addr ); |
---|
912 | |
---|
913 | if( atom->myflags & MYFLAG_BANNED ) |
---|
914 | { |
---|
915 | tordbg( t, "banned peer %s tried to reconnect", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
916 | tr_peerIoFree( io ); |
---|
917 | } |
---|
918 | else |
---|
919 | { |
---|
920 | tr_peer * peer = getExistingPeer( t, addr ); |
---|
921 | |
---|
922 | if( peer != NULL ) /* we already have this peer */ |
---|
923 | { |
---|
924 | tr_peerIoFree( io ); |
---|
925 | } |
---|
926 | else |
---|
927 | { |
---|
928 | peer = getPeer( t, addr ); |
---|
929 | tr_free( peer->client ); |
---|
930 | peer->client = peer_id ? tr_clientForId( peer_id ) : NULL; |
---|
931 | peer->port = port; |
---|
932 | peer->io = io; |
---|
933 | peer->msgs = tr_peerMsgsNew( t->tor, peer, msgsCallbackFunc, t, &peer->msgsTag ); |
---|
934 | atom->time = time( NULL ); |
---|
935 | } |
---|
936 | } |
---|
937 | } |
---|
938 | |
---|
939 | if( t != NULL ) |
---|
940 | torrentUnlock( t ); |
---|
941 | } |
---|
942 | |
---|
943 | void |
---|
944 | tr_peerMgrAddIncoming( tr_peerMgr * manager, |
---|
945 | struct in_addr * addr, |
---|
946 | uint16_t port, |
---|
947 | int socket ) |
---|
948 | { |
---|
949 | managerLock( manager ); |
---|
950 | |
---|
951 | if( getExistingHandshake( manager->incomingHandshakes, addr ) == NULL ) |
---|
952 | { |
---|
953 | tr_peerIo * io = tr_peerIoNewIncoming( manager->handle, addr, port, socket ); |
---|
954 | |
---|
955 | tr_handshake * handshake = tr_handshakeNew( io, |
---|
956 | manager->handle->encryptionMode, |
---|
957 | myHandshakeDoneCB, |
---|
958 | manager ); |
---|
959 | |
---|
960 | tr_ptrArrayInsertSorted( manager->incomingHandshakes, handshake, handshakeCompare ); |
---|
961 | } |
---|
962 | |
---|
963 | managerUnlock( manager ); |
---|
964 | } |
---|
965 | |
---|
966 | void |
---|
967 | tr_peerMgrAddPex( tr_peerMgr * manager, |
---|
968 | const uint8_t * torrentHash, |
---|
969 | int from, |
---|
970 | const tr_pex * pex, |
---|
971 | int pexCount ) |
---|
972 | { |
---|
973 | Torrent * t; |
---|
974 | const tr_pex * end; |
---|
975 | |
---|
976 | managerLock( manager ); |
---|
977 | |
---|
978 | t = getExistingTorrent( manager, torrentHash ); |
---|
979 | for( end=pex+pexCount; pex!=end; ++pex ) |
---|
980 | ensureAtomExists( t, &pex->in_addr, pex->port, pex->flags, from ); |
---|
981 | |
---|
982 | managerUnlock( manager ); |
---|
983 | } |
---|
984 | |
---|
985 | void |
---|
986 | tr_peerMgrAddPeers( tr_peerMgr * manager, |
---|
987 | const uint8_t * torrentHash, |
---|
988 | int from, |
---|
989 | const uint8_t * peerCompact, |
---|
990 | int peerCount ) |
---|
991 | { |
---|
992 | int i; |
---|
993 | const uint8_t * walk = peerCompact; |
---|
994 | Torrent * t; |
---|
995 | |
---|
996 | managerLock( manager ); |
---|
997 | |
---|
998 | t = getExistingTorrent( manager, torrentHash ); |
---|
999 | for( i=0; t!=NULL && i<peerCount; ++i ) |
---|
1000 | { |
---|
1001 | struct in_addr addr; |
---|
1002 | uint16_t port; |
---|
1003 | memcpy( &addr, walk, 4 ); walk += 4; |
---|
1004 | memcpy( &port, walk, 2 ); walk += 2; |
---|
1005 | ensureAtomExists( t, &addr, port, 0, from ); |
---|
1006 | } |
---|
1007 | |
---|
1008 | managerUnlock( manager ); |
---|
1009 | } |
---|
1010 | |
---|
1011 | /** |
---|
1012 | *** |
---|
1013 | **/ |
---|
1014 | |
---|
1015 | void |
---|
1016 | tr_peerMgrSetBlame( tr_peerMgr * manager, |
---|
1017 | const uint8_t * torrentHash, |
---|
1018 | int pieceIndex, |
---|
1019 | int success ) |
---|
1020 | { |
---|
1021 | if( !success ) |
---|
1022 | { |
---|
1023 | int peerCount, i; |
---|
1024 | Torrent * t = getExistingTorrent( manager, torrentHash ); |
---|
1025 | tr_peer ** peers; |
---|
1026 | |
---|
1027 | assert( torrentIsLocked( t ) ); |
---|
1028 | |
---|
1029 | peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1030 | for( i=0; i<peerCount; ++i ) |
---|
1031 | { |
---|
1032 | struct peer_atom * atom; |
---|
1033 | tr_peer * peer; |
---|
1034 | |
---|
1035 | peer = peers[i]; |
---|
1036 | if( !tr_bitfieldHas( peer->blame, pieceIndex ) ) |
---|
1037 | continue; |
---|
1038 | |
---|
1039 | ++peer->strikes; |
---|
1040 | tordbg( t, "peer %s contributed to corrupt piece (%d); now has %d strikes", |
---|
1041 | tr_peerIoAddrStr(&peer->in_addr,peer->port), |
---|
1042 | pieceIndex, (int)peer->strikes ); |
---|
1043 | if( peer->strikes < MAX_BAD_PIECES_PER_PEER ) |
---|
1044 | continue; |
---|
1045 | |
---|
1046 | atom = getExistingAtom( t, &peer->in_addr ); |
---|
1047 | atom->myflags |= MYFLAG_BANNED; |
---|
1048 | peer->doPurge = 1; |
---|
1049 | tordbg( t, "banning peer %s due to corrupt data", tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1050 | } |
---|
1051 | } |
---|
1052 | } |
---|
1053 | |
---|
1054 | int |
---|
1055 | tr_pexCompare( const void * va, const void * vb ) |
---|
1056 | { |
---|
1057 | const tr_pex * a = (const tr_pex *) va; |
---|
1058 | const tr_pex * b = (const tr_pex *) vb; |
---|
1059 | int i = memcmp( &a->in_addr, &b->in_addr, sizeof(struct in_addr) ); |
---|
1060 | if( i ) return i; |
---|
1061 | if( a->port < b->port ) return -1; |
---|
1062 | if( a->port > b->port ) return 1; |
---|
1063 | return 0; |
---|
1064 | } |
---|
1065 | |
---|
1066 | int tr_pexCompare( const void * a, const void * b ); |
---|
1067 | |
---|
1068 | static int |
---|
1069 | peerPrefersCrypto( const tr_peer * peer ) |
---|
1070 | { |
---|
1071 | if( peer->encryption_preference == ENCRYPTION_PREFERENCE_YES ) |
---|
1072 | return TRUE; |
---|
1073 | |
---|
1074 | if( peer->encryption_preference == ENCRYPTION_PREFERENCE_NO ) |
---|
1075 | return FALSE; |
---|
1076 | |
---|
1077 | return tr_peerIoIsEncrypted( peer->io ); |
---|
1078 | }; |
---|
1079 | |
---|
1080 | int |
---|
1081 | tr_peerMgrGetPeers( tr_peerMgr * manager, |
---|
1082 | const uint8_t * torrentHash, |
---|
1083 | tr_pex ** setme_pex ) |
---|
1084 | { |
---|
1085 | const Torrent * t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1086 | int i, peerCount; |
---|
1087 | const tr_peer ** peers; |
---|
1088 | tr_pex * pex; |
---|
1089 | tr_pex * walk; |
---|
1090 | |
---|
1091 | torrentLock( (Torrent*)t ); |
---|
1092 | |
---|
1093 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1094 | pex = walk = tr_new( tr_pex, peerCount ); |
---|
1095 | |
---|
1096 | for( i=0; i<peerCount; ++i, ++walk ) |
---|
1097 | { |
---|
1098 | const tr_peer * peer = peers[i]; |
---|
1099 | |
---|
1100 | walk->in_addr = peer->in_addr; |
---|
1101 | |
---|
1102 | walk->port = peer->port; |
---|
1103 | |
---|
1104 | walk->flags = 0; |
---|
1105 | if( peerPrefersCrypto(peer) ) walk->flags |= ADDED_F_ENCRYPTION_FLAG; |
---|
1106 | if( peer->progress >= 1.0 ) walk->flags |= ADDED_F_SEED_FLAG; |
---|
1107 | } |
---|
1108 | |
---|
1109 | assert( ( walk - pex ) == peerCount ); |
---|
1110 | qsort( pex, peerCount, sizeof(tr_pex), tr_pexCompare ); |
---|
1111 | *setme_pex = pex; |
---|
1112 | |
---|
1113 | torrentUnlock( (Torrent*)t ); |
---|
1114 | |
---|
1115 | return peerCount; |
---|
1116 | } |
---|
1117 | |
---|
1118 | static int reconnectPulse( void * vtorrent ); |
---|
1119 | static int rechokePulse( void * vtorrent ); |
---|
1120 | |
---|
1121 | void |
---|
1122 | tr_peerMgrStartTorrent( tr_peerMgr * manager, |
---|
1123 | const uint8_t * torrentHash ) |
---|
1124 | { |
---|
1125 | Torrent * t; |
---|
1126 | |
---|
1127 | managerLock( manager ); |
---|
1128 | |
---|
1129 | t = getExistingTorrent( manager, torrentHash ); |
---|
1130 | |
---|
1131 | assert( t != NULL ); |
---|
1132 | assert( ( t->isRunning != 0 ) == ( t->reconnectTimer != NULL ) ); |
---|
1133 | assert( ( t->isRunning != 0 ) == ( t->rechokeTimer != NULL ) ); |
---|
1134 | |
---|
1135 | if( !t->isRunning ) |
---|
1136 | { |
---|
1137 | t->isRunning = 1; |
---|
1138 | |
---|
1139 | t->reconnectTimer = tr_timerNew( t->manager->handle, |
---|
1140 | reconnectPulse, t, |
---|
1141 | RECONNECT_PERIOD_MSEC ); |
---|
1142 | |
---|
1143 | t->rechokeTimer = tr_timerNew( t->manager->handle, |
---|
1144 | rechokePulse, t, |
---|
1145 | RECHOKE_PERIOD_MSEC ); |
---|
1146 | |
---|
1147 | reconnectPulse( t ); |
---|
1148 | |
---|
1149 | rechokePulse( t ); |
---|
1150 | } |
---|
1151 | |
---|
1152 | managerUnlock( manager ); |
---|
1153 | } |
---|
1154 | |
---|
1155 | static void |
---|
1156 | stopTorrent( Torrent * t ) |
---|
1157 | { |
---|
1158 | assert( torrentIsLocked( t ) ); |
---|
1159 | |
---|
1160 | t->isRunning = 0; |
---|
1161 | tr_timerFree( &t->rechokeTimer ); |
---|
1162 | tr_timerFree( &t->reconnectTimer ); |
---|
1163 | |
---|
1164 | /* disconnect the peers. */ |
---|
1165 | tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)peerDestructor ); |
---|
1166 | tr_ptrArrayClear( t->peers ); |
---|
1167 | |
---|
1168 | /* disconnect the handshakes. handshakeAbort calls handshakeDoneCB(), |
---|
1169 | * which removes the handshake from t->outgoingHandshakes... */ |
---|
1170 | while( !tr_ptrArrayEmpty( t->outgoingHandshakes ) ) |
---|
1171 | tr_handshakeAbort( tr_ptrArrayNth( t->outgoingHandshakes, 0 ) ); |
---|
1172 | } |
---|
1173 | void |
---|
1174 | tr_peerMgrStopTorrent( tr_peerMgr * manager, |
---|
1175 | const uint8_t * torrentHash) |
---|
1176 | { |
---|
1177 | managerLock( manager ); |
---|
1178 | |
---|
1179 | stopTorrent( getExistingTorrent( manager, torrentHash ) ); |
---|
1180 | |
---|
1181 | managerUnlock( manager ); |
---|
1182 | } |
---|
1183 | |
---|
1184 | void |
---|
1185 | tr_peerMgrAddTorrent( tr_peerMgr * manager, |
---|
1186 | tr_torrent * tor ) |
---|
1187 | { |
---|
1188 | Torrent * t; |
---|
1189 | |
---|
1190 | managerLock( manager ); |
---|
1191 | |
---|
1192 | assert( tor != NULL ); |
---|
1193 | assert( getExistingTorrent( manager, tor->info.hash ) == NULL ); |
---|
1194 | |
---|
1195 | t = torrentConstructor( manager, tor ); |
---|
1196 | tr_ptrArrayInsertSorted( manager->torrents, t, torrentCompare ); |
---|
1197 | |
---|
1198 | managerUnlock( manager ); |
---|
1199 | } |
---|
1200 | |
---|
1201 | void |
---|
1202 | tr_peerMgrRemoveTorrent( tr_peerMgr * manager, |
---|
1203 | const uint8_t * torrentHash ) |
---|
1204 | { |
---|
1205 | Torrent * t; |
---|
1206 | |
---|
1207 | managerLock( manager ); |
---|
1208 | |
---|
1209 | t = getExistingTorrent( manager, torrentHash ); |
---|
1210 | assert( t != NULL ); |
---|
1211 | stopTorrent( t ); |
---|
1212 | tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare ); |
---|
1213 | torrentDestructor( t ); |
---|
1214 | |
---|
1215 | managerUnlock( manager ); |
---|
1216 | } |
---|
1217 | |
---|
1218 | void |
---|
1219 | tr_peerMgrTorrentAvailability( const tr_peerMgr * manager, |
---|
1220 | const uint8_t * torrentHash, |
---|
1221 | int8_t * tab, |
---|
1222 | int tabCount ) |
---|
1223 | { |
---|
1224 | int i; |
---|
1225 | const Torrent * t; |
---|
1226 | const tr_torrent * tor; |
---|
1227 | float interval; |
---|
1228 | |
---|
1229 | managerLock( (tr_peerMgr*)manager ); |
---|
1230 | |
---|
1231 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1232 | tor = t->tor; |
---|
1233 | interval = tor->info.pieceCount / (float)tabCount; |
---|
1234 | |
---|
1235 | memset( tab, 0, tabCount ); |
---|
1236 | |
---|
1237 | for( i=0; i<tabCount; ++i ) |
---|
1238 | { |
---|
1239 | const int piece = i * interval; |
---|
1240 | |
---|
1241 | if( tor == NULL ) |
---|
1242 | tab[i] = 0; |
---|
1243 | else if( tr_cpPieceIsComplete( tor->completion, piece ) ) |
---|
1244 | tab[i] = -1; |
---|
1245 | else { |
---|
1246 | int j, peerCount; |
---|
1247 | const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount ); |
---|
1248 | for( j=0; j<peerCount; ++j ) |
---|
1249 | if( tr_bitfieldHas( peers[j]->have, i ) ) |
---|
1250 | ++tab[i]; |
---|
1251 | } |
---|
1252 | } |
---|
1253 | |
---|
1254 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1255 | } |
---|
1256 | |
---|
1257 | /* Returns the pieces that we and/or a connected peer has */ |
---|
1258 | tr_bitfield* |
---|
1259 | tr_peerMgrGetAvailable( const tr_peerMgr * manager, |
---|
1260 | const uint8_t * torrentHash ) |
---|
1261 | { |
---|
1262 | int i, size; |
---|
1263 | const Torrent * t; |
---|
1264 | const tr_peer ** peers; |
---|
1265 | tr_bitfield * pieces; |
---|
1266 | |
---|
1267 | managerLock( (tr_peerMgr*)manager ); |
---|
1268 | |
---|
1269 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1270 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size ); |
---|
1271 | pieces = tr_bitfieldDup( tr_cpPieceBitfield( t->tor->completion ) ); |
---|
1272 | for( i=0; i<size; ++i ) |
---|
1273 | if( peers[i]->io != NULL ) |
---|
1274 | tr_bitfieldAnd( pieces, peers[i]->have ); |
---|
1275 | |
---|
1276 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1277 | return pieces; |
---|
1278 | } |
---|
1279 | |
---|
1280 | void |
---|
1281 | tr_peerMgrTorrentStats( const tr_peerMgr * manager, |
---|
1282 | const uint8_t * torrentHash, |
---|
1283 | int * setmePeersKnown, |
---|
1284 | int * setmePeersConnected, |
---|
1285 | int * setmePeersSendingToUs, |
---|
1286 | int * setmePeersGettingFromUs, |
---|
1287 | int * setmePeersFrom ) |
---|
1288 | { |
---|
1289 | int i, size; |
---|
1290 | const Torrent * t; |
---|
1291 | const tr_peer ** peers; |
---|
1292 | |
---|
1293 | managerLock( (tr_peerMgr*)manager ); |
---|
1294 | |
---|
1295 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1296 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size ); |
---|
1297 | |
---|
1298 | *setmePeersKnown = tr_ptrArraySize( t->pool ); |
---|
1299 | *setmePeersConnected = 0; |
---|
1300 | *setmePeersSendingToUs = 0; |
---|
1301 | *setmePeersGettingFromUs = 0; |
---|
1302 | |
---|
1303 | for( i=0; i<TR_PEER_FROM__MAX; ++i ) |
---|
1304 | setmePeersFrom[i] = 0; |
---|
1305 | |
---|
1306 | for( i=0; i<size; ++i ) |
---|
1307 | { |
---|
1308 | const tr_peer * peer = peers[i]; |
---|
1309 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1310 | |
---|
1311 | if( peer->io == NULL ) /* not connected */ |
---|
1312 | continue; |
---|
1313 | |
---|
1314 | ++*setmePeersConnected; |
---|
1315 | |
---|
1316 | ++setmePeersFrom[atom->from]; |
---|
1317 | |
---|
1318 | if( peer->rateToPeer > 0.01 ) |
---|
1319 | ++*setmePeersGettingFromUs; |
---|
1320 | |
---|
1321 | if( peer->rateToClient > 0.01 ) |
---|
1322 | ++*setmePeersSendingToUs; |
---|
1323 | } |
---|
1324 | |
---|
1325 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1326 | } |
---|
1327 | |
---|
1328 | struct tr_peer_stat * |
---|
1329 | tr_peerMgrPeerStats( const tr_peerMgr * manager, |
---|
1330 | const uint8_t * torrentHash, |
---|
1331 | int * setmeCount UNUSED ) |
---|
1332 | { |
---|
1333 | int i, size; |
---|
1334 | const Torrent * t; |
---|
1335 | const tr_peer ** peers; |
---|
1336 | tr_peer_stat * ret; |
---|
1337 | |
---|
1338 | assert( manager != NULL ); |
---|
1339 | managerLock( (tr_peerMgr*)manager ); |
---|
1340 | |
---|
1341 | t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash ); |
---|
1342 | peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size ); |
---|
1343 | |
---|
1344 | ret = tr_new0( tr_peer_stat, size ); |
---|
1345 | |
---|
1346 | for( i=0; i<size; ++i ) |
---|
1347 | { |
---|
1348 | const tr_peer * peer = peers[i]; |
---|
1349 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1350 | tr_peer_stat * stat = ret + i; |
---|
1351 | |
---|
1352 | tr_netNtop( &peer->in_addr, stat->addr, sizeof(stat->addr) ); |
---|
1353 | stat->port = peer->port; |
---|
1354 | stat->from = atom->from; |
---|
1355 | stat->client = tr_strdup( peer->client ? peer->client : "" ); |
---|
1356 | stat->progress = peer->progress; |
---|
1357 | stat->isEncrypted = tr_peerIoIsEncrypted( peer->io ) ? 1 : 0; |
---|
1358 | stat->uploadToRate = peer->rateToPeer; |
---|
1359 | stat->downloadFromRate = peer->rateToClient; |
---|
1360 | stat->isDownloading = stat->uploadToRate > 0.01; |
---|
1361 | stat->isUploading = stat->downloadFromRate > 0.01; |
---|
1362 | } |
---|
1363 | |
---|
1364 | *setmeCount = size; |
---|
1365 | |
---|
1366 | managerUnlock( (tr_peerMgr*)manager ); |
---|
1367 | return ret; |
---|
1368 | } |
---|
1369 | |
---|
1370 | /** |
---|
1371 | *** |
---|
1372 | **/ |
---|
1373 | |
---|
1374 | struct ChokeData |
---|
1375 | { |
---|
1376 | tr_peer * peer; |
---|
1377 | float rate; |
---|
1378 | int randomKey; |
---|
1379 | int preferred; |
---|
1380 | int doUnchoke; |
---|
1381 | }; |
---|
1382 | |
---|
1383 | static int |
---|
1384 | compareChoke( const void * va, const void * vb ) |
---|
1385 | { |
---|
1386 | const struct ChokeData * a = va; |
---|
1387 | const struct ChokeData * b = vb; |
---|
1388 | |
---|
1389 | if( a->preferred != b->preferred ) |
---|
1390 | return a->preferred ? -1 : 1; |
---|
1391 | |
---|
1392 | if( a->preferred ) |
---|
1393 | { |
---|
1394 | if( a->rate > b->rate ) return -1; |
---|
1395 | if( a->rate < b->rate ) return 1; |
---|
1396 | return 0; |
---|
1397 | } |
---|
1398 | else |
---|
1399 | { |
---|
1400 | return a->randomKey - b->randomKey; |
---|
1401 | } |
---|
1402 | } |
---|
1403 | |
---|
1404 | static int |
---|
1405 | clientIsSnubbedBy( const tr_peer * peer ) |
---|
1406 | { |
---|
1407 | assert( peer != NULL ); |
---|
1408 | |
---|
1409 | return peer->peerSentPieceDataAt < (time(NULL) - SNUBBED_SEC); |
---|
1410 | } |
---|
1411 | |
---|
1412 | /** |
---|
1413 | *** |
---|
1414 | **/ |
---|
1415 | |
---|
1416 | static double |
---|
1417 | getWeightedThroughput( const tr_peer * peer ) |
---|
1418 | { |
---|
1419 | return ( 3 * peer->rateToPeer ) |
---|
1420 | + ( 1 * peer->rateToClient ); |
---|
1421 | } |
---|
1422 | |
---|
1423 | static void |
---|
1424 | rechoke( Torrent * t ) |
---|
1425 | { |
---|
1426 | int i, peerCount, size=0, unchoked=0; |
---|
1427 | const time_t ignorePeersNewerThan = time(NULL) - MIN_CHOKE_PERIOD_SEC; |
---|
1428 | tr_peer ** peers = getConnectedPeers( t, &peerCount ); |
---|
1429 | struct ChokeData * choke = tr_new0( struct ChokeData, peerCount ); |
---|
1430 | |
---|
1431 | assert( torrentIsLocked( t ) ); |
---|
1432 | |
---|
1433 | /* sort the peers by preference and rate */ |
---|
1434 | for( i=0; i<peerCount; ++i ) |
---|
1435 | { |
---|
1436 | tr_peer * peer = peers[i]; |
---|
1437 | struct ChokeData * node; |
---|
1438 | if( peer->chokeChangedAt > ignorePeersNewerThan ) |
---|
1439 | continue; |
---|
1440 | |
---|
1441 | node = &choke[size++]; |
---|
1442 | node->peer = peer; |
---|
1443 | node->preferred = peer->peerIsInterested && !clientIsSnubbedBy(peer); |
---|
1444 | node->randomKey = tr_rand( INT_MAX ); |
---|
1445 | node->rate = getWeightedThroughput( peer ); |
---|
1446 | } |
---|
1447 | |
---|
1448 | qsort( choke, size, sizeof(struct ChokeData), compareChoke ); |
---|
1449 | |
---|
1450 | for( i=0; i<size && i<NUM_UNCHOKED_PEERS_PER_TORRENT; ++i ) { |
---|
1451 | choke[i].doUnchoke = 1; |
---|
1452 | ++unchoked; |
---|
1453 | } |
---|
1454 | |
---|
1455 | for( ; i<size; ++i ) { |
---|
1456 | choke[i].doUnchoke = 1; |
---|
1457 | ++unchoked; |
---|
1458 | if( choke[i].peer->peerIsInterested ) |
---|
1459 | break; |
---|
1460 | } |
---|
1461 | |
---|
1462 | for( i=0; i<size; ++i ) |
---|
1463 | tr_peerMsgsSetChoke( choke[i].peer->msgs, !choke[i].doUnchoke ); |
---|
1464 | |
---|
1465 | /* cleanup */ |
---|
1466 | tr_free( choke ); |
---|
1467 | tr_free( peers ); |
---|
1468 | } |
---|
1469 | |
---|
1470 | static int |
---|
1471 | rechokePulse( void * vtorrent ) |
---|
1472 | { |
---|
1473 | Torrent * t = vtorrent; |
---|
1474 | torrentLock( t ); |
---|
1475 | rechoke( t ); |
---|
1476 | torrentUnlock( t ); |
---|
1477 | return TRUE; |
---|
1478 | } |
---|
1479 | |
---|
1480 | /*** |
---|
1481 | **** |
---|
1482 | **** |
---|
1483 | **** |
---|
1484 | ***/ |
---|
1485 | |
---|
1486 | #define LAISSEZ_FAIRE_PERIOD_SECS 90 |
---|
1487 | |
---|
1488 | static tr_peer ** |
---|
1489 | getWeakConnections( Torrent * t, int * setmeSize ) |
---|
1490 | { |
---|
1491 | int i, insize, outsize; |
---|
1492 | tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &insize ); |
---|
1493 | struct tr_peer ** ret = tr_new( tr_peer*, insize ); |
---|
1494 | const int clientIsSeed = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE; |
---|
1495 | const time_t now = time( NULL ); |
---|
1496 | |
---|
1497 | assert( torrentIsLocked( t ) ); |
---|
1498 | |
---|
1499 | for( i=outsize=0; i<insize; ++i ) |
---|
1500 | { |
---|
1501 | tr_peer * peer = peers[i]; |
---|
1502 | int isWeak; |
---|
1503 | const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr ); |
---|
1504 | const int peerIsSeed = atom->flags & ADDED_F_SEED_FLAG; |
---|
1505 | const double throughput = getWeightedThroughput( peer ); |
---|
1506 | |
---|
1507 | assert( atom != NULL ); |
---|
1508 | |
---|
1509 | if( peer->doPurge ) |
---|
1510 | isWeak = TRUE; |
---|
1511 | else if( throughput >= 3 ) |
---|
1512 | isWeak = FALSE; |
---|
1513 | else if( peerIsSeed && clientIsSeed ) |
---|
1514 | isWeak = t->tor->pexDisabled || (now-atom->time>=30); |
---|
1515 | else if( !atom->time || ( ( now - atom->time ) < LAISSEZ_FAIRE_PERIOD_SECS ) ) |
---|
1516 | isWeak = FALSE; |
---|
1517 | else |
---|
1518 | isWeak = ( now - peer->pieceDataActivityDate ) > 180; |
---|
1519 | |
---|
1520 | if( isWeak ) |
---|
1521 | ret[outsize++] = peer; |
---|
1522 | } |
---|
1523 | |
---|
1524 | *setmeSize = outsize; |
---|
1525 | return ret; |
---|
1526 | } |
---|
1527 | |
---|
1528 | static int |
---|
1529 | compareAtomByTime( const void * va, const void * vb ) |
---|
1530 | { |
---|
1531 | const struct peer_atom * a = * (const struct peer_atom**) va; |
---|
1532 | const struct peer_atom * b = * (const struct peer_atom**) vb; |
---|
1533 | if( a->time < b->time ) return -1; |
---|
1534 | if( a->time > b->time ) return 1; |
---|
1535 | return 0; |
---|
1536 | } |
---|
1537 | |
---|
1538 | static struct peer_atom ** |
---|
1539 | getPeerCandidates( Torrent * t, int * setmeSize ) |
---|
1540 | { |
---|
1541 | int i, insize, outsize; |
---|
1542 | struct peer_atom ** atoms; |
---|
1543 | struct peer_atom ** ret; |
---|
1544 | const time_t now = time( NULL ); |
---|
1545 | const int seed = tr_cpGetStatus( t->tor->completion ) != TR_CP_INCOMPLETE; |
---|
1546 | |
---|
1547 | assert( torrentIsLocked( t ) ); |
---|
1548 | |
---|
1549 | atoms = (struct peer_atom**) tr_ptrArrayPeek( t->pool, &insize ); |
---|
1550 | ret = tr_new( struct peer_atom*, insize ); |
---|
1551 | for( i=outsize=0; i<insize; ++i ) |
---|
1552 | { |
---|
1553 | struct peer_atom * atom = atoms[i]; |
---|
1554 | |
---|
1555 | /* peer fed us too much bad data ... we only keep it around |
---|
1556 | * now to weed it out in case someone sends it to us via pex */ |
---|
1557 | if( atom->myflags & MYFLAG_BANNED ) { |
---|
1558 | tordbg( t, "RECONNECT peer %d (%s) is banned...", |
---|
1559 | i, tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1560 | continue; |
---|
1561 | } |
---|
1562 | |
---|
1563 | /* we don't need two connections to the same peer... */ |
---|
1564 | if( peerIsInUse( t, &atom->addr ) ) { |
---|
1565 | tordbg( t, "RECONNECT peer %d (%s) is in use..", |
---|
1566 | i, tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1567 | continue; |
---|
1568 | } |
---|
1569 | |
---|
1570 | /* no need to connect if we're both seeds... */ |
---|
1571 | if( seed && (atom->flags & ADDED_F_SEED_FLAG) ) { |
---|
1572 | tordbg( t, "RECONNECT peer %d (%s) is a seed and so are we..", |
---|
1573 | i, tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1574 | continue; |
---|
1575 | } |
---|
1576 | |
---|
1577 | /* if we used this peer recently, give someone else a turn */ |
---|
1578 | if( ( now - atom->time ) < LAISSEZ_FAIRE_PERIOD_SECS ) { |
---|
1579 | tordbg( t, "RECONNECT peer %d (%s) is in its grace period..", |
---|
1580 | i, tr_peerIoAddrStr(&atom->addr,atom->port) ); |
---|
1581 | continue; |
---|
1582 | } |
---|
1583 | |
---|
1584 | ret[outsize++] = atom; |
---|
1585 | } |
---|
1586 | |
---|
1587 | qsort( ret, outsize, sizeof(struct peer_atom*), compareAtomByTime ); |
---|
1588 | *setmeSize = outsize; |
---|
1589 | return ret; |
---|
1590 | } |
---|
1591 | |
---|
1592 | static int |
---|
1593 | reconnectPulse( void * vtorrent ) |
---|
1594 | { |
---|
1595 | Torrent * t = vtorrent; |
---|
1596 | |
---|
1597 | torrentLock( t ); |
---|
1598 | |
---|
1599 | if( !t->isRunning ) |
---|
1600 | { |
---|
1601 | removeAllPeers( t ); |
---|
1602 | } |
---|
1603 | else |
---|
1604 | { |
---|
1605 | int i, nCandidates, nWeak, nAdd; |
---|
1606 | struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates ); |
---|
1607 | struct tr_peer ** connections = getWeakConnections( t, &nWeak ); |
---|
1608 | const int peerCount = tr_ptrArraySize( t->peers ); |
---|
1609 | |
---|
1610 | if( nWeak || nCandidates ) |
---|
1611 | tordbg( t, "reconnect pulse for [%s]: %d weak connections, " |
---|
1612 | "%d connection candidates, %d atoms, max per pulse is %d", |
---|
1613 | t->tor->info.name, nWeak, nCandidates, |
---|
1614 | tr_ptrArraySize(t->pool), |
---|
1615 | (int)MAX_RECONNECTIONS_PER_PULSE ); |
---|
1616 | |
---|
1617 | /* disconnect some peers */ |
---|
1618 | for( i=0; i<nWeak; ++i ) |
---|
1619 | removePeer( t, connections[i] ); |
---|
1620 | |
---|
1621 | /* add some new ones */ |
---|
1622 | nAdd = MAX_CONNECTED_PEERS_PER_TORRENT - peerCount; |
---|
1623 | for( i=0; i<nAdd && i<nCandidates && i<MAX_RECONNECTIONS_PER_PULSE; ++i ) |
---|
1624 | { |
---|
1625 | tr_peerMgr * mgr = t->manager; |
---|
1626 | |
---|
1627 | struct peer_atom * atom = candidates[i]; |
---|
1628 | |
---|
1629 | tr_peerIo * io = tr_peerIoNewOutgoing( mgr->handle, &atom->addr, atom->port, t->hash ); |
---|
1630 | |
---|
1631 | tr_handshake * handshake = tr_handshakeNew( io, |
---|
1632 | mgr->handle->encryptionMode, |
---|
1633 | myHandshakeDoneCB, |
---|
1634 | mgr ); |
---|
1635 | |
---|
1636 | assert( tr_peerIoGetTorrentHash( io ) != NULL ); |
---|
1637 | |
---|
1638 | tr_ptrArrayInsertSorted( t->outgoingHandshakes, handshake, handshakeCompare ); |
---|
1639 | |
---|
1640 | atom->time = time( NULL ); |
---|
1641 | } |
---|
1642 | |
---|
1643 | /* cleanup */ |
---|
1644 | tr_free( connections ); |
---|
1645 | tr_free( candidates ); |
---|
1646 | } |
---|
1647 | |
---|
1648 | torrentUnlock( t ); |
---|
1649 | return TRUE; |
---|
1650 | } |
---|