From d390c4c871cf1236495638615cc267e304ea41fb Mon Sep 17 00:00:00 2001 From: Josh Elsasser Date: Mon, 26 Mar 2007 19:19:33 +0000 Subject: [PATCH] Don't save or load cached peers for private torrents. --- libtransmission/fastresume.h | 29 +++++++++++++++++------------ libtransmission/internal.h | 2 ++ libtransmission/peer.c | 15 +++++++++++++++ libtransmission/peeraz.h | 1 + libtransmission/peerext.h | 15 ++++++--------- libtransmission/torrent.c | 31 ++++++++++++++++++++++++++++++- libtransmission/transmission.h | 14 ++++++++++++++ 7 files changed, 85 insertions(+), 22 deletions(-) diff --git a/libtransmission/fastresume.h b/libtransmission/fastresume.h index bb94ba4fa..8f070b028 100644 --- a/libtransmission/fastresume.h +++ b/libtransmission/fastresume.h @@ -171,11 +171,14 @@ static void fastResumeSave( tr_io_t * io ) total = tor->uploadedCur + tor->uploadedPrev; fastResumeWriteData( FR_ID_UPLOADED, &total, 8, 1, file ); - /* Write IPs and ports of connectable peers, if any */ - if( ( size = tr_peerGetConnectable( tor, &buf ) ) > 0 ) + if( !( TR_FLAG_PRIVATE & tor->info.flags ) ) { - fastResumeWriteData( FR_ID_PEERS, buf, size, 1, file ); - free( buf ); + /* Write IPs and ports of connectable peers, if any */ + if( ( size = tr_peerGetConnectable( tor, &buf ) ) > 0 ) + { + fastResumeWriteData( FR_ID_PEERS, buf, size, 1, file ); + free( buf ); + } } fclose( file ); @@ -374,18 +377,20 @@ static int fastResumeLoad( tr_io_t * io ) break; case FR_ID_PEERS: - { - uint8_t * buf = malloc( len ); - if( 1 != fread( buf, len, 1, file ) ) + if( !( TR_FLAG_PRIVATE & tor->info.flags ) ) { + uint8_t * buf = malloc( len ); + if( 1 != fread( buf, len, 1, file ) ) + { + free( buf ); + fclose( file ); + return 1; + } + tr_torrentAddCompact( tor, TR_PEER_FROM_CACHE, + buf, len / 6 ); free( buf ); - fclose( file ); - return 1; } - tr_torrentAddCompact( tor, TR_PEER_FROM_CACHE, buf, len / 6 ); - free( buf ); continue; - } default: break; diff --git a/libtransmission/internal.h b/libtransmission/internal.h index 1d1c3e129..f7b2f6bea 100644 --- a/libtransmission/internal.h +++ b/libtransmission/internal.h @@ -201,6 +201,8 @@ struct tr_torrent_s uint64_t uploadedCur; uint64_t uploadedPrev; + uint8_t pexDisabled; + tr_stat_t stats[2]; int statCur; diff --git a/libtransmission/peer.c b/libtransmission/peer.c index 9097e211b..9e35c163d 100644 --- a/libtransmission/peer.c +++ b/libtransmission/peer.c @@ -223,7 +223,22 @@ void tr_peerDestroy( tr_peer_t * peer ) void tr_peerSetPrivate( tr_peer_t * peer, int private ) { + if( peer->private == private ) + { + return; + } + peer->private = private; + + if( !private ) + { + peer->lastPex = 0; + } + + if( EXTENDED_HANDSHAKE == peer->extStatus ) + { + sendExtended( peer->tor, peer, EXTENDED_HANDSHAKE_ID ); + } } void tr_peerSetTorrent( tr_peer_t * peer, tr_torrent_t * tor ) diff --git a/libtransmission/peeraz.h b/libtransmission/peeraz.h index 0ef0719f6..d1368c78e 100644 --- a/libtransmission/peeraz.h +++ b/libtransmission/peeraz.h @@ -151,6 +151,7 @@ makeAZHandshake( tr_torrent_t * tor, tr_peer_t * peer, int * buflen ) { continue; } + /* XXX azureus sucks, we can't enable or disable pex after handshake */ if( AZ_MSG_AZ_PEER_EXCHANGE == azmsgId( idx ) && peer->private ) { /* no point in saying we can do pex if the torrent is private */ diff --git a/libtransmission/peerext.h b/libtransmission/peerext.h index 3ecdd79e7..d6d777ade 100644 --- a/libtransmission/peerext.h +++ b/libtransmission/peerext.h @@ -127,16 +127,13 @@ makeExtendedHandshake( tr_torrent_t * tor, tr_peer_t * peer, int * len ) /* create dict of supported extended messages */ tr_bencInit( msgsval, TYPE_DICT ); - if( !peer->private ) + if( tr_bencDictAppendNofree( msgsval, "ut_pex", &pexval, NULL ) ) { - /* for public torrents advertise utorrent pex message */ - if( tr_bencDictAppendNofree( msgsval, "ut_pex", &pexval, NULL ) ) - { - tr_bencFree( &val ); - return NULL; - } - tr_bencInitInt( pexval, EXTENDED_PEX_ID ); + tr_bencFree( &val ); + return NULL; } + /* for public torrents advertise utorrent pex message */ + tr_bencInitInt( pexval, ( peer->private ? 0 : EXTENDED_PEX_ID ) ); /* our listening port */ if( 0 < tor->publicPort ) @@ -232,7 +229,7 @@ parseExtendedHandshake( tr_peer_t * peer, uint8_t * buf, int len ) if( NULL != sub && TYPE_INT == sub->type ) { peer->pexStatus = 0; - if( !peer->private && 0x0 < sub->val.i && 0xff >= sub->val.i ) + if( 0x0 < sub->val.i && 0xff >= sub->val.i ) { peer->pexStatus = sub->val.i; } diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 2ed6ef51b..10d2c4f66 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -137,6 +137,8 @@ static tr_torrent_t * torrentRealInit( tr_handle_t * h, tr_torrent_t * tor, "%%%02x", inf->hash[i] ); } + tor->pexDisabled = 0; + /* Block size: usually 16 ko, or less if we have to */ tor->blockSize = MIN( inf->pieceSize, 1 << 14 ); tor->blockCount = ( inf->totalSize + tor->blockSize - 1 ) / @@ -270,6 +272,32 @@ static void torrentReallyStop( tr_torrent_t * tor ) tr_lockUnlock( &tor->lock ); } +void tr_torrentDisablePex( tr_torrent_t * tor, int disable ) +{ + int ii; + + if( TR_FLAG_PRIVATE & tor->info.flags ) + { + return; + } + + tr_lockLock( &tor->lock ); + + if( tor->pexDisabled == disable ) + { + tr_lockUnlock( &tor->lock ); + return; + } + + tor->pexDisabled = disable; + for( ii = 0; ii < tor->peerCount; ii++ ) + { + tr_peerSetPrivate( tor->peers[ii], disable ); + } + + tr_lockUnlock( &tor->lock ); +} + int tr_getFinished( tr_torrent_t * tor ) { if( tor->finished ) @@ -635,7 +663,8 @@ void tr_torrentAttachPeer( tr_torrent_t * tor, tr_peer_t * peer ) } } - tr_peerSetPrivate( peer, tor->info.flags & TR_FLAG_PRIVATE ); + tr_peerSetPrivate( peer, tor->info.flags & TR_FLAG_PRIVATE || + tor->pexDisabled ); tr_peerSetTorrent( peer, tor ); tor->peers[tor->peerCount++] = peer; } diff --git a/libtransmission/transmission.h b/libtransmission/transmission.h index a60a7c16d..cb8839eb0 100644 --- a/libtransmission/transmission.h +++ b/libtransmission/transmission.h @@ -223,6 +223,20 @@ tr_torrent_t * tr_torrentInit( tr_handle_t *, const char * path, tr_torrent_t * tr_torrentInitSaved( tr_handle_t *, const char * hashStr, int flags, int * error ); +/*********************************************************************** + * tr_torrentDisablePex + *********************************************************************** + * Disable or enable peer exchange for this torrent. Peer exchange is + * enabled by default, except for private torrents where pex is + * disabled and cannot be enabled. + **********************************************************************/ +void tr_torrentDisablePex( tr_torrent_t *, int disable ); + +/*********************************************************************** + * tr_torrentScrape + *********************************************************************** + * Return torrent metainfo. + **********************************************************************/ typedef struct tr_info_s tr_info_t; tr_info_t * tr_torrentInfo( tr_torrent_t * );