mirror of https://github.com/aria2/aria2
Merge branch 'master' into dynamic-select-file
commit
9d40c47799
|
@ -30,7 +30,7 @@ RUN apt-get update && \
|
||||||
|
|
||||||
RUN curl -L -O https://gmplib.org/download/gmp/gmp-6.1.0.tar.lz && \
|
RUN curl -L -O https://gmplib.org/download/gmp/gmp-6.1.0.tar.lz && \
|
||||||
curl -L -o gmp-6.1.0.patch https://gmplib.org/repo/gmp-6.1/raw-rev/67d4ee9dead1 && \
|
curl -L -o gmp-6.1.0.patch https://gmplib.org/repo/gmp-6.1/raw-rev/67d4ee9dead1 && \
|
||||||
curl -L -O http://downloads.sourceforge.net/project/expat/expat/2.1.0/expat-2.1.0.tar.gz && \
|
curl -L -O http://downloads.sourceforge.net/project/expat/expat/2.1.1/expat-2.1.1.tar.gz && \
|
||||||
curl -L -O https://www.sqlite.org/2016/sqlite-autoconf-3120100.tar.gz && \
|
curl -L -O https://www.sqlite.org/2016/sqlite-autoconf-3120100.tar.gz && \
|
||||||
curl -L -O http://zlib.net/zlib-1.2.8.tar.xz && \
|
curl -L -O http://zlib.net/zlib-1.2.8.tar.xz && \
|
||||||
curl -L -O http://c-ares.haxx.se/download/c-ares-1.11.0.tar.gz && \
|
curl -L -O http://c-ares.haxx.se/download/c-ares-1.11.0.tar.gz && \
|
||||||
|
@ -49,8 +49,8 @@ RUN tar xf gmp-6.1.0.tar.lz && \
|
||||||
CFLAGS="-mtune=generic -O2 -g0" && \
|
CFLAGS="-mtune=generic -O2 -g0" && \
|
||||||
make install
|
make install
|
||||||
|
|
||||||
RUN tar xf expat-2.1.0.tar.gz && \
|
RUN tar xf expat-2.1.1.tar.gz && \
|
||||||
cd expat-2.1.0 && \
|
cd expat-2.1.1 && \
|
||||||
./configure \
|
./configure \
|
||||||
--disable-shared \
|
--disable-shared \
|
||||||
--enable-static \
|
--enable-static \
|
||||||
|
|
|
@ -43,7 +43,7 @@ aria2c executable was generated using android-ndk-r10d.
|
||||||
The following libraries were statically linked.
|
The following libraries were statically linked.
|
||||||
|
|
||||||
* openssl 1.0.2h
|
* openssl 1.0.2h
|
||||||
* expat 2.1.0
|
* expat 2.1.1
|
||||||
* c-ares 1.11.0
|
* c-ares 1.11.0
|
||||||
* libssh2 1.7.0
|
* libssh2 1.7.0
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ The executable is statically linked, so no extra DLLs are
|
||||||
necessary. The linked libraries are:
|
necessary. The linked libraries are:
|
||||||
|
|
||||||
* gmp 6.1.0
|
* gmp 6.1.0
|
||||||
* expat 2.1.0
|
* expat 2.1.1
|
||||||
* sqlite 3.10.2
|
* sqlite 3.10.2
|
||||||
* zlib 1.2.8
|
* zlib 1.2.8
|
||||||
* c-ares 1.11.0
|
* c-ares 1.11.0
|
||||||
|
|
|
@ -48,16 +48,12 @@ namespace aria2 {
|
||||||
|
|
||||||
const char BtBitfieldMessage::NAME[] = "bitfield";
|
const char BtBitfieldMessage::NAME[] = "bitfield";
|
||||||
|
|
||||||
BtBitfieldMessage::BtBitfieldMessage()
|
BtBitfieldMessage::BtBitfieldMessage() : SimpleBtMessage(ID, NAME) {}
|
||||||
: SimpleBtMessage(ID, NAME), bitfieldLength_(0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
BtBitfieldMessage::BtBitfieldMessage(const unsigned char* bitfield,
|
BtBitfieldMessage::BtBitfieldMessage(const unsigned char* bitfield,
|
||||||
size_t bitfieldLength)
|
size_t bitfieldLength)
|
||||||
: SimpleBtMessage(ID, NAME), bitfieldLength_(0)
|
: SimpleBtMessage(ID, NAME), bitfield_(bitfield, bitfield + bitfieldLength)
|
||||||
{
|
{
|
||||||
setBitfield(bitfield, bitfieldLength);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
BtBitfieldMessage::~BtBitfieldMessage() {}
|
BtBitfieldMessage::~BtBitfieldMessage() {}
|
||||||
|
@ -65,13 +61,7 @@ BtBitfieldMessage::~BtBitfieldMessage() {}
|
||||||
void BtBitfieldMessage::setBitfield(const unsigned char* bitfield,
|
void BtBitfieldMessage::setBitfield(const unsigned char* bitfield,
|
||||||
size_t bitfieldLength)
|
size_t bitfieldLength)
|
||||||
{
|
{
|
||||||
if (bitfield_.get() == bitfield) {
|
bitfield_.assign(bitfield, bitfield + bitfieldLength);
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bitfieldLength_ = bitfieldLength;
|
|
||||||
bitfield_ = make_unique<unsigned char[]>(bitfieldLength_);
|
|
||||||
memcpy(bitfield_.get(), bitfield, bitfieldLength_);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<BtBitfieldMessage>
|
std::unique_ptr<BtBitfieldMessage>
|
||||||
|
@ -89,15 +79,15 @@ void BtBitfieldMessage::doReceivedAction()
|
||||||
if (isMetadataGetMode()) {
|
if (isMetadataGetMode()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
getPieceStorage()->updatePieceStats(bitfield_.get(), bitfieldLength_,
|
getPieceStorage()->updatePieceStats(bitfield_.data(), bitfield_.size(),
|
||||||
getPeer()->getBitfield());
|
getPeer()->getBitfield());
|
||||||
getPeer()->setBitfield(bitfield_.get(), bitfieldLength_);
|
getPeer()->setBitfield(bitfield_.data(), bitfield_.size());
|
||||||
if (getPeer()->isSeeder() && getPieceStorage()->downloadFinished()) {
|
if (getPeer()->isSeeder() && getPieceStorage()->downloadFinished()) {
|
||||||
throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
|
throw DL_ABORT_EX(MSG_GOOD_BYE_SEEDER);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* BtBitfieldMessage::createMessage()
|
std::vector<unsigned char> BtBitfieldMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 1+bitfieldLength, 4bytes
|
* len --- 1+bitfieldLength, 4bytes
|
||||||
|
@ -105,20 +95,19 @@ unsigned char* BtBitfieldMessage::createMessage()
|
||||||
* bitfield --- bitfield, bitfieldLength bytes
|
* bitfield --- bitfield, bitfieldLength bytes
|
||||||
* total: 5+bitfieldLength bytes
|
* total: 5+bitfieldLength bytes
|
||||||
*/
|
*/
|
||||||
const size_t msgLength = 5 + bitfieldLength_;
|
const size_t msgLength = 5 + bitfield_.size();
|
||||||
auto msg = new unsigned char[msgLength];
|
auto msg = std::vector<unsigned char>(msgLength);
|
||||||
bittorrent::createPeerMessageString(msg, msgLength, 1 + bitfieldLength_, ID);
|
bittorrent::createPeerMessageString(msg.data(), msgLength,
|
||||||
memcpy(msg + 5, bitfield_.get(), bitfieldLength_);
|
1 + bitfield_.size(), ID);
|
||||||
|
std::copy(std::begin(bitfield_), std::end(bitfield_), std::begin(msg) + 5);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BtBitfieldMessage::getMessageLength() { return 5 + bitfieldLength_; }
|
|
||||||
|
|
||||||
std::string BtBitfieldMessage::toString() const
|
std::string BtBitfieldMessage::toString() const
|
||||||
{
|
{
|
||||||
std::string s = NAME;
|
std::string s = NAME;
|
||||||
s += " ";
|
s += ' ';
|
||||||
s += util::toHex(bitfield_.get(), bitfieldLength_);
|
s += util::toHex(bitfield_.data(), bitfield_.size());
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,8 +41,7 @@ namespace aria2 {
|
||||||
|
|
||||||
class BtBitfieldMessage : public SimpleBtMessage {
|
class BtBitfieldMessage : public SimpleBtMessage {
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<unsigned char[]> bitfield_;
|
std::vector<unsigned char> bitfield_;
|
||||||
size_t bitfieldLength_;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BtBitfieldMessage();
|
BtBitfieldMessage();
|
||||||
|
@ -57,18 +56,16 @@ public:
|
||||||
|
|
||||||
void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
|
void setBitfield(const unsigned char* bitfield, size_t bitfieldLength);
|
||||||
|
|
||||||
const unsigned char* getBitfield() const { return bitfield_.get(); }
|
const unsigned char* getBitfield() const { return bitfield_.data(); }
|
||||||
|
|
||||||
size_t getBitfieldLength() const { return bitfieldLength_; }
|
size_t getBitfieldLength() const { return bitfield_.size(); }
|
||||||
|
|
||||||
static std::unique_ptr<BtBitfieldMessage> create(const unsigned char* data,
|
static std::unique_ptr<BtBitfieldMessage> create(const unsigned char* data,
|
||||||
size_t dataLength);
|
size_t dataLength);
|
||||||
|
|
||||||
virtual void doReceivedAction() CXX11_OVERRIDE;
|
virtual void doReceivedAction() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual std::string toString() const CXX11_OVERRIDE;
|
virtual std::string toString() const CXX11_OVERRIDE;
|
||||||
};
|
};
|
||||||
|
|
|
@ -58,7 +58,7 @@ BtExtendedMessage::BtExtendedMessage(
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* BtExtendedMessage::createMessage()
|
std::vector<unsigned char> BtExtendedMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 2+extpayload.length, 4bytes
|
* len --- 2+extpayload.length, 4bytes
|
||||||
|
@ -69,21 +69,15 @@ unsigned char* BtExtendedMessage::createMessage()
|
||||||
*/
|
*/
|
||||||
std::string payload = extensionMessage_->getPayload();
|
std::string payload = extensionMessage_->getPayload();
|
||||||
msgLength_ = 6 + payload.size();
|
msgLength_ = 6 + payload.size();
|
||||||
auto msg = new unsigned char[msgLength_];
|
auto msg = std::vector<unsigned char>(msgLength_);
|
||||||
bittorrent::createPeerMessageString(msg, msgLength_, 2 + payload.size(), ID);
|
auto p = msg.data();
|
||||||
*(msg + 5) = extensionMessage_->getExtensionMessageID();
|
bittorrent::createPeerMessageString(p, msgLength_, 2 + payload.size(), ID);
|
||||||
memcpy(msg + 6, payload.data(), payload.size());
|
p += 5;
|
||||||
|
*p++ = extensionMessage_->getExtensionMessageID();
|
||||||
|
std::copy(std::begin(payload), std::end(payload), p);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BtExtendedMessage::getMessageLength()
|
|
||||||
{
|
|
||||||
if (!msgLength_) {
|
|
||||||
msgLength_ = 6 + extensionMessage_->getPayload().size();
|
|
||||||
}
|
|
||||||
return msgLength_;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool BtExtendedMessage::sendPredicate() const
|
bool BtExtendedMessage::sendPredicate() const
|
||||||
{
|
{
|
||||||
return getPeer()->isExtendedMessagingEnabled();
|
return getPeer()->isExtendedMessagingEnabled();
|
||||||
|
|
|
@ -61,9 +61,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction() CXX11_OVERRIDE;
|
virtual void doReceivedAction() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual bool sendPredicate() const CXX11_OVERRIDE;
|
virtual bool sendPredicate() const CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -81,10 +81,10 @@ BtHandshakeMessage::create(const unsigned char* data, size_t dataLength)
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* BtHandshakeMessage::createMessage()
|
std::vector<unsigned char> BtHandshakeMessage::createMessage()
|
||||||
{
|
{
|
||||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||||
auto dst = msg;
|
auto dst = msg.data();
|
||||||
*dst++ = pstrlen_;
|
*dst++ = pstrlen_;
|
||||||
dst = std::copy(std::begin(pstr_), std::end(pstr_), dst);
|
dst = std::copy(std::begin(pstr_), std::end(pstr_), dst);
|
||||||
dst = std::copy(std::begin(reserved_), std::end(reserved_), dst);
|
dst = std::copy(std::begin(reserved_), std::end(reserved_), dst);
|
||||||
|
@ -93,8 +93,6 @@ unsigned char* BtHandshakeMessage::createMessage()
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BtHandshakeMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
|
||||||
|
|
||||||
std::string BtHandshakeMessage::toString() const
|
std::string BtHandshakeMessage::toString() const
|
||||||
{
|
{
|
||||||
return fmt("%s peerId=%s, reserved=%s", NAME,
|
return fmt("%s peerId=%s, reserved=%s", NAME,
|
||||||
|
|
|
@ -79,9 +79,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction() CXX11_OVERRIDE{};
|
virtual void doReceivedAction() CXX11_OVERRIDE{};
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual std::string toString() const CXX11_OVERRIDE;
|
virtual std::string toString() const CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -39,17 +39,13 @@ namespace aria2 {
|
||||||
|
|
||||||
const char BtKeepAliveMessage::NAME[] = "keep alive";
|
const char BtKeepAliveMessage::NAME[] = "keep alive";
|
||||||
|
|
||||||
unsigned char* BtKeepAliveMessage::createMessage()
|
std::vector<unsigned char> BtKeepAliveMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 0, 4bytes
|
* len --- 0, 4bytes
|
||||||
* total: 4bytes
|
* total: 4bytes
|
||||||
*/
|
*/
|
||||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
return std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||||
memset(msg, 0, MESSAGE_LENGTH);
|
|
||||||
return msg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BtKeepAliveMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -52,9 +52,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction() CXX11_OVERRIDE {}
|
virtual void doReceivedAction() CXX11_OVERRIDE {}
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual std::string toString() const CXX11_OVERRIDE { return NAME; }
|
virtual std::string toString() const CXX11_OVERRIDE { return NAME; }
|
||||||
};
|
};
|
||||||
|
|
|
@ -213,17 +213,16 @@ void BtPieceMessage::send()
|
||||||
void BtPieceMessage::pushPieceData(int64_t offset, int32_t length) const
|
void BtPieceMessage::pushPieceData(int64_t offset, int32_t length) const
|
||||||
{
|
{
|
||||||
assert(length <= static_cast<int32_t>(16_k));
|
assert(length <= static_cast<int32_t>(16_k));
|
||||||
auto buf = make_unique<unsigned char[]>(length + MESSAGE_HEADER_LENGTH);
|
auto buf = std::vector<unsigned char>(length + MESSAGE_HEADER_LENGTH);
|
||||||
createMessageHeader(buf.get());
|
createMessageHeader(buf.data());
|
||||||
ssize_t r;
|
ssize_t r;
|
||||||
r = getPieceStorage()->getDiskAdaptor()->readData(
|
r = getPieceStorage()->getDiskAdaptor()->readData(
|
||||||
buf.get() + MESSAGE_HEADER_LENGTH, length, offset);
|
buf.data() + MESSAGE_HEADER_LENGTH, length, offset);
|
||||||
if (r == length) {
|
if (r == length) {
|
||||||
const auto& peer = getPeer();
|
const auto& peer = getPeer();
|
||||||
getPeerConnection()->pushBytes(
|
getPeerConnection()->pushBytes(
|
||||||
buf.release(), length + MESSAGE_HEADER_LENGTH,
|
std::move(buf), make_unique<PieceSendUpdate>(downloadContext_, peer,
|
||||||
make_unique<PieceSendUpdate>(downloadContext_, peer,
|
MESSAGE_HEADER_LENGTH));
|
||||||
MESSAGE_HEADER_LENGTH));
|
|
||||||
peer->updateUploadSpeed(length);
|
peer->updateUploadSpeed(length);
|
||||||
downloadContext_->updateUploadSpeed(length);
|
downloadContext_->updateUploadSpeed(length);
|
||||||
}
|
}
|
||||||
|
|
|
@ -100,7 +100,7 @@ void BtPortMessage::doReceivedAction()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* BtPortMessage::createMessage()
|
std::vector<unsigned char> BtPortMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 5, 4bytes
|
* len --- 5, 4bytes
|
||||||
|
@ -108,14 +108,12 @@ unsigned char* BtPortMessage::createMessage()
|
||||||
* port --- port number, 2bytes
|
* port --- port number, 2bytes
|
||||||
* total: 7bytes
|
* total: 7bytes
|
||||||
*/
|
*/
|
||||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 3, ID);
|
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 3, ID);
|
||||||
bittorrent::setShortIntParam(&msg[5], port_);
|
bittorrent::setShortIntParam(&msg[5], port_);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t BtPortMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
|
||||||
|
|
||||||
std::string BtPortMessage::toString() const
|
std::string BtPortMessage::toString() const
|
||||||
{
|
{
|
||||||
return fmt("%s port=%u", NAME, port_);
|
return fmt("%s port=%u", NAME, port_);
|
||||||
|
|
|
@ -71,9 +71,7 @@ public:
|
||||||
|
|
||||||
virtual void doReceivedAction() CXX11_OVERRIDE;
|
virtual void doReceivedAction() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual std::string toString() const CXX11_OVERRIDE;
|
virtual std::string toString() const CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -626,7 +626,7 @@ void DefaultPieceStorage::initStorage()
|
||||||
{
|
{
|
||||||
if (downloadContext_->getFileEntries().size() == 1) {
|
if (downloadContext_->getFileEntries().size() == 1) {
|
||||||
A2_LOG_DEBUG("Instantiating DirectDiskAdaptor");
|
A2_LOG_DEBUG("Instantiating DirectDiskAdaptor");
|
||||||
auto directDiskAdaptor = make_unique<DirectDiskAdaptor>();
|
auto directDiskAdaptor = std::make_shared<DirectDiskAdaptor>();
|
||||||
directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
|
directDiskAdaptor->setTotalLength(downloadContext_->getTotalLength());
|
||||||
directDiskAdaptor->setFileEntries(
|
directDiskAdaptor->setFileEntries(
|
||||||
downloadContext_->getFileEntries().begin(),
|
downloadContext_->getFileEntries().begin(),
|
||||||
|
@ -638,7 +638,7 @@ void DefaultPieceStorage::initStorage()
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
A2_LOG_DEBUG("Instantiating MultiDiskAdaptor");
|
A2_LOG_DEBUG("Instantiating MultiDiskAdaptor");
|
||||||
auto multiDiskAdaptor = make_unique<MultiDiskAdaptor>();
|
auto multiDiskAdaptor = std::make_shared<MultiDiskAdaptor>();
|
||||||
multiDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
|
multiDiskAdaptor->setFileEntries(downloadContext_->getFileEntries().begin(),
|
||||||
downloadContext_->getFileEntries().end());
|
downloadContext_->getFileEntries().end());
|
||||||
multiDiskAdaptor->setPieceLength(downloadContext_->getPieceLength());
|
multiDiskAdaptor->setPieceLength(downloadContext_->getPieceLength());
|
||||||
|
|
|
@ -39,28 +39,11 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
|
||||||
bool FallocFileAllocationIterator::gainPrivilegeAttempted_ = false;
|
|
||||||
#endif // __MINGW32__
|
|
||||||
|
|
||||||
FallocFileAllocationIterator::FallocFileAllocationIterator(BinaryStream* stream,
|
FallocFileAllocationIterator::FallocFileAllocationIterator(BinaryStream* stream,
|
||||||
int64_t offset,
|
int64_t offset,
|
||||||
int64_t totalLength)
|
int64_t totalLength)
|
||||||
: stream_(stream), offset_(offset), totalLength_(totalLength)
|
: stream_(stream), offset_(offset), totalLength_(totalLength)
|
||||||
{
|
{
|
||||||
#ifdef __MINGW32__
|
|
||||||
// Windows build: --file-allocation=falloc uses SetFileValidData
|
|
||||||
// which requires SE_MANAGE_VOLUME_NAME privilege. SetFileValidData
|
|
||||||
// has security implications (see
|
|
||||||
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365544%28v=vs.85%29.aspx).
|
|
||||||
if (!gainPrivilegeAttempted_) {
|
|
||||||
if (!util::gainPrivilege(SE_MANAGE_VOLUME_NAME)) {
|
|
||||||
A2_LOG_WARN("--file-allocation=falloc will not work properly.");
|
|
||||||
}
|
|
||||||
|
|
||||||
gainPrivilegeAttempted_ = true;
|
|
||||||
}
|
|
||||||
#endif // __MINGW32__
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void FallocFileAllocationIterator::allocateChunk()
|
void FallocFileAllocationIterator::allocateChunk()
|
||||||
|
|
|
@ -47,10 +47,6 @@ private:
|
||||||
int64_t offset_;
|
int64_t offset_;
|
||||||
int64_t totalLength_;
|
int64_t totalLength_;
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
|
||||||
static bool gainPrivilegeAttempted_;
|
|
||||||
#endif // __MINGW32__
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
FallocFileAllocationIterator(BinaryStream* stream, int64_t offset,
|
FallocFileAllocationIterator(BinaryStream* stream, int64_t offset,
|
||||||
int64_t totalLength);
|
int64_t totalLength);
|
||||||
|
|
|
@ -39,7 +39,7 @@
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
unsigned char* IndexBtMessage::createMessage()
|
std::vector<unsigned char> IndexBtMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 5, 4bytes
|
* len --- 5, 4bytes
|
||||||
|
@ -47,14 +47,12 @@ unsigned char* IndexBtMessage::createMessage()
|
||||||
* piece index --- index, 4bytes
|
* piece index --- index, 4bytes
|
||||||
* total: 9bytes
|
* total: 9bytes
|
||||||
*/
|
*/
|
||||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 5, getId());
|
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 5, getId());
|
||||||
bittorrent::setIntParam(&msg[5], index_);
|
bittorrent::setIntParam(&msg[5], index_);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t IndexBtMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
|
||||||
|
|
||||||
std::string IndexBtMessage::toString() const
|
std::string IndexBtMessage::toString() const
|
||||||
{
|
{
|
||||||
return fmt("%s index=%lu", getName(), static_cast<unsigned long>(index_));
|
return fmt("%s index=%lu", getName(), static_cast<unsigned long>(index_));
|
||||||
|
|
|
@ -65,9 +65,7 @@ public:
|
||||||
|
|
||||||
size_t getIndex() const { return index_; }
|
size_t getIndex() const { return index_; }
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual std::string toString() const CXX11_OVERRIDE;
|
virtual std::string toString() const CXX11_OVERRIDE;
|
||||||
};
|
};
|
||||||
|
|
|
@ -64,7 +64,7 @@ namespace {
|
||||||
|
|
||||||
const size_t MAX_PAD_LENGTH = 512;
|
const size_t MAX_PAD_LENGTH = 512;
|
||||||
const size_t CRYPTO_BITFIELD_LENGTH = 4;
|
const size_t CRYPTO_BITFIELD_LENGTH = 4;
|
||||||
const unsigned char VC[] = {0, 0, 0, 0, 0, 0, 0, 0};
|
constexpr auto VC = std::array<unsigned char, MSEHandshake::VC_LENGTH>{};
|
||||||
|
|
||||||
const unsigned char* PRIME = reinterpret_cast<const unsigned char*>(
|
const unsigned char* PRIME = reinterpret_cast<const unsigned char*>(
|
||||||
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B"
|
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74020BBEA63B"
|
||||||
|
@ -123,13 +123,15 @@ void MSEHandshake::initEncryptionFacility(bool initiator)
|
||||||
void MSEHandshake::sendPublicKey()
|
void MSEHandshake::sendPublicKey()
|
||||||
{
|
{
|
||||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Sending public key.", cuid_));
|
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Sending public key.", cuid_));
|
||||||
auto buf = make_unique<unsigned char[]>(KEY_LENGTH + MAX_PAD_LENGTH);
|
auto buf = std::vector<unsigned char>(KEY_LENGTH + MAX_PAD_LENGTH);
|
||||||
dh_->getPublicKey(buf.get(), KEY_LENGTH);
|
dh_->getPublicKey(buf.data(), KEY_LENGTH);
|
||||||
|
|
||||||
size_t padLength =
|
size_t padLength =
|
||||||
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
||||||
dh_->generateNonce(buf.get() + KEY_LENGTH, padLength);
|
dh_->generateNonce(buf.data() + KEY_LENGTH, padLength);
|
||||||
socketBuffer_.pushBytes(buf.release(), KEY_LENGTH + padLength);
|
buf.resize(KEY_LENGTH + padLength);
|
||||||
|
|
||||||
|
socketBuffer_.pushBytes(std::move(buf));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MSEHandshake::read()
|
void MSEHandshake::read()
|
||||||
|
@ -209,16 +211,16 @@ void MSEHandshake::initCipher(const unsigned char* infoHash)
|
||||||
enc.init(peerCipherKey, sizeof(peerCipherKey));
|
enc.init(peerCipherKey, sizeof(peerCipherKey));
|
||||||
// discard first 1024 bytes ARC4 output.
|
// discard first 1024 bytes ARC4 output.
|
||||||
enc.encrypt(garbage.size(), garbage.data(), garbage.data());
|
enc.encrypt(garbage.size(), garbage.data(), garbage.data());
|
||||||
enc.encrypt(VC_LENGTH, initiatorVCMarker_, VC);
|
enc.encrypt(VC_LENGTH, initiatorVCMarker_, VC.data());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Given data is pushed to socketBuffer_ and data will be deleted by
|
// Given data is pushed to socketBuffer_ and data will be deleted by
|
||||||
// socketBuffer_.
|
// socketBuffer_.
|
||||||
void MSEHandshake::encryptAndSendData(unsigned char* data, size_t length)
|
void MSEHandshake::encryptAndSendData(std::vector<unsigned char> data)
|
||||||
{
|
{
|
||||||
encryptor_->encrypt(length, data, data);
|
encryptor_->encrypt(data.size(), data.data(), data.data());
|
||||||
socketBuffer_.pushBytes(data, length);
|
socketBuffer_.pushBytes(std::move(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void MSEHandshake::createReq1Hash(unsigned char* md) const
|
void MSEHandshake::createReq1Hash(unsigned char* md) const
|
||||||
|
@ -264,27 +266,25 @@ void MSEHandshake::sendInitiatorStep2()
|
||||||
{
|
{
|
||||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Sending negotiation step2.", cuid_));
|
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Sending negotiation step2.", cuid_));
|
||||||
// Assuming no exception
|
// Assuming no exception
|
||||||
auto md = make_unique<unsigned char[]>((size_t)20);
|
auto md = std::vector<unsigned char>(20);
|
||||||
createReq1Hash(md.get());
|
createReq1Hash(md.data());
|
||||||
socketBuffer_.pushBytes(md.release(), 20);
|
socketBuffer_.pushBytes(std::move(md));
|
||||||
// Assuming no exception
|
// Assuming no exception
|
||||||
md = make_unique<unsigned char[]>((size_t)20);
|
md = std::vector<unsigned char>(20);
|
||||||
createReq23Hash(md.get(), infoHash_);
|
createReq23Hash(md.data(), infoHash_);
|
||||||
socketBuffer_.pushBytes(md.release(), 20);
|
socketBuffer_.pushBytes(std::move(md));
|
||||||
// buffer is filled in this order:
|
// buffer is filled in this order:
|
||||||
// VC(VC_LENGTH bytes),
|
// VC(VC_LENGTH bytes),
|
||||||
// crypto_provide(CRYPTO_BITFIELD_LENGTH bytes),
|
// crypto_provide(CRYPTO_BITFIELD_LENGTH bytes),
|
||||||
// len(padC)(2 bytes),
|
// len(padC)(2 bytes),
|
||||||
// padC(len(padC) bytes <= MAX_PAD_LENGTH),
|
// padC(len(padC) bytes <= MAX_PAD_LENGTH),
|
||||||
// len(IA)(2 bytes)
|
// len(IA)(2 bytes)
|
||||||
auto buffer = make_unique<unsigned char[]>(
|
auto buffer = std::vector<unsigned char>(VC_LENGTH + CRYPTO_BITFIELD_LENGTH +
|
||||||
40 + VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 + MAX_PAD_LENGTH + 2);
|
2 + MAX_PAD_LENGTH + 2);
|
||||||
unsigned char* ptr = buffer.get();
|
auto ptr = std::begin(buffer);
|
||||||
// VC
|
// VC
|
||||||
memcpy(ptr, VC, sizeof(VC));
|
ptr += VC_LENGTH;
|
||||||
ptr += sizeof(VC);
|
|
||||||
// crypto_provide
|
// crypto_provide
|
||||||
memset(ptr, 0, CRYPTO_BITFIELD_LENGTH);
|
|
||||||
if (!option_->getAsBool(PREF_BT_FORCE_ENCRYPTION) &&
|
if (!option_->getAsBool(PREF_BT_FORCE_ENCRYPTION) &&
|
||||||
option_->get(PREF_BT_MIN_CRYPTO_LEVEL) == V_PLAIN) {
|
option_->get(PREF_BT_MIN_CRYPTO_LEVEL) == V_PLAIN) {
|
||||||
ptr[3] = CRYPTO_PLAIN_TEXT;
|
ptr[3] = CRYPTO_PLAIN_TEXT;
|
||||||
|
@ -294,24 +294,21 @@ void MSEHandshake::sendInitiatorStep2()
|
||||||
// len(padC)
|
// len(padC)
|
||||||
uint16_t padCLength =
|
uint16_t padCLength =
|
||||||
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
||||||
{
|
uint16_t padCLengthBE = htons(padCLength);
|
||||||
uint16_t padCLengthBE = htons(padCLength);
|
ptr = std::copy_n(reinterpret_cast<unsigned char*>(&padCLengthBE),
|
||||||
memcpy(ptr, &padCLengthBE, sizeof(padCLengthBE));
|
sizeof(padCLengthBE), ptr);
|
||||||
}
|
|
||||||
ptr += 2;
|
|
||||||
// padC
|
// padC
|
||||||
memset(ptr, 0, padCLength);
|
|
||||||
ptr += padCLength;
|
ptr += padCLength;
|
||||||
// len(IA)
|
// len(IA)
|
||||||
// currently, IA is zero-length.
|
// currently, IA is zero-length.
|
||||||
uint16_t iaLength = 0;
|
uint16_t iaLength = 0;
|
||||||
{
|
{
|
||||||
uint16_t iaLengthBE = htons(iaLength);
|
uint16_t iaLengthBE = htons(iaLength);
|
||||||
memcpy(ptr, &iaLengthBE, sizeof(iaLengthBE));
|
ptr = std::copy_n(reinterpret_cast<unsigned char*>(&iaLengthBE),
|
||||||
|
sizeof(iaLengthBE), ptr);
|
||||||
}
|
}
|
||||||
ptr += 2;
|
buffer.erase(ptr, std::end(buffer));
|
||||||
size_t buflen = ptr - buffer.get();
|
encryptAndSendData(std::move(buffer));
|
||||||
encryptAndSendData(buffer.release(), buflen);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// This function reads exactly until the end of VC marker is reached.
|
// This function reads exactly until the end of VC marker is reached.
|
||||||
|
@ -494,8 +491,8 @@ bool MSEHandshake::receiveReceiverIA()
|
||||||
wantRead_ = true;
|
wantRead_ = true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
ia_ = make_unique<unsigned char[]>(iaLength_);
|
ia_ = std::vector<unsigned char>(iaLength_);
|
||||||
decryptor_->encrypt(iaLength_, ia_.get(), rbuf_);
|
decryptor_->encrypt(iaLength_, ia_.data(), rbuf_);
|
||||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - IA received.", cuid_));
|
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - IA received.", cuid_));
|
||||||
// shift rbuf_
|
// shift rbuf_
|
||||||
shiftBuffer(iaLength_);
|
shiftBuffer(iaLength_);
|
||||||
|
@ -509,27 +506,24 @@ void MSEHandshake::sendReceiverStep2()
|
||||||
// cryptoSelect(CRYPTO_BITFIELD_LENGTH bytes),
|
// cryptoSelect(CRYPTO_BITFIELD_LENGTH bytes),
|
||||||
// len(padD)(2bytes),
|
// len(padD)(2bytes),
|
||||||
// padD(len(padD)bytes <= MAX_PAD_LENGTH)
|
// padD(len(padD)bytes <= MAX_PAD_LENGTH)
|
||||||
auto buffer = make_unique<unsigned char[]>(
|
auto buffer = std::vector<unsigned char>(VC_LENGTH + CRYPTO_BITFIELD_LENGTH +
|
||||||
VC_LENGTH + CRYPTO_BITFIELD_LENGTH + 2 + MAX_PAD_LENGTH);
|
2 + MAX_PAD_LENGTH);
|
||||||
unsigned char* ptr = buffer.get();
|
auto ptr = std::begin(buffer);
|
||||||
// VC
|
// VC
|
||||||
memcpy(ptr, VC, sizeof(VC));
|
ptr += VC_LENGTH;
|
||||||
ptr += sizeof(VC);
|
|
||||||
// crypto_select
|
// crypto_select
|
||||||
memset(ptr, 0, CRYPTO_BITFIELD_LENGTH);
|
|
||||||
ptr[3] = negotiatedCryptoType_;
|
ptr[3] = negotiatedCryptoType_;
|
||||||
ptr += CRYPTO_BITFIELD_LENGTH;
|
ptr += CRYPTO_BITFIELD_LENGTH;
|
||||||
// len(padD)
|
// len(padD)
|
||||||
uint16_t padDLength =
|
uint16_t padDLength =
|
||||||
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
SimpleRandomizer::getInstance()->getRandomNumber(MAX_PAD_LENGTH + 1);
|
||||||
uint16_t padDLengthBE = htons(padDLength);
|
uint16_t padDLengthBE = htons(padDLength);
|
||||||
memcpy(ptr, &padDLengthBE, sizeof(padDLengthBE));
|
ptr = std::copy_n(reinterpret_cast<unsigned char*>(&padDLengthBE),
|
||||||
ptr += sizeof(padDLengthBE);
|
sizeof(padDLengthBE), ptr);
|
||||||
// padD, all zeroed
|
// padD, all zeroed
|
||||||
memset(ptr, 0, padDLength);
|
|
||||||
ptr += padDLength;
|
ptr += padDLength;
|
||||||
size_t buflen = ptr - buffer.get();
|
buffer.erase(ptr, std::end(buffer));
|
||||||
encryptAndSendData(buffer.release(), buflen);
|
encryptAndSendData(std::move(buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t MSEHandshake::verifyPadLength(const unsigned char* padlenbuf,
|
uint16_t MSEHandshake::verifyPadLength(const unsigned char* padlenbuf,
|
||||||
|
@ -549,7 +543,7 @@ void MSEHandshake::verifyVC(unsigned char* vcbuf)
|
||||||
{
|
{
|
||||||
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Verifying VC.", cuid_));
|
A2_LOG_DEBUG(fmt("CUID#%" PRId64 " - Verifying VC.", cuid_));
|
||||||
decryptor_->encrypt(VC_LENGTH, vcbuf, vcbuf);
|
decryptor_->encrypt(VC_LENGTH, vcbuf, vcbuf);
|
||||||
if (memcmp(VC, vcbuf, VC_LENGTH) != 0) {
|
if (!std::equal(std::begin(VC), std::end(VC), vcbuf)) {
|
||||||
throw DL_ABORT_EX(
|
throw DL_ABORT_EX(
|
||||||
fmt("Invalid VC: %s", util::toHex(vcbuf, VC_LENGTH).c_str()));
|
fmt("Invalid VC: %s", util::toHex(vcbuf, VC_LENGTH).c_str()));
|
||||||
}
|
}
|
||||||
|
|
|
@ -67,14 +67,15 @@ public:
|
||||||
CRYPTO_ARC4 = 0x02u
|
CRYPTO_ARC4 = 0x02u
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static constexpr size_t VC_LENGTH = 8U;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static const size_t PRIME_BITS = 768U;
|
static constexpr size_t PRIME_BITS = 768U;
|
||||||
static const size_t KEY_LENGTH = (PRIME_BITS + 7U) / 8U;
|
static constexpr size_t KEY_LENGTH = (PRIME_BITS + 7U) / 8U;
|
||||||
static const size_t VC_LENGTH = 8U;
|
|
||||||
// The largest buffering occurs when receiver receives step2
|
// The largest buffering occurs when receiver receives step2
|
||||||
// handshake. We believe that IA is less than or equal to
|
// handshake. We believe that IA is less than or equal to
|
||||||
// BtHandshakeMessage::MESSAGE_LENGTH
|
// BtHandshakeMessage::MESSAGE_LENGTH
|
||||||
static const size_t MAX_BUFFER_LENGTH = 636U;
|
static constexpr size_t MAX_BUFFER_LENGTH = 636U;
|
||||||
|
|
||||||
cuid_t cuid_;
|
cuid_t cuid_;
|
||||||
std::shared_ptr<SocketCore> socket_;
|
std::shared_ptr<SocketCore> socket_;
|
||||||
|
@ -97,10 +98,10 @@ private:
|
||||||
size_t markerIndex_;
|
size_t markerIndex_;
|
||||||
uint16_t padLength_;
|
uint16_t padLength_;
|
||||||
uint16_t iaLength_;
|
uint16_t iaLength_;
|
||||||
std::unique_ptr<unsigned char[]> ia_;
|
std::vector<unsigned char> ia_;
|
||||||
std::unique_ptr<MessageDigest> sha1_;
|
std::unique_ptr<MessageDigest> sha1_;
|
||||||
|
|
||||||
void encryptAndSendData(unsigned char* data, size_t length);
|
void encryptAndSendData(std::vector<unsigned char> data);
|
||||||
|
|
||||||
void createReq1Hash(unsigned char* md) const;
|
void createReq1Hash(unsigned char* md) const;
|
||||||
|
|
||||||
|
@ -171,7 +172,7 @@ public:
|
||||||
void sendReceiverStep2();
|
void sendReceiverStep2();
|
||||||
|
|
||||||
// returns plain text IA
|
// returns plain text IA
|
||||||
const unsigned char* getIA() const { return ia_.get(); }
|
const unsigned char* getIA() const { return ia_.data(); }
|
||||||
|
|
||||||
size_t getIALength() const { return iaLength_; }
|
size_t getIALength() const { return iaLength_; }
|
||||||
|
|
||||||
|
|
|
@ -83,13 +83,13 @@ PeerConnection::PeerConnection(cuid_t cuid, const std::shared_ptr<Peer>& peer,
|
||||||
|
|
||||||
PeerConnection::~PeerConnection() {}
|
PeerConnection::~PeerConnection() {}
|
||||||
|
|
||||||
void PeerConnection::pushBytes(unsigned char* data, size_t len,
|
void PeerConnection::pushBytes(std::vector<unsigned char> data,
|
||||||
std::unique_ptr<ProgressUpdate> progressUpdate)
|
std::unique_ptr<ProgressUpdate> progressUpdate)
|
||||||
{
|
{
|
||||||
if (encryptionEnabled_) {
|
if (encryptionEnabled_) {
|
||||||
encryptor_->encrypt(len, data, data);
|
encryptor_->encrypt(data.size(), data.data(), data.data());
|
||||||
}
|
}
|
||||||
socketBuffer_.pushBytes(data, len, std::move(progressUpdate));
|
socketBuffer_.pushBytes(std::move(data), std::move(progressUpdate));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength)
|
bool PeerConnection::receiveMessage(unsigned char* data, size_t& dataLength)
|
||||||
|
|
|
@ -95,7 +95,7 @@ public:
|
||||||
|
|
||||||
// Pushes data into send buffer. After this call, this object gets
|
// Pushes data into send buffer. After this call, this object gets
|
||||||
// ownership of data, so caller must not delete or alter it.
|
// ownership of data, so caller must not delete or alter it.
|
||||||
void pushBytes(unsigned char* data, size_t len,
|
void pushBytes(std::vector<unsigned char> data,
|
||||||
std::unique_ptr<ProgressUpdate> progressUpdate =
|
std::unique_ptr<ProgressUpdate> progressUpdate =
|
||||||
std::unique_ptr<ProgressUpdate>{});
|
std::unique_ptr<ProgressUpdate>{});
|
||||||
|
|
||||||
|
|
|
@ -45,7 +45,7 @@ RangeBtMessage::RangeBtMessage(uint8_t id, const char* name, size_t index,
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* RangeBtMessage::createMessage()
|
std::vector<unsigned char> RangeBtMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 13, 4bytes
|
* len --- 13, 4bytes
|
||||||
|
@ -55,16 +55,14 @@ unsigned char* RangeBtMessage::createMessage()
|
||||||
* length -- length, 4bytes
|
* length -- length, 4bytes
|
||||||
* total: 17bytes
|
* total: 17bytes
|
||||||
*/
|
*/
|
||||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 13, getId());
|
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 13, getId());
|
||||||
bittorrent::setIntParam(&msg[5], index_);
|
bittorrent::setIntParam(&msg[5], index_);
|
||||||
bittorrent::setIntParam(&msg[9], begin_);
|
bittorrent::setIntParam(&msg[9], begin_);
|
||||||
bittorrent::setIntParam(&msg[13], length_);
|
bittorrent::setIntParam(&msg[13], length_);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t RangeBtMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
|
||||||
|
|
||||||
std::string RangeBtMessage::toString() const
|
std::string RangeBtMessage::toString() const
|
||||||
{
|
{
|
||||||
return fmt("%s index=%lu, begin=%d, length=%d", getName(),
|
return fmt("%s index=%lu, begin=%d, length=%d", getName(),
|
||||||
|
|
|
@ -75,9 +75,7 @@ public:
|
||||||
|
|
||||||
void setLength(int32_t length) { length_ = length; }
|
void setLength(int32_t length) { length_ = length; }
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual std::string toString() const CXX11_OVERRIDE;
|
virtual std::string toString() const CXX11_OVERRIDE;
|
||||||
};
|
};
|
||||||
|
|
|
@ -604,6 +604,35 @@ void RequestGroup::initPieceStorage()
|
||||||
segmentMan_ =
|
segmentMan_ =
|
||||||
std::make_shared<SegmentMan>(downloadContext_, tempPieceStorage);
|
std::make_shared<SegmentMan>(downloadContext_, tempPieceStorage);
|
||||||
pieceStorage_ = tempPieceStorage;
|
pieceStorage_ = tempPieceStorage;
|
||||||
|
|
||||||
|
#ifdef __MINGW32__
|
||||||
|
// Windows build: --file-allocation=falloc uses SetFileValidData
|
||||||
|
// which requires SE_MANAGE_VOLUME_NAME privilege. SetFileValidData
|
||||||
|
// has security implications (see
|
||||||
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/aa365544%28v=vs.85%29.aspx).
|
||||||
|
static auto gainPrivilegeAttempted = false;
|
||||||
|
|
||||||
|
if (!gainPrivilegeAttempted &&
|
||||||
|
pieceStorage_->getDiskAdaptor()->getFileAllocationMethod() ==
|
||||||
|
DiskAdaptor::FILE_ALLOC_FALLOC &&
|
||||||
|
isFileAllocationEnabled()) {
|
||||||
|
if (!util::gainPrivilege(SE_MANAGE_VOLUME_NAME)) {
|
||||||
|
A2_LOG_WARN("--file-allocation=falloc will not work properly.");
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
A2_LOG_DEBUG("SE_MANAGE_VOLUME_NAME privilege acquired");
|
||||||
|
|
||||||
|
A2_LOG_WARN(
|
||||||
|
"--file-allocation=falloc will use SetFileValidData() API, and "
|
||||||
|
"aria2 uses uninitialized disk space which may contain "
|
||||||
|
"confidential data as the download file space. If it is "
|
||||||
|
"undesirable, --file-allocation=prealloc is slower, but safer "
|
||||||
|
"option.");
|
||||||
|
}
|
||||||
|
|
||||||
|
gainPrivilegeAttempted = true;
|
||||||
|
}
|
||||||
|
#endif // __MINGW32__
|
||||||
}
|
}
|
||||||
|
|
||||||
void RequestGroup::dropPieceStorage()
|
void RequestGroup::dropPieceStorage()
|
||||||
|
|
|
@ -56,11 +56,10 @@ void SimpleBtMessage::send()
|
||||||
A2_LOG_INFO(fmt(MSG_SEND_PEER_MESSAGE, getCuid(),
|
A2_LOG_INFO(fmt(MSG_SEND_PEER_MESSAGE, getCuid(),
|
||||||
getPeer()->getIPAddress().c_str(), getPeer()->getPort(),
|
getPeer()->getIPAddress().c_str(), getPeer()->getPort(),
|
||||||
toString().c_str()));
|
toString().c_str()));
|
||||||
unsigned char* msg = createMessage();
|
auto msg = createMessage();
|
||||||
size_t msgLength = getMessageLength();
|
|
||||||
A2_LOG_DEBUG(
|
A2_LOG_DEBUG(
|
||||||
fmt("msglength = %lu bytes", static_cast<unsigned long>(msgLength)));
|
fmt("msglength = %lu bytes", static_cast<unsigned long>(msg.size())));
|
||||||
getPeerConnection()->pushBytes(msg, msgLength, getProgressUpdate());
|
getPeerConnection()->pushBytes(std::move(msg), getProgressUpdate());
|
||||||
}
|
}
|
||||||
|
|
||||||
std::unique_ptr<ProgressUpdate> SimpleBtMessage::getProgressUpdate()
|
std::unique_ptr<ProgressUpdate> SimpleBtMessage::getProgressUpdate()
|
||||||
|
|
|
@ -37,6 +37,8 @@
|
||||||
|
|
||||||
#include "AbstractBtMessage.h"
|
#include "AbstractBtMessage.h"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
struct ProgressUpdate;
|
struct ProgressUpdate;
|
||||||
|
@ -47,9 +49,7 @@ public:
|
||||||
|
|
||||||
virtual void send() CXX11_OVERRIDE;
|
virtual void send() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual unsigned char* createMessage() = 0;
|
virtual std::vector<unsigned char> createMessage() = 0;
|
||||||
|
|
||||||
virtual size_t getMessageLength() = 0;
|
|
||||||
|
|
||||||
virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
|
virtual std::unique_ptr<ProgressUpdate> getProgressUpdate();
|
||||||
|
|
||||||
|
|
|
@ -47,31 +47,34 @@
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
SocketBuffer::ByteArrayBufEntry::ByteArrayBufEntry(
|
SocketBuffer::ByteArrayBufEntry::ByteArrayBufEntry(
|
||||||
unsigned char* bytes, size_t length,
|
std::vector<unsigned char> bytes,
|
||||||
std::unique_ptr<ProgressUpdate> progressUpdate)
|
std::unique_ptr<ProgressUpdate> progressUpdate)
|
||||||
: BufEntry(std::move(progressUpdate)), bytes_(bytes), length_(length)
|
: BufEntry(std::move(progressUpdate)), bytes_(std::move(bytes))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry() { delete[] bytes_; }
|
SocketBuffer::ByteArrayBufEntry::~ByteArrayBufEntry() {}
|
||||||
|
|
||||||
ssize_t
|
ssize_t
|
||||||
SocketBuffer::ByteArrayBufEntry::send(const std::shared_ptr<SocketCore>& socket,
|
SocketBuffer::ByteArrayBufEntry::send(const std::shared_ptr<SocketCore>& socket,
|
||||||
size_t offset)
|
size_t offset)
|
||||||
{
|
{
|
||||||
return socket->writeData(bytes_ + offset, length_ - offset);
|
return socket->writeData(bytes_.data() + offset, bytes_.size() - offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SocketBuffer::ByteArrayBufEntry::final(size_t offset) const
|
bool SocketBuffer::ByteArrayBufEntry::final(size_t offset) const
|
||||||
{
|
{
|
||||||
return length_ <= offset;
|
return bytes_.size() <= offset;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t SocketBuffer::ByteArrayBufEntry::getLength() const { return length_; }
|
size_t SocketBuffer::ByteArrayBufEntry::getLength() const
|
||||||
|
{
|
||||||
|
return bytes_.size();
|
||||||
|
}
|
||||||
|
|
||||||
const unsigned char* SocketBuffer::ByteArrayBufEntry::getData() const
|
const unsigned char* SocketBuffer::ByteArrayBufEntry::getData() const
|
||||||
{
|
{
|
||||||
return bytes_;
|
return bytes_.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
SocketBuffer::StringBufEntry::StringBufEntry(
|
SocketBuffer::StringBufEntry::StringBufEntry(
|
||||||
|
@ -106,12 +109,12 @@ SocketBuffer::SocketBuffer(std::shared_ptr<SocketCore> socket)
|
||||||
|
|
||||||
SocketBuffer::~SocketBuffer() {}
|
SocketBuffer::~SocketBuffer() {}
|
||||||
|
|
||||||
void SocketBuffer::pushBytes(unsigned char* bytes, size_t len,
|
void SocketBuffer::pushBytes(std::vector<unsigned char> bytes,
|
||||||
std::unique_ptr<ProgressUpdate> progressUpdate)
|
std::unique_ptr<ProgressUpdate> progressUpdate)
|
||||||
{
|
{
|
||||||
if (len > 0) {
|
if (!bytes.empty()) {
|
||||||
bufq_.push_back(
|
bufq_.push_back(make_unique<ByteArrayBufEntry>(std::move(bytes),
|
||||||
make_unique<ByteArrayBufEntry>(bytes, len, std::move(progressUpdate)));
|
std::move(progressUpdate)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -40,6 +40,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
@ -77,7 +78,7 @@ private:
|
||||||
|
|
||||||
class ByteArrayBufEntry : public BufEntry {
|
class ByteArrayBufEntry : public BufEntry {
|
||||||
public:
|
public:
|
||||||
ByteArrayBufEntry(unsigned char* bytes, size_t length,
|
ByteArrayBufEntry(std::vector<unsigned char> bytes,
|
||||||
std::unique_ptr<ProgressUpdate> progressUpdate);
|
std::unique_ptr<ProgressUpdate> progressUpdate);
|
||||||
virtual ~ByteArrayBufEntry();
|
virtual ~ByteArrayBufEntry();
|
||||||
virtual ssize_t send(const std::shared_ptr<SocketCore>& socket,
|
virtual ssize_t send(const std::shared_ptr<SocketCore>& socket,
|
||||||
|
@ -87,8 +88,7 @@ private:
|
||||||
virtual const unsigned char* getData() const CXX11_OVERRIDE;
|
virtual const unsigned char* getData() const CXX11_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
unsigned char* bytes_;
|
std::vector<unsigned char> bytes_;
|
||||||
size_t length_;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class StringBufEntry : public BufEntry {
|
class StringBufEntry : public BufEntry {
|
||||||
|
@ -123,13 +123,11 @@ public:
|
||||||
SocketBuffer(const SocketBuffer&) = delete;
|
SocketBuffer(const SocketBuffer&) = delete;
|
||||||
SocketBuffer& operator=(const SocketBuffer&) = delete;
|
SocketBuffer& operator=(const SocketBuffer&) = delete;
|
||||||
|
|
||||||
// Feeds data pointed by bytes with length len into queue. This
|
// Feeds |bytes| into queue. This function doesn't send data. If
|
||||||
// object gets ownership of bytes, so caller must not delete or
|
|
||||||
// later bytes after this call. This function doesn't send data. If
|
|
||||||
// progressUpdate is not null, its update() function will be called
|
// progressUpdate is not null, its update() function will be called
|
||||||
// each time the data is sent. It will be deleted by this object. It
|
// each time the data is sent. It will be deleted by this object. It
|
||||||
// can be null.
|
// can be null.
|
||||||
void pushBytes(unsigned char* bytes, size_t len,
|
void pushBytes(std::vector<unsigned char> bytes,
|
||||||
std::unique_ptr<ProgressUpdate> progressUpdate = nullptr);
|
std::unique_ptr<ProgressUpdate> progressUpdate = nullptr);
|
||||||
|
|
||||||
// Feeds data into queue. This function doesn't send data. If
|
// Feeds data into queue. This function doesn't send data. If
|
||||||
|
|
|
@ -42,20 +42,18 @@ ZeroBtMessage::ZeroBtMessage(uint8_t id, const char* name)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* ZeroBtMessage::createMessage()
|
std::vector<unsigned char> ZeroBtMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 1, 4bytes
|
* len --- 1, 4bytes
|
||||||
* id --- ?, 1byte
|
* id --- ?, 1byte
|
||||||
* total: 5bytes
|
* total: 5bytes
|
||||||
*/
|
*/
|
||||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = std::vector<unsigned char>(MESSAGE_LENGTH);
|
||||||
bittorrent::createPeerMessageString(msg, MESSAGE_LENGTH, 1, getId());
|
bittorrent::createPeerMessageString(msg.data(), MESSAGE_LENGTH, 1, getId());
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t ZeroBtMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
|
||||||
|
|
||||||
std::string ZeroBtMessage::toString() const { return getName(); }
|
std::string ZeroBtMessage::toString() const { return getName(); }
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -56,9 +56,7 @@ protected:
|
||||||
public:
|
public:
|
||||||
ZeroBtMessage(uint8_t id, const char* name);
|
ZeroBtMessage(uint8_t id, const char* name);
|
||||||
|
|
||||||
virtual unsigned char* createMessage() CXX11_OVERRIDE;
|
virtual std::vector<unsigned char> createMessage() CXX11_OVERRIDE;
|
||||||
|
|
||||||
virtual size_t getMessageLength() CXX11_OVERRIDE;
|
|
||||||
|
|
||||||
virtual std::string toString() const CXX11_OVERRIDE;
|
virtual std::string toString() const CXX11_OVERRIDE;
|
||||||
};
|
};
|
||||||
|
|
|
@ -341,16 +341,6 @@ error_code::Value option_processing(Option& op, bool standalone,
|
||||||
op.remove(PREF_DEFERRED_INPUT);
|
op.remove(PREF_DEFERRED_INPUT);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef __MINGW32__
|
|
||||||
if (op.get(PREF_FILE_ALLOCATION) == V_FALLOC) {
|
|
||||||
A2_LOG_WARN(
|
|
||||||
"--file-allocation=falloc will use SetFileValidData() API, and "
|
|
||||||
"aria2 uses uninitialized disk space which may contain "
|
|
||||||
"confidential data as the download file space. If it is "
|
|
||||||
"undesirable, --file-allocation=prealloc is slower, but safer option.");
|
|
||||||
}
|
|
||||||
#endif // __MINGW32__
|
|
||||||
|
|
||||||
return error_code::FINISHED;
|
return error_code::FINISHED;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -70,9 +70,9 @@ void BtAllowedFastMessageTest::testCreateMessage()
|
||||||
unsigned char data[9];
|
unsigned char data[9];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 5, 17);
|
bittorrent::createPeerMessageString(data, sizeof(data), 5, 17);
|
||||||
bittorrent::setIntParam(&data[5], 12345);
|
bittorrent::setIntParam(&data[5], 12345);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtAllowedFastMessageTest::testDoReceivedAction()
|
void BtAllowedFastMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -76,10 +76,9 @@ void BtBitfieldMessageTest::testCreateMessage()
|
||||||
unsigned char data[5 + 2];
|
unsigned char data[5 + 2];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 3, 5);
|
bittorrent::createPeerMessageString(data, sizeof(data), 3, 5);
|
||||||
memcpy(&data[5], bitfield, sizeof(bitfield));
|
memcpy(&data[5], bitfield, sizeof(bitfield));
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 7) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)7, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)7, msg.getMessageLength());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtBitfieldMessageTest::testDoReceivedAction()
|
void BtBitfieldMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -95,9 +95,9 @@ void BtCancelMessageTest::testCreateMessage()
|
||||||
bittorrent::setIntParam(&data[5], 12345);
|
bittorrent::setIntParam(&data[5], 12345);
|
||||||
bittorrent::setIntParam(&data[9], 256);
|
bittorrent::setIntParam(&data[9], 256);
|
||||||
bittorrent::setIntParam(&data[13], 1_k);
|
bittorrent::setIntParam(&data[13], 1_k);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtCancelMessageTest::testDoReceivedAction()
|
void BtCancelMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -109,9 +109,9 @@ void BtChokeMessageTest::testCreateMessage()
|
||||||
BtChokeMessage msg;
|
BtChokeMessage msg;
|
||||||
unsigned char data[5];
|
unsigned char data[5];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 0);
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 0);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtChokeMessageTest::testDoReceivedAction()
|
void BtChokeMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -78,10 +78,9 @@ void BtExtendedMessageTest::testCreateMessage()
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 13, 20);
|
bittorrent::createPeerMessageString(data, sizeof(data), 13, 20);
|
||||||
*(data + 5) = extendedMessageID;
|
*(data + 5) = extendedMessageID;
|
||||||
memcpy(data + 6, payload.c_str(), payload.size());
|
memcpy(data + 6, payload.c_str(), payload.size());
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)17, msg.getMessageLength());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtExtendedMessageTest::testDoReceivedAction()
|
void BtExtendedMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -73,23 +73,23 @@ void BtHandshakeMessageTest::testCreate()
|
||||||
|
|
||||||
void BtHandshakeMessageTest::testCreateMessage()
|
void BtHandshakeMessageTest::testCreateMessage()
|
||||||
{
|
{
|
||||||
unsigned char infoHash[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
constexpr unsigned char infoHash[] = {
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
|
||||||
unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
constexpr unsigned char peerId[] = {0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
||||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
|
||||||
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
|
0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0};
|
||||||
|
|
||||||
std::shared_ptr<BtHandshakeMessage> msg(new BtHandshakeMessage());
|
auto msg = std::make_shared<BtHandshakeMessage>();
|
||||||
msg->setInfoHash(infoHash);
|
msg->setInfoHash(infoHash);
|
||||||
msg->setPeerId(peerId);
|
msg->setPeerId(peerId);
|
||||||
|
|
||||||
unsigned char data[68];
|
unsigned char data[68];
|
||||||
createHandshakeMessageData(data);
|
createHandshakeMessageData(data);
|
||||||
unsigned char* rawmsg = msg->createMessage();
|
auto rawmsg = msg->createMessage();
|
||||||
|
CPPUNIT_ASSERT_EQUAL((size_t)68, rawmsg.size());
|
||||||
CPPUNIT_ASSERT_EQUAL(util::toHex((const unsigned char*)data, 68),
|
CPPUNIT_ASSERT_EQUAL(util::toHex((const unsigned char*)data, 68),
|
||||||
util::toHex(rawmsg, 68));
|
util::toHex(rawmsg.data(), 68));
|
||||||
delete[] rawmsg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtHandshakeMessageTest::testToString()
|
void BtHandshakeMessageTest::testToString()
|
||||||
|
|
|
@ -65,9 +65,9 @@ void BtHaveAllMessageTest::testCreateMessage()
|
||||||
BtHaveAllMessage msg;
|
BtHaveAllMessage msg;
|
||||||
unsigned char data[5];
|
unsigned char data[5];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 14);
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 14);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtHaveAllMessageTest::testDoReceivedAction()
|
void BtHaveAllMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -71,9 +71,9 @@ void BtHaveMessageTest::testCreateMessage()
|
||||||
unsigned char data[9];
|
unsigned char data[9];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 5, 4);
|
bittorrent::createPeerMessageString(data, sizeof(data), 5, 4);
|
||||||
bittorrent::setIntParam(&data[5], 12345);
|
bittorrent::setIntParam(&data[5], 12345);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtHaveMessageTest::testDoReceivedAction()
|
void BtHaveMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -62,9 +62,9 @@ void BtHaveNoneMessageTest::testCreateMessage()
|
||||||
BtHaveNoneMessage msg;
|
BtHaveNoneMessage msg;
|
||||||
unsigned char data[5];
|
unsigned char data[5];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 15);
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 15);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtHaveNoneMessageTest::testDoReceivedAction()
|
void BtHaveNoneMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -64,9 +64,9 @@ void BtInterestedMessageTest::testCreateMessage()
|
||||||
BtInterestedMessage msg;
|
BtInterestedMessage msg;
|
||||||
unsigned char data[5];
|
unsigned char data[5];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 2);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtInterestedMessageTest::testDoReceivedAction()
|
void BtInterestedMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -27,10 +27,9 @@ void BtKeepAliveMessageTest::testCreateMessage()
|
||||||
memset(data, 0, sizeof(data));
|
memset(data, 0, sizeof(data));
|
||||||
BtKeepAliveMessage message;
|
BtKeepAliveMessage message;
|
||||||
CPPUNIT_ASSERT_EQUAL((uint8_t)99, message.getId());
|
CPPUNIT_ASSERT_EQUAL((uint8_t)99, message.getId());
|
||||||
CPPUNIT_ASSERT_EQUAL((size_t)4, message.getMessageLength());
|
auto rawmsg = message.createMessage();
|
||||||
unsigned char* rawmsg = message.createMessage();
|
CPPUNIT_ASSERT_EQUAL((size_t)4, rawmsg.size());
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 4) == 0);
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
delete[] rawmsg;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtKeepAliveMessageTest::testToString()
|
void BtKeepAliveMessageTest::testToString()
|
||||||
|
|
|
@ -64,9 +64,9 @@ void BtNotInterestedMessageTest::testCreateMessage()
|
||||||
BtNotInterestedMessage msg;
|
BtNotInterestedMessage msg;
|
||||||
unsigned char data[5];
|
unsigned char data[5];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 3);
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 3);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtNotInterestedMessageTest::testDoReceivedAction()
|
void BtNotInterestedMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -99,9 +99,9 @@ void BtPortMessageTest::testCreateMessage()
|
||||||
unsigned char data[7];
|
unsigned char data[7];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 3, 9);
|
bittorrent::createPeerMessageString(data, sizeof(data), 3, 9);
|
||||||
bittorrent::setShortIntParam(&data[5], 6881);
|
bittorrent::setShortIntParam(&data[5], 6881);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 7) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)7, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtPortMessageTest::testDoReceivedAction()
|
void BtPortMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -129,9 +129,9 @@ void BtRejectMessageTest::testCreateMessage()
|
||||||
bittorrent::setIntParam(&data[5], 12345);
|
bittorrent::setIntParam(&data[5], 12345);
|
||||||
bittorrent::setIntParam(&data[9], 256);
|
bittorrent::setIntParam(&data[9], 256);
|
||||||
bittorrent::setIntParam(&data[13], 1_k);
|
bittorrent::setIntParam(&data[13], 1_k);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtRejectMessageTest::testDoReceivedAction()
|
void BtRejectMessageTest::testDoReceivedAction()
|
||||||
|
|
|
@ -151,9 +151,9 @@ void BtRequestMessageTest::testCreateMessage()
|
||||||
bittorrent::setIntParam(&data[5], 12345);
|
bittorrent::setIntParam(&data[5], 12345);
|
||||||
bittorrent::setIntParam(&data[9], 256);
|
bittorrent::setIntParam(&data[9], 256);
|
||||||
bittorrent::setIntParam(&data[13], 1_k);
|
bittorrent::setIntParam(&data[13], 1_k);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 17) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)17, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking()
|
void BtRequestMessageTest::testDoReceivedAction_hasPieceAndAmNotChoking()
|
||||||
|
|
|
@ -62,9 +62,9 @@ void BtSuggestPieceMessageTest::testCreateMessage()
|
||||||
unsigned char data[9];
|
unsigned char data[9];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 5, 13);
|
bittorrent::createPeerMessageString(data, sizeof(data), 5, 13);
|
||||||
bittorrent::setIntParam(&data[5], 12345);
|
bittorrent::setIntParam(&data[5], 12345);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 9) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)9, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtSuggestPieceMessageTest::testToString()
|
void BtSuggestPieceMessageTest::testToString()
|
||||||
|
|
|
@ -64,9 +64,9 @@ void BtUnchokeMessageTest::testCreateMessage()
|
||||||
BtUnchokeMessage msg;
|
BtUnchokeMessage msg;
|
||||||
unsigned char data[5];
|
unsigned char data[5];
|
||||||
bittorrent::createPeerMessageString(data, sizeof(data), 1, 1);
|
bittorrent::createPeerMessageString(data, sizeof(data), 1, 1);
|
||||||
unsigned char* rawmsg = msg.createMessage();
|
auto rawmsg = msg.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 5) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)5, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtUnchokeMessageTest::testDoReceivedAction()
|
void BtUnchokeMessageTest::testDoReceivedAction()
|
||||||
|
|
Loading…
Reference in New Issue