diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 2b84ed337..c44ae4b35 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -1336,6 +1336,12 @@ tr_peerMgrAddIncoming( tr_peerMgr * manager, managerUnlock( manager ); } +static int +tr_isPex( const tr_pex * pex ) +{ + return pex && tr_isAddress( &pex->addr ); +} + void tr_peerMgrAddPex( tr_peerMgr * manager, const uint8_t * torrentHash, @@ -1343,9 +1349,10 @@ tr_peerMgrAddPex( tr_peerMgr * manager, const tr_pex * pex ) { Torrent * t; - managerLock( manager ); + assert( tr_isPex( pex ) ); + t = getExistingTorrent( manager, torrentHash ); if( !tr_sessionIsAddressBlocked( t->manager->session, &pex->addr ) ) ensureAtomExists( t, &pex->addr, pex->port, pex->flags, from ); @@ -1463,11 +1470,17 @@ 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; + + assert( tr_isPex( a ) ); + assert( tr_isPex( b ) ); + + if(( i = tr_compareAddresses( &a->addr, &b->addr ))) + return i; + + if( a->port != b->port ) + return a->port < b->port ? -1 : 1; - if( i ) return i; - if( a->port < b->port ) return -1; - if( a->port > b->port ) return 1; return 0; } @@ -1529,14 +1542,14 @@ tr_peerMgrGetPeers( tr_peerMgr * manager, } } -#warning this for loop can be removed when we're sure the bug is fixed +#warning this for loop can be removed when we are sure the bug is fixed for( i=0; i