summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-10-22 14:43:42 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2009-10-22 14:43:42 +0000
commit3349fe081156776aba058b90beebb68d4ee73e75 (patch)
treec3dd678e21ea59576a4933e0c052210a22c2cd45 /src
parent7e694d38cb4ab5c5499936af0c3a00701bdbd37e (diff)
2009-10-22 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Moved generateRandomData to Util.cc Moved flipBit to bitfield.cc. Moved generateRandomKey to bittorrent_helper.cc. * src/DHTBucket.cc * src/DHTGetPeersMessage.cc * src/DHTGetPeersReplyMessage.cc * src/DHTMessage.cc * src/DHTNode.cc * src/DHTTokenTracker.cc * src/DHTUtil.cc: Removed. * src/DHTUtil.h: Removed. * src/Makefile.am * src/Util.cc * src/Util.h * src/bitfield.h * src/bittorrent_helper.cc * src/bittorrent_helper.h * test/ARC4Test.cc * test/BNodeTest.cc * test/DHTAnnouncePeerMessageTest.cc * test/DHTAnnouncePeerReplyMessageTest.cc * test/DHTFindNodeMessageTest.cc * test/DHTFindNodeReplyMessageTest.cc * test/DHTGetPeersMessageTest.cc * test/DHTGetPeersReplyMessageTest.cc * test/DHTPingMessageTest.cc * test/DHTPingReplyMessageTest.cc * test/DHTTokenTrackerTest.cc * test/DHTUtilTest.cc: Removed. * test/Makefile.am * test/UtilTest.cc
Diffstat (limited to 'src')
-rw-r--r--src/DHTBucket.cc11
-rw-r--r--src/DHTGetPeersMessage.cc1
-rw-r--r--src/DHTGetPeersReplyMessage.cc1
-rw-r--r--src/DHTMessage.cc4
-rw-r--r--src/DHTNode.cc4
-rw-r--r--src/DHTTokenTracker.cc6
-rw-r--r--src/DHTUtil.cc80
-rw-r--r--src/DHTUtil.h56
-rw-r--r--src/Makefile.am3
-rw-r--r--src/Makefile.in26
-rw-r--r--src/Util.cc29
-rw-r--r--src/Util.h6
-rw-r--r--src/bitfield.h2
-rw-r--r--src/bittorrent_helper.cc11
-rw-r--r--src/bittorrent_helper.h2
15 files changed, 73 insertions, 169 deletions
diff --git a/src/DHTBucket.cc b/src/DHTBucket.cc
index 775fba82..c991aebd 100644
--- a/src/DHTBucket.cc
+++ b/src/DHTBucket.cc
@@ -38,13 +38,14 @@
#include <cassert>
#include <algorithm>
-#include "DHTUtil.h"
#include "DHTNode.h"
#include "LogFactory.h"
#include "Logger.h"
#include "Util.h"
#include "DHTConstants.h"
#include "a2functional.h"
+#include "bittorrent_helper.h"
+#include "bitfield.h"
namespace aria2 {
@@ -73,10 +74,10 @@ DHTBucket::~DHTBucket() {}
void DHTBucket::getRandomNodeID(unsigned char* nodeID) const
{
if(_prefixLength == 0) {
- DHTUtil::generateRandomKey(nodeID);
+ bittorrent::generateRandomKey(nodeID);
} else {
size_t lastByteIndex = (_prefixLength-1)/8;
- DHTUtil::generateRandomKey(nodeID);
+ bittorrent::generateRandomKey(nodeID);
memcpy(nodeID, _min, lastByteIndex+1);
}
}
@@ -179,11 +180,11 @@ SharedHandle<DHTBucket> DHTBucket::split()
unsigned char rMax[DHT_ID_LENGTH];
memcpy(rMax, _max, DHT_ID_LENGTH);
- DHTUtil::flipBit(rMax, DHT_ID_LENGTH, _prefixLength);
+ bitfield::flipBit(rMax, DHT_ID_LENGTH, _prefixLength);
unsigned char rMin[DHT_ID_LENGTH];
memcpy(rMin, _min, DHT_ID_LENGTH);
- DHTUtil::flipBit(_min, DHT_ID_LENGTH, _prefixLength);
+ bitfield::flipBit(_min, DHT_ID_LENGTH, _prefixLength);
++_prefixLength;
SharedHandle<DHTBucket> rBucket(new DHTBucket(_prefixLength,
diff --git a/src/DHTGetPeersMessage.cc b/src/DHTGetPeersMessage.cc
index e6c2af43..ca08fcfb 100644
--- a/src/DHTGetPeersMessage.cc
+++ b/src/DHTGetPeersMessage.cc
@@ -42,7 +42,6 @@
#include "DHTMessageDispatcher.h"
#include "DHTMessageCallback.h"
#include "DHTPeerAnnounceStorage.h"
-#include "DHTUtil.h"
#include "Peer.h"
#include "DHTTokenTracker.h"
#include "Util.h"
diff --git a/src/DHTGetPeersReplyMessage.cc b/src/DHTGetPeersReplyMessage.cc
index 2d69237b..8a57b139 100644
--- a/src/DHTGetPeersReplyMessage.cc
+++ b/src/DHTGetPeersReplyMessage.cc
@@ -44,7 +44,6 @@
#include "DHTMessageCallback.h"
#include "bittorrent_helper.h"
#include "Peer.h"
-#include "DHTUtil.h"
#include "Util.h"
#include "bencode.h"
#include "a2functional.h"
diff --git a/src/DHTMessage.cc b/src/DHTMessage.cc
index 09dcccba..86f51e2a 100644
--- a/src/DHTMessage.cc
+++ b/src/DHTMessage.cc
@@ -34,7 +34,7 @@
/* copyright --> */
#include "DHTMessage.h"
#include "DHTNode.h"
-#include "DHTUtil.h"
+#include "Util.h"
namespace aria2 {
@@ -59,7 +59,7 @@ DHTMessage::~DHTMessage() {}
void DHTMessage::generateTransactionID()
{
unsigned char tid[DHT_TRANSACTION_ID_LENGTH];
- DHTUtil::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
+ util::generateRandomData(tid, DHT_TRANSACTION_ID_LENGTH);
_transactionID = std::string(&tid[0], &tid[DHT_TRANSACTION_ID_LENGTH]);
}
diff --git a/src/DHTNode.cc b/src/DHTNode.cc
index 7c74e1f7..a28565ed 100644
--- a/src/DHTNode.cc
+++ b/src/DHTNode.cc
@@ -36,9 +36,9 @@
#include <cstring>
-#include "DHTUtil.h"
#include "Util.h"
#include "a2functional.h"
+#include "bittorrent_helper.h"
namespace aria2 {
@@ -54,7 +54,7 @@ DHTNode::DHTNode(const unsigned char* id):_port(0), _rtt(0), _condition(1), _las
void DHTNode::generateID()
{
- DHTUtil::generateRandomKey(_id);
+ bittorrent::generateRandomKey(_id);
}
bool DHTNode::operator==(const DHTNode& node) const
diff --git a/src/DHTTokenTracker.cc b/src/DHTTokenTracker.cc
index adbd4608..852775cf 100644
--- a/src/DHTTokenTracker.cc
+++ b/src/DHTTokenTracker.cc
@@ -36,7 +36,7 @@
#include <cstring>
-#include "DHTUtil.h"
+#include "Util.h"
#include "bittorrent_helper.h"
#include "DlAbortEx.h"
#include "DHTConstants.h"
@@ -47,7 +47,7 @@ namespace aria2 {
DHTTokenTracker::DHTTokenTracker()
{
- DHTUtil::generateRandomData(_secret[0], SECRET_SIZE);
+ util::generateRandomData(_secret[0], SECRET_SIZE);
memcpy(_secret[1], _secret[0], SECRET_SIZE);
}
@@ -97,7 +97,7 @@ bool DHTTokenTracker::validateToken(const std::string& token,
void DHTTokenTracker::updateTokenSecret()
{
memcpy(_secret[1], _secret[0], SECRET_SIZE);
- DHTUtil::generateRandomData(_secret[0], SECRET_SIZE);
+ util::generateRandomData(_secret[0], SECRET_SIZE);
}
} // namespace aria2
diff --git a/src/DHTUtil.cc b/src/DHTUtil.cc
deleted file mode 100644
index 4118debb..00000000
--- a/src/DHTUtil.cc
+++ /dev/null
@@ -1,80 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#include "DHTUtil.h"
-#include "MessageDigestHelper.h"
-#include <cassert>
-#include <fstream>
-#include <iomanip>
-#ifdef HAVE_LIBGCRYPT
-# include <gcrypt.h>
-#elif HAVE_LIBSSL
-# include <openssl/rand.h>
-# include "SimpleRandomizer.h"
-#endif // HAVE_LIBSSL
-
-namespace aria2 {
-
-void DHTUtil::generateRandomKey(unsigned char* key)
-{
- unsigned char bytes[40];
- generateRandomData(bytes, sizeof(bytes));
- MessageDigestHelper::digest(key, 20, MessageDigestContext::SHA1, bytes, sizeof(bytes));
-}
-
-void DHTUtil::generateRandomData(unsigned char* data, size_t length)
-{
-#ifdef HAVE_LIBGCRYPT
- gcry_randomize(data, length, GCRY_STRONG_RANDOM);
-#elif HAVE_LIBSSL
- if(RAND_bytes(data, length) != 1) {
- for(size_t i = 0; i < length; ++i) {
- data[i] = SimpleRandomizer::getInstance()->getRandomNumber(UINT8_MAX+1);
- }
- }
-#else
- std::ifstream i("/dev/urandom", std::ios::binary);
- i.read(data, length);
-#endif // HAVE_LIBSSL
-}
-
-void DHTUtil::flipBit(unsigned char* data, size_t length, size_t bitIndex)
-{
- size_t byteIndex = bitIndex/8;
- assert(byteIndex <= length);
- unsigned char mask = 128 >> (bitIndex%8);
- data[byteIndex] ^= mask;
-}
-
-} // namespace aria2
diff --git a/src/DHTUtil.h b/src/DHTUtil.h
deleted file mode 100644
index 5ab0ab02..00000000
--- a/src/DHTUtil.h
+++ /dev/null
@@ -1,56 +0,0 @@
-/* <!-- copyright */
-/*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2006 Tatsuhiro Tsujikawa
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
-/* copyright --> */
-#ifndef _D_DHT_UTIL_H_
-#define _D_DHT_UTIL_H_
-
-#include "common.h"
-#include <cstdlib>
-
-namespace aria2 {
-
-class DHTUtil {
-public:
-
- static void generateRandomKey(unsigned char* key);
-
- static void generateRandomData(unsigned char* data, size_t length);
-
- static void flipBit(unsigned char* data, size_t length, size_t bitIndex);
-
-};
-
-} // namespace aria2
-
-#endif // _D_DHT_UTIL_H_
diff --git a/src/Makefile.am b/src/Makefile.am
index 86d7b736..36fac928 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -197,7 +197,7 @@ SRCS = Socket.h\
SequentialDispatcherCommand.h\
PieceSelector.h\
LongestSequencePieceSelector.cc LongestSequencePieceSelector.h\
- bitfield.h\
+ bitfield.cc bitfield.h\
BDE.cc BDE.h\
CreateRequestCommand.cc CreateRequestCommand.h\
DownloadResultCode.h
@@ -347,7 +347,6 @@ SRCS += PeerAbstractCommand.cc PeerAbstractCommand.h\
HandshakeExtensionMessage.cc HandshakeExtensionMessage.h\
UTPexExtensionMessage.cc UTPexExtensionMessage.h\
DHTNode.cc DHTNode.h\
- DHTUtil.cc DHTUtil.h\
DHTBucket.cc DHTBucket.h\
DHTRoutingTable.cc DHTRoutingTable.h\
DHTMessageEntry.cc DHTMessageEntry.h\
diff --git a/src/Makefile.in b/src/Makefile.in
index ba01535b..ad582868 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -146,7 +146,6 @@ bin_PROGRAMS = aria2c$(EXEEXT)
@ENABLE_BITTORRENT_TRUE@ HandshakeExtensionMessage.cc HandshakeExtensionMessage.h\
@ENABLE_BITTORRENT_TRUE@ UTPexExtensionMessage.cc UTPexExtensionMessage.h\
@ENABLE_BITTORRENT_TRUE@ DHTNode.cc DHTNode.h\
-@ENABLE_BITTORRENT_TRUE@ DHTUtil.cc DHTUtil.h\
@ENABLE_BITTORRENT_TRUE@ DHTBucket.cc DHTBucket.h\
@ENABLE_BITTORRENT_TRUE@ DHTRoutingTable.cc DHTRoutingTable.h\
@ENABLE_BITTORRENT_TRUE@ DHTMessageEntry.cc DHTMessageEntry.h\
@@ -410,7 +409,7 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
SelectEventPoll.cc SelectEventPoll.h SequentialPicker.h \
SequentialDispatcherCommand.h PieceSelector.h \
LongestSequencePieceSelector.cc LongestSequencePieceSelector.h \
- bitfield.h BDE.cc BDE.h CreateRequestCommand.cc \
+ bitfield.cc bitfield.h BDE.cc BDE.h CreateRequestCommand.cc \
CreateRequestCommand.h DownloadResultCode.h \
XmlRpcRequestParserController.cc \
XmlRpcRequestParserController.h \
@@ -490,12 +489,12 @@ am__libaria2c_a_SOURCES_DIST = Socket.h SocketCore.cc SocketCore.h \
ExtensionMessageFactory.h DefaultExtensionMessageFactory.cc \
DefaultExtensionMessageFactory.h HandshakeExtensionMessage.cc \
HandshakeExtensionMessage.h UTPexExtensionMessage.cc \
- UTPexExtensionMessage.h DHTNode.cc DHTNode.h DHTUtil.cc \
- DHTUtil.h DHTBucket.cc DHTBucket.h DHTRoutingTable.cc \
- DHTRoutingTable.h DHTMessageEntry.cc DHTMessageEntry.h \
- DHTMessageDispatcher.h DHTMessageDispatcherImpl.cc \
- DHTMessageDispatcherImpl.h DHTMessageReceiver.cc \
- DHTMessageReceiver.h DHTMessageTracker.cc DHTMessageTracker.h \
+ UTPexExtensionMessage.h DHTNode.cc DHTNode.h DHTBucket.cc \
+ DHTBucket.h DHTRoutingTable.cc DHTRoutingTable.h \
+ DHTMessageEntry.cc DHTMessageEntry.h DHTMessageDispatcher.h \
+ DHTMessageDispatcherImpl.cc DHTMessageDispatcherImpl.h \
+ DHTMessageReceiver.cc DHTMessageReceiver.h \
+ DHTMessageTracker.cc DHTMessageTracker.h \
DHTMessageTrackerEntry.cc DHTMessageTrackerEntry.h \
DHTMessage.cc DHTMessage.h DHTConnection.h \
DHTConnectionImpl.cc DHTConnectionImpl.h DHTAbstractMessage.cc \
@@ -645,8 +644,7 @@ am__objects_6 =
@ENABLE_BITTORRENT_TRUE@ DefaultExtensionMessageFactory.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ HandshakeExtensionMessage.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ UTPexExtensionMessage.$(OBJEXT) \
-@ENABLE_BITTORRENT_TRUE@ DHTNode.$(OBJEXT) DHTUtil.$(OBJEXT) \
-@ENABLE_BITTORRENT_TRUE@ DHTBucket.$(OBJEXT) \
+@ENABLE_BITTORRENT_TRUE@ DHTNode.$(OBJEXT) DHTBucket.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DHTRoutingTable.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DHTMessageEntry.$(OBJEXT) \
@ENABLE_BITTORRENT_TRUE@ DHTMessageDispatcherImpl.$(OBJEXT) \
@@ -800,8 +798,8 @@ am__objects_27 = SocketCore.$(OBJEXT) Command.$(OBJEXT) \
NsCookieParser.$(OBJEXT) CookieStorage.$(OBJEXT) \
SocketBuffer.$(OBJEXT) OptionHandlerException.$(OBJEXT) \
URIResult.$(OBJEXT) SelectEventPoll.$(OBJEXT) \
- LongestSequencePieceSelector.$(OBJEXT) BDE.$(OBJEXT) \
- CreateRequestCommand.$(OBJEXT) $(am__objects_1) \
+ LongestSequencePieceSelector.$(OBJEXT) bitfield.$(OBJEXT) \
+ BDE.$(OBJEXT) CreateRequestCommand.$(OBJEXT) $(am__objects_1) \
$(am__objects_2) $(am__objects_3) $(am__objects_4) \
$(am__objects_5) $(am__objects_6) $(am__objects_7) \
$(am__objects_8) $(am__objects_9) $(am__objects_10) \
@@ -1137,7 +1135,7 @@ SRCS = Socket.h SocketCore.cc SocketCore.h BinaryStream.h Command.cc \
SelectEventPoll.cc SelectEventPoll.h SequentialPicker.h \
SequentialDispatcherCommand.h PieceSelector.h \
LongestSequencePieceSelector.cc LongestSequencePieceSelector.h \
- bitfield.h BDE.cc BDE.h CreateRequestCommand.cc \
+ bitfield.cc bitfield.h BDE.cc BDE.h CreateRequestCommand.cc \
CreateRequestCommand.h DownloadResultCode.h $(am__append_1) \
$(am__append_2) $(am__append_3) $(am__append_4) \
$(am__append_5) $(am__append_6) $(am__append_7) \
@@ -1362,7 +1360,6 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTTokenTracker.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTTokenUpdateCommand.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTUnknownMessage.Po@am__quote@
-@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DHTUtil.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultAuthResolver.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtAnnounce.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DefaultBtInteractive.Po@am__quote@
@@ -1531,6 +1528,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/ZeroBtMessage.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/asctime_r.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bencode.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bitfield.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/bittorrent_helper.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/daemon.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/download_helper.Po@am__quote@
diff --git a/src/Util.cc b/src/Util.cc
index 08412da8..f20b044e 100644
--- a/src/Util.cc
+++ b/src/Util.cc
@@ -46,6 +46,8 @@
#include <sstream>
#include <ostream>
#include <algorithm>
+#include <fstream>
+#include <iomanip>
#ifndef HAVE_SLEEP
# ifdef HAVE_WINSOCK_H
# define WIN32_LEAN_AND_MEAN
@@ -53,6 +55,13 @@
# endif // HAVE_WINSOCK_H
#endif // HAVE_SLEEP
+#ifdef HAVE_LIBGCRYPT
+# include <gcrypt.h>
+#elif HAVE_LIBSSL
+# include <openssl/rand.h>
+# include "SimpleRandomizer.h"
+#endif // HAVE_LIBSSL
+
#include "File.h"
#include "message.h"
#include "Randomizer.h"
@@ -792,4 +801,24 @@ std::map<size_t, std::string> Util::createIndexPathMap(std::istream& i)
return indexPathMap;
}
+namespace util {
+
+void generateRandomData(unsigned char* data, size_t length)
+{
+#ifdef HAVE_LIBGCRYPT
+ gcry_randomize(data, length, GCRY_STRONG_RANDOM);
+#elif HAVE_LIBSSL
+ if(RAND_bytes(data, length) != 1) {
+ for(size_t i = 0; i < length; ++i) {
+ data[i] = SimpleRandomizer::getInstance()->getRandomNumber(UINT8_MAX+1);
+ }
+ }
+#else
+ std::ifstream i("/dev/urandom", std::ios::binary);
+ i.read(data, length);
+#endif // HAVE_LIBSSL
+}
+
+} // namespace util
+
} // namespace aria2
diff --git a/src/Util.h b/src/Util.h
index 06ddaf1f..8640b700 100644
--- a/src/Util.h
+++ b/src/Util.h
@@ -332,6 +332,12 @@ static OutputIterator split(const std::string& src, OutputIterator out,
return out;
}
+namespace util {
+
+void generateRandomData(unsigned char* data, size_t length);
+
+} // namespace util
+
} // namespace aria2
#endif // _D_UTIL_H_
diff --git a/src/bitfield.h b/src/bitfield.h
index 23eb6bb9..26f4d024 100644
--- a/src/bitfield.h
+++ b/src/bitfield.h
@@ -113,6 +113,8 @@ inline size_t countSetBit(const unsigned char* bitfield, size_t nbits)
return count;
}
+void flipBit(unsigned char* data, size_t length, size_t bitIndex);
+
} // namespace bitfield
} // namespace aria2
diff --git a/src/bittorrent_helper.cc b/src/bittorrent_helper.cc
index 27400212..53c8a006 100644
--- a/src/bittorrent_helper.cc
+++ b/src/bittorrent_helper.cc
@@ -52,7 +52,6 @@
#include "a2netcompat.h"
#include "BtConstants.h"
#include "bitfield.h"
-#include "DHTUtil.h"
namespace aria2 {
@@ -625,8 +624,7 @@ std::string generatePeerId(const std::string& peerIdPrefix)
unsigned char buf[20];
int len = 20-peerIdPrefix.size();
if(len > 0) {
- DHTUtil::generateRandomData(buf, len);
-
+ util::generateRandomData(buf, len);
peerId += std::string(&buf[0], &buf[len]);
} if(peerId.size() > 20) {
peerId.erase(20);
@@ -837,6 +835,13 @@ void assertID
}
}
+void generateRandomKey(unsigned char* key)
+{
+ unsigned char bytes[40];
+ util::generateRandomData(bytes, sizeof(bytes));
+ MessageDigestHelper::digest(key, 20, MessageDigestContext::SHA1, bytes, sizeof(bytes));
+}
+
} // namespace bittorrent
} // namespace aria2
diff --git a/src/bittorrent_helper.h b/src/bittorrent_helper.h
index a99c05b0..c9d64579 100644
--- a/src/bittorrent_helper.h
+++ b/src/bittorrent_helper.h
@@ -208,6 +208,8 @@ void assertPayloadLengthEqual
void assertID
(uint8_t expected, const unsigned char* data, const std::string& msgName);
+void generateRandomKey(unsigned char* key);
+
} // namespace bittorrent
} // namespace aria2