2009-10-05 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

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
pull/1/head
Tatsuhiro Tsujikawa 2009-10-05 13:04:06 +00:00
parent 51ba780c76
commit 436448dd8a
8 changed files with 41 additions and 29 deletions

View File

@ -1,3 +1,16 @@
2009-10-05 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
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 <t-tujikawa@users.sourceforge.net>
Fixed memory leak.

View File

@ -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());

View File

@ -1140,7 +1140,7 @@ OptionHandlers OptionHandlerFactory::createOptionHandlers()
SharedHandle<OptionHandler> op(new DefaultOptionHandler
(PREF_PEER_ID_PREFIX,
TEXT_PEER_ID_PREFIX,
"-aria2-"));
"aria2/"PACKAGE_VERSION"-"));
op->addTag(TAG_BITTORRENT);
handlers.push_back(op);
}

View File

@ -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>& 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>& 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<const unsigned char*>(generateStaticPeerId(DEFAULT_PEER_ID_PREFIX, SimpleRandomizer::getInstance()).data());
reinterpret_cast<const unsigned char*>
(generateStaticPeerId(DEFAULT_PEER_ID_PREFIX).data());
} else {
return reinterpret_cast<const unsigned char*>(peerId.data());
}

View File

@ -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>& 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>& 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

View File

@ -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);

View File

@ -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 \

View File

@ -284,11 +284,9 @@ void BittorrentHelperTest::testGetInfoHashAsString() {
}
void BittorrentHelperTest::testGetPeerId() {
SharedHandle<DownloadContext> dctx(new DownloadContext());
CPPUNIT_ASSERT_EQUAL
(std::string("-aria-AAAAAAAAAAAAAA"),
generatePeerId("-aria-",
SharedHandle<Randomizer>(new FixedNumberRandomizer())));
std::string peerId = generatePeerId("aria2-");
CPPUNIT_ASSERT(peerId.find("aria2-") == 0);
CPPUNIT_ASSERT_EQUAL((size_t)20, peerId.size());
}
void BittorrentHelperTest::testComputeFastSet()