From c685ea9de9efdea119828ded02414f72b8271f31 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Thu, 5 Aug 2010 11:54:44 +0000 Subject: [PATCH] 2010-08-05 Tatsuhiro Tsujikawa 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 --- ChangeLog | 12 +++++++ src/BtAnnounce.cc | 2 ++ src/BtAnnounce.h | 2 ++ src/DefaultBtAnnounce.cc | 10 ++++++ src/UTPexExtensionMessage.cc | 45 +++++++++++++++++++---- src/UTPexExtensionMessage.h | 4 ++- test/DefaultBtAnnounceTest.cc | 16 +++++++-- test/UTPexExtensionMessageTest.cc | 59 ++++++++++++++++++++++++++----- 8 files changed, 131 insertions(+), 19 deletions(-) diff --git a/ChangeLog b/ChangeLog index dcf7d260..4d9eb506 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2010-08-05 Tatsuhiro Tsujikawa + + 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 Added AI_V4MAPPED to DEFAULT_AI_FLAGS diff --git a/src/BtAnnounce.cc b/src/BtAnnounce.cc index 64976a26..25b321d7 100644 --- a/src/BtAnnounce.cc +++ b/src/BtAnnounce.cc @@ -52,4 +52,6 @@ const std::string BtAnnounce::INCOMPLETE("incomplete"); const std::string BtAnnounce::PEERS("peers"); +const std::string BtAnnounce::PEERS6("peers6"); + } // namespace aria2 diff --git a/src/BtAnnounce.h b/src/BtAnnounce.h index 4c25aa95..9d42b195 100644 --- a/src/BtAnnounce.h +++ b/src/BtAnnounce.h @@ -123,6 +123,8 @@ public: static const std::string PEERS; + static const std::string PEERS6; + static const time_t DEFAULT_ANNOUNCE_INTERVAL = 120; }; diff --git a/src/DefaultBtAnnounce.cc b/src/DefaultBtAnnounce.cc index e47cf6b6..5ea3ebdc 100644 --- a/src/DefaultBtAnnounce.cc +++ b/src/DefaultBtAnnounce.cc @@ -280,6 +280,16 @@ DefaultBtAnnounce::processAnnounceResponse(const unsigned char* trackerResponse, peerStorage_->addPeer(peers); } } + const SharedHandle& peer6Data = dict->get(BtAnnounce::PEERS6); + if(peer6Data.isNull()) { + logger_->info("No peers6 received."); + } else { + if(!btRuntime_->isHalt() && btRuntime_->lessThanMinPeers()) { + std::vector > peers; + bittorrent::extractPeer(peer6Data, AF_INET6, std::back_inserter(peers)); + peerStorage_->addPeer(peers); + } + } } bool DefaultBtAnnounce::noMoreAnnounce() { diff --git a/src/UTPexExtensionMessage.cc b/src/UTPexExtensionMessage.cc index 4618850f..3ac1e9a1 100644 --- a/src/UTPexExtensionMessage.cc +++ b/src/UTPexExtensionMessage.cc @@ -58,24 +58,41 @@ UTPexExtensionMessage::~UTPexExtensionMessage() {} std::string UTPexExtensionMessage::getPayload() { - std::pair freshPeerPair = + std::pair, + std::pair > freshPeerPair = createCompactPeerListAndFlag(freshPeers_); - std::pair droppedPeerPair = + std::pair, + std::pair > 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::pair, + std::pair > UTPexExtensionMessage::createCompactPeerListAndFlag (const std::vector >& peers) { std::string addrstring; std::string flagstring; + std::string addrstring6; + std::string flagstring6; for(std::vector >::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(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; } diff --git a/src/UTPexExtensionMessage.h b/src/UTPexExtensionMessage.h index 74b5f4fb..8c554b4c 100644 --- a/src/UTPexExtensionMessage.h +++ b/src/UTPexExtensionMessage.h @@ -65,7 +65,9 @@ private: size_t maxDroppedPeer_; - std::pair + + std::pair, + std::pair > createCompactPeerListAndFlag(const std::vector >& peers); public: diff --git a/test/DefaultBtAnnounceTest.cc b/test/DefaultBtAnnounceTest.cc index 1ad8a175..e7eaa283 100644 --- a/test/DefaultBtAnnounceTest.cc +++ b/test/DefaultBtAnnounceTest.cc @@ -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(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 = 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 diff --git a/test/UTPexExtensionMessageTest.cc b/test/UTPexExtensionMessageTest.cc index ea3decfc..6deb95af 100644 --- a/test/UTPexExtensionMessageTest.cc +++ b/test/UTPexExtensionMessageTest.cc @@ -81,19 +81,33 @@ void UTPexExtensionMessageTest::testGetBencodedData() p4->startBadCondition(); CPPUNIT_ASSERT(msg.addDroppedPeer(p4)); + SharedHandle p5(new Peer("1002:1035:4527:3546:7854:1237:3247:3217", + 6881)); + CPPUNIT_ASSERT(msg.addFreshPeer(p5)); + SharedHandle 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 p2(new Peer("10.1.1.2", 9999)); + SharedHandle p2(new Peer("1002:1035:4527:3546:7854:1237:3247:3217", + 9999)); msg.addFreshPeer(p2); SharedHandle p3(new Peer("192.168.0.2", 6882)); p3->startBadCondition(); msg.addDroppedPeer(p3); - SharedHandle p4(new Peer("10.1.1.3", 10000)); + SharedHandle 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 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 p = peerStorage_->getPeers()[2]; + CPPUNIT_ASSERT_EQUAL(std::string("192.168.0.2"), p->getIPAddress()); + } + { + SharedHandle 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 msg = @@ -174,14 +206,18 @@ void UTPexExtensionMessageTest::testCreate() (reinterpret_cast(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 = "";