From 436448dd8a75d67daa794171c50f32bc4004be7c Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Mon, 5 Oct 2009 13:04:06 +0000 Subject: [PATCH] 2009-10-05 Tatsuhiro Tsujikawa Included version number in Peer ID and client version. Peer ID now starts with "aria2/VERSION-", where VERSION is MAJOR.MINOR.MICRO. Client version is aria2/VERSION. * src/DefaultBtInteractive.cc * src/OptionHandlerFactory.cc * src/bittorrent_helper.cc * src/bittorrent_helper.h * src/main.cc * src/usage_text.h * test/BittorrentHelperTest.cc --- ChangeLog | 13 +++++++++++++ src/DefaultBtInteractive.cc | 2 +- src/OptionHandlerFactory.cc | 2 +- src/bittorrent_helper.cc | 25 +++++++++++++++---------- src/bittorrent_helper.h | 10 ++++------ src/main.cc | 3 +-- src/usage_text.h | 7 +++---- test/BittorrentHelperTest.cc | 8 +++----- 8 files changed, 41 insertions(+), 29 deletions(-) diff --git a/ChangeLog b/ChangeLog index 01d20e39..295073f2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2009-10-05 Tatsuhiro Tsujikawa + + Included version number in Peer ID and client version. Peer ID + now starts with "aria2/VERSION-", where VERSION is + MAJOR.MINOR.MICRO. Client version is aria2/VERSION. + * src/DefaultBtInteractive.cc + * src/OptionHandlerFactory.cc + * src/bittorrent_helper.cc + * src/bittorrent_helper.h + * src/main.cc + * src/usage_text.h + * test/BittorrentHelperTest.cc + 2009-10-05 Tatsuhiro Tsujikawa Fixed memory leak. diff --git a/src/DefaultBtInteractive.cc b/src/DefaultBtInteractive.cc index a69a9cdf..9e508c40 100644 --- a/src/DefaultBtInteractive.cc +++ b/src/DefaultBtInteractive.cc @@ -161,7 +161,7 @@ void DefaultBtInteractive::addPortMessageToQueue() void DefaultBtInteractive::addHandshakeExtendedMessageToQueue() { - static const std::string CLIENT_ARIA2("aria2"); + static const std::string CLIENT_ARIA2("aria2/"PACKAGE_VERSION); HandshakeExtensionMessageHandle m(new HandshakeExtensionMessage()); m->setClientVersion(CLIENT_ARIA2); m->setTCPPort(_btRuntime->getListenPort()); diff --git a/src/OptionHandlerFactory.cc b/src/OptionHandlerFactory.cc index 9480d2c3..2c4593ab 100644 --- a/src/OptionHandlerFactory.cc +++ b/src/OptionHandlerFactory.cc @@ -1140,7 +1140,7 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers() SharedHandle op(new DefaultOptionHandler (PREF_PEER_ID_PREFIX, TEXT_PEER_ID_PREFIX, - "-aria2-")); + "aria2/"PACKAGE_VERSION"-")); op->addTag(TAG_BITTORRENT); handlers.push_back(op); } diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc index 95707e39..a161a98a 100644 --- a/src/bittorrent_helper.cc +++ b/src/bittorrent_helper.cc @@ -52,6 +52,7 @@ #include "a2netcompat.h" #include "BtConstants.h" #include "bitfield.h" +#include "DHTUtil.h" namespace aria2 { @@ -93,7 +94,7 @@ static const std::string C_COMMENT_UTF8("comment.utf-8"); static const std::string C_CREATED_BY("created by"); -static const std::string DEFAULT_PEER_ID_PREFIX("-aria2-"); +static const std::string DEFAULT_PEER_ID_PREFIX("aria2-"); const std::string INFO_HASH("infoHash"); @@ -618,12 +619,16 @@ void computeFastSet } } -std::string generatePeerId -(const std::string& peerIdPrefix, const SharedHandle& randomizer) +std::string generatePeerId(const std::string& peerIdPrefix) { std::string peerId = peerIdPrefix; - peerId += Util::randomAlpha(20-peerIdPrefix.size(), randomizer); - if(peerId.size() > 20) { + unsigned char buf[20]; + int len = 20-peerIdPrefix.size(); + if(len > 0) { + DHTUtil::generateRandomData(buf, len); + + peerId += std::string(&buf[0], &buf[len]); + } if(peerId.size() > 20) { peerId.erase(20); } return peerId; @@ -631,11 +636,10 @@ std::string generatePeerId static std::string peerId; -const std::string& generateStaticPeerId -(const std::string& peerIdPrefix, const SharedHandle& randomizer) +const std::string& generateStaticPeerId(const std::string& peerIdPrefix) { if(peerId.empty()) { - peerId = generatePeerId(peerIdPrefix, randomizer); + peerId = generatePeerId(peerIdPrefix); } return peerId; } @@ -646,12 +650,13 @@ void setStaticPeerId(const std::string& newPeerId) } // If PeerID is not generated, it is created with default peerIdPrefix -// (-aria2-) and SimpleRandomizer. +// (aria2-). const unsigned char* getStaticPeerId() { if(peerId.empty()) { return - reinterpret_cast(generateStaticPeerId(DEFAULT_PEER_ID_PREFIX, SimpleRandomizer::getInstance()).data()); + reinterpret_cast + (generateStaticPeerId(DEFAULT_PEER_ID_PREFIX).data()); } else { return reinterpret_cast(peerId.data()); } diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h index e92a0b0c..a99c05b0 100644 --- a/src/bittorrent_helper.h +++ b/src/bittorrent_helper.h @@ -119,17 +119,15 @@ void loadFromMemory(const std::string& context, // length. This function uses peerIdPrefix as a Peer ID and it is // less than 20bytes, random bytes are generated and appened to it. If // peerIdPrefix is larger than 20bytes, first 20bytes are used. -std::string generatePeerId -(const std::string& peerIdPrefix, const SharedHandle& randomizer); +std::string generatePeerId(const std::string& peerIdPrefix); // Generates Peer ID and stores it in static variable. This function -// uses generatePeerId(peerIdPrefix, randomizer) to produce Peer ID. -const std::string& generateStaticPeerId -(const std::string& peerIdPrefix, const SharedHandle& randomizer); +// uses generatePeerId(peerIdPrefix) to produce Peer ID. +const std::string& generateStaticPeerId(const std::string& peerIdPrefix); // Returns Peer ID statically stored by generateStaticPeerId(). If // Peer ID is not stored yet, this function calls -// generateStaticPeerId("-aria2-", randomizer) +// generateStaticPeerId("aria2-") const unsigned char* getStaticPeerId(); // Set newPeerId as a static Peer ID. newPeerId must be 20-byte diff --git a/src/main.cc b/src/main.cc index 36e98cdf..11ade560 100644 --- a/src/main.cc +++ b/src/main.cc @@ -174,8 +174,7 @@ downloadresultcode::RESULT main(int argc, char* argv[]) SimpleRandomizer::init(); BitfieldManFactory::setDefaultRandomizer(SimpleRandomizer::getInstance()); #ifdef ENABLE_BITTORRENT - bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX), - SimpleRandomizer::getInstance()); + bittorrent::generateStaticPeerId(op->get(PREF_PEER_ID_PREFIX)); #endif // ENABLE_BITTORRENT if(op->get(PREF_LOG) == "-") { LogFactory::setLogFile(DEV_STDOUT); diff --git a/src/usage_text.h b/src/usage_text.h index 8f7da985..7f5dc20d 100644 --- a/src/usage_text.h +++ b/src/usage_text.h @@ -287,10 +287,9 @@ _(" --seed-ratio=RATIO Specify share ratio. Seed completed torrents\n" #define TEXT_PEER_ID_PREFIX \ _(" --peer-id-prefix=PEER_ID_PREFIX Specify the prefix of peer ID. The peer ID in\n"\ " BitTorrent is 20 byte length. If more than 20\n"\ - " bytes are specified, only first 20\n"\ - " bytes are used. If less than 20 bytes are\n"\ - " specified, the random alphabet characters are\n"\ - " added to make it's length 20 bytes.") + " bytes are specified, only first 20 bytes are\n"\ + " used. If less than 20 bytes are specified, random\n"\ + " byte data are added to make its length 20 bytes.") #define TEXT_ENABLE_PEER_EXCHANGE \ _(" --enable-peer-exchange[=true|false] Enable Peer Exchange extension.") #define TEXT_ENABLE_DHT \ diff --git a/test/BittorrentHelperTest.cc b/test/BittorrentHelperTest.cc index 1cef6a5f..29ca2bcc 100644 --- a/test/BittorrentHelperTest.cc +++ b/test/BittorrentHelperTest.cc @@ -284,11 +284,9 @@ void BittorrentHelperTest::testGetInfoHashAsString() { } void BittorrentHelperTest::testGetPeerId() { - SharedHandle dctx(new DownloadContext()); - CPPUNIT_ASSERT_EQUAL - (std::string("-aria-AAAAAAAAAAAAAA"), - generatePeerId("-aria-", - SharedHandle(new FixedNumberRandomizer()))); + std::string peerId = generatePeerId("aria2-"); + CPPUNIT_ASSERT(peerId.find("aria2-") == 0); + CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size()); } void BittorrentHelperTest::testComputeFastSet()