2010-02-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Return SharedHandle<LpdMessage>(new LpdMessage()) if received data
	is bad in LpdMessageReceiver.
	* src/LpdMessage.h
	* src/LpdMessageReceiver.cc
	* src/LpdMessageReceiver.h
	* src/LpdReceiveMessageCommand.cc
	* test/LpdMessageReceiverTest.cc
This commit is contained in:
Tatsuhiro Tsujikawa 2010-02-22 16:11:33 +00:00
parent 6ae5882f3c
commit 232deb47b5
6 changed files with 37 additions and 5 deletions

View file

@ -1,3 +1,13 @@
2010-02-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Return SharedHandle<LpdMessage>(new LpdMessage()) if received data
is bad in LpdMessageReceiver.
* src/LpdMessage.h
* src/LpdMessageReceiver.cc
* src/LpdMessageReceiver.h
* src/LpdReceiveMessageCommand.cc
* test/LpdMessageReceiverTest.cc
2010-02-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2010-02-23 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added --bt-lpd-interface option to specify the interface to use Added --bt-lpd-interface option to specify the interface to use

View file

@ -46,6 +46,8 @@ private:
SharedHandle<Peer> _peer; SharedHandle<Peer> _peer;
std::string _infoHash; std::string _infoHash;
public: public:
LpdMessage() {}
LpdMessage(const SharedHandle<Peer>& peer, const std::string& infoHash): LpdMessage(const SharedHandle<Peer>& peer, const std::string& infoHash):
_peer(peer), _infoHash(infoHash) {} _peer(peer), _infoHash(infoHash) {}

View file

@ -83,6 +83,7 @@ SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
HttpHeaderProcessor proc; HttpHeaderProcessor proc;
proc.update(buf, length); proc.update(buf, length);
if(!proc.eoh()) { if(!proc.eoh()) {
msg.reset(new LpdMessage());
return msg; return msg;
} }
SharedHandle<HttpHeader> header = proc.getHttpRequestHeader(); SharedHandle<HttpHeader> header = proc.getHttpRequestHeader();
@ -95,6 +96,7 @@ SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
(infoHash = util::fromHex(infoHashString)).empty() || (infoHash = util::fromHex(infoHashString)).empty() ||
port == 0) { port == 0) {
_logger->info("LPD bad request. infohash=%s", infoHashString.c_str()); _logger->info("LPD bad request. infohash=%s", infoHashString.c_str());
msg.reset(new LpdMessage());
return msg; return msg;
} }
SharedHandle<Peer> peer(new Peer(peerAddr.first, port, false)); SharedHandle<Peer> peer(new Peer(peerAddr.first, port, false));
@ -102,10 +104,12 @@ SharedHandle<LpdMessage> LpdMessageReceiver::receiveMessage()
peer->setLocalPeer(true); peer->setLocalPeer(true);
} }
msg.reset(new LpdMessage(peer, infoHash)); msg.reset(new LpdMessage(peer, infoHash));
return msg;
} catch(RecoverableException& e) { } catch(RecoverableException& e) {
_logger->info("Failed to receive LPD message.", e); _logger->info("Failed to receive LPD message.", e);
msg.reset(new LpdMessage());
return msg;
} }
return msg;
} }
} // namespace aria2 } // namespace aria2

View file

@ -60,8 +60,10 @@ public:
// No throw. // No throw.
bool init(const std::string& localAddr); bool init(const std::string& localAddr);
// Receives LPD message and process it. Returns false if message is // Receives LPD message and returns LpdMessage which contains
// not available. // sender(peer) and infohash. If no data is available on socket,
// returns SharedHandle<LpdMessage>(). If received data is bad,
// then returns SharedHandle<LpdMessage>(new LpdMessage())
SharedHandle<LpdMessage> receiveMessage(); SharedHandle<LpdMessage> receiveMessage();
SharedHandle<SocketCore> getSocket() const SharedHandle<SocketCore> getSocket() const

View file

@ -83,6 +83,10 @@ bool LpdReceiveMessageCommand::execute()
if(m.isNull()) { if(m.isNull()) {
break; break;
} }
if(m->getPeer().isNull()) {
// bad message
continue;
}
SharedHandle<BtRegistry> reg = _e->getBtRegistry(); SharedHandle<BtRegistry> reg = _e->getBtRegistry();
SharedHandle<DownloadContext> dctx = SharedHandle<DownloadContext> dctx =
reg->getDownloadContext(m->getInfoHash()); reg->getDownloadContext(m->getInfoHash());

View file

@ -60,7 +60,10 @@ void LpdMessageReceiverTest::testReceiveMessage()
sendsock->writeData(request.c_str(), request.size(), sendsock->writeData(request.c_str(), request.size(),
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT); LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
CPPUNIT_ASSERT(rcv.receiveMessage().isNull()); msg = rcv.receiveMessage();
CPPUNIT_ASSERT(!msg.isNull());
CPPUNIT_ASSERT(msg->getPeer().isNull());
CPPUNIT_ASSERT(msg->getInfoHash().empty());
// Bad port // Bad port
request = request =
@ -70,7 +73,14 @@ void LpdMessageReceiverTest::testReceiveMessage()
sendsock->writeData(request.c_str(), request.size(), sendsock->writeData(request.c_str(), request.size(),
LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT); LPD_MULTICAST_ADDR, LPD_MULTICAST_PORT);
CPPUNIT_ASSERT(rcv.receiveMessage().isNull()); msg = rcv.receiveMessage();
CPPUNIT_ASSERT(!msg.isNull());
CPPUNIT_ASSERT(msg->getPeer().isNull());
CPPUNIT_ASSERT(msg->getInfoHash().empty());
// No data available
msg = rcv.receiveMessage();
CPPUNIT_ASSERT(msg.isNull());
} }
} // namespace aria2 } // namespace aria2