From ea9e0b260f55162ec55b2c699b7186f79dc45ef9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 17 Apr 2008 03:48:56 +0000 Subject: [PATCH] (1) fix choke/unchoke error. (2) if a peer keeps trying to reconnect to us, hang up during the handshake. --- libtransmission/handshake.c | 18 +++++++++++++++--- libtransmission/peer-mgr.c | 22 ++++++++++++++++++++++ libtransmission/peer-mgr.h | 4 ++++ libtransmission/peer-msgs.c | 4 +++- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/libtransmission/handshake.c b/libtransmission/handshake.c index 193211004..4b0653947 100644 --- a/libtransmission/handshake.c +++ b/libtransmission/handshake.c @@ -26,6 +26,7 @@ #include "crypto.h" #include "handshake.h" #include "peer-io.h" +#include "peer-mgr.h" #include "torrent.h" #include "trevent.h" #include "utils.h" @@ -810,11 +811,22 @@ readCryptoProvide( tr_handshake * handshake, struct evbuffer * inbuf ) tr_sha1( req3, "req3", 4, handshake->mySecret, KEY_LEN, NULL ); for( i=0; ihandle, obfuscatedTorrentHash ); - if( tor != NULL ) { + if(( tor = tr_torrentFindFromObfuscatedHash( handshake->handle, obfuscatedTorrentHash ))) + { dbgmsg( handshake, "found the torrent; it's [%s]", tor->info.name ); tr_peerIoSetTorrentHash( handshake->io, tor->info.hash ); - } else { + if( !tr_torrentAllowsPex( tor ) && + tr_peerMgrPeerIsSeed( handshake->handle->peerMgr, + tor->info.hash, + tr_peerIoGetAddress( handshake->io, NULL ))) + { + dbgmsg( handshake, "a peer has tried to reconnect to us!" ); + tr_handshakeDone( handshake, FALSE ); + return READ_DONE; + } + } + else + { dbgmsg( handshake, "can't find that torrent..." ); tr_handshakeDone( handshake, FALSE ); return READ_DONE; diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 1a038b955..1020f5aed 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -558,6 +558,28 @@ clientIsUploadingTo( const tr_peer * peer ) return peer->peerIsInterested && !peer->peerIsChoked; } +/*** +**** +***/ + +int +tr_peerMgrPeerIsSeed( const tr_peerMgr * mgr, + const uint8_t * torrentHash, + const struct in_addr * addr ) +{ + int isSeed = FALSE; + const Torrent * t = NULL; + const struct peer_atom * atom = NULL; + + t = getExistingTorrent( (tr_peerMgr*)mgr, torrentHash ); + if( t ) + atom = getExistingAtom( t, addr ); + if( atom ) + isSeed = ( atom->flags & ADDED_F_SEED_FLAG ) != 0; + + return isSeed; +} + /*** **** Refill ***/ diff --git a/libtransmission/peer-mgr.h b/libtransmission/peer-mgr.h index 12c60558f..7662672da 100644 --- a/libtransmission/peer-mgr.h +++ b/libtransmission/peer-mgr.h @@ -41,6 +41,10 @@ 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 struct in_addr * addr ); + void tr_peerMgrAddIncoming( tr_peerMgr * manager, struct in_addr * addr, uint16_t port, diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index fd6304fa7..deb52c30c 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -1278,12 +1278,14 @@ readBtMessage( tr_peermsgs * msgs, struct evbuffer * inbuf, size_t inlen ) case BT_BITFIELD: { const int clientIsSeed = tr_torrentIsSeed( msgs->torrent ); + int peerIsSeed; dbgmsg( msgs, "got a bitfield" ); msgs->peerSentBitfield = 1; tr_peerIoReadBytes( msgs->io, inbuf, msgs->info->have->bits, msglen ); updatePeerProgress( msgs ); maybeSendFastAllowedSet( msgs ); - tr_peerMsgsSetChoke( msgs, !clientIsSeed || (msgs->info->progress<1.0) ); + peerIsSeed = msgs->info->progress >= 1.0; + tr_peerMsgsSetChoke( msgs, clientIsSeed && peerIsSeed ); fireNeedReq( msgs ); break; }