2006-07-16 19:39:23 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* $Id$
|
|
|
|
*
|
2007-03-23 08:28:01 +00:00
|
|
|
* Copyright (c) 2005-2007 Transmission authors and contributors
|
2006-07-16 19:39:23 +00:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
static int peerCmp( tr_peer_t * peer1, tr_peer_t * peer2 )
|
|
|
|
{
|
|
|
|
/* Wait until we got the peers' ids */
|
2007-03-23 08:28:01 +00:00
|
|
|
if( peer1->status <= PEER_STATUS_HANDSHAKE ||
|
|
|
|
peer2->status <= PEER_STATUS_HANDSHAKE )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return memcmp( peer1->id, peer2->id, 20 );
|
|
|
|
}
|
|
|
|
|
2007-08-08 06:01:52 +00:00
|
|
|
static int
|
|
|
|
checkPeer( tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-08-08 06:01:52 +00:00
|
|
|
int ret;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor = peer->tor;
|
2007-08-08 06:01:52 +00:00
|
|
|
const uint64_t now = tr_date( );
|
|
|
|
const uint64_t idleTime = now - peer->date;
|
2007-08-08 15:07:06 +00:00
|
|
|
const uint64_t idleSecs = idleTime / 1000u;
|
|
|
|
uint64_t lo, hi, limit;
|
2007-08-08 06:01:52 +00:00
|
|
|
int relaxStrictnessIfFewerThanN;
|
|
|
|
double strictness;
|
|
|
|
|
|
|
|
/* when deciding whether or not to keep a peer, judge its responsiveness
|
|
|
|
on a sliding scale that's based on how many other peers are available */
|
2007-08-08 15:07:06 +00:00
|
|
|
relaxStrictnessIfFewerThanN =
|
|
|
|
(int)(((TR_MAX_PEER_COUNT * PERCENT_PEER_WANTED) / 100.0) + 0.5);
|
2007-08-08 06:01:52 +00:00
|
|
|
|
|
|
|
/* if we have >= relaxIfFewerThan, strictness is 100%.
|
|
|
|
if we have zero connections, strictness is 0% */
|
|
|
|
if( tor->peerCount >= relaxStrictnessIfFewerThanN )
|
|
|
|
strictness = 1.0;
|
|
|
|
else
|
|
|
|
strictness = tor->peerCount / (double)relaxStrictnessIfFewerThanN;
|
|
|
|
|
|
|
|
/* test: has it been too long since we were properly connected to them? */
|
|
|
|
lo = MIN_CON_TIMEOUT;
|
|
|
|
hi = MAX_CON_TIMEOUT;
|
2007-08-08 15:07:06 +00:00
|
|
|
limit = lo + ((hi-lo) * strictness);
|
|
|
|
if( peer->status < PEER_STATUS_CONNECTED && idleTime > limit ) {
|
|
|
|
peer_dbg( "connection timeout, idled %"PRIu64" seconds", idleSecs );
|
2007-08-08 06:01:52 +00:00
|
|
|
return TR_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test: have we been waiting on a request for too long? */
|
|
|
|
lo = MIN_UPLOAD_IDLE;
|
|
|
|
hi = MAX_UPLOAD_IDLE;
|
2007-08-08 15:07:06 +00:00
|
|
|
limit = lo + ((hi-lo) * strictness);
|
|
|
|
if( peer->inRequestCount && idleTime > limit ) {
|
|
|
|
peer_dbg( "idle uploader timeout, idled %"PRIu64" seconds", idleSecs );
|
2007-08-08 06:01:52 +00:00
|
|
|
return TR_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test: has it been too long since the peer gave us any response at all? */
|
|
|
|
lo = MIN_KEEP_ALIVE;
|
|
|
|
hi = MAX_KEEP_ALIVE;
|
2007-08-08 15:07:06 +00:00
|
|
|
limit = lo + ((hi-lo) * strictness);
|
|
|
|
if( idleTime > limit ) {
|
|
|
|
peer_dbg( "peer timeout, idled %"PRIu64" seconds", idleSecs );
|
2007-08-08 06:01:52 +00:00
|
|
|
return TR_ERROR;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-03-23 08:28:01 +00:00
|
|
|
if( PEER_STATUS_CONNECTED == peer->status )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-05-24 20:09:32 +00:00
|
|
|
/* Send keep-alive every 1 minute and 45 seconds */
|
|
|
|
if( now > peer->keepAlive + 105000 )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
|
|
|
sendKeepAlive( peer );
|
2007-03-23 08:28:01 +00:00
|
|
|
peer->keepAlive = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Resend extended handshake if our public port changed */
|
|
|
|
if( EXTENDED_HANDSHAKE == peer->extStatus &&
|
|
|
|
tor->publicPort != peer->advertisedPort )
|
|
|
|
{
|
|
|
|
sendExtended( tor, peer, EXTENDED_HANDSHAKE_ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Send peer list */
|
|
|
|
if( !peer->private && 0 < peer->pexStatus )
|
|
|
|
{
|
|
|
|
if( 0 == peer->lastPex )
|
|
|
|
{
|
|
|
|
/* randomize time when first pex message is sent */
|
|
|
|
peer->lastPex = now - 1000 * tr_rand( PEX_INTERVAL );
|
|
|
|
}
|
|
|
|
if( now > peer->lastPex + 1000 * PEX_INTERVAL )
|
|
|
|
{
|
2007-03-28 06:28:34 +00:00
|
|
|
if( EXTENDED_HANDSHAKE == peer->extStatus )
|
2007-03-23 08:28:01 +00:00
|
|
|
{
|
2007-03-28 06:28:34 +00:00
|
|
|
ret = sendExtended( tor, peer, EXTENDED_PEX_ID );
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
2007-03-28 06:28:34 +00:00
|
|
|
else if( peer->azproto )
|
|
|
|
{
|
|
|
|
ret = sendAZPex( tor, peer );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
assert( 0 );
|
|
|
|
ret = TR_ERROR;
|
|
|
|
}
|
|
|
|
if( ret )
|
|
|
|
{
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
peer->lastPex = now + 1000 *
|
|
|
|
( PEX_INTERVAL + tr_rand( PEX_INTERVAL / 10 ) );
|
2007-03-23 08:28:01 +00:00
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-21 06:42:05 +00:00
|
|
|
return TR_OK;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static int isPieceInteresting( const tr_torrent * tor,
|
|
|
|
const tr_peer_t * peer,
|
|
|
|
int piece )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-07-24 14:51:46 +00:00
|
|
|
if( tor->info.pieces[piece].dnd ) /* we don't want it */
|
2007-07-06 16:17:37 +00:00
|
|
|
return 0;
|
|
|
|
|
2007-07-24 14:51:46 +00:00
|
|
|
if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we already have it */
|
2007-06-18 03:40:41 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( !tr_bitfieldHas( peer->bitfield, piece ) ) /* peer doesn't have it */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if( tr_bitfieldHas( peer->banfield, piece ) ) /* peer is banned for it */
|
|
|
|
return 0;
|
|
|
|
|
2007-07-06 16:17:37 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-07-16 19:39:23 +00:00
|
|
|
/***********************************************************************
|
|
|
|
* isInteresting
|
|
|
|
***********************************************************************
|
2007-06-18 03:40:41 +00:00
|
|
|
* Returns 1 if 'peer' has at least one piece that we want but
|
|
|
|
* haven't completed, or 0 otherwise.
|
2006-07-16 19:39:23 +00:00
|
|
|
**********************************************************************/
|
2007-09-20 16:32:01 +00:00
|
|
|
static int isInteresting( const tr_torrent * tor, const tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
int i;
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_bitfield * bitfield = tr_cpPieceBitfield( tor->completion );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
|
|
|
if( !peer->bitfield )
|
|
|
|
{
|
|
|
|
/* We don't know what this peer has */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-03-05 23:03:38 +00:00
|
|
|
assert( bitfield->len == peer->bitfield->len );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
for( i=0; i<tor->info.pieceCount; ++i )
|
|
|
|
if( isPieceInteresting( tor, peer, i ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
return 1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2007-07-13 22:22:46 +00:00
|
|
|
|
|
|
|
static void
|
2007-09-20 16:32:01 +00:00
|
|
|
updateInterest( tr_torrent * tor, tr_peer_t * peer )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-07-13 22:22:46 +00:00
|
|
|
const int i = !!isInteresting( tor, peer );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-07-13 22:22:46 +00:00
|
|
|
if( i != peer->isInteresting )
|
|
|
|
sendInterest( peer, i );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/** utility structure used by getPreferredPieces() and comparePieces() */
|
|
|
|
typedef struct
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
int piece;
|
|
|
|
tr_priority_t priority;
|
|
|
|
int missingBlockCount;
|
|
|
|
int peerCount;
|
|
|
|
}
|
|
|
|
PieceCompareData;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/** utility function used by getPreferredPieces */
|
|
|
|
int comparePieces (const void * aIn, const void * bIn)
|
|
|
|
{
|
|
|
|
const PieceCompareData * a = (const PieceCompareData*) aIn;
|
|
|
|
const PieceCompareData * b = (const PieceCompareData*) bIn;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/* if one piece has a higher priority, it goes first */
|
|
|
|
if (a->priority != b->priority)
|
|
|
|
return a->priority > b->priority ? -1 : 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/* otherwise if one has fewer missing blocks, it goes first */
|
|
|
|
if (a->missingBlockCount != b->missingBlockCount)
|
|
|
|
return a->missingBlockCount < b->missingBlockCount ? -1 : 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/* otherwise if one has fewer peers, it goes first */
|
|
|
|
if (a->peerCount != b->peerCount)
|
|
|
|
return a->peerCount < b->peerCount ? -1 : 1;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
/* otherwise go with the earlier piece */
|
|
|
|
return a->piece - b->piece;
|
|
|
|
}
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static int* getPreferredPieces( const tr_torrent * tor,
|
|
|
|
const tr_peer_t * peer,
|
|
|
|
int * pieceCount,
|
|
|
|
int * isEndgame )
|
2007-06-18 03:40:41 +00:00
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_info * inf = &tor->info;
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
int i;
|
|
|
|
int poolSize = 0;
|
2007-07-24 18:59:37 +00:00
|
|
|
int endgame = FALSE;
|
2007-06-29 06:24:55 +00:00
|
|
|
int * pool = tr_new( int, inf->pieceCount );
|
2006-07-16 19:39:23 +00:00
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
for( i=0; i<inf->pieceCount; ++i )
|
|
|
|
if( isPieceInteresting( tor, peer, i ) )
|
|
|
|
if( tr_cpMissingBlocksForPiece( tor->completion, i ) )
|
|
|
|
pool[poolSize++] = i;
|
|
|
|
|
2007-07-24 18:59:37 +00:00
|
|
|
if( !poolSize ) {
|
|
|
|
endgame = TRUE;
|
|
|
|
for( i=0; i<inf->pieceCount; ++i )
|
|
|
|
if( isPieceInteresting( tor, peer, i ) )
|
|
|
|
pool[poolSize++] = i;
|
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#if 0
|
|
|
|
fprintf (stderr, "old pool: ");
|
|
|
|
for (i=0; i<15 && i<poolSize; ++i ) fprintf (stderr, "%d, ", pool[i] );
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
#endif
|
|
|
|
|
2007-07-24 18:59:37 +00:00
|
|
|
/* sort the rest from most interesting to least...
|
|
|
|
but not in endgame, because it asks for pieces in a
|
|
|
|
scattershot manner anyway and doesn't need them sorted */
|
|
|
|
if( !endgame && ( poolSize > 1 ) )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-29 06:24:55 +00:00
|
|
|
PieceCompareData * p = tr_new( PieceCompareData, poolSize );
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
for( i=0; i<poolSize; ++i )
|
2006-07-16 19:39:23 +00:00
|
|
|
{
|
2007-06-18 03:40:41 +00:00
|
|
|
int j;
|
|
|
|
const int piece = pool[i];
|
|
|
|
|
|
|
|
p[i].piece = piece;
|
|
|
|
p[i].priority = inf->pieces[piece].priority;
|
|
|
|
p[i].missingBlockCount = tr_cpMissingBlocksForPiece( tor->completion, piece );
|
|
|
|
p[i].peerCount = 0;
|
|
|
|
|
|
|
|
for( j=0; j<tor->peerCount; ++j )
|
|
|
|
if( tr_bitfieldHas( tor->peers[j]->bitfield, piece ) )
|
|
|
|
++p[i].peerCount;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
2007-06-18 03:40:41 +00:00
|
|
|
|
|
|
|
qsort (p, poolSize, sizeof(PieceCompareData), comparePieces);
|
|
|
|
|
|
|
|
for( i=0; i<poolSize; ++i )
|
|
|
|
pool[i] = p[i].piece;
|
|
|
|
|
2007-06-29 06:24:55 +00:00
|
|
|
tr_free( p );
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 03:40:41 +00:00
|
|
|
#if 0
|
|
|
|
fprintf (stderr, "new pool: ");
|
|
|
|
for (i=0; i<15 && i<poolSize; ++i ) fprintf (stderr, "%d, ", pool[i] );
|
|
|
|
fprintf (stderr, "\n");
|
|
|
|
#endif
|
|
|
|
|
2007-07-24 18:59:37 +00:00
|
|
|
*isEndgame = endgame;
|
2007-06-18 03:40:41 +00:00
|
|
|
*pieceCount = poolSize;
|
|
|
|
return pool;
|
2006-07-16 19:39:23 +00:00
|
|
|
}
|