diff --git a/libtransmission/peer-mgr.c b/libtransmission/peer-mgr.c index 3cca063bd..09cbe033f 100644 --- a/libtransmission/peer-mgr.c +++ b/libtransmission/peer-mgr.c @@ -810,6 +810,7 @@ refillPulse( void * vtorrent ) const uint32_t begin = (block * tor->blockSize) - (index * tor->info.pieceSize); const uint32_t length = tr_torBlockCountBytes( tor, (int)block ); + assert( tr_torrentReqIsValid( tor, index, begin, length ) ); assert( _tr_block( tor, index, begin ) == (int)block ); assert( begin < (uint32_t)tr_torPieceCountBytes( tor, (int)index ) ); assert( (begin + length) <= (uint32_t)tr_torPieceCountBytes( tor, (int)index ) ); diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index dc5889e30..123c7552d 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -60,9 +60,6 @@ enum TR_LTEP_PEX = 1, - /* drop requests who want too much */ - MAX_REQUEST_BYTE_COUNT = (16 * 1024), - MIN_CHOKE_PERIOD_SEC = (10), /* idle seconds before we send a keepalive */ @@ -675,31 +672,7 @@ reqIsValid( const tr_peermsgs * msgs, uint32_t offset, uint32_t length ) { - const tr_torrent * tor = msgs->torrent; - int err = 0; - - if( index >= (uint32_t) tor->info.pieceCount ) - err = 1; - else if ( (int)offset >= tr_torPieceCountBytes( tor, (int)index ) ) - err = 2; - else if( length > MAX_REQUEST_BYTE_COUNT ) - err = 3; - else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize ) - err = 4; - - if( err ) - { - fprintf( stderr, "(ticket #751) err is %d\n", err ); - fprintf( stderr, "(ticket #751) req.index is %"PRIu32"\n", index ); - fprintf( stderr, "(ticket #751) req.offset is %"PRIu32"\n", offset ); - fprintf( stderr, "(ticket #751) req.length is %"PRIu32"\n", length ); - fprintf( stderr, "(ticket #751) tor->info.totalSize is %"PRIu64"\n", tor->info.totalSize ); - fprintf( stderr, "(ticket #751) tor->info.pieceCount is %d\n", tor->info.pieceCount ); - fprintf( stderr, "(ticket #751) tr_torPieceCountBytes is %d\n", tr_torPieceCountBytes( tor, (int)index ) ); - fprintf( stderr, "(ticket #751) tr_pieceOffset is %"PRIu64"\n", tr_pieceOffset( tor, index, offset, length ) ); - } - - return !err; + return tr_torrentReqIsValid( msgs->torrent, index, offset, length ); } static int diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index db5506e36..16c89b4e1 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -1281,13 +1281,48 @@ tr_torrentGetMaxConnectedPeers( const tr_torrent * tor ) **** ***/ -int _tr_block( const tr_torrent * tor, int index, int begin ) +int +_tr_block( const tr_torrent * tor, int index, int begin ) { const tr_info * inf = &tor->info; return index * ( inf->pieceSize / tor->blockSize ) + begin / tor->blockSize; } +int +tr_torrentReqIsValid( const tr_torrent * tor, + uint32_t index, + uint32_t offset, + uint32_t length ) +{ + static const uint32_t MAX_REQUEST_BYTE_COUNT = (16 * 1024); + int err = 0; + + if( index >= (uint32_t) tor->info.pieceCount ) + err = 1; + else if ( (int)offset >= tr_torPieceCountBytes( tor, (int)index ) ) + err = 2; + else if( length > MAX_REQUEST_BYTE_COUNT ) + err = 3; + else if( tr_pieceOffset( tor, index, offset, length ) > tor->info.totalSize ) + err = 4; + + if( err ) + { + fprintf( stderr, "(ticket #751) err is %d\n", err ); + fprintf( stderr, "(ticket #751) req.index is %"PRIu32"\n", index ); + fprintf( stderr, "(ticket #751) req.offset is %"PRIu32"\n", offset ); + fprintf( stderr, "(ticket #751) req.length is %"PRIu32"\n", length ); + fprintf( stderr, "(ticket #751) tor->info.totalSize is %"PRIu64"\n", tor->info.totalSize ); + fprintf( stderr, "(ticket #751) tor->info.pieceCount is %d\n", tor->info.pieceCount ); + fprintf( stderr, "(ticket #751) tr_torPieceCountBytes is %d\n", tr_torPieceCountBytes( tor, (int)index ) ); + fprintf( stderr, "(ticket #751) tr_pieceOffset is %"PRIu64"\n", tr_pieceOffset( tor, index, offset, length ) ); + } + + return !err; +} + + uint64_t tr_pieceOffset( const tr_torrent * tor, int index, int begin, int length ) { diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 79440d681..d92dc78db 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -1,5 +1,5 @@ /****************************************************************************** - * $Id:$ + * $Id$ * * Copyright (c) 2005-2008 Transmission authors and contributors * @@ -75,6 +75,11 @@ int tr_torrentAllowsPex( const tr_torrent * ); #define tr_block(a,b) _tr_block(tor,a,b) int _tr_block( const tr_torrent * tor, int index, int begin ); +int tr_torrentReqIsValid( const tr_torrent * tor, + uint32_t index, + uint32_t offset, + uint32_t length ); + uint64_t tr_pieceOffset( const tr_torrent * tor, int index, int begin, int length ); void tr_torrentInitFilePriority( tr_torrent * tor,