From 643716c6f05f893ca6645ec029e26dcf54b7d40b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 5 May 2008 18:54:19 +0000 Subject: [PATCH] randomize the announce addresses within multitracker tiers --- libtransmission/torrent.c | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/libtransmission/torrent.c b/libtransmission/torrent.c index 3f00a9ca9..9323d565c 100644 --- a/libtransmission/torrent.c +++ b/libtransmission/torrent.c @@ -28,6 +28,7 @@ #include #include /* memcmp */ +#include /* qsort */ #include "transmission.h" #include "bencode.h" @@ -289,6 +290,41 @@ tr_torrentInitFilePieces( tr_torrent * tor ) tor->info.pieces[pp].priority = calculatePiecePriority( tor, pp, -1 ); } +struct RandomTracker +{ + tr_tracker_info tracker; + int random_value; +}; + +static int +compareRandomTracker( const void * va, const void * vb ) +{ + const struct RandomTracker * a = va; + const struct RandomTracker * b = vb; + + if( a->tracker.tier != b->tracker.tier ) + return a->tracker.tier - b->tracker.tier; + + return a->random_value - b->random_value; +} + +static void +randomizeTiers( tr_info * info ) +{ + int i; + const int n = info->trackerCount; + struct RandomTracker * r; + r = tr_new0( struct RandomTracker, n ); + for( i=0; itrackers[i]; + r[i].random_value = tr_rand( INT_MAX ); + } + qsort( r, n, sizeof( struct RandomTracker ), compareRandomTracker ); + for( i=0; itrackers[i] = r[i].tracker; + tr_free( r ); +} + static void torrentRealInit( tr_handle * h, tr_torrent * tor, @@ -303,6 +339,8 @@ torrentRealInit( tr_handle * h, tor->handle = h; + randomizeTiers( info ); + /** * Decide on a block size. constraints: * (1) most clients decline requests over 16 KiB