2007-09-20 16:32:01 +00:00
|
|
|
/*
|
|
|
|
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
|
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
2007-11-09 20:07:52 +00:00
|
|
|
#include <assert.h>
|
2007-11-25 16:57:08 +00:00
|
|
|
#include <string.h> /* memcpy, memcmp, strstr */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include <stdlib.h> /* qsort */
|
|
|
|
#include <stdio.h> /* printf */
|
|
|
|
#include <limits.h> /* INT_MAX */
|
|
|
|
|
2007-10-25 13:59:46 +00:00
|
|
|
#include <libgen.h> /* basename */
|
|
|
|
#include <arpa/inet.h> /* inet_ntoa */
|
|
|
|
|
2007-10-02 14:35:02 +00:00
|
|
|
#include <event.h>
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "transmission.h"
|
|
|
|
#include "clients.h"
|
|
|
|
#include "completion.h"
|
2007-09-28 16:40:21 +00:00
|
|
|
#include "crypto.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "handshake.h"
|
|
|
|
#include "net.h"
|
|
|
|
#include "peer-io.h"
|
|
|
|
#include "peer-mgr.h"
|
|
|
|
#include "peer-mgr-private.h"
|
|
|
|
#include "peer-msgs.h"
|
2007-09-29 06:37:03 +00:00
|
|
|
#include "platform.h"
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "ptrarray.h"
|
2007-09-30 23:55:49 +00:00
|
|
|
#include "ratecontrol.h"
|
2007-10-01 15:17:15 +00:00
|
|
|
#include "shared.h"
|
2007-12-25 05:37:32 +00:00
|
|
|
#include "torrent.h"
|
2007-11-17 23:43:33 +00:00
|
|
|
#include "trcompat.h" /* strlcpy */
|
2007-09-20 16:32:01 +00:00
|
|
|
#include "trevent.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2007-11-18 01:00:49 +00:00
|
|
|
/**
|
|
|
|
*** The "SWIFT" system is described by Karthik Tamilmani,
|
|
|
|
*** Vinay Pai, and Alexander Mohr of Stony Brook University
|
|
|
|
*** in their paper "SWIFT: A System With Incentives For Trading"
|
|
|
|
*** http://citeseer.ist.psu.edu/tamilmani04swift.html
|
|
|
|
***
|
|
|
|
*** More SWIFT constants are defined in peer-mgr-private.h
|
|
|
|
**/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Allow new peers to download this many bytes from
|
|
|
|
* us when getting started. This can prevent gridlock
|
|
|
|
* with other peers using tit-for-tat algorithms
|
|
|
|
*/
|
|
|
|
static const int SWIFT_INITIAL_CREDIT = 64 * 1024; /* 64 KiB */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* We expend a fraction of our torrent's total upload speed
|
|
|
|
* on largesse by uniformly distributing free credit to
|
|
|
|
* all of our peers. This too helps prevent gridlock.
|
|
|
|
*/
|
2007-11-25 17:07:12 +00:00
|
|
|
static const double SWIFT_LARGESSE = 0.15; /* 15% of our UL */
|
2007-11-18 01:00:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* How frequently to extend largesse-based credit
|
|
|
|
*/
|
|
|
|
static const int SWIFT_PERIOD_MSEC = 5000;
|
|
|
|
|
|
|
|
|
2007-09-22 14:18:52 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
/* how frequently to change which peers are choked */
|
2007-10-06 18:20:52 +00:00
|
|
|
RECHOKE_PERIOD_MSEC = (1000),
|
2007-09-22 14:18:52 +00:00
|
|
|
|
|
|
|
/* how frequently to decide which peers live and die */
|
2007-11-29 02:56:31 +00:00
|
|
|
RECONNECT_PERIOD_MSEC = (2 * 1000),
|
2007-09-22 14:18:52 +00:00
|
|
|
|
|
|
|
/* how frequently to refill peers' request lists */
|
2007-10-06 18:20:52 +00:00
|
|
|
REFILL_PERIOD_MSEC = 666,
|
2007-09-22 14:18:52 +00:00
|
|
|
|
|
|
|
/* don't change a peer's choke status more often than this */
|
|
|
|
MIN_CHOKE_PERIOD_SEC = 10,
|
|
|
|
|
|
|
|
/* following the BT spec, we consider ourselves `snubbed' if
|
|
|
|
* we're we don't get piece data from a peer in this long */
|
|
|
|
SNUBBED_SEC = 60,
|
2007-09-22 00:22:10 +00:00
|
|
|
|
2007-10-13 13:54:05 +00:00
|
|
|
/* when many peers are available, keep idle ones this long */
|
2007-11-14 05:02:03 +00:00
|
|
|
MIN_UPLOAD_IDLE_SECS = (60 * 3),
|
2007-10-13 13:54:05 +00:00
|
|
|
|
|
|
|
/* when few peers are available, keep idle ones this long */
|
2007-11-14 05:02:03 +00:00
|
|
|
MAX_UPLOAD_IDLE_SECS = (60 * 10),
|
2007-10-13 13:54:05 +00:00
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
/* set this too high and there will be a lot of churn.
|
|
|
|
* set it too low and you'll get peers too slowly */
|
2007-11-29 02:56:31 +00:00
|
|
|
MAX_RECONNECTIONS_PER_PULSE = 2,
|
2007-10-02 00:05:40 +00:00
|
|
|
|
|
|
|
/* corresponds to ut_pex's added.f flags */
|
2007-10-01 16:31:17 +00:00
|
|
|
ADDED_F_ENCRYPTION_FLAG = 1,
|
2007-10-02 00:05:40 +00:00
|
|
|
/* corresponds to ut_pex's added.f flags */
|
2007-10-08 01:31:27 +00:00
|
|
|
ADDED_F_SEED_FLAG = 2,
|
|
|
|
|
|
|
|
/* number of bad pieces a peer is allowed to send before we ban them */
|
|
|
|
MAX_BAD_PIECES_PER_PEER = 3,
|
2007-12-15 04:26:31 +00:00
|
|
|
|
2007-10-08 01:31:27 +00:00
|
|
|
/* use for bitwise operations w/peer_atom.myflags */
|
2007-12-15 04:26:31 +00:00
|
|
|
MYFLAG_BANNED = 1,
|
|
|
|
|
|
|
|
/* unreachable for now... but not banned. if they try to connect to us it's okay */
|
|
|
|
MYFLAG_UNREACHABLE = 2
|
2007-09-22 14:18:52 +00:00
|
|
|
};
|
2007-09-22 00:22:10 +00:00
|
|
|
|
2007-10-01 16:31:17 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
/* We keep one of these for every peer we know about, whether
|
|
|
|
* it's connected or not, so the struct must be small.
|
|
|
|
* When our current connections underperform, we dip back
|
2007-10-01 16:31:17 +00:00
|
|
|
* into this list for new ones. */
|
2007-09-30 23:55:49 +00:00
|
|
|
struct peer_atom
|
|
|
|
{
|
|
|
|
uint8_t from;
|
|
|
|
uint8_t flags; /* these match the added_f flags */
|
2007-10-08 01:31:27 +00:00
|
|
|
uint8_t myflags; /* flags that aren't defined in added_f */
|
2007-09-30 23:55:49 +00:00
|
|
|
uint16_t port;
|
2007-10-16 00:55:17 +00:00
|
|
|
uint16_t numFails;
|
|
|
|
struct in_addr addr;
|
2007-09-30 23:55:49 +00:00
|
|
|
time_t time;
|
|
|
|
};
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_ptrArray * outgoingHandshakes; /* tr_handshake */
|
2007-09-30 23:55:49 +00:00
|
|
|
tr_ptrArray * pool; /* struct peer_atom */
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArray * peers; /* tr_peer */
|
2007-09-22 00:22:10 +00:00
|
|
|
tr_timer * reconnectTimer;
|
|
|
|
tr_timer * rechokeTimer;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timer * refillTimer;
|
2007-11-18 01:00:49 +00:00
|
|
|
tr_timer * swiftTimer;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_torrent * tor;
|
|
|
|
tr_bitfield * requested;
|
|
|
|
|
|
|
|
unsigned int isRunning : 1;
|
|
|
|
|
|
|
|
struct tr_peerMgr * manager;
|
|
|
|
}
|
|
|
|
Torrent;
|
|
|
|
|
|
|
|
struct tr_peerMgr
|
|
|
|
{
|
|
|
|
tr_handle * handle;
|
|
|
|
tr_ptrArray * torrents; /* Torrent */
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_ptrArray * incomingHandshakes; /* tr_handshake */
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-10-02 14:35:02 +00:00
|
|
|
static void
|
|
|
|
myDebug( const char * file, int line, const Torrent * t, const char * fmt, ... )
|
|
|
|
{
|
|
|
|
FILE * fp = tr_getLog( );
|
|
|
|
if( fp != NULL )
|
|
|
|
{
|
|
|
|
va_list args;
|
2007-10-06 18:20:52 +00:00
|
|
|
char timestr[64];
|
2007-10-22 18:52:36 +00:00
|
|
|
struct evbuffer * buf = evbuffer_new( );
|
|
|
|
char * myfile = tr_strdup( file );
|
|
|
|
|
2007-10-15 16:01:42 +00:00
|
|
|
evbuffer_add_printf( buf, "[%s] ", tr_getLogTimeStr( timestr, sizeof(timestr) ) );
|
|
|
|
if( t != NULL )
|
|
|
|
evbuffer_add_printf( buf, "%s ", t->tor->info.name );
|
2007-10-02 14:35:02 +00:00
|
|
|
va_start( args, fmt );
|
|
|
|
evbuffer_add_vprintf( buf, fmt, args );
|
|
|
|
va_end( args );
|
2007-10-22 18:52:36 +00:00
|
|
|
evbuffer_add_printf( buf, " (%s:%d)\n", basename(myfile), line );
|
2007-10-06 18:20:52 +00:00
|
|
|
fwrite( EVBUFFER_DATA(buf), 1, EVBUFFER_LENGTH(buf), fp );
|
2007-10-22 18:52:36 +00:00
|
|
|
|
|
|
|
tr_free( myfile );
|
2007-10-02 14:35:02 +00:00
|
|
|
evbuffer_free( buf );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#define tordbg(t, fmt...) myDebug(__FILE__, __LINE__, t, ##fmt )
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
static void
|
|
|
|
managerLock( struct tr_peerMgr * manager )
|
|
|
|
{
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalLock( manager->handle );
|
2007-09-29 06:37:03 +00:00
|
|
|
}
|
|
|
|
static void
|
|
|
|
managerUnlock( struct tr_peerMgr * manager )
|
|
|
|
{
|
2007-10-01 15:17:15 +00:00
|
|
|
tr_globalUnlock( manager->handle );
|
2007-09-29 06:37:03 +00:00
|
|
|
}
|
|
|
|
static void
|
|
|
|
torrentLock( Torrent * torrent )
|
|
|
|
{
|
|
|
|
managerLock( torrent->manager );
|
|
|
|
}
|
|
|
|
static void
|
|
|
|
torrentUnlock( Torrent * torrent )
|
|
|
|
{
|
|
|
|
managerUnlock( torrent->manager );
|
|
|
|
}
|
|
|
|
static int
|
2007-09-30 23:55:49 +00:00
|
|
|
torrentIsLocked( const Torrent * t )
|
2007-09-29 06:37:03 +00:00
|
|
|
{
|
2007-10-02 00:05:40 +00:00
|
|
|
return tr_globalIsLocked( t->manager->handle );
|
2007-09-29 06:37:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
static int
|
|
|
|
compareAddresses( const struct in_addr * a, const struct in_addr * b )
|
|
|
|
{
|
|
|
|
return tr_compareUint32( a->s_addr, b->s_addr );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static int
|
|
|
|
handshakeCompareToAddr( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const tr_handshake * a = va;
|
2007-09-30 23:55:49 +00:00
|
|
|
return compareAddresses( tr_handshakeGetAddr( a, NULL ), vb );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-09-21 15:31:46 +00:00
|
|
|
handshakeCompare( const void * a, const void * b )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-09-21 15:31:46 +00:00
|
|
|
return handshakeCompareToAddr( a, tr_handshakeGetAddr( b, NULL ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_handshake*
|
2007-10-02 16:12:44 +00:00
|
|
|
getExistingHandshake( tr_ptrArray * handshakes, const struct in_addr * in_addr )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-02 16:12:44 +00:00
|
|
|
return tr_ptrArrayFindSorted( handshakes,
|
2007-09-20 16:32:01 +00:00
|
|
|
in_addr,
|
|
|
|
handshakeCompareToAddr );
|
|
|
|
}
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
static int
|
|
|
|
comparePeerAtomToAddress( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const struct peer_atom * a = va;
|
|
|
|
return compareAddresses( &a->addr, vb );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
comparePeerAtoms( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const struct peer_atom * b = vb;
|
|
|
|
return comparePeerAtomToAddress( va, &b->addr );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
static int
|
|
|
|
torrentCompare( const void * va, const void * vb )
|
|
|
|
{
|
|
|
|
const Torrent * a = va;
|
|
|
|
const Torrent * b = vb;
|
|
|
|
return memcmp( a->hash, b->hash, SHA_DIGEST_LENGTH );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
torrentCompareToHash( const void * va, const void * vb )
|
|
|
|
{
|
2007-10-03 16:42:43 +00:00
|
|
|
const Torrent * a = va;
|
|
|
|
const uint8_t * b_hash = vb;
|
2007-09-20 16:32:01 +00:00
|
|
|
return memcmp( a->hash, b_hash, SHA_DIGEST_LENGTH );
|
|
|
|
}
|
|
|
|
|
|
|
|
static Torrent*
|
|
|
|
getExistingTorrent( tr_peerMgr * manager, const uint8_t * hash )
|
|
|
|
{
|
|
|
|
return (Torrent*) tr_ptrArrayFindSorted( manager->torrents,
|
|
|
|
hash,
|
|
|
|
torrentCompareToHash );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
peerCompare( const void * va, const void * vb )
|
|
|
|
{
|
2007-10-03 16:42:43 +00:00
|
|
|
const tr_peer * a = va;
|
|
|
|
const tr_peer * b = vb;
|
2007-09-30 23:55:49 +00:00
|
|
|
return compareAddresses( &a->in_addr, &b->in_addr );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
peerCompareToAddr( const void * va, const void * vb )
|
|
|
|
{
|
2007-10-03 16:42:43 +00:00
|
|
|
const tr_peer * a = va;
|
2007-09-30 23:55:49 +00:00
|
|
|
return compareAddresses( &a->in_addr, vb );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static tr_peer*
|
|
|
|
getExistingPeer( Torrent * torrent, const struct in_addr * in_addr )
|
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
assert( torrentIsLocked( torrent ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( in_addr != NULL );
|
|
|
|
|
|
|
|
return (tr_peer*) tr_ptrArrayFindSorted( torrent->peers,
|
|
|
|
in_addr,
|
|
|
|
peerCompareToAddr );
|
|
|
|
}
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
static struct peer_atom*
|
|
|
|
getExistingAtom( const Torrent * t, const struct in_addr * addr )
|
|
|
|
{
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
return tr_ptrArrayFindSorted( t->pool, addr, comparePeerAtomToAddress );
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-10-02 16:12:44 +00:00
|
|
|
peerIsInUse( const Torrent * ct, const struct in_addr * addr )
|
2007-09-30 23:55:49 +00:00
|
|
|
{
|
2007-10-02 16:12:44 +00:00
|
|
|
Torrent * t = (Torrent*) ct;
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
assert( torrentIsLocked ( t ) );
|
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
return getExistingPeer( t, addr )
|
|
|
|
|| getExistingHandshake( t->outgoingHandshakes, addr )
|
|
|
|
|| getExistingHandshake( t->manager->incomingHandshakes, addr );
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_peer*
|
|
|
|
peerConstructor( const struct in_addr * in_addr )
|
|
|
|
{
|
|
|
|
tr_peer * p;
|
|
|
|
p = tr_new0( tr_peer, 1 );
|
2007-11-18 01:00:49 +00:00
|
|
|
p->credit = SWIFT_INITIAL_CREDIT;
|
2007-10-06 18:20:52 +00:00
|
|
|
p->rcToClient = tr_rcInit( );
|
|
|
|
p->rcToPeer = tr_rcInit( );
|
2007-10-03 16:42:43 +00:00
|
|
|
memcpy( &p->in_addr, in_addr, sizeof(struct in_addr) );
|
|
|
|
return p;
|
2007-09-30 23:55:49 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_peer*
|
2007-09-30 23:55:49 +00:00
|
|
|
getPeer( Torrent * torrent, const struct in_addr * in_addr )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
tr_peer * peer;
|
|
|
|
|
|
|
|
assert( torrentIsLocked( torrent ) );
|
|
|
|
|
|
|
|
peer = getExistingPeer( torrent, in_addr );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
if( peer == NULL ) {
|
|
|
|
peer = peerConstructor( in_addr );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayInsertSorted( torrent->peers, peer, peerCompare );
|
|
|
|
}
|
|
|
|
|
|
|
|
return peer;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2007-10-04 20:49:37 +00:00
|
|
|
peerDestructor( tr_peer * peer )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
assert( peer != NULL );
|
2007-10-04 20:49:37 +00:00
|
|
|
assert( peer->msgs != NULL );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-04 20:49:37 +00:00
|
|
|
tr_peerMsgsUnsubscribe( peer->msgs, peer->msgsTag );
|
|
|
|
tr_peerMsgsFree( peer->msgs );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-04 20:49:37 +00:00
|
|
|
tr_peerIoFree( peer->io );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_bitfieldFree( peer->have );
|
|
|
|
tr_bitfieldFree( peer->blame );
|
2007-10-06 18:20:52 +00:00
|
|
|
tr_rcClose( peer->rcToClient );
|
|
|
|
tr_rcClose( peer->rcToPeer );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_free( peer->client );
|
|
|
|
tr_free( peer );
|
|
|
|
}
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
static void
|
|
|
|
removePeer( Torrent * t, tr_peer * peer )
|
|
|
|
{
|
|
|
|
tr_peer * removed;
|
|
|
|
struct peer_atom * atom;
|
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
|
|
|
atom = getExistingAtom( t, &peer->in_addr );
|
|
|
|
assert( atom != NULL );
|
|
|
|
atom->time = time( NULL );
|
|
|
|
|
|
|
|
removed = tr_ptrArrayRemoveSorted ( t->peers, peer, peerCompare );
|
|
|
|
assert( removed == peer );
|
2007-10-03 16:42:43 +00:00
|
|
|
peerDestructor( removed );
|
2007-09-30 23:55:49 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 16:50:51 +00:00
|
|
|
static void
|
|
|
|
removeAllPeers( Torrent * t )
|
|
|
|
{
|
|
|
|
while( !tr_ptrArrayEmpty( t->peers ) )
|
|
|
|
removePeer( t, tr_ptrArrayNth( t->peers, 0 ) );
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2007-10-02 16:12:44 +00:00
|
|
|
torrentDestructor( Torrent * t )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
uint8_t hash[SHA_DIGEST_LENGTH];
|
|
|
|
|
|
|
|
assert( t != NULL );
|
2007-10-02 16:12:44 +00:00
|
|
|
assert( !t->isRunning );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( t->peers != NULL );
|
2007-09-29 06:37:03 +00:00
|
|
|
assert( torrentIsLocked( t ) );
|
2007-10-02 16:12:44 +00:00
|
|
|
assert( tr_ptrArrayEmpty( t->outgoingHandshakes ) );
|
|
|
|
assert( tr_ptrArrayEmpty( t->peers ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
memcpy( hash, t->hash, SHA_DIGEST_LENGTH );
|
|
|
|
|
2007-09-22 00:53:11 +00:00
|
|
|
tr_timerFree( &t->reconnectTimer );
|
2007-09-22 00:22:10 +00:00
|
|
|
tr_timerFree( &t->rechokeTimer );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_timerFree( &t->refillTimer );
|
2007-11-18 01:00:49 +00:00
|
|
|
tr_timerFree( &t->swiftTimer );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_bitfieldFree( t->requested );
|
2007-09-30 23:55:49 +00:00
|
|
|
tr_ptrArrayFree( t->pool, (PtrArrayForeachFunc)tr_free );
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_ptrArrayFree( t->outgoingHandshakes, NULL );
|
2007-10-04 20:31:19 +00:00
|
|
|
tr_ptrArrayFree( t->peers, NULL );
|
2007-10-02 16:12:44 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_free( t );
|
|
|
|
}
|
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
static Torrent*
|
|
|
|
torrentConstructor( tr_peerMgr * manager, tr_torrent * tor )
|
|
|
|
{
|
|
|
|
Torrent * t;
|
|
|
|
|
|
|
|
t = tr_new0( Torrent, 1 );
|
|
|
|
t->manager = manager;
|
|
|
|
t->tor = tor;
|
|
|
|
t->pool = tr_ptrArrayNew( );
|
|
|
|
t->peers = tr_ptrArrayNew( );
|
|
|
|
t->outgoingHandshakes = tr_ptrArrayNew( );
|
|
|
|
t->requested = tr_bitfieldNew( tor->blockCount );
|
|
|
|
memcpy( t->hash, tor->info.hash, SHA_DIGEST_LENGTH );
|
|
|
|
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-09-28 16:40:21 +00:00
|
|
|
struct tr_bitfield *
|
|
|
|
tr_peerMgrGenerateAllowedSet( const uint32_t setCount,
|
|
|
|
const uint32_t pieceCount,
|
|
|
|
const uint8_t infohash[20],
|
|
|
|
const struct in_addr * ip )
|
|
|
|
{
|
|
|
|
/* This has been checked against the spec example implementation. Feeding it with :
|
|
|
|
setCount = 9, pieceCount = 1313, infohash = Oxaa,0xaa,...0xaa, ip = 80.4.4.200
|
|
|
|
generate :
|
|
|
|
1059, 431, 808, 1217, 287, 376, 1188, 353, 508
|
|
|
|
but since we're storing in a bitfield, it won't be in this order... */
|
|
|
|
/* TODO : We should translate link-local IPv4 adresses to external IP,
|
|
|
|
* so that being on same local network gives us the same allowed pieces */
|
|
|
|
|
2007-10-27 21:29:41 +00:00
|
|
|
uint8_t *seed;
|
|
|
|
char buf[4];
|
|
|
|
uint32_t allowedPieceCount = 0;
|
|
|
|
tr_bitfield_t * ret;
|
|
|
|
|
2007-11-10 22:29:55 +00:00
|
|
|
#if 0
|
2007-09-28 16:40:21 +00:00
|
|
|
printf( "%d piece allowed fast set for torrent with %d pieces and hex infohash\n", setCount, pieceCount );
|
|
|
|
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",
|
|
|
|
infohash[0], infohash[1], infohash[2], infohash[3], infohash[4], infohash[5], infohash[6], infohash[7], infohash[8], infohash[9],
|
|
|
|
infohash[10], infohash[11], infohash[12], infohash[13], infohash[14], infohash[15], infohash[16], infohash[7], infohash[18], infohash[19],
|
|
|
|
inet_ntoa( *ip ) );
|
2007-11-10 22:29:55 +00:00
|
|
|
#endif
|
2007-09-28 16:40:21 +00:00
|
|
|
|
2007-10-27 21:29:41 +00:00
|
|
|
seed = malloc(4 + SHA_DIGEST_LENGTH);
|
2007-09-28 16:40:21 +00:00
|
|
|
|
|
|
|
ret = tr_bitfieldNew( pieceCount );
|
|
|
|
|
|
|
|
/* We need a seed based on most significant bytes of peer address
|
|
|
|
concatenated with torrent's infohash */
|
|
|
|
*(uint32_t*)buf = ntohl( htonl(ip->s_addr) & 0xffffff00 );
|
|
|
|
|
|
|
|
memcpy( seed, &buf, 4 );
|
|
|
|
memcpy( seed + 4, infohash, SHA_DIGEST_LENGTH );
|
|
|
|
|
|
|
|
tr_sha1( seed, seed, 4 + SHA_DIGEST_LENGTH, NULL );
|
|
|
|
|
|
|
|
while ( allowedPieceCount < setCount )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for ( i = 0 ; i < 5 && allowedPieceCount < setCount ; i++ )
|
|
|
|
{
|
|
|
|
/* We generate indices from 4-byte chunks of the seed */
|
|
|
|
uint32_t j = i * 4;
|
|
|
|
uint32_t y = ntohl( *(uint32_t*)(seed + j) );
|
|
|
|
uint32_t index = y % pieceCount;
|
|
|
|
|
|
|
|
if ( !tr_bitfieldHas( ret, index ) )
|
|
|
|
{
|
|
|
|
tr_bitfieldAdd( ret, index );
|
|
|
|
allowedPieceCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* We randomize the seed, in case we need to iterate more */
|
|
|
|
tr_sha1( seed, seed, SHA_DIGEST_LENGTH, NULL );
|
|
|
|
}
|
|
|
|
tr_free( seed );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerMgr*
|
|
|
|
tr_peerMgrNew( tr_handle * handle )
|
|
|
|
{
|
|
|
|
tr_peerMgr * m = tr_new0( tr_peerMgr, 1 );
|
|
|
|
m->handle = handle;
|
|
|
|
m->torrents = tr_ptrArrayNew( );
|
2007-10-02 16:12:44 +00:00
|
|
|
m->incomingHandshakes = tr_ptrArrayNew( );
|
2007-09-20 16:32:01 +00:00
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMgrFree( tr_peerMgr * manager )
|
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
managerLock( manager );
|
|
|
|
|
2007-10-02 14:35:02 +00:00
|
|
|
/* free the handshakes. Abort invokes handshakeDoneCB(), which removes
|
|
|
|
* the item from manager->handshakes, so this is a little roundabout... */
|
2007-10-02 16:12:44 +00:00
|
|
|
while( !tr_ptrArrayEmpty( manager->incomingHandshakes ) )
|
|
|
|
tr_handshakeAbort( tr_ptrArrayNth( manager->incomingHandshakes, 0 ) );
|
|
|
|
tr_ptrArrayFree( manager->incomingHandshakes, NULL );
|
2007-10-02 14:35:02 +00:00
|
|
|
|
2007-10-02 19:25:18 +00:00
|
|
|
/* free the torrents. */
|
|
|
|
tr_ptrArrayFree( manager->torrents, (PtrArrayForeachFunc)torrentDestructor );
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_free( manager );
|
|
|
|
}
|
|
|
|
|
2007-10-02 02:01:57 +00:00
|
|
|
static tr_peer**
|
|
|
|
getConnectedPeers( Torrent * t, int * setmeCount )
|
|
|
|
{
|
|
|
|
int i, peerCount, connectionCount;
|
|
|
|
tr_peer **peers;
|
|
|
|
tr_peer **ret;
|
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
|
|
|
peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
|
|
|
|
ret = tr_new( tr_peer*, peerCount );
|
|
|
|
|
|
|
|
for( i=connectionCount=0; i<peerCount; ++i )
|
|
|
|
if( peers[i]->msgs != NULL )
|
|
|
|
ret[connectionCount++] = peers[i];
|
|
|
|
|
|
|
|
*setmeCount = connectionCount;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/***
|
|
|
|
**** Refill
|
|
|
|
***/
|
|
|
|
|
|
|
|
struct tr_refill_piece
|
|
|
|
{
|
|
|
|
tr_priority_t priority;
|
2007-10-31 04:23:51 +00:00
|
|
|
int percentDone;
|
2007-10-02 14:35:02 +00:00
|
|
|
uint16_t random;
|
2007-09-20 16:32:01 +00:00
|
|
|
uint32_t piece;
|
|
|
|
uint32_t peerCount;
|
2007-09-28 16:40:21 +00:00
|
|
|
uint32_t fastAllowed;
|
2007-11-10 16:06:00 +00:00
|
|
|
uint32_t suggested;
|
2007-09-20 16:32:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static int
|
|
|
|
compareRefillPiece (const void * aIn, const void * bIn)
|
|
|
|
{
|
|
|
|
const struct tr_refill_piece * a = aIn;
|
|
|
|
const struct tr_refill_piece * b = bIn;
|
2007-10-10 16:39:12 +00:00
|
|
|
|
2007-12-25 05:42:33 +00:00
|
|
|
/* if one piece has a higher priority, it goes first */
|
2007-12-25 06:37:21 +00:00
|
|
|
if( a->priority != b->priority )
|
2007-12-25 05:42:33 +00:00
|
|
|
return a->priority > b->priority ? -1 : 1;
|
|
|
|
|
|
|
|
/* try to fill partial pieces */
|
|
|
|
if( a->percentDone != b->percentDone )
|
|
|
|
return a->percentDone > b->percentDone ? -1 : 1;
|
|
|
|
|
2007-10-10 16:39:12 +00:00
|
|
|
/* if one *might be* fastallowed to us, get it first...
|
2007-11-10 16:06:00 +00:00
|
|
|
* I'm putting it on top so we prioritize those pieces at
|
2007-10-10 16:39:12 +00:00
|
|
|
* startup, then we'll have them, and we'll be denied access
|
|
|
|
* to them */
|
|
|
|
if (a->fastAllowed != b->fastAllowed)
|
|
|
|
return a->fastAllowed < b->fastAllowed ? -1 : 1;
|
|
|
|
|
2007-11-10 16:06:00 +00:00
|
|
|
/* otherwise if one was suggested to us, get it */
|
|
|
|
if (a->suggested != b->suggested)
|
|
|
|
return a->suggested < b->suggested ? -1 : 1;
|
2007-10-10 16:39:12 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* otherwise if one has fewer peers, it goes first */
|
|
|
|
if (a->peerCount != b->peerCount)
|
|
|
|
return a->peerCount < b->peerCount ? -1 : 1;
|
|
|
|
|
2007-10-01 14:24:22 +00:00
|
|
|
/* otherwise go with our random seed */
|
2007-10-02 14:35:02 +00:00
|
|
|
return tr_compareUint16( a->random, b->random );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
isPieceInteresting( const tr_torrent * tor,
|
|
|
|
int piece )
|
|
|
|
{
|
|
|
|
if( tor->info.pieces[piece].dnd ) /* we don't want it */
|
|
|
|
return 0;
|
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
if( tr_cpPieceIsComplete( tor->completion, piece ) ) /* we have it */
|
2007-09-20 16:32:01 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t*
|
|
|
|
getPreferredPieces( Torrent * t,
|
|
|
|
uint32_t * pieceCount )
|
|
|
|
{
|
|
|
|
const tr_torrent * tor = t->tor;
|
|
|
|
const tr_info * inf = &tor->info;
|
|
|
|
int i;
|
|
|
|
uint32_t poolSize = 0;
|
|
|
|
uint32_t * pool = tr_new( uint32_t, inf->pieceCount );
|
|
|
|
int peerCount;
|
2007-09-29 06:37:03 +00:00
|
|
|
tr_peer** peers;
|
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
2007-10-02 02:01:57 +00:00
|
|
|
peers = getConnectedPeers( t, &peerCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
for( i=0; i<inf->pieceCount; ++i )
|
|
|
|
if( isPieceInteresting( tor, i ) )
|
|
|
|
pool[poolSize++] = i;
|
|
|
|
|
|
|
|
/* sort the pool from most interesting to least... */
|
|
|
|
if( poolSize > 1 )
|
|
|
|
{
|
|
|
|
uint32_t j;
|
|
|
|
struct tr_refill_piece * p = tr_new( struct tr_refill_piece, poolSize );
|
|
|
|
|
|
|
|
for( j=0; j<poolSize; ++j )
|
|
|
|
{
|
|
|
|
int k;
|
|
|
|
const int piece = pool[j];
|
|
|
|
struct tr_refill_piece * setme = p + j;
|
|
|
|
|
|
|
|
setme->piece = piece;
|
|
|
|
setme->priority = inf->pieces[piece].priority;
|
|
|
|
setme->peerCount = 0;
|
2007-09-29 14:19:23 +00:00
|
|
|
setme->fastAllowed = 0;
|
2007-10-02 14:35:02 +00:00
|
|
|
setme->random = tr_rand( UINT16_MAX );
|
2007-10-31 04:23:51 +00:00
|
|
|
setme->percentDone = (int)( 100.0 * tr_cpPercentBlocksInPiece( tor->completion, piece ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
for( k=0; k<peerCount; ++k ) {
|
|
|
|
const tr_peer * peer = peers[k];
|
|
|
|
if( peer->peerIsInterested && !peer->clientIsChoked && tr_bitfieldHas( peer->have, piece ) )
|
|
|
|
++setme->peerCount;
|
2007-10-10 16:39:12 +00:00
|
|
|
/* The fast peer extension doesn't force a peer to actually HAVE a fast-allowed piece,
|
|
|
|
but we're guaranteed to get the same pieces from different peers,
|
|
|
|
so we'll build a list and pray one actually have this one */
|
2007-11-10 16:06:00 +00:00
|
|
|
setme->fastAllowed = tr_peerMsgsIsPieceFastAllowed( peer->msgs, i );
|
|
|
|
/* Also, if someone SUGGESTed a piece to us, prioritize it over non-suggested others
|
|
|
|
*/
|
|
|
|
setme->suggested = tr_peerMsgsIsPieceSuggested( peer->msgs, i );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-31 04:23:51 +00:00
|
|
|
qsort( p, poolSize, sizeof(struct tr_refill_piece), compareRefillPiece );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
for( j=0; j<poolSize; ++j )
|
|
|
|
pool[j] = p[j].piece;
|
|
|
|
|
|
|
|
tr_free( p );
|
|
|
|
}
|
|
|
|
|
2007-10-02 02:01:57 +00:00
|
|
|
tr_free( peers );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
*pieceCount = poolSize;
|
|
|
|
return pool;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint64_t*
|
|
|
|
getPreferredBlocks( Torrent * t, uint64_t * setmeCount )
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t pieceCount;
|
|
|
|
uint32_t * pieces;
|
|
|
|
uint64_t *req, *unreq, *ret, *walk;
|
|
|
|
int reqCount, unreqCount;
|
|
|
|
const tr_torrent * tor = t->tor;
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
pieces = getPreferredPieces( t, &pieceCount );
|
|
|
|
|
|
|
|
req = tr_new( uint64_t, pieceCount * tor->blockCountInPiece );
|
|
|
|
reqCount = 0;
|
|
|
|
unreq = tr_new( uint64_t, pieceCount * tor->blockCountInPiece );
|
|
|
|
unreqCount = 0;
|
|
|
|
|
|
|
|
for( i=0; i<pieceCount; ++i ) {
|
|
|
|
const uint32_t index = pieces[i];
|
|
|
|
const int begin = tr_torPieceFirstBlock( tor, index );
|
|
|
|
const int end = begin + tr_torPieceCountBlocks( tor, (int)index );
|
|
|
|
int block;
|
|
|
|
for( block=begin; block<end; ++block )
|
|
|
|
if( tr_cpBlockIsComplete( tor->completion, block ) )
|
|
|
|
continue;
|
|
|
|
else if( tr_bitfieldHas( t->requested, block ) )
|
|
|
|
req[reqCount++] = block;
|
|
|
|
else
|
|
|
|
unreq[unreqCount++] = block;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = walk = tr_new( uint64_t, unreqCount + reqCount );
|
|
|
|
memcpy( walk, unreq, sizeof(uint64_t) * unreqCount );
|
|
|
|
walk += unreqCount;
|
|
|
|
memcpy( walk, req, sizeof(uint64_t) * reqCount );
|
|
|
|
walk += reqCount;
|
|
|
|
assert( ( walk - ret ) == ( unreqCount + reqCount ) );
|
|
|
|
*setmeCount = walk - ret;
|
|
|
|
|
|
|
|
tr_free( req );
|
|
|
|
tr_free( unreq );
|
|
|
|
tr_free( pieces );
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
refillPulse( void * vtorrent )
|
|
|
|
{
|
|
|
|
Torrent * t = vtorrent;
|
|
|
|
tr_torrent * tor = t->tor;
|
|
|
|
uint32_t i;
|
|
|
|
int peerCount;
|
|
|
|
tr_peer ** peers;
|
|
|
|
uint64_t blockCount;
|
|
|
|
uint64_t * blocks;
|
|
|
|
|
|
|
|
if( !t->isRunning )
|
|
|
|
return TRUE;
|
2007-10-19 23:23:21 +00:00
|
|
|
if( tr_torrentIsSeed( t->tor ) )
|
2007-09-20 16:32:01 +00:00
|
|
|
return TRUE;
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
torrentLock( t );
|
2007-10-06 18:20:52 +00:00
|
|
|
tordbg( t, "Refilling Request Buffers..." );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
blocks = getPreferredBlocks( t, &blockCount );
|
2007-10-02 02:01:57 +00:00
|
|
|
peers = getConnectedPeers( t, &peerCount );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
for( i=0; peerCount && i<blockCount; ++i )
|
|
|
|
{
|
2007-10-30 20:11:23 +00:00
|
|
|
const uint64_t block = blocks[i];
|
2007-09-20 16:32:01 +00:00
|
|
|
const uint32_t index = tr_torBlockPiece( tor, block );
|
|
|
|
const uint32_t begin = (block * tor->blockSize) - (index * tor->info.pieceSize);
|
2007-10-30 20:11:23 +00:00
|
|
|
const uint32_t length = tr_torBlockCountBytes( tor, (int)block );
|
2007-09-20 16:32:01 +00:00
|
|
|
int j;
|
2007-10-30 20:11:23 +00:00
|
|
|
assert( _tr_block( tor, index, begin ) == (int)block );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( begin < (uint32_t)tr_torPieceCountBytes( tor, (int)index ) );
|
|
|
|
assert( (begin + length) <= (uint32_t)tr_torPieceCountBytes( tor, (int)index ) );
|
|
|
|
|
|
|
|
/* find a peer who can ask for this block */
|
|
|
|
for( j=0; j<peerCount; )
|
|
|
|
{
|
|
|
|
const int val = tr_peerMsgsAddRequest( peers[j]->msgs, index, begin, length );
|
|
|
|
switch( val )
|
|
|
|
{
|
|
|
|
case TR_ADDREQ_FULL:
|
|
|
|
case TR_ADDREQ_CLIENT_CHOKED:
|
|
|
|
memmove( peers+j, peers+j+1, sizeof(tr_peer*)*(--peerCount-j) );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_ADDREQ_MISSING:
|
2007-10-05 00:16:47 +00:00
|
|
|
case TR_ADDREQ_DUPLICATE:
|
2007-09-20 16:32:01 +00:00
|
|
|
++j;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TR_ADDREQ_OK:
|
|
|
|
tr_bitfieldAdd( t->requested, block );
|
|
|
|
j = peerCount;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert( 0 && "unhandled value" );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* cleanup */
|
2007-10-02 02:01:57 +00:00
|
|
|
tr_free( peers );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_free( blocks );
|
|
|
|
|
|
|
|
t->refillTimer = NULL;
|
2007-09-29 06:37:03 +00:00
|
|
|
torrentUnlock( t );
|
2007-09-20 16:32:01 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
broadcastClientHave( Torrent * t, uint32_t index )
|
|
|
|
{
|
|
|
|
int i, size;
|
2007-09-29 06:37:03 +00:00
|
|
|
tr_peer ** peers;
|
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
2007-10-02 02:01:57 +00:00
|
|
|
peers = getConnectedPeers( t, &size );
|
2007-09-20 16:32:01 +00:00
|
|
|
for( i=0; i<size; ++i )
|
|
|
|
tr_peerMsgsHave( peers[i]->msgs, index );
|
2007-10-02 02:01:57 +00:00
|
|
|
tr_free( peers );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
broadcastGotBlock( Torrent * t, uint32_t index, uint32_t offset, uint32_t length )
|
|
|
|
{
|
|
|
|
int i, size;
|
2007-09-29 06:37:03 +00:00
|
|
|
tr_peer ** peers;
|
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
2007-10-02 02:01:57 +00:00
|
|
|
peers = getConnectedPeers( t, &size );
|
2007-09-20 16:32:01 +00:00
|
|
|
for( i=0; i<size; ++i )
|
|
|
|
tr_peerMsgsCancel( peers[i]->msgs, index, offset, length );
|
2007-10-02 02:01:57 +00:00
|
|
|
tr_free( peers );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-11-13 05:36:43 +00:00
|
|
|
static void
|
|
|
|
addStrike( Torrent * t, tr_peer * peer )
|
|
|
|
{
|
|
|
|
tordbg( t, "increasing peer %s strike count to %d", tr_peerIoAddrStr(&peer->in_addr,peer->port), peer->strikes+1 );
|
|
|
|
|
|
|
|
if( ++peer->strikes >= MAX_BAD_PIECES_PER_PEER )
|
|
|
|
{
|
|
|
|
struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
|
|
|
|
atom->myflags |= MYFLAG_BANNED;
|
|
|
|
peer->doPurge = 1;
|
|
|
|
tordbg( t, "banning peer %s", tr_peerIoAddrStr(&atom->addr,atom->port) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2007-09-20 23:07:36 +00:00
|
|
|
msgsCallbackFunc( void * vpeer, void * vevent, void * vt )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-09-20 23:07:36 +00:00
|
|
|
tr_peer * peer = vpeer;
|
2007-09-20 16:32:01 +00:00
|
|
|
Torrent * t = (Torrent *) vt;
|
|
|
|
const tr_peermsgs_event * e = (const tr_peermsgs_event *) vevent;
|
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
torrentLock( t );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
switch( e->eventType )
|
|
|
|
{
|
|
|
|
case TR_PEERMSG_NEED_REQ:
|
|
|
|
if( t->refillTimer == NULL )
|
|
|
|
t->refillTimer = tr_timerNew( t->manager->handle,
|
|
|
|
refillPulse, t,
|
|
|
|
REFILL_PERIOD_MSEC );
|
|
|
|
break;
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
case TR_PEERMSG_CANCEL:
|
|
|
|
tr_bitfieldRem( t->requested, _tr_block( t->tor, e->pieceIndex, e->offset ) );
|
|
|
|
break;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
case TR_PEERMSG_CLIENT_HAVE:
|
|
|
|
broadcastClientHave( t, e->pieceIndex );
|
2007-09-23 13:53:44 +00:00
|
|
|
tr_torrentRecheckCompleteness( t->tor );
|
2007-09-20 16:32:01 +00:00
|
|
|
break;
|
|
|
|
|
2007-10-01 16:31:17 +00:00
|
|
|
case TR_PEERMSG_PEER_PROGRESS: {
|
|
|
|
struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
|
2007-09-20 23:07:36 +00:00
|
|
|
const int peerIsSeed = e->progress >= 1.0;
|
2007-10-02 20:55:14 +00:00
|
|
|
if( peerIsSeed ) {
|
|
|
|
tordbg( t, "marking peer %s as a seed", tr_peerIoAddrStr(&atom->addr,atom->port) );
|
2007-10-01 16:31:17 +00:00
|
|
|
atom->flags |= ADDED_F_SEED_FLAG;
|
2007-10-02 20:55:14 +00:00
|
|
|
} else {
|
|
|
|
tordbg( t, "marking peer %s as a non-seed", tr_peerIoAddrStr(&atom->addr,atom->port) );
|
2007-10-01 16:31:17 +00:00
|
|
|
atom->flags &= ~ADDED_F_SEED_FLAG;
|
2007-10-02 20:55:14 +00:00
|
|
|
} break;
|
2007-09-20 23:07:36 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
case TR_PEERMSG_CLIENT_BLOCK:
|
|
|
|
broadcastGotBlock( t, e->pieceIndex, e->offset, e->length );
|
|
|
|
break;
|
|
|
|
|
2007-11-13 05:36:43 +00:00
|
|
|
case TR_PEERMSG_GOT_ASSERT_ERROR:
|
|
|
|
addStrike( t, peer );
|
|
|
|
peer->doPurge = 1;
|
|
|
|
break;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
case TR_PEERMSG_GOT_ERROR:
|
2007-09-26 04:44:54 +00:00
|
|
|
peer->doPurge = 1;
|
2007-09-20 16:32:01 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
torrentUnlock( t );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-10-01 05:32:34 +00:00
|
|
|
static void
|
|
|
|
ensureAtomExists( Torrent * t, const struct in_addr * addr, uint16_t port, uint8_t flags, uint8_t from )
|
|
|
|
{
|
2007-10-16 03:14:07 +00:00
|
|
|
if( getExistingAtom( t, addr ) == NULL )
|
2007-10-01 05:32:34 +00:00
|
|
|
{
|
2007-10-16 03:14:07 +00:00
|
|
|
struct peer_atom * a;
|
2007-10-16 02:16:57 +00:00
|
|
|
a = tr_new0( struct peer_atom, 1 );
|
2007-10-01 05:32:34 +00:00
|
|
|
a->addr = *addr;
|
|
|
|
a->port = port;
|
|
|
|
a->flags = flags;
|
2007-10-16 03:14:07 +00:00
|
|
|
a->from = from;
|
2007-10-02 14:35:02 +00:00
|
|
|
tordbg( t, "got a new atom: %s", tr_peerIoAddrStr(&a->addr,a->port) );
|
2007-10-01 05:32:34 +00:00
|
|
|
tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
/* FIXME: this is kind of a mess. */
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
|
|
|
myHandshakeDoneCB( tr_handshake * handshake,
|
|
|
|
tr_peerIo * io,
|
|
|
|
int isConnected,
|
|
|
|
const uint8_t * peer_id,
|
|
|
|
void * vmanager )
|
|
|
|
{
|
|
|
|
int ok = isConnected;
|
|
|
|
uint16_t port;
|
2007-10-16 03:14:07 +00:00
|
|
|
const struct in_addr * addr;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerMgr * manager = (tr_peerMgr*) vmanager;
|
|
|
|
Torrent * t;
|
|
|
|
tr_handshake * ours;
|
|
|
|
|
|
|
|
assert( io != NULL );
|
|
|
|
assert( isConnected==0 || isConnected==1 );
|
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
t = tr_peerIoHasTorrentHash( io )
|
|
|
|
? getExistingTorrent( manager, tr_peerIoGetTorrentHash( io ) )
|
|
|
|
: NULL;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
if( tr_peerIoIsIncoming ( io ) )
|
|
|
|
ours = tr_ptrArrayRemoveSorted( manager->incomingHandshakes,
|
|
|
|
handshake, handshakeCompare );
|
|
|
|
else if( t != NULL )
|
|
|
|
ours = tr_ptrArrayRemoveSorted( t->outgoingHandshakes,
|
|
|
|
handshake, handshakeCompare );
|
|
|
|
else
|
|
|
|
ours = handshake;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
assert( ours != NULL );
|
|
|
|
assert( ours == handshake );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
if( t != NULL )
|
|
|
|
torrentLock( t );
|
|
|
|
|
2007-10-16 03:14:07 +00:00
|
|
|
addr = tr_peerIoGetAddress( io, &port );
|
2007-10-02 16:12:44 +00:00
|
|
|
|
2007-10-10 15:59:59 +00:00
|
|
|
if( !ok || !t || !t->isRunning )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-11-08 04:11:09 +00:00
|
|
|
if( t ) {
|
|
|
|
struct peer_atom * atom = getExistingAtom( t, addr );
|
|
|
|
if( atom )
|
|
|
|
++atom->numFails;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peerIoFree( io );
|
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
else /* looking good */
|
|
|
|
{
|
2007-10-08 01:31:27 +00:00
|
|
|
struct peer_atom * atom;
|
2007-10-01 05:32:34 +00:00
|
|
|
ensureAtomExists( t, addr, port, 0, TR_PEER_FROM_INCOMING );
|
2007-10-08 01:31:27 +00:00
|
|
|
atom = getExistingAtom( t, addr );
|
2007-10-11 00:09:58 +00:00
|
|
|
|
2007-10-08 01:31:27 +00:00
|
|
|
if( atom->myflags & MYFLAG_BANNED )
|
|
|
|
{
|
|
|
|
tordbg( t, "banned peer %s tried to reconnect", tr_peerIoAddrStr(&atom->addr,atom->port) );
|
|
|
|
tr_peerIoFree( io );
|
|
|
|
}
|
2007-12-20 21:44:16 +00:00
|
|
|
else if( tr_ptrArraySize( t->peers ) >= t->tor->maxConnectedPeers )
|
2007-11-09 15:19:12 +00:00
|
|
|
{
|
|
|
|
tr_peerIoFree( io );
|
|
|
|
}
|
2007-10-08 01:31:27 +00:00
|
|
|
else
|
|
|
|
{
|
2007-10-11 00:09:58 +00:00
|
|
|
tr_peer * peer = getExistingPeer( t, addr );
|
|
|
|
|
|
|
|
if( peer != NULL ) /* we already have this peer */
|
|
|
|
{
|
|
|
|
tr_peerIoFree( io );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
peer = getPeer( t, addr );
|
|
|
|
tr_free( peer->client );
|
|
|
|
peer->client = peer_id ? tr_clientForId( peer_id ) : NULL;
|
|
|
|
peer->port = port;
|
|
|
|
peer->io = io;
|
|
|
|
peer->msgs = tr_peerMsgsNew( t->tor, peer, msgsCallbackFunc, t, &peer->msgsTag );
|
|
|
|
atom->time = time( NULL );
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
if( t != NULL )
|
|
|
|
torrentUnlock( t );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMgrAddIncoming( tr_peerMgr * manager,
|
|
|
|
struct in_addr * addr,
|
2007-09-25 23:10:34 +00:00
|
|
|
uint16_t port,
|
2007-09-20 16:32:01 +00:00
|
|
|
int socket )
|
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
managerLock( manager );
|
|
|
|
|
2007-10-25 13:38:34 +00:00
|
|
|
if( getExistingHandshake( manager->incomingHandshakes, addr ) )
|
|
|
|
{
|
|
|
|
tr_netClose( socket );
|
|
|
|
}
|
|
|
|
else /* we don't have a connetion to them yet... */
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-15 16:01:42 +00:00
|
|
|
tr_peerIo * io;
|
|
|
|
tr_handshake * handshake;
|
2007-10-02 16:12:44 +00:00
|
|
|
|
2007-10-15 16:01:42 +00:00
|
|
|
tordbg( NULL, "Got an INCOMING connection with %s", tr_peerIoAddrStr( addr, port ) );
|
|
|
|
|
|
|
|
io = tr_peerIoNewIncoming( manager->handle, addr, port, socket );
|
|
|
|
|
|
|
|
handshake = tr_handshakeNew( io,
|
|
|
|
manager->handle->encryptionMode,
|
|
|
|
myHandshakeDoneCB,
|
|
|
|
manager );
|
2007-10-02 16:12:44 +00:00
|
|
|
|
|
|
|
tr_ptrArrayInsertSorted( manager->incomingHandshakes, handshake, handshakeCompare );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMgrAddPex( tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash,
|
2007-10-16 00:55:17 +00:00
|
|
|
uint8_t from,
|
2007-09-20 16:32:01 +00:00
|
|
|
const tr_pex * pex,
|
|
|
|
int pexCount )
|
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
Torrent * t;
|
|
|
|
const tr_pex * end;
|
|
|
|
|
|
|
|
managerLock( manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( manager, torrentHash );
|
2007-09-30 23:55:49 +00:00
|
|
|
for( end=pex+pexCount; pex!=end; ++pex )
|
2007-10-01 05:32:34 +00:00
|
|
|
ensureAtomExists( t, &pex->in_addr, pex->port, pex->flags, from );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMgrAddPeers( tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash,
|
2007-10-16 00:55:17 +00:00
|
|
|
uint8_t from,
|
2007-09-20 16:32:01 +00:00
|
|
|
const uint8_t * peerCompact,
|
|
|
|
int peerCount )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
const uint8_t * walk = peerCompact;
|
2007-09-29 06:37:03 +00:00
|
|
|
Torrent * t;
|
|
|
|
|
|
|
|
managerLock( manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( manager, torrentHash );
|
2007-09-20 16:32:01 +00:00
|
|
|
for( i=0; t!=NULL && i<peerCount; ++i )
|
|
|
|
{
|
|
|
|
struct in_addr addr;
|
|
|
|
uint16_t port;
|
|
|
|
memcpy( &addr, walk, 4 ); walk += 4;
|
|
|
|
memcpy( &port, walk, 2 ); walk += 2;
|
2007-10-01 05:32:34 +00:00
|
|
|
ensureAtomExists( t, &addr, port, 0, from );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
|
|
|
void
|
2007-10-08 01:31:27 +00:00
|
|
|
tr_peerMgrSetBlame( tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash,
|
|
|
|
int pieceIndex,
|
|
|
|
int success )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-10-08 01:31:27 +00:00
|
|
|
if( !success )
|
|
|
|
{
|
|
|
|
int peerCount, i;
|
|
|
|
Torrent * t = getExistingTorrent( manager, torrentHash );
|
|
|
|
tr_peer ** peers;
|
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
|
|
|
peers = (tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
|
|
|
|
for( i=0; i<peerCount; ++i )
|
|
|
|
{
|
2007-11-13 05:36:43 +00:00
|
|
|
tr_peer * peer = peers[i];
|
|
|
|
if( tr_bitfieldHas( peer->blame, pieceIndex ) )
|
|
|
|
{
|
|
|
|
tordbg( t, "peer %s contributed to corrupt piece (%d); now has %d strikes",
|
|
|
|
tr_peerIoAddrStr(&peer->in_addr,peer->port),
|
|
|
|
pieceIndex, (int)peer->strikes+1 );
|
|
|
|
addStrike( t, peer );
|
|
|
|
}
|
2007-10-08 01:31:27 +00:00
|
|
|
}
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
tr_pexCompare( const void * va, const void * vb )
|
|
|
|
{
|
2007-10-02 02:01:57 +00:00
|
|
|
const tr_pex * a = (const tr_pex *) va;
|
|
|
|
const tr_pex * b = (const tr_pex *) vb;
|
|
|
|
int i = memcmp( &a->in_addr, &b->in_addr, sizeof(struct in_addr) );
|
|
|
|
if( i ) return i;
|
|
|
|
if( a->port < b->port ) return -1;
|
|
|
|
if( a->port > b->port ) return 1;
|
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-10-02 02:01:57 +00:00
|
|
|
int tr_pexCompare( const void * a, const void * b );
|
|
|
|
|
2007-09-29 00:46:41 +00:00
|
|
|
static int
|
|
|
|
peerPrefersCrypto( const tr_peer * peer )
|
|
|
|
{
|
|
|
|
if( peer->encryption_preference == ENCRYPTION_PREFERENCE_YES )
|
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
if( peer->encryption_preference == ENCRYPTION_PREFERENCE_NO )
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return tr_peerIoIsEncrypted( peer->io );
|
|
|
|
};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
tr_peerMgrGetPeers( tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash,
|
|
|
|
tr_pex ** setme_pex )
|
|
|
|
{
|
|
|
|
const Torrent * t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
|
2007-10-01 04:12:24 +00:00
|
|
|
int i, peerCount;
|
|
|
|
const tr_peer ** peers;
|
|
|
|
tr_pex * pex;
|
|
|
|
tr_pex * walk;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
torrentLock( (Torrent*)t );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
|
|
|
|
pex = walk = tr_new( tr_pex, peerCount );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
for( i=0; i<peerCount; ++i, ++walk )
|
|
|
|
{
|
|
|
|
const tr_peer * peer = peers[i];
|
|
|
|
|
|
|
|
walk->in_addr = peer->in_addr;
|
|
|
|
|
|
|
|
walk->port = peer->port;
|
|
|
|
|
|
|
|
walk->flags = 0;
|
2007-10-01 16:31:17 +00:00
|
|
|
if( peerPrefersCrypto(peer) ) walk->flags |= ADDED_F_ENCRYPTION_FLAG;
|
|
|
|
if( peer->progress >= 1.0 ) walk->flags |= ADDED_F_SEED_FLAG;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert( ( walk - pex ) == peerCount );
|
|
|
|
qsort( pex, peerCount, sizeof(tr_pex), tr_pexCompare );
|
|
|
|
*setme_pex = pex;
|
2007-09-29 06:37:03 +00:00
|
|
|
|
2007-10-01 15:17:15 +00:00
|
|
|
torrentUnlock( (Torrent*)t );
|
2007-10-01 00:08:12 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
return peerCount;
|
|
|
|
}
|
|
|
|
|
2007-10-02 19:54:14 +00:00
|
|
|
static int reconnectPulse( void * vtorrent );
|
|
|
|
static int rechokePulse( void * vtorrent );
|
2007-11-18 01:00:49 +00:00
|
|
|
static int swiftPulse( void * vtorrent );
|
2007-10-02 19:54:14 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_peerMgrStartTorrent( tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash )
|
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
Torrent * t;
|
|
|
|
|
|
|
|
managerLock( manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( manager, torrentHash );
|
2007-10-02 19:54:14 +00:00
|
|
|
|
|
|
|
assert( t != NULL );
|
2007-10-03 02:46:15 +00:00
|
|
|
assert( ( t->isRunning != 0 ) == ( t->reconnectTimer != NULL ) );
|
|
|
|
assert( ( t->isRunning != 0 ) == ( t->rechokeTimer != NULL ) );
|
2007-11-18 01:00:49 +00:00
|
|
|
assert( ( t->isRunning != 0 ) == ( t->swiftTimer != NULL ) );
|
2007-10-02 19:54:14 +00:00
|
|
|
|
2007-10-03 02:46:15 +00:00
|
|
|
if( !t->isRunning )
|
|
|
|
{
|
|
|
|
t->isRunning = 1;
|
2007-10-07 04:14:58 +00:00
|
|
|
|
|
|
|
t->reconnectTimer = tr_timerNew( t->manager->handle,
|
|
|
|
reconnectPulse, t,
|
|
|
|
RECONNECT_PERIOD_MSEC );
|
|
|
|
|
|
|
|
t->rechokeTimer = tr_timerNew( t->manager->handle,
|
|
|
|
rechokePulse, t,
|
|
|
|
RECHOKE_PERIOD_MSEC );
|
|
|
|
|
2007-11-18 01:00:49 +00:00
|
|
|
t->swiftTimer = tr_timerNew( t->manager->handle,
|
|
|
|
swiftPulse, t,
|
|
|
|
SWIFT_PERIOD_MSEC );
|
|
|
|
|
2007-10-07 04:14:58 +00:00
|
|
|
reconnectPulse( t );
|
|
|
|
|
|
|
|
rechokePulse( t );
|
2007-11-18 01:00:49 +00:00
|
|
|
|
|
|
|
swiftPulse( t );
|
2007-10-03 02:46:15 +00:00
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
static void
|
|
|
|
stopTorrent( Torrent * t )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
t->isRunning = 0;
|
2007-09-25 00:04:29 +00:00
|
|
|
tr_timerFree( &t->rechokeTimer );
|
|
|
|
tr_timerFree( &t->reconnectTimer );
|
2007-11-18 01:00:49 +00:00
|
|
|
tr_timerFree( &t->swiftTimer );
|
2007-10-02 02:01:57 +00:00
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
/* disconnect the peers. */
|
2007-10-03 16:42:43 +00:00
|
|
|
tr_ptrArrayForeach( t->peers, (PtrArrayForeachFunc)peerDestructor );
|
2007-10-02 16:12:44 +00:00
|
|
|
tr_ptrArrayClear( t->peers );
|
2007-10-02 02:01:57 +00:00
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
/* disconnect the handshakes. handshakeAbort calls handshakeDoneCB(),
|
|
|
|
* which removes the handshake from t->outgoingHandshakes... */
|
|
|
|
while( !tr_ptrArrayEmpty( t->outgoingHandshakes ) )
|
|
|
|
tr_handshakeAbort( tr_ptrArrayNth( t->outgoingHandshakes, 0 ) );
|
2007-09-29 06:37:03 +00:00
|
|
|
}
|
|
|
|
void
|
|
|
|
tr_peerMgrStopTorrent( tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash)
|
|
|
|
{
|
|
|
|
managerLock( manager );
|
|
|
|
|
|
|
|
stopTorrent( getExistingTorrent( manager, torrentHash ) );
|
|
|
|
|
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMgrAddTorrent( tr_peerMgr * manager,
|
|
|
|
tr_torrent * tor )
|
|
|
|
{
|
|
|
|
Torrent * t;
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
managerLock( manager );
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( tor != NULL );
|
|
|
|
assert( getExistingTorrent( manager, tor->info.hash ) == NULL );
|
|
|
|
|
2007-10-02 16:12:44 +00:00
|
|
|
t = torrentConstructor( manager, tor );
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_ptrArrayInsertSorted( manager->torrents, t, torrentCompare );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMgrRemoveTorrent( tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash )
|
|
|
|
{
|
2007-09-29 06:37:03 +00:00
|
|
|
Torrent * t;
|
|
|
|
|
|
|
|
managerLock( manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( manager, torrentHash );
|
2007-09-20 16:32:01 +00:00
|
|
|
assert( t != NULL );
|
2007-09-29 06:37:03 +00:00
|
|
|
stopTorrent( t );
|
2007-10-02 14:35:02 +00:00
|
|
|
tr_ptrArrayRemoveSorted( manager->torrents, t, torrentCompare );
|
2007-10-02 16:12:44 +00:00
|
|
|
torrentDestructor( t );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
tr_peerMgrTorrentAvailability( const tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash,
|
|
|
|
int8_t * tab,
|
|
|
|
int tabCount )
|
|
|
|
{
|
|
|
|
int i;
|
2007-09-29 06:37:03 +00:00
|
|
|
const Torrent * t;
|
|
|
|
const tr_torrent * tor;
|
|
|
|
float interval;
|
|
|
|
|
|
|
|
managerLock( (tr_peerMgr*)manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
|
|
|
|
tor = t->tor;
|
|
|
|
interval = tor->info.pieceCount / (float)tabCount;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
memset( tab, 0, tabCount );
|
|
|
|
|
|
|
|
for( i=0; i<tabCount; ++i )
|
|
|
|
{
|
|
|
|
const int piece = i * interval;
|
|
|
|
|
|
|
|
if( tor == NULL )
|
|
|
|
tab[i] = 0;
|
|
|
|
else if( tr_cpPieceIsComplete( tor->completion, piece ) )
|
|
|
|
tab[i] = -1;
|
|
|
|
else {
|
|
|
|
int j, peerCount;
|
|
|
|
const tr_peer ** peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &peerCount );
|
|
|
|
for( j=0; j<peerCount; ++j )
|
|
|
|
if( tr_bitfieldHas( peers[j]->have, i ) )
|
|
|
|
++tab[i];
|
|
|
|
}
|
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( (tr_peerMgr*)manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-27 03:03:38 +00:00
|
|
|
/* Returns the pieces that we and/or a connected peer has */
|
|
|
|
tr_bitfield*
|
|
|
|
tr_peerMgrGetAvailable( const tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash )
|
|
|
|
{
|
|
|
|
int i, size;
|
|
|
|
const Torrent * t;
|
|
|
|
const tr_peer ** peers;
|
|
|
|
tr_bitfield * pieces;
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
managerLock( (tr_peerMgr*)manager );
|
|
|
|
|
2007-09-27 03:03:38 +00:00
|
|
|
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
|
|
|
|
peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size );
|
|
|
|
pieces = tr_bitfieldDup( tr_cpPieceBitfield( t->tor->completion ) );
|
|
|
|
for( i=0; i<size; ++i )
|
|
|
|
if( peers[i]->io != NULL )
|
2007-11-11 16:33:04 +00:00
|
|
|
tr_bitfieldOr( pieces, peers[i]->have );
|
2007-09-27 03:03:38 +00:00
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
managerUnlock( (tr_peerMgr*)manager );
|
2007-09-27 03:03:38 +00:00
|
|
|
return pieces;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-21 15:47:26 +00:00
|
|
|
int
|
|
|
|
tr_peerMgrHasConnections( const tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash )
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
const Torrent * t;
|
|
|
|
managerLock( (tr_peerMgr*)manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
|
|
|
|
ret = t && tr_ptrArraySize( t->peers );
|
|
|
|
|
|
|
|
managerUnlock( (tr_peerMgr*)manager );
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void
|
|
|
|
tr_peerMgrTorrentStats( const tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash,
|
2007-10-01 15:51:54 +00:00
|
|
|
int * setmePeersKnown,
|
2007-09-20 16:32:01 +00:00
|
|
|
int * setmePeersConnected,
|
|
|
|
int * setmePeersSendingToUs,
|
|
|
|
int * setmePeersGettingFromUs,
|
|
|
|
int * setmePeersFrom )
|
|
|
|
{
|
|
|
|
int i, size;
|
2007-09-29 06:37:03 +00:00
|
|
|
const Torrent * t;
|
|
|
|
const tr_peer ** peers;
|
|
|
|
|
|
|
|
managerLock( (tr_peerMgr*)manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
|
|
|
|
peers = (const tr_peer **) tr_ptrArrayPeek( t->peers, &size );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-01 15:51:54 +00:00
|
|
|
*setmePeersKnown = tr_ptrArraySize( t->pool );
|
2007-09-20 16:32:01 +00:00
|
|
|
*setmePeersConnected = 0;
|
|
|
|
*setmePeersSendingToUs = 0;
|
|
|
|
*setmePeersGettingFromUs = 0;
|
|
|
|
|
2007-09-21 15:13:23 +00:00
|
|
|
for( i=0; i<TR_PEER_FROM__MAX; ++i )
|
|
|
|
setmePeersFrom[i] = 0;
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
for( i=0; i<size; ++i )
|
|
|
|
{
|
|
|
|
const tr_peer * peer = peers[i];
|
2007-10-01 15:36:31 +00:00
|
|
|
const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
if( peer->io == NULL ) /* not connected */
|
|
|
|
continue;
|
|
|
|
|
|
|
|
++*setmePeersConnected;
|
|
|
|
|
2007-10-01 15:36:31 +00:00
|
|
|
++setmePeersFrom[atom->from];
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
if( peer->rateToPeer > 0.01 )
|
2007-09-20 16:32:01 +00:00
|
|
|
++*setmePeersGettingFromUs;
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
if( peer->rateToClient > 0.01 )
|
2007-09-20 16:32:01 +00:00
|
|
|
++*setmePeersSendingToUs;
|
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( (tr_peerMgr*)manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct tr_peer_stat *
|
|
|
|
tr_peerMgrPeerStats( const tr_peerMgr * manager,
|
|
|
|
const uint8_t * torrentHash,
|
|
|
|
int * setmeCount UNUSED )
|
|
|
|
{
|
|
|
|
int i, size;
|
2007-09-29 06:37:03 +00:00
|
|
|
const Torrent * t;
|
2007-10-28 19:42:47 +00:00
|
|
|
tr_peer ** peers;
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peer_stat * ret;
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
assert( manager != NULL );
|
|
|
|
managerLock( (tr_peerMgr*)manager );
|
|
|
|
|
|
|
|
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
|
2007-10-28 19:42:47 +00:00
|
|
|
peers = getConnectedPeers( (Torrent*)t, &size );
|
2007-09-20 16:32:01 +00:00
|
|
|
ret = tr_new0( tr_peer_stat, size );
|
|
|
|
|
|
|
|
for( i=0; i<size; ++i )
|
|
|
|
{
|
|
|
|
const tr_peer * peer = peers[i];
|
2007-10-01 15:36:31 +00:00
|
|
|
const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
|
2007-10-01 15:41:42 +00:00
|
|
|
tr_peer_stat * stat = ret + i;
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
tr_netNtop( &peer->in_addr, stat->addr, sizeof(stat->addr) );
|
2007-11-17 23:43:33 +00:00
|
|
|
strlcpy( stat->client, (peer->client ? peer->client : ""), sizeof(stat->client) );
|
2007-09-20 16:32:01 +00:00
|
|
|
stat->port = peer->port;
|
2007-10-01 15:36:31 +00:00
|
|
|
stat->from = atom->from;
|
2007-09-20 16:32:01 +00:00
|
|
|
stat->progress = peer->progress;
|
|
|
|
stat->isEncrypted = tr_peerIoIsEncrypted( peer->io ) ? 1 : 0;
|
2007-10-06 18:20:52 +00:00
|
|
|
stat->uploadToRate = peer->rateToPeer;
|
|
|
|
stat->downloadFromRate = peer->rateToClient;
|
2007-09-20 16:32:01 +00:00
|
|
|
stat->isDownloading = stat->uploadToRate > 0.01;
|
|
|
|
stat->isUploading = stat->downloadFromRate > 0.01;
|
2007-11-17 23:43:33 +00:00
|
|
|
stat->status = peer->status;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*setmeCount = size;
|
2007-10-28 19:42:47 +00:00
|
|
|
tr_free( peers );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
managerUnlock( (tr_peerMgr*)manager );
|
2007-09-20 16:32:01 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
struct ChokeData
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
tr_peer * peer;
|
2007-11-18 06:15:13 +00:00
|
|
|
int rate;
|
2007-11-25 16:57:08 +00:00
|
|
|
uint8_t preferred;
|
|
|
|
uint8_t preferred_t;
|
|
|
|
uint8_t doUnchoke;
|
2007-10-03 16:42:43 +00:00
|
|
|
};
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
compareChoke( const void * va, const void * vb )
|
|
|
|
{
|
2007-10-03 16:42:43 +00:00
|
|
|
const struct ChokeData * a = va;
|
|
|
|
const struct ChokeData * b = vb;
|
2007-11-25 16:57:08 +00:00
|
|
|
if( a->rate > b->rate ) return -1;
|
|
|
|
if( a->rate < b->rate ) return 1;
|
|
|
|
if( a->preferred_t != b->preferred_t ) return a->preferred_t ? -1 : 1;
|
|
|
|
if( a->preferred != b->preferred ) return a->preferred ? -1 : 1;
|
2007-11-15 05:47:23 +00:00
|
|
|
return 0;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-11-25 16:57:08 +00:00
|
|
|
clientIsSnubbedBy( const tr_peer * peer, int clientIsSeed )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
assert( peer != NULL );
|
|
|
|
|
2007-11-25 16:57:08 +00:00
|
|
|
return !clientIsSeed
|
|
|
|
&& ( peer->peerSentPieceDataAt < (time(NULL) - SNUBBED_SEC ) );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2007-09-22 00:22:10 +00:00
|
|
|
/**
|
|
|
|
***
|
|
|
|
**/
|
|
|
|
|
2007-10-06 18:20:52 +00:00
|
|
|
static double
|
2007-11-18 06:15:13 +00:00
|
|
|
getWeightedThroughput( const tr_peer * peer, int clientIsSeed )
|
2007-10-06 18:20:52 +00:00
|
|
|
{
|
2007-11-18 06:15:13 +00:00
|
|
|
return (int)( 10.0 * ( clientIsSeed ? peer->rateToPeer
|
|
|
|
: peer->rateToClient ) );
|
2007-10-06 18:20:52 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void
|
2007-10-03 16:42:43 +00:00
|
|
|
rechoke( Torrent * t )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2007-11-18 06:15:13 +00:00
|
|
|
int i, peerCount, size=0, unchoked=0;
|
2007-10-15 16:01:42 +00:00
|
|
|
const time_t fibrillationTime = time(NULL) - MIN_CHOKE_PERIOD_SEC;
|
2007-10-02 02:01:57 +00:00
|
|
|
tr_peer ** peers = getConnectedPeers( t, &peerCount );
|
2007-10-03 16:42:43 +00:00
|
|
|
struct ChokeData * choke = tr_new0( struct ChokeData, peerCount );
|
2007-11-18 06:15:13 +00:00
|
|
|
const int clientIsSeed = tr_torrentIsSeed( t->tor );
|
2007-09-29 06:37:03 +00:00
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
2007-09-28 16:40:21 +00:00
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
/* sort the peers by preference and rate */
|
2007-09-22 00:22:10 +00:00
|
|
|
for( i=0; i<peerCount; ++i )
|
|
|
|
{
|
2007-09-20 16:32:01 +00:00
|
|
|
tr_peer * peer = peers[i];
|
2007-10-03 16:42:43 +00:00
|
|
|
struct ChokeData * node;
|
2007-11-18 06:15:13 +00:00
|
|
|
if( peer->chokeChangedAt > fibrillationTime ) {
|
|
|
|
if( !peer->peerIsChoked )
|
|
|
|
++unchoked;
|
2007-09-22 00:22:10 +00:00
|
|
|
continue;
|
2007-11-18 06:15:13 +00:00
|
|
|
}
|
2007-09-22 00:22:10 +00:00
|
|
|
|
|
|
|
node = &choke[size++];
|
2007-09-20 16:32:01 +00:00
|
|
|
node->peer = peer;
|
2007-11-25 16:57:08 +00:00
|
|
|
node->preferred = peer->peerIsInterested && !clientIsSnubbedBy( peer, clientIsSeed );
|
|
|
|
node->preferred_t = node->preferred && peer->client && strstr( peer->client, "Transmission" );
|
2007-11-18 06:15:13 +00:00
|
|
|
node->rate = getWeightedThroughput( peer, clientIsSeed );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2007-09-22 00:22:10 +00:00
|
|
|
|
2007-10-03 16:42:43 +00:00
|
|
|
qsort( choke, size, sizeof(struct ChokeData), compareChoke );
|
2007-09-20 16:32:01 +00:00
|
|
|
|
2007-12-20 21:44:16 +00:00
|
|
|
for( i=0; i<size && unchoked<t->tor->maxUnchokedPeers; ++i ) {
|
2007-09-20 16:32:01 +00:00
|
|
|
choke[i].doUnchoke = 1;
|
2007-11-18 06:15:13 +00:00
|
|
|
++unchoked;
|
|
|
|
}
|
2007-09-20 16:32:01 +00:00
|
|
|
|
|
|
|
for( ; i<size; ++i ) {
|
2007-11-18 06:15:13 +00:00
|
|
|
++unchoked;
|
2007-09-20 16:32:01 +00:00
|
|
|
choke[i].doUnchoke = 1;
|
|
|
|
if( choke[i].peer->peerIsInterested )
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i=0; i<size; ++i )
|
|
|
|
tr_peerMsgsSetChoke( choke[i].peer->msgs, !choke[i].doUnchoke );
|
|
|
|
|
|
|
|
/* cleanup */
|
|
|
|
tr_free( choke );
|
2007-10-02 02:01:57 +00:00
|
|
|
tr_free( peers );
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-09-22 00:22:10 +00:00
|
|
|
rechokePulse( void * vtorrent )
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
|
|
|
Torrent * t = vtorrent;
|
2007-09-29 06:37:03 +00:00
|
|
|
torrentLock( t );
|
2007-10-03 16:42:43 +00:00
|
|
|
rechoke( t );
|
2007-09-29 06:37:03 +00:00
|
|
|
torrentUnlock( t );
|
2007-09-20 16:32:01 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2007-09-22 00:22:10 +00:00
|
|
|
|
2007-11-18 01:00:49 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
static int
|
|
|
|
swiftPulse( void * vtorrent )
|
|
|
|
{
|
|
|
|
Torrent * t = vtorrent;
|
|
|
|
torrentLock( t );
|
|
|
|
|
|
|
|
if( !tr_torrentIsSeed( t->tor ) )
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int peerCount = 0;
|
|
|
|
int deadbeatCount = 0;
|
|
|
|
tr_peer ** peers = getConnectedPeers( t, &peerCount );
|
|
|
|
tr_peer ** deadbeats = tr_new( tr_peer*, peerCount );
|
|
|
|
|
2007-11-18 03:18:26 +00:00
|
|
|
const double ul_KiBsec = tr_rcRate( t->tor->upload );
|
|
|
|
const double ul_KiB = ul_KiBsec * (SWIFT_PERIOD_MSEC/1000.0);
|
|
|
|
const double ul_bytes = ul_KiB * 1024;
|
|
|
|
const double freeCreditTotal = ul_bytes * SWIFT_LARGESSE;
|
|
|
|
int freeCreditPerPeer;
|
|
|
|
|
2007-11-18 01:00:49 +00:00
|
|
|
for( i=0; i<peerCount; ++i ) {
|
|
|
|
tr_peer * peer = peers[i];
|
2007-11-18 03:18:26 +00:00
|
|
|
if( peer->credit <= 0 )
|
2007-11-18 01:00:49 +00:00
|
|
|
deadbeats[deadbeatCount++] = peer;
|
|
|
|
}
|
|
|
|
|
2007-11-18 03:18:26 +00:00
|
|
|
freeCreditPerPeer = (int)( freeCreditTotal / deadbeatCount );
|
|
|
|
for( i=0; i<deadbeatCount; ++i )
|
|
|
|
deadbeats[i]->credit = freeCreditPerPeer;
|
|
|
|
|
|
|
|
tordbg( t, "%d deadbeats, "
|
|
|
|
"who are each being granted %d bytes' credit "
|
|
|
|
"for a total of %.1f KiB, "
|
|
|
|
"%d%% of the torrent's ul speed %.1f\n",
|
|
|
|
deadbeatCount, freeCreditPerPeer,
|
|
|
|
ul_KiBsec*SWIFT_LARGESSE, (int)(SWIFT_LARGESSE*100), ul_KiBsec );
|
2007-11-18 01:00:49 +00:00
|
|
|
|
|
|
|
tr_free( deadbeats );
|
|
|
|
tr_free( peers );
|
|
|
|
}
|
|
|
|
|
|
|
|
torrentUnlock( t );
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
/***
|
|
|
|
****
|
2007-10-13 13:54:05 +00:00
|
|
|
**** Life and Death
|
2007-09-30 23:55:49 +00:00
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-10-13 13:54:05 +00:00
|
|
|
static int
|
|
|
|
shouldPeerBeClosed( const Torrent * t, const tr_peer * peer, int peerCount )
|
2007-09-30 23:55:49 +00:00
|
|
|
{
|
2007-10-13 13:54:05 +00:00
|
|
|
const tr_torrent * tor = t->tor;
|
2007-09-22 14:18:52 +00:00
|
|
|
const time_t now = time( NULL );
|
2007-10-13 13:54:05 +00:00
|
|
|
const struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
|
2007-09-22 14:18:52 +00:00
|
|
|
|
2007-10-13 13:54:05 +00:00
|
|
|
/* if it's marked for purging, close it */
|
|
|
|
if( peer->doPurge ) {
|
2007-10-13 14:02:10 +00:00
|
|
|
tordbg( t, "purging peer %s because its doPurge flag is set", tr_peerIoAddrStr(&atom->addr,atom->port) );
|
2007-10-13 13:54:05 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
2007-09-29 06:37:03 +00:00
|
|
|
|
2007-10-13 13:54:05 +00:00
|
|
|
/* if we're both seeds and it's been long enough for a pex exchange, close it */
|
|
|
|
if( 1 ) {
|
2007-10-19 23:23:21 +00:00
|
|
|
const int clientIsSeed = tr_torrentIsSeed( tor );
|
2007-10-02 20:55:14 +00:00
|
|
|
const int peerIsSeed = atom->flags & ADDED_F_SEED_FLAG;
|
2007-12-24 05:03:40 +00:00
|
|
|
if( peerIsSeed && clientIsSeed && ( !tr_torrentAllowsPex(tor) || (now-atom->time>=30) ) ) {
|
2007-10-13 14:02:10 +00:00
|
|
|
tordbg( t, "purging peer %s because we're both seeds", tr_peerIoAddrStr(&atom->addr,atom->port) );
|
2007-10-13 13:54:05 +00:00
|
|
|
return TRUE;
|
2007-10-11 03:12:48 +00:00
|
|
|
}
|
2007-10-13 13:54:05 +00:00
|
|
|
}
|
2007-10-01 03:24:52 +00:00
|
|
|
|
2007-10-13 13:54:05 +00:00
|
|
|
/* disconnect if it's been too long since piece data has been transferred.
|
|
|
|
* this is on a sliding scale based on number of available peers... */
|
|
|
|
if( 1 ) {
|
2007-12-20 21:44:16 +00:00
|
|
|
const int relaxStrictnessIfFewerThanN = (int)((tor->maxConnectedPeers * 0.9) + 0.5);
|
2007-10-13 13:54:05 +00:00
|
|
|
/* if we have >= relaxIfFewerThan, strictness is 100%.
|
|
|
|
* if we have zero connections, strictness is 0% */
|
|
|
|
const double strictness = peerCount >= relaxStrictnessIfFewerThanN
|
|
|
|
? 1.0
|
|
|
|
: peerCount / (double)relaxStrictnessIfFewerThanN;
|
|
|
|
const int lo = MIN_UPLOAD_IDLE_SECS;
|
|
|
|
const int hi = MAX_UPLOAD_IDLE_SECS;
|
|
|
|
const int limit = lo + ((hi-lo) * strictness);
|
|
|
|
const time_t then = peer->pieceDataActivityDate;
|
|
|
|
const int idleTime = then ? (now-then) : 0;
|
|
|
|
if( idleTime > limit ) {
|
2007-10-13 14:02:10 +00:00
|
|
|
tordbg( t, "purging peer %s because it's been %d secs since we shared anything",
|
2007-10-13 13:54:05 +00:00
|
|
|
tr_peerIoAddrStr(&atom->addr,atom->port), idleTime );
|
|
|
|
return TRUE;
|
|
|
|
}
|
2007-09-22 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
2007-10-13 13:54:05 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static tr_peer **
|
|
|
|
getPeersToClose( Torrent * t, int * setmeSize )
|
|
|
|
{
|
|
|
|
int i, peerCount, outsize;
|
|
|
|
tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers, &peerCount );
|
|
|
|
struct tr_peer ** ret = tr_new( tr_peer*, peerCount );
|
|
|
|
|
|
|
|
assert( torrentIsLocked( t ) );
|
|
|
|
|
|
|
|
for( i=outsize=0; i<peerCount; ++i )
|
|
|
|
if( shouldPeerBeClosed( t, peers[i], peerCount ) )
|
|
|
|
ret[outsize++] = peers[i];
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
*setmeSize = outsize;
|
|
|
|
return ret;
|
2007-09-22 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2007-11-08 04:11:09 +00:00
|
|
|
compareCandidates( const void * va, const void * vb )
|
2007-09-22 14:18:52 +00:00
|
|
|
{
|
2007-09-30 23:55:49 +00:00
|
|
|
const struct peer_atom * a = * (const struct peer_atom**) va;
|
|
|
|
const struct peer_atom * b = * (const struct peer_atom**) vb;
|
2007-11-08 04:11:09 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if(( i = tr_compareUint16( a->numFails, b->numFails )))
|
|
|
|
return i;
|
|
|
|
|
|
|
|
if( a->time != b->time )
|
|
|
|
return a->time < b->time ? -1 : 1;
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
return 0;
|
2007-09-22 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
static struct peer_atom **
|
|
|
|
getPeerCandidates( Torrent * t, int * setmeSize )
|
2007-09-22 00:22:10 +00:00
|
|
|
{
|
2007-10-15 16:01:42 +00:00
|
|
|
int i, atomCount, retCount;
|
2007-09-30 23:55:49 +00:00
|
|
|
struct peer_atom ** atoms;
|
|
|
|
struct peer_atom ** ret;
|
|
|
|
const time_t now = time( NULL );
|
2007-10-19 23:23:21 +00:00
|
|
|
const int seed = tr_torrentIsSeed( t->tor );
|
2007-09-22 14:18:52 +00:00
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
assert( torrentIsLocked( t ) );
|
2007-09-22 14:18:52 +00:00
|
|
|
|
2007-10-15 16:01:42 +00:00
|
|
|
atoms = (struct peer_atom**) tr_ptrArrayPeek( t->pool, &atomCount );
|
|
|
|
ret = tr_new( struct peer_atom*, atomCount );
|
|
|
|
for( i=retCount=0; i<atomCount; ++i )
|
2007-09-26 04:44:54 +00:00
|
|
|
{
|
2007-10-16 00:55:17 +00:00
|
|
|
int wait, minWait, maxWait;
|
2007-09-30 23:55:49 +00:00
|
|
|
struct peer_atom * atom = atoms[i];
|
2007-09-26 04:44:54 +00:00
|
|
|
|
2007-10-08 01:31:27 +00:00
|
|
|
/* peer fed us too much bad data ... we only keep it around
|
|
|
|
* now to weed it out in case someone sends it to us via pex */
|
2007-12-15 04:26:31 +00:00
|
|
|
if( atom->myflags & MYFLAG_BANNED )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* peer was unconnectable before, so we're not going to keep trying.
|
|
|
|
* this is needs a separate flag from `banned', since if they try
|
|
|
|
* to connect to us later, we'll let them in */
|
|
|
|
if( atom->myflags & MYFLAG_UNREACHABLE )
|
2007-10-08 01:31:27 +00:00
|
|
|
continue;
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
/* we don't need two connections to the same peer... */
|
2007-12-15 04:26:31 +00:00
|
|
|
if( peerIsInUse( t, &atom->addr ) )
|
2007-09-26 04:44:54 +00:00
|
|
|
continue;
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
/* no need to connect if we're both seeds... */
|
2007-12-15 04:26:31 +00:00
|
|
|
if( seed && (atom->flags & ADDED_F_SEED_FLAG) )
|
2007-09-30 23:55:49 +00:00
|
|
|
continue;
|
2007-09-26 04:44:54 +00:00
|
|
|
|
2007-11-08 04:11:09 +00:00
|
|
|
/* we're wasting our time trying to connect to this bozo. */
|
2007-12-15 04:26:31 +00:00
|
|
|
if( atom->numFails > 10 )
|
2007-11-08 04:11:09 +00:00
|
|
|
continue;
|
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
/* if we used this peer recently, give someone else a turn */
|
2007-11-25 17:07:12 +00:00
|
|
|
minWait = 60;
|
2007-11-08 04:11:09 +00:00
|
|
|
maxWait = (60 * 20); /* twenty minutes */
|
2007-11-25 17:07:12 +00:00
|
|
|
wait = atom->numFails * 30; /* add 30 secs to the wait interval for each consecutive failure*/
|
2007-10-16 00:55:17 +00:00
|
|
|
if( wait < minWait ) wait = minWait;
|
|
|
|
if( wait > maxWait ) wait = maxWait;
|
|
|
|
if( ( now - atom->time ) < wait ) {
|
|
|
|
tordbg( t, "RECONNECT peer %d (%s) is in its grace period of %d seconds..",
|
|
|
|
i, tr_peerIoAddrStr(&atom->addr,atom->port), wait );
|
2007-09-30 23:55:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2007-10-15 16:01:42 +00:00
|
|
|
ret[retCount++] = atom;
|
2007-09-22 14:18:52 +00:00
|
|
|
}
|
2007-09-25 22:30:41 +00:00
|
|
|
|
2007-11-08 04:11:09 +00:00
|
|
|
qsort( ret, retCount, sizeof(struct peer_atom*), compareCandidates );
|
2007-10-15 16:01:42 +00:00
|
|
|
*setmeSize = retCount;
|
2007-09-30 23:55:49 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2007-09-22 14:18:52 +00:00
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
static int
|
|
|
|
reconnectPulse( void * vtorrent )
|
|
|
|
{
|
|
|
|
Torrent * t = vtorrent;
|
2007-09-22 14:18:52 +00:00
|
|
|
|
2007-09-30 23:55:49 +00:00
|
|
|
torrentLock( t );
|
2007-09-22 14:18:52 +00:00
|
|
|
|
2007-10-01 16:50:51 +00:00
|
|
|
if( !t->isRunning )
|
|
|
|
{
|
|
|
|
removeAllPeers( t );
|
2007-09-22 14:18:52 +00:00
|
|
|
}
|
2007-10-01 16:50:51 +00:00
|
|
|
else
|
|
|
|
{
|
2007-12-15 04:26:31 +00:00
|
|
|
int i, nCandidates, nBad, addMax;
|
2007-10-01 16:50:51 +00:00
|
|
|
struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates );
|
2007-10-13 13:54:05 +00:00
|
|
|
struct tr_peer ** connections = getPeersToClose( t, &nBad );
|
2007-10-01 16:50:51 +00:00
|
|
|
|
2007-10-13 13:54:05 +00:00
|
|
|
if( nBad || nCandidates )
|
|
|
|
tordbg( t, "reconnect pulse for [%s]: %d bad connections, "
|
2007-10-09 04:50:10 +00:00
|
|
|
"%d connection candidates, %d atoms, max per pulse is %d",
|
2007-10-13 13:54:05 +00:00
|
|
|
t->tor->info.name, nBad, nCandidates,
|
2007-10-09 04:50:10 +00:00
|
|
|
tr_ptrArraySize(t->pool),
|
|
|
|
(int)MAX_RECONNECTIONS_PER_PULSE );
|
2007-10-01 16:50:51 +00:00
|
|
|
|
2007-10-16 00:55:17 +00:00
|
|
|
/* disconnect some peers.
|
|
|
|
if we got transferred piece data, then they might be good peers,
|
|
|
|
so reset their `numFails' weight to zero. otherwise we connected
|
|
|
|
to them fruitlessly, so mark it as another fail */
|
|
|
|
for( i=0; i<nBad; ++i ) {
|
|
|
|
tr_peer * peer = connections[i];
|
|
|
|
struct peer_atom * atom = getExistingAtom( t, &peer->in_addr );
|
|
|
|
if( peer->pieceDataActivityDate )
|
|
|
|
atom->numFails = 0;
|
|
|
|
else
|
|
|
|
++atom->numFails;
|
|
|
|
removePeer( t, peer );
|
|
|
|
}
|
2007-10-01 16:50:51 +00:00
|
|
|
|
|
|
|
/* add some new ones */
|
2007-12-15 04:26:31 +00:00
|
|
|
addMax = tr_ptrArraySize(t->pool)
|
2007-12-20 21:44:16 +00:00
|
|
|
? MAX_RECONNECTIONS_PER_PULSE
|
|
|
|
: t->tor->maxConnectedPeers;
|
2007-12-15 04:26:31 +00:00
|
|
|
|
2007-11-18 17:35:28 +00:00
|
|
|
for( i=0; i<nCandidates && i<MAX_RECONNECTIONS_PER_PULSE; ++i )
|
2007-10-02 16:12:44 +00:00
|
|
|
{
|
|
|
|
tr_peerMgr * mgr = t->manager;
|
2007-10-01 16:50:51 +00:00
|
|
|
struct peer_atom * atom = candidates[i];
|
2007-10-15 16:01:42 +00:00
|
|
|
tr_peerIo * io;
|
|
|
|
|
|
|
|
tordbg( t, "Starting an OUTGOING connection with %s",
|
|
|
|
tr_peerIoAddrStr( &atom->addr, atom->port ) );
|
|
|
|
|
|
|
|
io = tr_peerIoNewOutgoing( mgr->handle, &atom->addr, atom->port, t->hash );
|
2007-12-15 04:26:31 +00:00
|
|
|
if( io == NULL )
|
|
|
|
{
|
|
|
|
atom->myflags |= MYFLAG_UNREACHABLE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_handshake * handshake = tr_handshakeNew( io,
|
|
|
|
mgr->handle->encryptionMode,
|
|
|
|
myHandshakeDoneCB,
|
|
|
|
mgr );
|
2007-10-02 16:12:44 +00:00
|
|
|
|
2007-12-15 04:26:31 +00:00
|
|
|
assert( tr_peerIoGetTorrentHash( io ) != NULL );
|
2007-10-02 16:12:44 +00:00
|
|
|
|
2007-12-15 04:26:31 +00:00
|
|
|
tr_ptrArrayInsertSorted( t->outgoingHandshakes, handshake, handshakeCompare );
|
|
|
|
}
|
2007-10-02 16:12:44 +00:00
|
|
|
|
2007-10-01 16:50:51 +00:00
|
|
|
atom->time = time( NULL );
|
|
|
|
}
|
2007-09-22 14:18:52 +00:00
|
|
|
|
2007-10-01 16:50:51 +00:00
|
|
|
/* cleanup */
|
|
|
|
tr_free( connections );
|
|
|
|
tr_free( candidates );
|
2007-09-30 23:55:49 +00:00
|
|
|
}
|
|
|
|
|
2007-09-29 06:37:03 +00:00
|
|
|
torrentUnlock( t );
|
2007-09-22 00:22:10 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|