mirror of https://github.com/aria2/aria2
2008-03-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Rewritten the functions for binding port. * src/DHTConnectionImpl.{h, cc} * src/PeerListenCommand.{h, cc} * src/DHTSetup.cc * src/BtSetup.cc * test/DHTConnectionImplTest.ccpull/1/head
parent
032c7c2808
commit
7f40794931
|
@ -1,3 +1,12 @@
|
||||||
|
2008-03-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
|
Rewritten the functions for binding port.
|
||||||
|
* src/DHTConnectionImpl.{h, cc}
|
||||||
|
* src/PeerListenCommand.{h, cc}
|
||||||
|
* src/DHTSetup.cc
|
||||||
|
* src/BtSetup.cc
|
||||||
|
* test/DHTConnectionImplTest.cc
|
||||||
|
|
||||||
2008-03-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
2008-03-09 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
|
||||||
|
|
||||||
type cleanup for allmost all remaining files.
|
type cleanup for allmost all remaining files.
|
||||||
|
|
|
@ -115,13 +115,13 @@ Commands BtSetup::setup(RequestGroup* requestGroup,
|
||||||
if(PeerListenCommand::getNumInstance() == 0) {
|
if(PeerListenCommand::getNumInstance() == 0) {
|
||||||
PeerListenCommand* listenCommand = PeerListenCommand::getInstance(e);
|
PeerListenCommand* listenCommand = PeerListenCommand::getInstance(e);
|
||||||
IntSequence seq = Util::parseIntRange(option->get(PREF_LISTEN_PORT));
|
IntSequence seq = Util::parseIntRange(option->get(PREF_LISTEN_PORT));
|
||||||
int32_t port = listenCommand->bindPort(seq);
|
uint16_t port;
|
||||||
if(port == -1) {
|
if(listenCommand->bindPort(port, seq)) {
|
||||||
_logger->error(_("Errors occurred while binding port.\n"));
|
|
||||||
delete listenCommand;
|
|
||||||
} else {
|
|
||||||
BT_RUNTIME(btContext)->setListenPort(port);
|
BT_RUNTIME(btContext)->setListenPort(port);
|
||||||
commands.push_back(listenCommand);
|
commands.push_back(listenCommand);
|
||||||
|
} else {
|
||||||
|
_logger->error(_("Errors occurred while binding port.\n"));
|
||||||
|
delete listenCommand;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -47,30 +47,35 @@ DHTConnectionImpl::DHTConnectionImpl():_socket(new SocketCore(SOCK_DGRAM)),
|
||||||
|
|
||||||
DHTConnectionImpl::~DHTConnectionImpl() {}
|
DHTConnectionImpl::~DHTConnectionImpl() {}
|
||||||
|
|
||||||
uint16_t DHTConnectionImpl::bind(IntSequence& ports)
|
bool DHTConnectionImpl::bind(uint16_t& port, IntSequence& ports)
|
||||||
{
|
{
|
||||||
while(ports.hasNext()) {
|
while(ports.hasNext()) {
|
||||||
uint16_t port = bind(ports.next());
|
int sport = ports.next();
|
||||||
if(port > 0) {
|
if(!(0 < sport && 0 <= UINT16_MAX)) {
|
||||||
return port;
|
continue;
|
||||||
|
}
|
||||||
|
port = sport;
|
||||||
|
if(bind(port)) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t DHTConnectionImpl::bind(uint16_t port)
|
bool DHTConnectionImpl::bind(uint16_t& port)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
_socket->bind(port);
|
_socket->bind(port);
|
||||||
std::pair<std::string, uint16_t> svaddr;
|
std::pair<std::string, uint16_t> svaddr;
|
||||||
_socket->getAddrInfo(svaddr);
|
_socket->getAddrInfo(svaddr);
|
||||||
|
port = svaddr.second;
|
||||||
_logger->info("Bind socket for DHT. port=%u", port);
|
_logger->info("Bind socket for DHT. port=%u", port);
|
||||||
return svaddr.second;
|
return true;
|
||||||
} catch(RecoverableException* e) {
|
} catch(RecoverableException* e) {
|
||||||
_logger->error("Failed to bind for DHT. port=%u", e, port);
|
_logger->error("Failed to bind for DHT. port=%u", e, port);
|
||||||
delete e;
|
delete e;
|
||||||
}
|
}
|
||||||
return 0;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ssize_t DHTConnectionImpl::receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port)
|
ssize_t DHTConnectionImpl::receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port)
|
||||||
|
|
|
@ -54,9 +54,22 @@ public:
|
||||||
|
|
||||||
virtual ~DHTConnectionImpl();
|
virtual ~DHTConnectionImpl();
|
||||||
|
|
||||||
uint16_t bind(IntSequence& ports);
|
/**
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
bool bind(uint16_t& port, IntSequence& ports);
|
||||||
|
|
||||||
uint16_t bind(uint16_t port);
|
/**
|
||||||
|
* Binds port. The port number specified by port is used to bind.
|
||||||
|
* If successful, the binded port is assigned to port and returns true.
|
||||||
|
* Otherwise return false and port is undefined in this case.
|
||||||
|
*
|
||||||
|
* If you want to bind arbitrary port, give 0 as port and if successful,
|
||||||
|
* the binded port is assigned to port.
|
||||||
|
*/
|
||||||
|
bool bind(uint16_t& port);
|
||||||
|
|
||||||
virtual ssize_t receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port);
|
virtual ssize_t receiveMessage(unsigned char* data, size_t len, std::string& host, uint16_t& port);
|
||||||
|
|
||||||
|
|
|
@ -105,8 +105,8 @@ Commands DHTSetup::setup(DownloadEngine* e, const Option* option)
|
||||||
SharedHandle<DHTConnectionImpl> connection = new DHTConnectionImpl();
|
SharedHandle<DHTConnectionImpl> connection = new DHTConnectionImpl();
|
||||||
{
|
{
|
||||||
IntSequence seq = Util::parseIntRange(option->get(PREF_DHT_LISTEN_PORT));
|
IntSequence seq = Util::parseIntRange(option->get(PREF_DHT_LISTEN_PORT));
|
||||||
uint16_t port = connection->bind(seq);
|
uint16_t port;
|
||||||
if(port == 0) {
|
if(!connection->bind(port, seq)) {
|
||||||
throw new DlAbortEx("Error occurred while binding port for DHT");
|
throw new DlAbortEx("Error occurred while binding port for DHT");
|
||||||
}
|
}
|
||||||
localNode->setPort(port);
|
localNode->setPort(port);
|
||||||
|
|
|
@ -63,25 +63,27 @@ PeerListenCommand::~PeerListenCommand()
|
||||||
--__numInstance;
|
--__numInstance;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t PeerListenCommand::bindPort(IntSequence& seq)
|
bool PeerListenCommand::bindPort(uint16_t& port, IntSequence& seq)
|
||||||
{
|
{
|
||||||
while(seq.hasNext()) {
|
while(seq.hasNext()) {
|
||||||
int32_t port = seq.next();
|
int sport = seq.next();
|
||||||
|
if(!(0 < sport && sport <= UINT16_MAX)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
port = sport;
|
||||||
try {
|
try {
|
||||||
socket->bind(port);
|
socket->bind(port);
|
||||||
socket->beginListen();
|
socket->beginListen();
|
||||||
socket->setNonBlockingMode();
|
socket->setNonBlockingMode();
|
||||||
logger->info(MSG_LISTENING_PORT,
|
logger->info(MSG_LISTENING_PORT, cuid, port);
|
||||||
cuid, port);
|
return true;
|
||||||
return port;
|
|
||||||
} catch(RecoverableException* ex) {
|
} catch(RecoverableException* ex) {
|
||||||
logger->error(MSG_BIND_FAILURE,
|
logger->error(MSG_BIND_FAILURE, ex, cuid, port);
|
||||||
ex, cuid, port);
|
|
||||||
socket->closeConnection();
|
socket->closeConnection();
|
||||||
delete ex;
|
delete ex;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerListenCommand::execute() {
|
bool PeerListenCommand::execute() {
|
||||||
|
|
|
@ -61,7 +61,11 @@ public:
|
||||||
|
|
||||||
virtual bool execute();
|
virtual bool execute();
|
||||||
|
|
||||||
int32_t bindPort(IntSequence& seq);
|
/**
|
||||||
|
* Binds port. If successful, the binded port number is assinged to port and
|
||||||
|
* returns true, otherwise port is undefined and returns false.
|
||||||
|
*/
|
||||||
|
bool bindPort(uint16_t& port, IntSequence& seq);
|
||||||
|
|
||||||
void setLowestSpeedLimit(unsigned int speed)
|
void setLowestSpeedLimit(unsigned int speed)
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,9 +25,12 @@ void DHTConnectionImplTest::testWriteAndReadData()
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
DHTConnectionImpl con1;
|
DHTConnectionImpl con1;
|
||||||
/*uint16_t con1port =*/ con1.bind(0);
|
uint16_t con1port = 0;
|
||||||
|
CPPUNIT_ASSERT(con1.bind(con1port));
|
||||||
|
|
||||||
DHTConnectionImpl con2;
|
DHTConnectionImpl con2;
|
||||||
uint16_t con2port = con2.bind(0);
|
uint16_t con2port = 0;
|
||||||
|
CPPUNIT_ASSERT(con2.bind(con2port));
|
||||||
|
|
||||||
std::string message1 = "hello world.";
|
std::string message1 = "hello world.";
|
||||||
con1.sendMessage(reinterpret_cast<const unsigned char*>(message1.c_str()),
|
con1.sendMessage(reinterpret_cast<const unsigned char*>(message1.c_str()),
|
||||||
|
@ -43,9 +46,10 @@ void DHTConnectionImplTest::testWriteAndReadData()
|
||||||
std::string(&readbuffer[0], &readbuffer[rlength]));
|
std::string(&readbuffer[0], &readbuffer[rlength]));
|
||||||
}
|
}
|
||||||
} catch(Exception* e) {
|
} catch(Exception* e) {
|
||||||
|
std::string m = e->getMsg();
|
||||||
std::cerr << *e << std::endl;
|
std::cerr << *e << std::endl;
|
||||||
delete e;
|
delete e;
|
||||||
CPPUNIT_FAIL("exception thrown");
|
CPPUNIT_FAIL(m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue