diff options
| author | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-01-30 16:24:35 +0000 |
|---|---|---|
| committer | Tatsuhiro Tsujikawa <tatsuhiro.t@gmail.com> | 2009-01-30 16:24:35 +0000 |
| commit | dbc8f687c17a634d7f66bf70101708f08d460acf (patch) | |
| tree | ad3d81b7b256281edee68347b382258467b4a37c /src/DHTConnectionImpl.cc | |
| parent | e5c4b24454a37ad6eb5eae3a0685115e4078a06a (diff) | |
2009-01-31 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Choose port for BitTorrent/DHT randomly from the specified
range.
* src/DHTConnectionImpl.cc
* src/PeerListenCommand.cc
Diffstat (limited to 'src/DHTConnectionImpl.cc')
| -rw-r--r-- | src/DHTConnectionImpl.cc | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/src/DHTConnectionImpl.cc b/src/DHTConnectionImpl.cc index aa7638f8..35ff0f37 100644 --- a/src/DHTConnectionImpl.cc +++ b/src/DHTConnectionImpl.cc @@ -35,12 +35,15 @@ #include "DHTConnectionImpl.h" #include <utility> +#include <deque> +#include <algorithm> #include "LogFactory.h" #include "Logger.h" #include "RecoverableException.h" #include "Util.h" #include "Socket.h" +#include "SimpleRandomizer.h" namespace aria2 { @@ -51,12 +54,16 @@ DHTConnectionImpl::~DHTConnectionImpl() {} bool DHTConnectionImpl::bind(uint16_t& port, IntSequence& ports) { - while(ports.hasNext()) { - int sport = ports.next(); - if(!(0 < sport && 0 <= UINT16_MAX)) { + std::deque<int32_t> randPorts = ports.flush(); + std::random_shuffle(randPorts.begin(), randPorts.end(), + *SimpleRandomizer::getInstance().get()); + + for(std::deque<int32_t>::const_iterator portItr = randPorts.begin(); + portItr != randPorts.end(); ++portItr) { + if(!(0 < (*portItr) && (*portItr) <= UINT16_MAX)) { continue; } - port = sport; + port = (*portItr); if(bind(port)) { return true; } |
