mirror of https://github.com/aria2/aria2
Refactor BitTorrent message buffer usage
parent
cc8375f0b0
commit
a7237c69f7
|
@ -97,7 +97,7 @@ void BtBitfieldMessage::doReceivedAction()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* BtBitfieldMessage::createMessage()
|
std::vector<unsigned char> BtBitfieldMessage::createMessage()
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* len --- 1+bitfieldLength, 4bytes
|
* len --- 1+bitfieldLength, 4bytes
|
||||||
|
@ -106,9 +106,10 @@ unsigned char* BtBitfieldMessage::createMessage()
|
||||||
* total: 5+bitfieldLength bytes
|
* total: 5+bitfieldLength bytes
|
||||||
*/
|
*/
|
||||||
const size_t msgLength = 5 + bitfieldLength_;
|
const size_t msgLength = 5 + bitfieldLength_;
|
||||||
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 + bitfieldLength_, ID);
|
||||||
|
memcpy(msg.data() + 5, bitfield_.get(), bitfieldLength_);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -66,7 +66,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 size_t getMessageLength() 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,10 +69,12 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -61,7 +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 size_t getMessageLength() 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);
|
||||||
|
|
|
@ -79,7 +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 size_t getMessageLength() CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -39,15 +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; }
|
size_t BtKeepAliveMessage::getMessageLength() { return MESSAGE_LENGTH; }
|
||||||
|
|
|
@ -52,7 +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 size_t getMessageLength() CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -213,16 +213,15 @@ 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,8 +108,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -71,7 +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 size_t getMessageLength() CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -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,8 +47,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +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 size_t getMessageLength() 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);
|
||||||
memcpy(ptr, &padCLengthBE, sizeof(padCLengthBE));
|
ptr = std::copy_n(reinterpret_cast<unsigned char*>(&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,8 +55,8 @@ 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_);
|
||||||
|
|
|
@ -75,7 +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 size_t getMessageLength() CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -56,11 +56,11 @@ 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();
|
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,7 +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 size_t getMessageLength() = 0;
|
||||||
|
|
||||||
|
|
|
@ -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,15 +42,15 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +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 size_t getMessageLength() CXX11_OVERRIDE;
|
||||||
|
|
||||||
|
|
|
@ -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,9 +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());
|
CPPUNIT_ASSERT_EQUAL((size_t)7, msg.getMessageLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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,9 +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());
|
CPPUNIT_ASSERT_EQUAL((size_t)17, msg.getMessageLength());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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()
|
||||||
|
|
|
@ -28,9 +28,9 @@ void BtKeepAliveMessageTest::testCreateMessage()
|
||||||
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());
|
CPPUNIT_ASSERT_EQUAL((size_t)4, message.getMessageLength());
|
||||||
unsigned char* rawmsg = message.createMessage();
|
auto rawmsg = message.createMessage();
|
||||||
CPPUNIT_ASSERT(memcmp(rawmsg, data, 4) == 0);
|
CPPUNIT_ASSERT_EQUAL((size_t)4, rawmsg.size());
|
||||||
delete[] rawmsg;
|
CPPUNIT_ASSERT(std::equal(std::begin(rawmsg), std::end(rawmsg), data));
|
||||||
}
|
}
|
||||||
|
|
||||||
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