2010-08-05 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Added support for peers6 key in tracker response.  Added added6,
	added6.f and dropped6 in ut_pex.
	* src/BtAnnounce.cc
	* src/BtAnnounce.h
	* src/DefaultBtAnnounce.cc
	* src/UTPexExtensionMessage.cc
	* src/UTPexExtensionMessage.h
	* test/DefaultBtAnnounceTest.cc
	* test/UTPexExtensionMessageTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2010-08-05 11:54:44 +00:00
parent cba1283aae
commit c685ea9de9
8 changed files with 131 additions and 19 deletions

View File

@ -1,3 +1,15 @@
2010-08-05 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added support for peers6 key in tracker response. Added added6,
added6.f and dropped6 in ut_pex.
* src/BtAnnounce.cc
* src/BtAnnounce.h
* src/DefaultBtAnnounce.cc
* src/UTPexExtensionMessage.cc
* src/UTPexExtensionMessage.h
* test/DefaultBtAnnounceTest.cc
* test/UTPexExtensionMessageTest.cc
2010-08-05 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added AI_V4MAPPED to DEFAULT_AI_FLAGS

View File

@ -52,4 +52,6 @@ const std::string BtAnnounce::INCOMPLETE("incomplete");
const std::string BtAnnounce::PEERS("peers");
const std::string BtAnnounce::PEERS6("peers6");
} // namespace aria2

View File

@ -123,6 +123,8 @@ public:
static const std::string PEERS;
static const std::string PEERS6;
static const time_t DEFAULT_ANNOUNCE_INTERVAL = 120;
};

View File

@ -280,6 +280,16 @@ DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse,
peerStorage_->addPeer(peers);
}
}
const SharedHandle<ValueBase>& peer6Data = dict->get(BtAnnounce::PEERS6);
if(peer6Data.isNull()) {
logger_->info("No peers6 received.");
} else {
if(!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) {
std::vector<SharedHandle<Peer> > peers;
bittorrent::extractPeer(peer6Data, AF_INET6, std::back_inserter(peers));
peerStorage_->addPeer(peers);
}
}
}
bool DefaultBtAnnounce::noMoreAnnounce() {

View File

@ -58,24 +58,41 @@ UTPexExtensionMessage::~UTPexExtensionMessage() {}
std::string UTPexExtensionMessage::getPayload()
{
std::pair<std::string, std::string> freshPeerPair =
std::pair<std::pair<std::string, std::string>,
std::pair<std::string, std::string> > freshPeerPair =
createCompactPeerListAndFlag(freshPeers_);
std::pair<std::string, std::string> droppedPeerPair =
std::pair<std::pair<std::string, std::string>,
std::pair<std::string, std::string> > droppedPeerPair =
createCompactPeerListAndFlag(droppedPeers_);
Dict dict;
dict.put("added", freshPeerPair.first);
dict.put("added.f", freshPeerPair.second);
dict.put("dropped", droppedPeerPair.first);
if(!freshPeerPair.first.first.empty()) {
dict.put("added", freshPeerPair.first.first);
dict.put("added.f", freshPeerPair.first.second);
}
if(!droppedPeerPair.first.first.empty()) {
dict.put("dropped", droppedPeerPair.first.first);
}
if(!freshPeerPair.second.first.empty()) {
dict.put("added6", freshPeerPair.second.first);
dict.put("added6.f", freshPeerPair.second.second);
}
if(!droppedPeerPair.second.first.empty()) {
dict.put("dropped6", droppedPeerPair.second.first);
}
return bencode2::encode(&dict);
}
std::pair<std::string, std::string>
std::pair<std::pair<std::string, std::string>,
std::pair<std::string, std::string> >
UTPexExtensionMessage::createCompactPeerListAndFlag
(const std::vector<SharedHandle<Peer> >& peers)
{
std::string addrstring;
std::string flagstring;
std::string addrstring6;
std::string flagstring6;
for(std::vector<SharedHandle<Peer> >::const_iterator itr = peers.begin(),
eoi = peers.end(); itr != eoi; ++itr) {
unsigned char compact[COMPACT_LEN_IPV6];
@ -84,9 +101,13 @@ UTPexExtensionMessage::createCompactPeerListAndFlag
if(compactlen == COMPACT_LEN_IPV4) {
addrstring.append(&compact[0], &compact[compactlen]);
flagstring += (*itr)->isSeeder() ? 0x02 : 0x00;
} else if(compactlen == COMPACT_LEN_IPV6) {
addrstring6.append(&compact[0], &compact[compactlen]);
flagstring6 += (*itr)->isSeeder() ? 0x02 : 0x00;
}
}
return std::pair<std::string, std::string>(addrstring, flagstring);
return std::make_pair(std::make_pair(addrstring, flagstring),
std::make_pair(addrstring6, flagstring6));
}
std::string UTPexExtensionMessage::toString() const
@ -172,6 +193,16 @@ UTPexExtensionMessage::create(const unsigned char* data, size_t len)
bittorrent::extractPeer
(dropped, AF_INET, std::back_inserter(msg->droppedPeers_));
}
const String* added6 = asString(dict->get("added6"));
if(added6) {
bittorrent::extractPeer
(added6, AF_INET6, std::back_inserter(msg->freshPeers_));
}
const String* dropped6 = asString(dict->get("dropped6"));
if(dropped6) {
bittorrent::extractPeer
(dropped6, AF_INET6, std::back_inserter(msg->droppedPeers_));
}
}
return msg;
}

View File

@ -65,7 +65,9 @@ private:
size_t maxDroppedPeer_;
std::pair<std::string, std::string>
std::pair<std::pair<std::string, std::string>,
std::pair<std::string, std::string> >
createCompactPeerListAndFlag(const std::vector<SharedHandle<Peer> >& peers);
public:

View File

@ -377,16 +377,28 @@ void DefaultBtAnnounceTest::testProcessAnnounceResponse()
"8:intervali3000e"
"12:min intervali1800e"
"8:completei100e"
"10:incompletei200e"
"e";
"10:incompletei200e";
res += "5:peers6:";
res += util::fromHex("c0a800011ae1");
res += "6:peers618:";
res += util::fromHex("100210354527354678541237324732171ae1");
res += "e";
DefaultBtAnnounce an(dctx_, option_);
an.setPeerStorage(peerStorage_);
an.setBtRuntime(btRuntime_);
an.processAnnounceResponse(reinterpret_cast<const unsigned char*>(res.c_str()), res.size());
CPPUNIT_ASSERT_EQUAL(std::string("foo"), an.getTrackerID());
CPPUNIT_ASSERT_EQUAL((time_t)3000, an.getInterval());
CPPUNIT_ASSERT_EQUAL((time_t)1800, an.getMinInterval());
CPPUNIT_ASSERT_EQUAL((unsigned int)100, an.getComplete());
CPPUNIT_ASSERT_EQUAL((unsigned int)200, an.getIncomplete());
CPPUNIT_ASSERT_EQUAL((size_t)2, peerStorage_->getPeers().size());
SharedHandle<Peer> peer = peerStorage_->getPeers()[0];
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"), peer->getIPAddress());
peer = peerStorage_->getPeers()[1];
CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
peer->getIPAddress());
}
} // namespace aria2

View File

@ -81,19 +81,33 @@ void UTPexExtensionMessageTest::testGetBencodedData()
p4->startBadCondition();
CPPUNIT_ASSERT(msg.addDroppedPeer(p4));
SharedHandle<Peer> p5(new Peer("1002:1035:4527:3546:7854:1237:3247:3217",
6881));
CPPUNIT_ASSERT(msg.addFreshPeer(p5));
SharedHandle<Peer> p6(new Peer("2001:db8:bd05:1d2:288a:1fc0:1:10ee", 6882));
p6->startBadCondition();
CPPUNIT_ASSERT(msg.addDroppedPeer(p6));
unsigned char c1[COMPACT_LEN_IPV6];
unsigned char c2[COMPACT_LEN_IPV6];
unsigned char c3[COMPACT_LEN_IPV6];
unsigned char c4[COMPACT_LEN_IPV6];
unsigned char c5[COMPACT_LEN_IPV6];
unsigned char c6[COMPACT_LEN_IPV6];
bittorrent::packcompact(c1, p1->getIPAddress(), p1->getPort());
bittorrent::packcompact(c2, p2->getIPAddress(), p2->getPort());
bittorrent::packcompact(c3, p3->getIPAddress(), p3->getPort());
bittorrent::packcompact(c4, p4->getIPAddress(), p4->getPort());
bittorrent::packcompact(c5, p5->getIPAddress(), p5->getPort());
bittorrent::packcompact(c6, p6->getIPAddress(), p6->getPort());
std::string expected = "d5:added12:"+
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
"7:added.f2:"+util::fromHex("0200")+"7:dropped12:"+
std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
"7:added.f2:"+util::fromHex("0200")+
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
"8:added6.f1:"+util::fromHex("00")+
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
"e";
std::string bd = msg.getPayload();
CPPUNIT_ASSERT_EQUAL(util::percentEncode(expected),
@ -125,12 +139,13 @@ void UTPexExtensionMessageTest::testDoReceivedAction()
p1->allocateSessionResource(256*1024, 1024*1024);
p1->setAllBitfield();
msg.addFreshPeer(p1);// added seeder, check add.f flag
SharedHandle<Peer> p2(new Peer("10.1.1.2", 9999));
SharedHandle<Peer> p2(new Peer("1002:1035:4527:3546:7854:1237:3247:3217",
9999));
msg.addFreshPeer(p2);
SharedHandle<Peer> p3(new Peer("192.168.0.2", 6882));
p3->startBadCondition();
msg.addDroppedPeer(p3);
SharedHandle<Peer> p4(new Peer("10.1.1.3", 10000));
SharedHandle<Peer> p4(new Peer("2001:db8:bd05:1d2:288a:1fc0:1:10ee", 10000));
p4->startBadCondition();
msg.addDroppedPeer(p4);
msg.setPeerStorage(peerStorage_);
@ -145,9 +160,19 @@ void UTPexExtensionMessageTest::testDoReceivedAction()
}
{
SharedHandle<Peer> p = peerStorage_->getPeers()[1];
CPPUNIT_ASSERT_EQUAL(std::string("10.1.1.2"), p->getIPAddress());
CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
p->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)9999, p->getPort());
}
{
SharedHandle<Peer> p = peerStorage_->getPeers()[2];
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), p->getIPAddress());
}
{
SharedHandle<Peer> p = peerStorage_->getPeers()[3];
CPPUNIT_ASSERT_EQUAL(std::string("2001:db8:bd05:1d2:288a:1fc0:1:10ee"),
p->getIPAddress());
}
}
void UTPexExtensionMessageTest::testCreate()
@ -156,17 +181,24 @@ void UTPexExtensionMessageTest::testCreate()
unsigned char c2[COMPACT_LEN_IPV6];
unsigned char c3[COMPACT_LEN_IPV6];
unsigned char c4[COMPACT_LEN_IPV6];
unsigned char c5[COMPACT_LEN_IPV6];
unsigned char c6[COMPACT_LEN_IPV6];
bittorrent::packcompact(c1, "192.168.0.1", 6881);
bittorrent::packcompact(c2, "10.1.1.2", 9999);
bittorrent::packcompact(c3, "192.168.0.2", 6882);
bittorrent::packcompact(c4, "10.1.1.3",10000);
bittorrent::packcompact(c5, "1002:1035:4527:3546:7854:1237:3247:3217", 6997);
bittorrent::packcompact(c6, "2001:db8:bd05:1d2:288a:1fc0:1:10ee",6998);
char id[1] = { 1 };
std::string data = std::string(&id[0], &id[1])+"d5:added12:"+
std::string(&c1[0], &c1[6])+std::string(&c2[0], &c2[6])+
"7:added.f2:207:dropped12:"+
std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
"7:added.f2:"+util::fromHex("0200")+
"6:added618:"+std::string(&c5[0], &c5[COMPACT_LEN_IPV6])+
"8:added6.f1:"+util::fromHex("00")+
"7:dropped12:"+std::string(&c3[0], &c3[6])+std::string(&c4[0], &c4[6])+
"8:dropped618:"+std::string(&c6[0], &c6[COMPACT_LEN_IPV6])+
"e";
SharedHandle<UTPexExtensionMessage> msg =
@ -174,14 +206,18 @@ void UTPexExtensionMessageTest::testCreate()
(reinterpret_cast<const unsigned char*>(data.c_str()), data.size());
CPPUNIT_ASSERT_EQUAL((uint8_t)1, msg->getExtensionMessageID());
CPPUNIT_ASSERT_EQUAL((size_t)2, msg->getFreshPeers().size());
CPPUNIT_ASSERT_EQUAL((size_t)3, msg->getFreshPeers().size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.1"),
msg->getFreshPeers()[0]->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)6881, msg->getFreshPeers()[0]->getPort());
CPPUNIT_ASSERT_EQUAL(std::string("10.1.1.2"),
msg->getFreshPeers()[1]->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)9999, msg->getFreshPeers()[1]->getPort());
CPPUNIT_ASSERT_EQUAL((size_t)2, msg->getDroppedPeers().size());
CPPUNIT_ASSERT_EQUAL(std::string("1002:1035:4527:3546:7854:1237:3247:3217"),
msg->getFreshPeers()[2]->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)6997, msg->getFreshPeers()[2]->getPort());
CPPUNIT_ASSERT_EQUAL((size_t)3, msg->getDroppedPeers().size());
CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"),
msg->getDroppedPeers()[0]->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)6882, msg->getDroppedPeers()[0]->getPort());
@ -189,6 +225,11 @@ void UTPexExtensionMessageTest::testCreate()
msg->getDroppedPeers()[1]->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)10000,
msg->getDroppedPeers()[1]->getPort());
CPPUNIT_ASSERT_EQUAL(std::string("2001:db8:bd05:1d2:288a:1fc0:1:10ee"),
msg->getDroppedPeers()[2]->getIPAddress());
CPPUNIT_ASSERT_EQUAL((uint16_t)6998,
msg->getDroppedPeers()[2]->getPort());
try {
// 0 length data
std::string in = "";