diff --git a/src/DHTConnectionImpl.cc b/src/DHTConnectionImpl.cc index 104a9d3f..1306a5bf 100644 --- a/src/DHTConnectionImpl.cc +++ b/src/DHTConnectionImpl.cc @@ -55,18 +55,17 @@ DHTConnectionImpl::DHTConnectionImpl(int family) DHTConnectionImpl::~DHTConnectionImpl() {} bool DHTConnectionImpl::bind -(uint16_t& port, const std::string& addr, IntSequence& ports) +(uint16_t& port, const std::string& addr, SegList& sgl) { - std::vector randPorts = ports.flush(); - std::random_shuffle(randPorts.begin(), randPorts.end(), + std::vector ports; + while(sgl.hasNext()) { + ports.push_back(sgl.next()); + } + std::random_shuffle(ports.begin(), ports.end(), *SimpleRandomizer::getInstance().get()); - - for(std::vector::const_iterator portItr = randPorts.begin(), - eoi = randPorts.end(); portItr != eoi; ++portItr) { - if(!(0 < (*portItr) && (*portItr) <= 65535)) { - continue; - } - port = (*portItr); + for(std::vector::const_iterator i = ports.begin(), + eoi = ports.end(); i != eoi; ++i) { + port = *i; if(bind(port, addr)) { return true; } diff --git a/src/DHTConnectionImpl.h b/src/DHTConnectionImpl.h index c05d184c..a8da33b5 100644 --- a/src/DHTConnectionImpl.h +++ b/src/DHTConnectionImpl.h @@ -37,7 +37,7 @@ #include "DHTConnection.h" #include "SharedHandle.h" -#include "IntSequence.h" +#include "SegList.h" namespace aria2 { @@ -54,12 +54,13 @@ public: virtual ~DHTConnectionImpl(); /** - * Binds port. All number in ports are tried. If successful, the - * binded port is assigned to port and returns true. Otherwise - * return false and port is undefined in this case. If non-empty - * string addr is given, the socket is associated to the address. + * Binds port. All number in sgl are tried. All numbers in sgl must + * be in range [1, 65535], inclusive. If successful, the binded port + * is assigned to port and returns true. Otherwise return false and + * port is undefined in this case. If non-empty string addr is + * given, the socket is associated to the address. */ - bool bind(uint16_t& port, const std::string& addr, IntSequence& ports); + bool bind(uint16_t& port, const std::string& addr, SegList& sgl); /** * Binds port. The port number specified by port is used to bind. diff --git a/src/DHTSetup.cc b/src/DHTSetup.cc index 0a434344..17bd035d 100644 --- a/src/DHTSetup.cc +++ b/src/DHTSetup.cc @@ -112,13 +112,14 @@ void DHTSetup::setup SharedHandle connection(new DHTConnectionImpl(family)); { - IntSequence seq = - util::parseIntRange(e->getOption()->get(PREF_DHT_LISTEN_PORT)); + SegList sgl; + util::parseIntSegments(sgl, e->getOption()->get(PREF_DHT_LISTEN_PORT)); + sgl.normalize(); uint16_t port; const std::string& addr = e->getOption()->get(family == AF_INET?PREF_DHT_LISTEN_ADDR: PREF_DHT_LISTEN_ADDR6); - if(!connection->bind(port, addr, seq)) { + if(!connection->bind(port, addr, sgl)) { throw DL_ABORT_EX("Error occurred while binding port for DHT"); } localNode->setPort(port); diff --git a/src/OptionHandlerImpl.cc b/src/OptionHandlerImpl.cc index 0571233f..0d74c050 100644 --- a/src/OptionHandlerImpl.cc +++ b/src/OptionHandlerImpl.cc @@ -57,6 +57,7 @@ #include "a2io.h" #include "LogFactory.h" #include "uri.h" +#include "SegList.h" #ifdef ENABLE_MESSAGE_DIGEST # include "MessageDigest.h" #endif // ENABLE_MESSAGE_DIGEST @@ -113,9 +114,11 @@ IntegerRangeOptionHandler::~IntegerRangeOptionHandler() {} void IntegerRangeOptionHandler::parseArg (Option& option, const std::string& optarg) { - IntSequence seq = util::parseIntRange(optarg); - while(seq.hasNext()) { - int32_t v = seq.next(); + SegList sgl; + util::parseIntSegments(sgl, optarg); + sgl.normalize(); + while(sgl.hasNext()) { + int v = sgl.next(); if(v < min_ || max_ < v) { std::string msg = pref_->k; strappend(msg, " ", _("must be between %s and %s."));