(libT) minor cleanup: using tr_bool, fixing up bad code indentation, using %zu on size_t instead of casting to int...

This commit is contained in:
Charles Kerr 2008-12-05 22:56:19 +00:00
parent eb90a55608
commit 9e98dcd248
8 changed files with 263 additions and 457 deletions

View File

@ -246,8 +246,8 @@ parseHandshake( tr_handshake * handshake,
uint8_t reserved[HANDSHAKE_FLAGS_LEN];
uint8_t hash[SHA_DIGEST_LENGTH];
dbgmsg( handshake, "payload: need %d, got %d", (int)HANDSHAKE_SIZE,
(int)EVBUFFER_LENGTH( inbuf ) );
dbgmsg( handshake, "payload: need %d, got %zu",
(int)HANDSHAKE_SIZE, EVBUFFER_LENGTH( inbuf ) );
if( EVBUFFER_LENGTH( inbuf ) < HANDSHAKE_SIZE )
return READ_LATER;
@ -395,8 +395,7 @@ readYb( tr_handshake * handshake,
if( EVBUFFER_LENGTH( inbuf ) < needlen )
return READ_LATER;
isEncrypted = memcmp( EVBUFFER_DATA(
inbuf ), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
isEncrypted = memcmp( EVBUFFER_DATA( inbuf ), HANDSHAKE_NAME, HANDSHAKE_NAME_LEN );
if( isEncrypted )
{
needlen = KEY_LEN;
@ -407,9 +406,8 @@ readYb( tr_handshake * handshake,
dbgmsg( handshake, "got a %s handshake",
( isEncrypted ? "encrypted" : "plaintext" ) );
tr_peerIoSetEncryption(
handshake->io, isEncrypted ? PEER_ENCRYPTION_RC4
: PEER_ENCRYPTION_NONE );
tr_peerIoSetEncryption( handshake->io, isEncrypted ? PEER_ENCRYPTION_RC4
: PEER_ENCRYPTION_NONE );
if( !isEncrypted )
{
setState( handshake, AWAITING_HANDSHAKE );
@ -439,8 +437,8 @@ readYb( tr_handshake * handshake,
uint8_t req3[SHA_DIGEST_LENGTH];
uint8_t buf[SHA_DIGEST_LENGTH];
tr_sha1( req2, "req2", 4,
tr_cryptoGetTorrentHash(
handshake->crypto ), SHA_DIGEST_LENGTH, NULL );
tr_cryptoGetTorrentHash( handshake->crypto ),
SHA_DIGEST_LENGTH, NULL );
tr_sha1( req3, "req3", 4, secret, KEY_LEN, NULL );
for( i = 0; i < SHA_DIGEST_LENGTH; ++i )
buf[i] = req2[i] ^ req3[i];
@ -561,9 +559,8 @@ readPadD( tr_handshake * handshake,
const size_t needlen = handshake->pad_d_len;
uint8_t * tmp;
dbgmsg( handshake, "pad d: need %d, got %d", (int)needlen,
(int)EVBUFFER_LENGTH(
inbuf ) );
dbgmsg( handshake, "pad d: need %zu, got %zu",
needlen, EVBUFFER_LENGTH( inbuf ) );
if( EVBUFFER_LENGTH( inbuf ) < needlen )
return READ_LATER;
@ -593,9 +590,8 @@ readHandshake( tr_handshake * handshake,
uint8_t reserved[HANDSHAKE_FLAGS_LEN];
uint8_t hash[SHA_DIGEST_LENGTH];
dbgmsg( handshake, "payload: need %d, got %d",
(int)INCOMING_HANDSHAKE_LEN, (int)EVBUFFER_LENGTH(
inbuf ) );
dbgmsg( handshake, "payload: need %d, got %zu",
(int)INCOMING_HANDSHAKE_LEN, EVBUFFER_LENGTH( inbuf ) );
if( EVBUFFER_LENGTH( inbuf ) < INCOMING_HANDSHAKE_LEN )
return READ_LATER;
@ -713,14 +709,12 @@ readPeerId( tr_handshake * handshake,
return READ_LATER;
/* peer id */
tr_peerIoReadBytes( handshake->io, inbuf, handshake->peer_id,
PEER_ID_LEN );
tr_peerIoReadBytes( handshake->io, inbuf, handshake->peer_id, PEER_ID_LEN );
tr_peerIoSetPeersId( handshake->io, handshake->peer_id );
handshake->havePeerID = TRUE;
tr_clientForId( client, sizeof( client ), handshake->peer_id );
dbgmsg( handshake, "peer-id is [%s] ... isIncoming is %d", client,
tr_peerIoIsIncoming(
handshake->io ) );
tr_peerIoIsIncoming( handshake->io ) );
/* if we've somehow connected to ourselves, don't keep the connection */
peerIsGood =
@ -738,9 +732,8 @@ readYa( tr_handshake * handshake,
const uint8_t *myKey, *secret;
int len;
dbgmsg( handshake, "in readYa... need %d, have %d", (int)KEY_LEN,
(int)EVBUFFER_LENGTH(
inbuf ) );
dbgmsg( handshake, "in readYa... need %d, have %zu",
(int)KEY_LEN, EVBUFFER_LENGTH( inbuf ) );
if( EVBUFFER_LENGTH( inbuf ) < KEY_LEN )
return READ_LATER;
@ -771,10 +764,8 @@ readPadA( tr_handshake * handshake,
{
uint8_t * pch;
dbgmsg(
handshake,
"looking to get past pad a... & resync on hash('req',S) ... have %d bytes",
(int)EVBUFFER_LENGTH( inbuf ) );
dbgmsg( handshake, "looking to get past pad a... & resync on hash('req',S) ... have %zu bytes",
EVBUFFER_LENGTH( inbuf ) );
/**
*** Resynchronizing on HASH('req1',S)
**/
@ -784,9 +775,8 @@ readPadA( tr_handshake * handshake,
EVBUFFER_LENGTH( inbuf ) );
if( pch == NULL )
{
dbgmsg( handshake, "no luck so far.. draining %d bytes",
(int)EVBUFFER_LENGTH(
inbuf ) );
dbgmsg( handshake, "no luck so far.. draining %zu bytes",
EVBUFFER_LENGTH( inbuf ) );
evbuffer_drain( inbuf, EVBUFFER_LENGTH( inbuf ) );
return READ_LATER;
}
@ -849,10 +839,8 @@ readCryptoProvide( tr_handshake * handshake,
tr_torrentFindFromObfuscatedHash( handshake->handle,
obfuscatedTorrentHash ) ) )
{
dbgmsg(
handshake,
"got INCOMING connection's encrypted handshake for torrent [%s]",
tor->info.name );
dbgmsg( handshake, "got INCOMING connection's encrypted handshake for torrent [%s]",
tor->info.name );
tr_peerIoSetTorrentHash( handshake->io, tor->info.hash );
if( !tr_torrentAllowsPex( tor )
&& tr_peerMgrPeerIsSeed( handshake->handle->peerMgr,
@ -913,15 +901,13 @@ readIA( tr_handshake * handshake,
struct evbuffer * outbuf;
uint32_t crypto_select;
dbgmsg( handshake, "reading IA... have %d, need %d",
(int)EVBUFFER_LENGTH(
inbuf ), (int)needlen );
dbgmsg( handshake, "reading IA... have %zu, need %zu",
EVBUFFER_LENGTH( inbuf ), needlen );
if( EVBUFFER_LENGTH( inbuf ) < needlen )
return READ_LATER;
/**
*** B->A: ENCRYPT(VC, crypto_select, len(padD), padD), ENCRYPT2(Payload
***Stream)
*** B->A: ENCRYPT(VC, crypto_select, len(padD), padD), ENCRYPT2(Payload Stream)
**/
tr_cryptoEncryptInit( handshake->crypto );
@ -950,7 +936,7 @@ readIA( tr_handshake * handshake,
}
dbgmsg( handshake, "sending pad d" );
/* ENCRYPT(VC, crypto_provide, len(PadC), PadC
/* ENCRYPT(VC, crypto_provide, len(PadD), PadD
* PadD is reserved for future extensions to the handshake...
* standard practice at this time is for it to be zero-length */
{
@ -988,8 +974,8 @@ readPayloadStream( tr_handshake * handshake,
int i;
const size_t needlen = HANDSHAKE_SIZE;
dbgmsg( handshake, "reading payload stream... have %d, need %d",
(int)EVBUFFER_LENGTH( inbuf ), (int)needlen );
dbgmsg( handshake, "reading payload stream... have %zu, need %zu",
EVBUFFER_LENGTH( inbuf ), needlen );
if( EVBUFFER_LENGTH( inbuf ) < needlen )
return READ_LATER;
@ -1012,10 +998,10 @@ readPayloadStream( tr_handshake * handshake,
static ReadState
canRead( struct tr_iobuf * iobuf, void * arg, size_t * piece )
{
tr_handshake * handshake = arg;
tr_handshake * handshake = arg;
struct evbuffer * inbuf = tr_iobuf_input( iobuf );
ReadState ret;
int readyForMore = TRUE;
tr_bool readyForMore = TRUE;
/* no piece data in handshake */
*piece = 0;

View File

@ -24,11 +24,11 @@ struct tr_peerIo;
typedef struct tr_handshake tr_handshake;
/* returns true on success, false on error */
typedef int ( *handshakeDoneCB )( struct tr_handshake * handshake,
struct tr_peerIo * io,
int isConnected,
const uint8_t * peerId,
void * userData );
typedef tr_bool ( *handshakeDoneCB )( struct tr_handshake * handshake,
struct tr_peerIo * io,
int isConnected,
const uint8_t * peerId,
void * userData );
tr_handshake * tr_handshakeNew( struct tr_peerIo * io,
tr_encryption_mode encryptionMode,

View File

@ -495,7 +495,7 @@ tr_peerIoGetPeersId( const tr_peerIo * io )
void
tr_peerIoEnableFEXT( tr_peerIo * io,
int flag )
tr_bool flag )
{
assert( isPeerIo( io ) );
assert( isFlag( flag ) );
@ -504,7 +504,7 @@ tr_peerIoEnableFEXT( tr_peerIo * io,
io->fastExtensionSupported = flag;
}
int
tr_bool
tr_peerIoSupportsFEXT( const tr_peerIo * io )
{
assert( isPeerIo( io ) );
@ -517,8 +517,8 @@ tr_peerIoSupportsFEXT( const tr_peerIo * io )
**/
void
tr_peerIoEnableLTEP( tr_peerIo * io,
int flag )
tr_peerIoEnableLTEP( tr_peerIo * io,
tr_bool flag )
{
assert( isPeerIo( io ) );
assert( isFlag( flag ) );
@ -527,7 +527,7 @@ tr_peerIoEnableLTEP( tr_peerIo * io,
io->extendedProtocolSupported = flag;
}
int
tr_bool
tr_peerIoSupportsLTEP( const tr_peerIo * io )
{
assert( isPeerIo( io ) );

View File

@ -49,13 +49,13 @@ void tr_peerIoFree ( tr_peerIo * io );
***
**/
void tr_peerIoEnableLTEP( tr_peerIo * io, int flag );
void tr_peerIoEnableLTEP( tr_peerIo * io, tr_bool flag );
int tr_peerIoSupportsLTEP( const tr_peerIo * io );
tr_bool tr_peerIoSupportsLTEP( const tr_peerIo * io );
void tr_peerIoEnableFEXT( tr_peerIo * io, int flag );
void tr_peerIoEnableFEXT( tr_peerIo * io, tr_bool flag );
int tr_peerIoSupportsFEXT( const tr_peerIo * io );
tr_bool tr_peerIoSupportsFEXT( const tr_peerIo * io );
/**
***

View File

@ -299,7 +299,7 @@ getExistingAtom( const Torrent * t,
return tr_ptrArrayFindSorted( t->pool, addr, comparePeerAtomToAddress );
}
static int
static tr_bool
peerIsInUse( const Torrent * ct,
const tr_address * addr )
{
@ -308,15 +308,14 @@ peerIsInUse( const Torrent * ct,
assert( torrentIsLocked ( t ) );
return getExistingPeer( t, addr )
|| getExistingHandshake( t->outgoingHandshakes, addr )
|| getExistingHandshake( t->manager->incomingHandshakes, addr );
|| getExistingHandshake( t->outgoingHandshakes, addr )
|| getExistingHandshake( t->manager->incomingHandshakes, addr );
}
static tr_peer*
peerConstructor( tr_torrent * tor, const tr_address * addr )
{
tr_peer * p;
p = tr_new0( tr_peer, 1 );
p->addr = *addr;
p->bandwidth = tr_bandwidthNew( tor->session, tor->bandwidth );
@ -486,10 +485,9 @@ tr_peerMgrFree( tr_peerMgr * manager )
}
static tr_peer**
getConnectedPeers( Torrent * t,
int * setmeCount )
getConnectedPeers( Torrent * t, int * setmeCount )
{
int i, peerCount, connectionCount;
int i, peerCount, connectionCount;
tr_peer **peers;
tr_peer **ret;
@ -522,13 +520,13 @@ clientIsUploadingTo( const tr_peer * peer )
****
***/
int
tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr,
const uint8_t * torrentHash,
const tr_address * addr )
tr_bool
tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr,
const uint8_t * torrentHash,
const tr_address * addr )
{
int isSeed = FALSE;
const Torrent * t = NULL;
tr_bool isSeed = FALSE;
const Torrent * t = NULL;
const struct peer_atom * atom = NULL;
t = getExistingTorrent( (tr_peerMgr*)mgr, torrentHash );
@ -592,8 +590,7 @@ struct tr_refill_piece
};
static int
compareRefillPiece( const void * aIn,
const void * bIn )
compareRefillPiece( const void * aIn, const void * bIn )
{
const struct tr_refill_piece * a = aIn;
const struct tr_refill_piece * b = bIn;
@ -622,8 +619,7 @@ compareRefillPiece( const void * aIn,
}
static tr_piece_index_t *
getPreferredPieces( Torrent * t,
tr_piece_index_t * pieceCount )
getPreferredPieces( Torrent * t, tr_piece_index_t * pieceCount )
{
const tr_torrent * tor = t->tor;
const tr_info * inf = &tor->info;
@ -821,7 +817,7 @@ refillPulse( void * vtorrent )
/* find a peer who can ask for this block */
for( j=0; !handled && j<peerCount; )
{
const int val = tr_peerMsgsAddRequest( peers[j]->msgs, index, offset, length, FALSE );
const tr_addreq_t val = tr_peerMsgsAddRequest( peers[j]->msgs, index, offset, length, FALSE );
switch( val )
{
case TR_ADDREQ_FULL:
@ -1042,7 +1038,6 @@ peerCallbackFunc( void * vpeer,
{
const time_t now = time( NULL );
tr_torrent * tor = t->tor;
tor->activityDate = now;
/* only add this to downloadedCur if we got it from a peer --
@ -1072,19 +1067,12 @@ peerCallbackFunc( void * vpeer,
if( peer )
{
struct peer_atom * atom = getExistingAtom( t, &peer->addr );
const int peerIsSeed = e->progress >= 1.0;
if( peerIsSeed )
{
tordbg( t, "marking peer %s as a seed",
tr_peerIoAddrStr( &atom->addr,
atom->port ) );
const int peerIsSeed = e->progress >= 1.0;
if( peerIsSeed ) {
tordbg( t, "marking peer %s as a seed", tr_peerIoAddrStr( &atom->addr, atom->port ) );
atom->flags |= ADDED_F_SEED_FLAG;
}
else
{
tordbg( t, "marking peer %s as a non-seed",
tr_peerIoAddrStr( &atom->addr,
atom->port ) );
} else {
tordbg( t, "marking peer %s as a non-seed", tr_peerIoAddrStr( &atom->addr, atom->port ) );
atom->flags &= ~ADDED_F_SEED_FLAG;
}
}
@ -1095,8 +1083,7 @@ peerCallbackFunc( void * vpeer,
{
tr_torrent * tor = t->tor;
tr_block_index_t block = _tr_block( tor, e->pieceIndex,
e->offset );
tr_block_index_t block = _tr_block( tor, e->pieceIndex, e->offset );
tr_cpBlockAdd( tor->completion, block );
decrementPieceRequests( t, e->pieceIndex );
@ -1106,13 +1093,12 @@ peerCallbackFunc( void * vpeer,
if( tr_cpPieceIsComplete( tor->completion, e->pieceIndex ) )
{
const tr_piece_index_t p = e->pieceIndex;
const int ok = tr_ioTestPiece( tor, p );
const tr_bool ok = tr_ioTestPiece( tor, p );
if( !ok )
{
tr_torerr( tor,
_( "Piece %lu, which was just downloaded, failed its checksum test" ),
(unsigned long)p );
tr_torerr( tor, _( "Piece %lu, which was just downloaded, failed its checksum test" ),
(unsigned long)p );
}
tr_torrentSetHasPiece( tor, p, ok );
@ -1123,7 +1109,7 @@ peerCallbackFunc( void * vpeer,
gotBadPiece( t, p );
else
{
int i, peerCount;
int i, peerCount;
tr_peer ** peers = getConnectedPeers( t, &peerCount );
for( i = 0; i < peerCount; ++i )
tr_peerMsgsHave( peers[i]->msgs, p );
@ -1180,8 +1166,7 @@ ensureAtomExists( Torrent * t,
a->port = port;
a->flags = flags;
a->from = from;
tordbg( t, "got a new atom: %s",
tr_peerIoAddrStr( &a->addr, a->port ) );
tordbg( t, "got a new atom: %s", tr_peerIoAddrStr( &a->addr, a->port ) );
tr_ptrArrayInsertSorted( t->pool, a, comparePeerAtoms );
}
}
@ -1195,20 +1180,19 @@ getMaxPeerCount( const tr_torrent * tor )
static int
getPeerCount( const Torrent * t )
{
return tr_ptrArraySize( t->peers ) + tr_ptrArraySize(
t->outgoingHandshakes );
return tr_ptrArraySize( t->peers ) + tr_ptrArraySize( t->outgoingHandshakes );
}
/* FIXME: this is kind of a mess. */
static int
static tr_bool
myHandshakeDoneCB( tr_handshake * handshake,
tr_peerIo * io,
int isConnected,
const uint8_t * peer_id,
void * vmanager )
{
int ok = isConnected;
int success = FALSE;
tr_bool ok = isConnected;
tr_bool success = FALSE;
tr_port port;
const tr_address * addr;
tr_peerMgr * manager = vmanager;
@ -1260,8 +1244,7 @@ myHandshakeDoneCB( tr_handshake * handshake,
if( atom->myflags & MYFLAG_BANNED )
{
tordbg( t, "banned peer %s tried to reconnect",
tr_peerIoAddrStr( &atom->addr,
atom->port ) );
tr_peerIoAddrStr( &atom->addr, atom->port ) );
tr_peerIoFree( io );
}
else if( tr_peerIoIsIncoming( io )
@ -1317,8 +1300,7 @@ tr_peerMgrAddIncoming( tr_peerMgr * manager,
if( tr_sessionIsAddressBlocked( manager->session, addr ) )
{
tr_dbg( "Banned IP address \"%s\" tried to connect to us",
tr_ntop_non_ts( addr ) );
tr_dbg( "Banned IP address \"%s\" tried to connect to us", tr_ntop_non_ts( addr ) );
tr_netClose( socket );
}
else if( getExistingHandshake( manager->incomingHandshakes, addr ) )
@ -1410,11 +1392,9 @@ tr_peerMgrSetBlame( tr_peerMgr * manager,
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->addr, peer->port ),
pieceIndex, (int)peer->strikes + 1 );
tordbg( t, "peer %s contributed to corrupt piece (%d); now has %d strikes",
tr_peerIoAddrStr( &peer->addr, peer->port ),
pieceIndex, (int)peer->strikes + 1 );
addStrike( t, peer );
}
}
@ -1422,12 +1402,11 @@ tr_peerMgrSetBlame( tr_peerMgr * manager,
}
int
tr_pexCompare( const void * va,
const void * vb )
tr_pexCompare( const void * va, const void * vb )
{
const tr_pex * a = va;
const tr_pex * b = vb;
int i = tr_compareAddresses( &a->addr, &b->addr );
int i = tr_compareAddresses( &a->addr, &b->addr );
if( i ) return i;
if( a->port < b->port ) return -1;
@ -1435,9 +1414,6 @@ tr_pexCompare( const void * va,
return 0;
}
int tr_pexCompare( const void * a,
const void * b );
static int
peerPrefersCrypto( const tr_peer * peer )
{
@ -1460,8 +1436,8 @@ tr_peerMgrGetPeers( tr_peerMgr * manager,
managerLock( manager );
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
if( !t )
t = getExistingTorrent( manager, torrentHash );
if( t == NULL )
{
*setme_pex = NULL;
}
@ -1472,7 +1448,7 @@ tr_peerMgrGetPeers( tr_peerMgr * manager,
tr_pex * pex = tr_new( tr_pex, peerCount );
tr_pex * walk = pex;
for( i = 0; i < peerCount; ++i, ++walk )
for( i=0; i<peerCount; ++i, ++walk )
{
const tr_peer * peer = peers[i];
const struct peer_atom * atom = getExistingAtom( t, &peer->addr );
@ -1651,7 +1627,6 @@ tr_peerMgrGetAvailable( const tr_peerMgr * manager,
Torrent * t;
tr_peer ** peers;
tr_bitfield * pieces;
managerLock( manager );
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
@ -1669,14 +1644,12 @@ int
tr_peerMgrHasConnections( const tr_peerMgr * manager,
const uint8_t * torrentHash )
{
int ret;
int ret;
const Torrent * t;
managerLock( manager );
t = getExistingTorrent( (tr_peerMgr*)manager, torrentHash );
ret = t && ( !tr_ptrArrayEmpty( t->peers )
|| !tr_ptrArrayEmpty( t->webseeds ) );
ret = t && ( !tr_ptrArrayEmpty( t->peers ) || !tr_ptrArrayEmpty( t->webseeds ) );
managerUnlock( manager );
return ret;
@ -1684,18 +1657,18 @@ tr_peerMgrHasConnections( const tr_peerMgr * manager,
void
tr_peerMgrTorrentStats( const tr_peerMgr * manager,
const uint8_t * torrentHash,
int * setmePeersKnown,
int * setmePeersConnected,
int * setmeSeedsConnected,
int * setmeWebseedsSendingToUs,
int * setmePeersSendingToUs,
int * setmePeersGettingFromUs,
int * setmePeersFrom )
const uint8_t * torrentHash,
int * setmePeersKnown,
int * setmePeersConnected,
int * setmeSeedsConnected,
int * setmeWebseedsSendingToUs,
int * setmePeersSendingToUs,
int * setmePeersGettingFromUs,
int * setmePeersFrom )
{
int i, size;
const Torrent * t;
const tr_peer ** peers;
int i, size;
const Torrent * t;
const tr_peer ** peers;
const tr_webseed ** webseeds;
managerLock( manager );
@ -1710,36 +1683,36 @@ tr_peerMgrTorrentStats( const tr_peerMgr * manager,
*setmePeersSendingToUs = 0;
*setmeWebseedsSendingToUs = 0;
for( i = 0; i < TR_PEER_FROM__MAX; ++i )
for( i=0; i<TR_PEER_FROM__MAX; ++i )
setmePeersFrom[i] = 0;
for( i = 0; i < size; ++i )
for( i=0; i<size; ++i )
{
const tr_peer * peer = peers[i];
const tr_peer * peer = peers[i];
const struct peer_atom * atom = getExistingAtom( t, &peer->addr );
if( peer->io == NULL ) /* not connected */
continue;
++ * setmePeersConnected;
++*setmePeersConnected;
++setmePeersFrom[atom->from];
if( clientIsDownloadingFrom( peer ) )
++ * setmePeersSendingToUs;
++*setmePeersSendingToUs;
if( clientIsUploadingTo( peer ) )
++ * setmePeersGettingFromUs;
++*setmePeersGettingFromUs;
if( atom->flags & ADDED_F_SEED_FLAG )
++ * setmeSeedsConnected;
++*setmeSeedsConnected;
}
webseeds = (const tr_webseed **) tr_ptrArrayPeek( t->webseeds, &size );
for( i = 0; i < size; ++i )
for( i=0; i<size; ++i )
{
if( tr_webseedIsActive( webseeds[i] ) )
++ * setmeWebseedsSendingToUs;
++*setmeWebseedsSendingToUs;
}
managerUnlock( manager );
@ -1749,11 +1722,11 @@ float*
tr_peerMgrWebSpeeds( const tr_peerMgr * manager,
const uint8_t * torrentHash )
{
const Torrent * t;
const Torrent * t;
const tr_webseed ** webseeds;
int i;
int webseedCount;
float * ret;
int i;
int webseedCount;
float * ret;
assert( manager );
managerLock( manager );
@ -1764,7 +1737,7 @@ tr_peerMgrWebSpeeds( const tr_peerMgr * manager,
assert( webseedCount == t->tor->info.webseedCount );
ret = tr_new0( float, webseedCount );
for( i = 0; i < webseedCount; ++i )
for( i=0; i<webseedCount; ++i )
if( !tr_webseedGetSpeed( webseeds[i], &ret[i] ) )
ret[i] = -1.0;
@ -1773,8 +1746,7 @@ tr_peerMgrWebSpeeds( const tr_peerMgr * manager,
}
double
tr_peerGetPieceSpeed( const tr_peer * peer,
tr_direction direction )
tr_peerGetPieceSpeed( const tr_peer * peer, tr_direction direction )
{
assert( peer );
assert( direction==TR_CLIENT_TO_PEER || direction==TR_PEER_TO_CLIENT );
@ -1807,9 +1779,8 @@ tr_peerMgrPeerStats( const tr_peerMgr * manager,
const struct peer_atom * atom = getExistingAtom( t, &peer->addr );
tr_peer_stat * stat = ret + i;
tr_ntop( &peer->addr, stat->addr, sizeof( stat->addr ) );
tr_strlcpy( stat->client, ( peer->client ? peer->client : "" ),
sizeof( stat->client ) );
tr_ntop( &peer->addr, stat->addr, sizeof(stat->addr) );
tr_strlcpy( stat->client, (peer->client ? peer->client : ""), sizeof(stat->client) );
stat->port = ntohs( peer->port );
stat->from = atom->from;
stat->progress = peer->progress;
@ -1831,8 +1802,7 @@ tr_peerMgrPeerStats( const tr_peerMgr * manager,
else if( stat->clientIsInterested ) *pch++ = 'd';
if( stat->isUploadingTo ) *pch++ = 'U';
else if( stat->peerIsInterested ) *pch++ = 'u';
if( !stat->clientIsChoked && !stat->clientIsInterested ) *pch++ =
'K';
if( !stat->clientIsChoked && !stat->clientIsInterested ) *pch++ = 'K';
if( !stat->peerIsChoked && !stat->peerIsInterested ) *pch++ = '?';
if( stat->isEncrypted ) *pch++ = 'E';
if( stat->from == TR_PEER_FROM_PEX ) *pch++ = 'X';
@ -1946,8 +1916,7 @@ rechoke( Torrent * t )
* rate to decide which peers to unchoke.
*/
unchokedInterested = 0;
for( i = 0; i < size && unchokedInterested < MAX_UNCHOKED_PEERS; ++i )
{
for( i=0; i<size && unchokedInterested<MAX_UNCHOKED_PEERS; ++i ) {
choke[i].doUnchoke = 1;
if( choke[i].isInterested )
++unchokedInterested;
@ -1956,26 +1925,26 @@ rechoke( Torrent * t )
/* optimistic unchoke */
if( i < size )
{
int n;
int n;
struct ChokeData * c;
tr_ptrArray * randPool = tr_ptrArrayNew( );
tr_ptrArray * randPool = tr_ptrArrayNew( );
for( ; i < size; ++i )
for( ; i<size; ++i )
{
if( choke[i].isInterested )
{
const tr_peer * peer = choke[i].peer;
int x = 1, y;
int x = 1, y;
if( isNew( peer ) ) x *= 3;
if( isSame( peer ) ) x *= 3;
for( y = 0; y < x; ++y )
for( y=0; y<x; ++y )
tr_ptrArrayAppend( randPool, &choke[i] );
}
}
if( ( n = tr_ptrArraySize( randPool ) ) )
if(( n = tr_ptrArraySize( randPool )))
{
c = tr_ptrArrayNth( randPool, tr_cryptoWeakRandInt( n ) );
c = tr_ptrArrayNth( randPool, tr_cryptoWeakRandInt( n ));
c->doUnchoke = 1;
t->optimistic = c->peer;
}
@ -2021,8 +1990,7 @@ shouldPeerBeClosed( const Torrent * t,
if( peer->doPurge )
{
tordbg( t, "purging peer %s because its doPurge flag is set",
tr_peerIoAddrStr( &atom->addr,
atom->port ) );
tr_peerIoAddrStr( &atom->addr, atom->port ) );
return TRUE;
}
@ -2035,20 +2003,15 @@ shouldPeerBeClosed( const Torrent * t,
peerHasEverything = TRUE;
else if( peer->progress < tr_cpPercentDone( tor->completion ) )
peerHasEverything = FALSE;
else
{
tr_bitfield * tmp =
tr_bitfieldDup( tr_cpPieceBitfield( tor->completion ) );
else {
tr_bitfield * tmp = tr_bitfieldDup( tr_cpPieceBitfield( tor->completion ) );
tr_bitfieldDifference( tmp, peer->have );
peerHasEverything = tr_bitfieldCountTrueBits( tmp ) == 0;
tr_bitfieldFree( tmp );
}
if( peerHasEverything
&& ( !tr_torrentAllowsPex( tor ) || ( now - atom->time >= 30 ) ) )
{
if( peerHasEverything && ( !tr_torrentAllowsPex(tor) || (now-atom->time>=30 ))) {
tordbg( t, "purging peer %s because we're both seeds",
tr_peerIoAddrStr( &atom->addr,
atom->port ) );
tr_peerIoAddrStr( &atom->addr, atom->port ) );
return TRUE;
}
}
@ -2056,25 +2019,20 @@ shouldPeerBeClosed( const Torrent * t,
/* 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... */
{
const int relaxStrictnessIfFewerThanN =
(int)( ( getMaxPeerCount( tor ) * 0.9 ) + 0.5 );
const int relaxStrictnessIfFewerThanN = (int)( ( getMaxPeerCount( tor ) * 0.9 ) + 0.5 );
/* if we have >= relaxIfFewerThan, strictness is 100%.
* if we have zero connections, strictness is 0% */
const float strictness = peerCount >= relaxStrictnessIfFewerThanN
? 1.0
: peerCount /
(float)relaxStrictnessIfFewerThanN;
const int lo = MIN_UPLOAD_IDLE_SECS;
const int hi = MAX_UPLOAD_IDLE_SECS;
const int limit = lo + ( ( hi - lo ) * strictness );
const float strictness = peerCount >= relaxStrictnessIfFewerThanN
? 1.0
: peerCount / (float)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 )
{
tordbg(
t,
"purging peer %s because it's been %d secs since we shared anything",
tr_peerIoAddrStr( &atom->addr, atom->port ), idleTime );
const int idleTime = then ? ( now - then ) : 0;
if( idleTime > limit ) {
tordbg( t, "purging peer %s because it's been %d secs since we shared anything",
tr_peerIoAddrStr( &atom->addr, atom->port ), idleTime );
return TRUE;
}
}
@ -2083,12 +2041,10 @@ shouldPeerBeClosed( const Torrent * t,
}
static tr_peer **
getPeersToClose( Torrent * t,
int * setmeSize )
getPeersToClose( Torrent * t, int * setmeSize )
{
int i, peerCount, outsize;
tr_peer ** peers = (tr_peer**) tr_ptrArrayPeek( t->peers,
&peerCount );
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 ) );
@ -2141,8 +2097,7 @@ getReconnectIntervalSecs( const struct peer_atom * atom )
/* if we were recently connected to this peer and transferring piece
* data, try to reconnect to them sooner rather that later -- we don't
* want network troubles to get in the way of a good peer. */
if( ( now - atom->piece_data_time ) <=
( MINIMUM_RECONNECT_INTERVAL_SECS * 2 ) )
if( ( now - atom->piece_data_time ) <= ( MINIMUM_RECONNECT_INTERVAL_SECS * 2 ) )
sec = MINIMUM_RECONNECT_INTERVAL_SECS;
/* don't allow reconnects more often than our minimum */
@ -2151,36 +2106,21 @@ getReconnectIntervalSecs( const struct peer_atom * atom )
/* otherwise, the interval depends on how many times we've tried
* and failed to connect to the peer */
else switch( atom->numFails )
{
case 0:
sec = 0; break;
case 1:
sec = 5; break;
case 2:
sec = 2 * 60; break;
case 3:
sec = 15 * 60; break;
case 4:
sec = 30 * 60; break;
case 5:
sec = 60 * 60; break;
default:
sec = 120 * 60; break;
}
else switch( atom->numFails ) {
case 0: sec = 0; break;
case 1: sec = 5; break;
case 2: sec = 2 * 60; break;
case 3: sec = 15 * 60; break;
case 4: sec = 30 * 60; break;
case 5: sec = 60 * 60; break;
default: sec = 120 * 60; break;
}
return sec;
}
static struct peer_atom **
getPeerCandidates( Torrent * t,
int * setmeSize )
getPeerCandidates( Torrent * t, int * setmeSize )
{
int i, atomCount, retCount;
struct peer_atom ** atoms;
@ -2221,11 +2161,8 @@ getPeerCandidates( Torrent * t,
interval = getReconnectIntervalSecs( atom );
if( ( now - atom->time ) < interval )
{
tordbg(
t,
"RECONNECT peer %d (%s) is in its grace period of %d seconds..",
i, tr_peerIoAddrStr( &atom->addr,
atom->port ), interval );
tordbg( t, "RECONNECT peer %d (%s) is in its grace period of %d seconds..",
i, tr_peerIoAddrStr( &atom->addr, atom->port ), interval );
continue;
}
@ -2264,32 +2201,29 @@ reconnectPulse( void * vtorrent )
}
else
{
int i, nCandidates, nBad;
int i, nCandidates, nBad;
struct peer_atom ** candidates = getPeerCandidates( t, &nCandidates );
struct tr_peer ** connections = getPeersToClose( t, &nBad );
struct tr_peer ** connections = getPeersToClose( t, &nBad );
if( nBad || nCandidates )
tordbg(
t, "reconnect pulse for [%s]: %d bad connections, "
tordbg( t, "reconnect pulse for [%s]: %d bad connections, "
"%d connection candidates, %d atoms, max per pulse is %d",
t->tor->info.name, nBad, nCandidates,
tr_ptrArraySize( t->pool ),
(int)MAX_RECONNECTIONS_PER_PULSE );
t->tor->info.name, nBad, nCandidates,
tr_ptrArraySize( t->pool ),
(int)MAX_RECONNECTIONS_PER_PULSE );
/* disconnect some peers.
if we 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];
for( i = 0; i < nBad; ++i ) {
tr_peer * peer = connections[i];
struct peer_atom * atom = getExistingAtom( t, &peer->addr );
if( peer->pieceDataActivityDate )
atom->numFails = 0;
else
++atom->numFails;
tordbg( t, "removing bad peer %s",
tr_peerIoGetAddrStr( peer->io ) );
tordbg( t, "removing bad peer %s", tr_peerIoGetAddrStr( peer->io ) );
removePeer( t, peer );
}
@ -2297,8 +2231,7 @@ reconnectPulse( void * vtorrent )
for( i = 0; ( i < nCandidates )
&& ( i < MAX_RECONNECTIONS_PER_PULSE )
&& ( getPeerCount( t ) < getMaxPeerCount( t->tor ) )
&& ( newConnectionsThisSecond < MAX_CONNECTIONS_PER_SECOND );
++i )
&& ( newConnectionsThisSecond < MAX_CONNECTIONS_PER_SECOND ); ++i )
{
tr_peerMgr * mgr = t->manager;
struct peer_atom * atom = candidates[i];
@ -2307,21 +2240,17 @@ reconnectPulse( void * vtorrent )
tordbg( t, "Starting an OUTGOING connection with %s",
tr_peerIoAddrStr( &atom->addr, atom->port ) );
io =
tr_peerIoNewOutgoing( mgr->session, &atom->addr, atom->port,
t->hash );
io = tr_peerIoNewOutgoing( mgr->session, &atom->addr, atom->port, t->hash );
if( io == NULL )
{
atom->myflags |= MYFLAG_UNREACHABLE;
}
else
{
tr_handshake * handshake = tr_handshakeNew(
io,
mgr->session->
encryptionMode,
myHandshakeDoneCB,
mgr );
tr_handshake * handshake = tr_handshakeNew( io,
mgr->session->encryptionMode,
myHandshakeDoneCB,
mgr );
assert( tr_peerIoGetTorrentHash( io ) );

View File

@ -53,9 +53,9 @@ tr_peerMgr* tr_peerMgrNew( struct tr_handle * );
void tr_peerMgrFree( tr_peerMgr * manager );
int tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr,
const uint8_t * torrentHash,
const tr_address * addr );
tr_bool tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr,
const uint8_t * torrentHash,
const tr_address * addr );
void tr_peerMgrAddIncoming( tr_peerMgr * manager,
tr_address * addr,

View File

@ -122,8 +122,7 @@ struct peer_request
};
static int
compareRequest( const void * va,
const void * vb )
compareRequest( const void * va, const void * vb )
{
const struct peer_request * a = va;
const struct peer_request * b = vb;
@ -170,12 +169,10 @@ reqListClear( struct request_list * list )
}
static void
reqListCopy( struct request_list * dest,
const struct request_list * src )
reqListCopy( struct request_list * dest, const struct request_list * src )
{
dest->count = dest->max = src->count;
dest->requests =
tr_memdup( src->requests, dest->count * sizeof( struct peer_request ) );
dest->requests = tr_memdup( src->requests, dest->count * sizeof( struct peer_request ) );
}
static void
@ -185,8 +182,8 @@ reqListRemoveOne( struct request_list * list,
assert( 0 <= i && i < list->count );
memmove( &list->requests[i],
&list->requests[i + 1],
sizeof( struct peer_request ) * ( --list->count - i ) );
&list->requests[i + 1],
sizeof( struct peer_request ) * ( --list->count - i ) );
}
static void
@ -316,11 +313,9 @@ struct tr_peermsgs
**/
static void
myDebug( const char * file,
int line,
myDebug( const char * file, int line,
const struct tr_peermsgs * msgs,
const char * fmt,
... )
const char * fmt, ... )
{
FILE * fp = tr_getLog( );
@ -516,8 +511,7 @@ protocolSendHaveNone( tr_peermsgs * msgs )
static const tr_peer_event blankEvent = { 0, 0, 0, 0, 0.0f, 0, 0, 0 };
static void
publish( tr_peermsgs * msgs,
tr_peer_event * e )
publish( tr_peermsgs * msgs, tr_peer_event * e )
{
assert( msgs->info );
assert( msgs->info->msgs == msgs );
@ -526,11 +520,9 @@ publish( tr_peermsgs * msgs,
}
static void
fireError( tr_peermsgs * msgs,
int err )
fireError( tr_peermsgs * msgs, int err )
{
tr_peer_event e = blankEvent;
e.eventType = TR_PEER_ERROR;
e.err = err;
publish( msgs, &e );
@ -557,18 +549,15 @@ static void
firePeerProgress( tr_peermsgs * msgs )
{
tr_peer_event e = blankEvent;
e.eventType = TR_PEER_PEER_PROGRESS;
e.progress = msgs->info->progress;
publish( msgs, &e );
}
static void
fireGotBlock( tr_peermsgs * msgs,
const struct peer_request * req )
fireGotBlock( tr_peermsgs * msgs, const struct peer_request * req )
{
tr_peer_event e = blankEvent;
e.eventType = TR_PEER_CLIENT_GOT_BLOCK;
e.pieceIndex = req->index;
e.offset = req->offset;
@ -691,7 +680,7 @@ static void
updateFastSet( tr_peermsgs * msgs UNUSED )
{
#if 0
const int fext = tr_peerIoSupportsFEXT( msgs->io );
const tr_bool fext = tr_peerIoSupportsFEXT( msgs->io );
const int peerIsNeedy = msgs->info->progress < 0.10;
if( fext && peerIsNeedy && !msgs->haveFastSet )
@ -723,9 +712,8 @@ isPieceInteresting( const tr_peermsgs * peer,
const tr_torrent * torrent = peer->torrent;
return ( !torrent->info.pieces[piece].dnd ) /* we want it */
&& ( !tr_cpPieceIsComplete( torrent->completion, piece ) ) /* !have
*/
&& ( tr_bitfieldHas( peer->info->have, piece ) ); /* peer has it */
&& ( !tr_cpPieceIsComplete( torrent->completion, piece ) ) /* !have */
&& ( tr_bitfieldHas( peer->info->have, piece ) ); /* peer has it */
}
/* "interested" means we'll ask for piece data if they unchoke us */
@ -768,13 +756,11 @@ sendInterest( tr_peermsgs * msgs,
assert( weAreInterested == 0 || weAreInterested == 1 );
msgs->info->clientIsInterested = weAreInterested;
dbgmsg( msgs, "Sending %s",
weAreInterested ? "Interested" : "Not Interested" );
dbgmsg( msgs, "Sending %s", weAreInterested ? "Interested" : "Not Interested" );
tr_peerIoWriteUint32( msgs->io, out, sizeof( uint8_t ) );
tr_peerIoWriteUint8 (
msgs->io, out, weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED );
tr_peerIoWriteUint8 ( msgs->io, out, weAreInterested ? BT_INTERESTED : BT_NOT_INTERESTED );
pokeBatchPeriod( msgs, HIGH_PRIORITY_INTERVAL_SECS );
dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH( out ) );
dbgOutMessageLen( msgs );
}
static void
@ -819,8 +805,7 @@ tr_peerMsgsSetChoke( tr_peermsgs * msgs,
if( msgs->info->chokeChangedAt > fibrillationTime )
{
dbgmsg( msgs, "Not changing choke to %d to avoid fibrillation",
choke );
dbgmsg( msgs, "Not changing choke to %d to avoid fibrillation", choke );
}
else if( msgs->info->peerIsChoked != choke )
{
@ -868,10 +853,10 @@ requestIsValid( const tr_peermsgs * msgs, const struct peer_request * req )
static void
expireOldRequests( tr_peermsgs * msgs, const time_t now )
{
int i;
time_t oldestAllowed;
int i;
time_t oldestAllowed;
struct request_list tmp = REQUEST_LIST_INIT;
const int fext = tr_peerIoSupportsFEXT( msgs->io );
const tr_bool fext = tr_peerIoSupportsFEXT( msgs->io );
/* cancel requests that have been queued for too long */
oldestAllowed = now - QUEUED_REQUEST_TTL_SECS;
@ -915,8 +900,7 @@ pumpRequestQueue( tr_peermsgs * msgs, const time_t now )
while( ( count < max ) && reqListPop( &msgs->clientWillAskFor, &req ) )
{
const tr_block_index_t block =
_tr_block( msgs->torrent, req.index, req.offset );
const tr_block_index_t block = _tr_block( msgs->torrent, req.index, req.offset );
assert( requestIsValid( msgs, &req ) );
assert( tr_bitfieldHas( msgs->info->have, req.index ) );
@ -935,8 +919,7 @@ pumpRequestQueue( tr_peermsgs * msgs, const time_t now )
}
if( sent )
dbgmsg( msgs,
"pump sent %d requests, now have %d active and %d queued",
dbgmsg( msgs, "pump sent %d requests, now have %d active and %d queued",
sent,
msgs->clientAskedFor.count,
msgs->clientWillAskFor.count );
@ -1014,7 +997,7 @@ tr_peerMsgsAddRequest( tr_peermsgs * msgs,
static void
cancelAllRequestsToPeer( tr_peermsgs * msgs, tr_bool sendCancel )
{
int i;
int i;
struct request_list a = msgs->clientWillAskFor;
struct request_list b = msgs->clientAskedFor;
dbgmsg( msgs, "cancelling all requests to peer" );
@ -1074,10 +1057,10 @@ tr_peerMsgsCancel( tr_peermsgs * msgs,
static void
sendLtepHandshake( tr_peermsgs * msgs )
{
tr_benc val, *m;
char * buf;
int len;
int pex;
tr_benc val, *m;
char * buf;
int len;
int pex;
struct evbuffer * out = msgs->outMessages;
if( msgs->clientSentLtepHandshake )
@ -1094,7 +1077,7 @@ sendLtepHandshake( tr_peermsgs * msgs )
else
pex = 1;
tr_bencInitDict( &val, 4 );
tr_bencInitDict( &val, 5 );
tr_bencDictAddInt( &val, "e", msgs->session->encryptionMode != TR_CLEAR_PREFERRED );
tr_bencDictAddInt( &val, "p", tr_sessionGetPeerPort( msgs->session ) );
tr_bencDictAddInt( &val, "upload_only", tr_torrentIsSeed( msgs->torrent ) );
@ -1109,7 +1092,7 @@ sendLtepHandshake( tr_peermsgs * msgs )
tr_peerIoWriteUint8 ( msgs->io, out, LTEP_HANDSHAKE );
tr_peerIoWriteBytes ( msgs->io, out, buf, len );
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
dbgmsg( msgs, "outMessage size is now %d", (int)EVBUFFER_LENGTH( out ) );
dbgOutMessageLen( msgs );
/* cleanup */
tr_bencFree( &val );
@ -1128,7 +1111,7 @@ parseLtepHandshake( tr_peermsgs * msgs,
tr_peerIoReadBytes( msgs->io, inbuf, tmp, len );
msgs->peerSentLtepHandshake = 1;
if( tr_bencLoad( tmp, len, &val, NULL ) || val.type != TYPE_DICT )
if( tr_bencLoad( tmp, len, &val, NULL ) || !tr_bencIsDict( &val ) )
{
dbgmsg( msgs, "GET extended-handshake, couldn't get dictionary" );
tr_free( tmp );
@ -1140,14 +1123,12 @@ parseLtepHandshake( tr_peermsgs * msgs,
/* does the peer prefer encrypted connections? */
if( tr_bencDictFindInt( &val, "e", &i ) )
msgs->info->encryption_preference = i ? ENCRYPTION_PREFERENCE_YES
: ENCRYPTION_PREFERENCE_NO;
: ENCRYPTION_PREFERENCE_NO;
/* check supported messages for utorrent pex */
msgs->peerSupportsPex = 0;
if( tr_bencDictFindDict( &val, "m", &sub ) )
{
if( tr_bencDictFindInt( sub, "ut_pex", &i ) )
{
if( tr_bencDictFindDict( &val, "m", &sub ) ) {
if( tr_bencDictFindInt( sub, "ut_pex", &i ) ) {
msgs->ut_pex_id = (uint8_t) i;
msgs->peerSupportsPex = msgs->ut_pex_id == 0 ? 0 : 1;
dbgmsg( msgs, "msgs->ut_pex is %d", (int)msgs->ut_pex_id );
@ -1159,8 +1140,7 @@ parseLtepHandshake( tr_peermsgs * msgs,
fireUploadOnly( msgs, i!=0 );
/* get peer's listening port */
if( tr_bencDictFindInt( &val, "p", &i ) )
{
if( tr_bencDictFindInt( &val, "p", &i ) ) {
msgs->info->port = htons( (uint16_t)i );
dbgmsg( msgs, "msgs->port is now %hu", msgs->info->port );
}
@ -1170,32 +1150,28 @@ parseLtepHandshake( tr_peermsgs * msgs,
}
static void
parseUtPex( tr_peermsgs * msgs,
int msglen,
struct evbuffer * inbuf )
parseUtPex( tr_peermsgs * msgs, int msglen, struct evbuffer * inbuf )
{
int loaded = 0;
uint8_t * tmp = tr_new( uint8_t, msglen );
tr_benc val;
int loaded = 0;
uint8_t * tmp = tr_new( uint8_t, msglen );
tr_benc val;
const tr_torrent * tor = msgs->torrent;
const uint8_t * added;
size_t added_len;
const uint8_t * added;
size_t added_len;
tr_peerIoReadBytes( msgs->io, inbuf, tmp, msglen );
if( tr_torrentAllowsPex( tor )
&& ( ( loaded = !tr_bencLoad( tmp, msglen, &val, NULL ) ) )
&& tr_bencDictFindRaw( &val, "added", &added, &added_len ) )
&& (( loaded = !tr_bencLoad( tmp, msglen, &val, NULL )))
&& tr_bencDictFindRaw( &val, "added", &added, &added_len ))
{
const uint8_t * added_f = NULL;
tr_pex * pex;
size_t i, n;
size_t added_f_len = 0;
tr_bencDictFindRaw( &val, "added.f", &added_f, &added_f_len );
pex =
tr_peerMgrCompactToPex( added, added_len, added_f, added_f_len,
&n );
for( i = 0; i < n; ++i )
pex = tr_peerMgrCompactToPex( added, added_len, added_f, added_f_len, &n );
for( i=0; i<n; ++i )
tr_peerMgrAddPex( msgs->session->peerMgr, tor->info.hash,
TR_PEER_FROM_PEX, pex + i );
tr_free( pex );
@ -1308,7 +1284,7 @@ static void
peerMadeRequest( tr_peermsgs * msgs,
const struct peer_request * req )
{
const int fext = tr_peerIoSupportsFEXT( msgs->io );
const tr_bool fext = tr_peerIoSupportsFEXT( msgs->io );
const int reqIsValid = requestIsValid( msgs, req );
const int clientHasPiece = reqIsValid && tr_cpPieceIsComplete( msgs->torrent->completion, req->index );
const int peerIsChoked = msgs->info->peerIsChoked;
@ -1331,9 +1307,7 @@ peerMadeRequest( tr_peermsgs * msgs,
}
static int
messageLengthIsCorrect( const tr_peermsgs * msg,
uint8_t id,
uint32_t len )
messageLengthIsCorrect( const tr_peermsgs * msg, uint8_t id, uint32_t len )
{
switch( id )
{
@ -1395,45 +1369,39 @@ readBtPiece( tr_peermsgs * msgs,
tr_peerIoReadUint32( msgs->io, inbuf, &req->index );
tr_peerIoReadUint32( msgs->io, inbuf, &req->offset );
req->length = msgs->incoming.length - 9;
dbgmsg( msgs, "got incoming block header %u:%u->%u", req->index,
req->offset,
req->length );
dbgmsg( msgs, "got incoming block header %u:%u->%u", req->index, req->offset, req->length );
return READ_NOW;
}
else
{
int err;
int err;
/* read in another chunk of data */
const size_t nLeft = req->length - EVBUFFER_LENGTH(
msgs->incoming.block );
size_t n = MIN( nLeft, inlen );
uint8_t * buf = tr_new( uint8_t, n );
const size_t nLeft = req->length - EVBUFFER_LENGTH( msgs->incoming.block );
size_t n = MIN( nLeft, inlen );
uint8_t * buf = tr_new( uint8_t, n );
assert( EVBUFFER_LENGTH( inbuf ) >= n );
tr_peerIoReadBytes( msgs->io, inbuf, buf, n );
evbuffer_add( msgs->incoming.block, buf, n );
fireClientGotData( msgs, n, TRUE );
*setme_piece_bytes_read += n;
tr_free( buf );
dbgmsg( msgs, "got %d bytes for block %u:%u->%u ... %d remain",
(int)n, req->index, req->offset, req->length,
dbgmsg( msgs, "got %zu bytes for block %u:%u->%u ... %d remain",
n, req->index, req->offset, req->length,
(int)( req->length - EVBUFFER_LENGTH( msgs->incoming.block ) ) );
if( EVBUFFER_LENGTH( msgs->incoming.block ) < req->length )
return READ_LATER;
/* we've got the whole block ... process it */
err = clientGotBlock( msgs, EVBUFFER_DATA(
msgs->incoming.block ), req );
err = clientGotBlock( msgs, EVBUFFER_DATA( msgs->incoming.block ), req );
/* cleanup */
evbuffer_drain( msgs->incoming.block,
EVBUFFER_LENGTH( msgs->incoming.block ) );
evbuffer_drain( msgs->incoming.block, EVBUFFER_LENGTH( msgs->incoming.block ) );
req->length = 0;
msgs->state = AWAITING_BT_LENGTH;
if( !err )
return READ_NOW;
else
{
else {
fireError( msgs, err );
return READ_ERR;
}
@ -1441,29 +1409,24 @@ readBtPiece( tr_peermsgs * msgs,
}
static int
readBtMessage( tr_peermsgs * msgs,
struct evbuffer * inbuf,
size_t inlen )
readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen )
{
uint32_t ui32;
uint32_t msglen = msgs->incoming.length;
const uint8_t id = msgs->incoming.id;
const size_t startBufLen = EVBUFFER_LENGTH( inbuf );
const int fext = tr_peerIoSupportsFEXT( msgs->io );
const tr_bool fext = tr_peerIoSupportsFEXT( msgs->io );
--msglen; /* id length */
if( inlen < msglen )
return READ_LATER;
dbgmsg( msgs, "got BT id %d, len %d, buffer size is %d", (int)id,
(int)msglen,
(int)inlen );
dbgmsg( msgs, "got BT id %d, len %d, buffer size is %zu", (int)id, (int)msglen, inlen );
if( !messageLengthIsCorrect( msgs, id, msglen + 1 ) )
{
dbgmsg( msgs, "bad packet - BT message #%d with a length of %d",
(int)id, (int)msglen );
dbgmsg( msgs, "bad packet - BT message #%d with a length of %d", (int)id, (int)msglen );
fireError( msgs, EMSGSIZE );
return READ_ERR;
}
@ -1496,8 +1459,7 @@ readBtMessage( tr_peermsgs * msgs,
case BT_HAVE:
tr_peerIoReadUint32( msgs->io, inbuf, &ui32 );
dbgmsg( msgs, "got Have: %u", ui32 );
if( tr_bitfieldAdd( msgs->info->have, ui32 ) )
{
if( tr_bitfieldAdd( msgs->info->have, ui32 ) ) {
fireError( msgs, ERANGE );
return READ_ERR;
}
@ -1510,8 +1472,7 @@ readBtMessage( tr_peermsgs * msgs,
{
dbgmsg( msgs, "got a bitfield" );
msgs->peerSentBitfield = 1;
tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits,
msglen );
tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits, msglen );
updatePeerProgress( msgs );
fireNeedReq( msgs );
break;
@ -1629,8 +1590,7 @@ readBtMessage( tr_peermsgs * msgs,
}
static void
decrementDownloadedCount( tr_peermsgs * msgs,
uint32_t byteCount )
decrementDownloadedCount( tr_peermsgs * msgs, uint32_t byteCount )
{
tr_torrent * tor = msgs->torrent;
@ -1638,18 +1598,16 @@ decrementDownloadedCount( tr_peermsgs * msgs,
}
static void
clientGotUnwantedBlock( tr_peermsgs * msgs,
const struct peer_request * req )
clientGotUnwantedBlock( tr_peermsgs * msgs, const struct peer_request * req )
{
decrementDownloadedCount( msgs, req->length );
}
static void
addPeerToBlamefield( tr_peermsgs * msgs,
uint32_t index )
addPeerToBlamefield( tr_peermsgs * msgs, uint32_t index )
{
if( !msgs->info->blame )
msgs->info->blame = tr_bitfieldNew( msgs->torrent->info.pieceCount );
msgs->info->blame = tr_bitfieldNew( msgs->torrent->info.pieceCount );
tr_bitfieldAdd( msgs->info->blame, index );
}
@ -1659,30 +1617,27 @@ clientGotBlock( tr_peermsgs * msgs,
const uint8_t * data,
const struct peer_request * req )
{
int err;
tr_torrent * tor = msgs->torrent;
int err;
tr_torrent * tor = msgs->torrent;
const tr_block_index_t block = _tr_block( tor, req->index, req->offset );
assert( msgs );
assert( req );
if( req->length != tr_torBlockCountBytes( msgs->torrent, block ) )
{
if( req->length != tr_torBlockCountBytes( msgs->torrent, block ) ) {
dbgmsg( msgs, "wrong block size -- expected %u, got %d",
tr_torBlockCountBytes( msgs->torrent, block ), req->length );
return EMSGSIZE;
}
/* save the block */
dbgmsg( msgs, "got block %u:%u->%u", req->index, req->offset,
req->length );
dbgmsg( msgs, "got block %u:%u->%u", req->index, req->offset, req->length );
/**
*** Remove the block from our `we asked for this' list
**/
if( !reqListRemove( &msgs->clientAskedFor, req ) )
{
if( !reqListRemove( &msgs->clientAskedFor, req ) ) {
clientGotUnwantedBlock( msgs, req );
dbgmsg( msgs, "we didn't ask for this message..." );
return 0;
@ -1695,8 +1650,7 @@ clientGotBlock( tr_peermsgs * msgs,
*** Error checks
**/
if( tr_cpBlockIsComplete( tor->completion, block ) )
{
if( tr_cpBlockIsComplete( tor->completion, block ) ) {
dbgmsg( msgs, "we have this block already..." );
clientGotUnwantedBlock( msgs, req );
return 0;
@ -1787,7 +1741,7 @@ fillOutputBuffer( tr_peermsgs * msgs, time_t now )
size_t bytesWritten = 0;
struct peer_request req;
const int haveMessages = EVBUFFER_LENGTH( msgs->outMessages ) != 0;
const int fext = tr_peerIoSupportsFEXT( msgs->io );
const tr_bool fext = tr_peerIoSupportsFEXT( msgs->io );
/**
*** Protocol messages
@ -1946,8 +1900,8 @@ sendBitfield( tr_peermsgs * msgs )
sizeof( uint8_t ) + field->byteCount );
tr_peerIoWriteUint8 ( msgs->io, out, BT_BITFIELD );
tr_peerIoWriteBytes ( msgs->io, out, field->bits, field->byteCount );
dbgmsg( msgs, "sending bitfield... outMessage size is now %d",
(int)EVBUFFER_LENGTH( out ) );
dbgmsg( msgs, "sending bitfield... outMessage size is now %zu",
EVBUFFER_LENGTH( out ) );
pokeBatchPeriod( msgs, IMMEDIATE_PRIORITY_INTERVAL_SECS );
for( i = 0; i < lazyCount; ++i )
@ -1959,7 +1913,7 @@ sendBitfield( tr_peermsgs * msgs )
static void
tellPeerWhatWeHave( tr_peermsgs * msgs )
{
const int fext = tr_peerIoSupportsFEXT( msgs->io );
const tr_bool fext = tr_peerIoSupportsFEXT( msgs->io );
if( fext && ( tr_cpGetStatus( msgs->torrent->completion ) == TR_SEED ) )
{

View File

@ -65,19 +65,14 @@ static void
fireNeedReq( tr_webseed * w )
{
tr_peer_event e = blankEvent;
e.eventType = TR_PEER_NEED_REQ;
publish( w, &e );
}
static void
fireClientGotBlock( tr_webseed * w,
uint32_t pieceIndex,
uint32_t offset,
uint32_t length )
fireClientGotBlock( tr_webseed * w, uint32_t pieceIndex, uint32_t offset, uint32_t length )
{
tr_peer_event e = blankEvent;
e.eventType = TR_PEER_CLIENT_GOT_BLOCK;
e.pieceIndex = pieceIndex;
e.offset = offset;
@ -86,15 +81,12 @@ fireClientGotBlock( tr_webseed * w,
}
static void
fireClientGotData( tr_webseed * w,
uint32_t length )
fireClientGotData( tr_webseed * w, uint32_t length )
{
tr_peer_event e = blankEvent;
e.eventType = TR_PEER_CLIENT_GOT_DATA;
e.length = length;
e.wasPieceData = TRUE;
publish( w, &e );
}
@ -124,75 +116,21 @@ makeURL( tr_webseed * w,
{
switch( *str )
{
case ',':
case '-':
case '.':
case '/':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case ',': case '-': case '.': case '/':
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
case 'p': case 'q': case 'r': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y': case 'z':
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
case 'P': case 'Q': case 'R': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y': case 'Z':
evbuffer_add( out, str, 1 );
break;
default:
evbuffer_add_printf( out, "%%%02X", *str );
break;
@ -314,8 +252,7 @@ tr_webseedIsActive( const tr_webseed * w )
}
int
tr_webseedGetSpeed( const tr_webseed * w,
float * setme_KiBs )
tr_webseedGetSpeed( const tr_webseed * w, float * setme_KiBs )
{
const int isActive = tr_webseedIsActive( w );