mirror of https://github.com/aria2/aria2
Refactor BtHandshakeMessage
parent
29c96d59ba
commit
cc8375f0b0
|
@ -37,16 +37,13 @@
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "BtConstants.h"
|
|
||||||
#include "a2functional.h"
|
#include "a2functional.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
|
const unsigned char BtHandshakeMessage::BT_PSTR[] = "BitTorrent protocol";
|
||||||
const char BtHandshakeMessage::NAME[] = "handshake";
|
const char BtHandshakeMessage::NAME[] = "handshake";
|
||||||
|
|
||||||
const unsigned char* BtHandshakeMessage::BT_PSTR =
|
|
||||||
reinterpret_cast<const unsigned char*>("BitTorrent protocol");
|
|
||||||
|
|
||||||
BtHandshakeMessage::BtHandshakeMessage() : SimpleBtMessage(ID, NAME) { init(); }
|
BtHandshakeMessage::BtHandshakeMessage() : SimpleBtMessage(ID, NAME) { init(); }
|
||||||
|
|
||||||
BtHandshakeMessage::BtHandshakeMessage(const unsigned char* infoHash,
|
BtHandshakeMessage::BtHandshakeMessage(const unsigned char* infoHash,
|
||||||
|
@ -54,19 +51,17 @@ BtHandshakeMessage::BtHandshakeMessage(const unsigned char* infoHash,
|
||||||
: SimpleBtMessage(ID, NAME)
|
: SimpleBtMessage(ID, NAME)
|
||||||
{
|
{
|
||||||
init();
|
init();
|
||||||
memcpy(infoHash_, infoHash, INFO_HASH_LENGTH);
|
std::copy_n(infoHash, infoHash_.size(), std::begin(infoHash_));
|
||||||
memcpy(peerId_, peerId, PEER_ID_LENGTH);
|
std::copy_n(peerId, peerId_.size(), std::begin(peerId_));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BtHandshakeMessage::~BtHandshakeMessage() {}
|
||||||
|
|
||||||
void BtHandshakeMessage::init()
|
void BtHandshakeMessage::init()
|
||||||
{
|
{
|
||||||
pstrlen_ = 19;
|
pstrlen_ = 19;
|
||||||
pstr_ = new unsigned char[PSTR_LENGTH];
|
std::copy_n(BT_PSTR, pstr_.size(), std::begin(pstr_));
|
||||||
reserved_ = new unsigned char[RESERVED_LENGTH];
|
std::fill(std::begin(reserved_), std::end(reserved_), 0);
|
||||||
infoHash_ = new unsigned char[INFO_HASH_LENGTH];
|
|
||||||
peerId_ = new unsigned char[PEER_ID_LENGTH];
|
|
||||||
memcpy(pstr_, BT_PSTR, PSTR_LENGTH);
|
|
||||||
memset(reserved_, 0, RESERVED_LENGTH);
|
|
||||||
// fast extension
|
// fast extension
|
||||||
reserved_[7] |= 0x04u;
|
reserved_[7] |= 0x04u;
|
||||||
// extended messaging
|
// extended messaging
|
||||||
|
@ -76,23 +71,25 @@ void BtHandshakeMessage::init()
|
||||||
std::unique_ptr<BtHandshakeMessage>
|
std::unique_ptr<BtHandshakeMessage>
|
||||||
BtHandshakeMessage::create(const unsigned char* data, size_t dataLength)
|
BtHandshakeMessage::create(const unsigned char* data, size_t dataLength)
|
||||||
{
|
{
|
||||||
auto message = make_unique<BtHandshakeMessage>();
|
auto msg = make_unique<BtHandshakeMessage>();
|
||||||
message->pstrlen_ = data[0];
|
msg->pstrlen_ = data[0];
|
||||||
memcpy(message->pstr_, &data[1], PSTR_LENGTH);
|
std::copy_n(&data[1], msg->pstr_.size(), std::begin(msg->pstr_));
|
||||||
memcpy(message->reserved_, &data[20], RESERVED_LENGTH);
|
std::copy_n(&data[20], msg->reserved_.size(), std::begin(msg->reserved_));
|
||||||
memcpy(message->infoHash_, &data[28], INFO_HASH_LENGTH);
|
std::copy_n(&data[28], msg->infoHash_.size(), std::begin(msg->infoHash_));
|
||||||
memcpy(message->peerId_, &data[48], PEER_ID_LENGTH);
|
std::copy_n(&data[48], msg->peerId_.size(), std::begin(msg->peerId_));
|
||||||
return message;
|
|
||||||
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned char* BtHandshakeMessage::createMessage()
|
unsigned char* BtHandshakeMessage::createMessage()
|
||||||
{
|
{
|
||||||
auto msg = new unsigned char[MESSAGE_LENGTH];
|
auto msg = new unsigned char[MESSAGE_LENGTH];
|
||||||
msg[0] = pstrlen_;
|
auto dst = msg;
|
||||||
memcpy(msg + 1, pstr_, PSTR_LENGTH);
|
*dst++ = pstrlen_;
|
||||||
memcpy(msg + 20, reserved_, RESERVED_LENGTH);
|
dst = std::copy(std::begin(pstr_), std::end(pstr_), dst);
|
||||||
memcpy(msg + 28, infoHash_, INFO_HASH_LENGTH);
|
dst = std::copy(std::begin(reserved_), std::end(reserved_), dst);
|
||||||
memcpy(msg + 48, peerId_, PEER_ID_LENGTH);
|
dst = std::copy(std::begin(infoHash_), std::end(infoHash_), dst);
|
||||||
|
std::copy(std::begin(peerId_), std::end(peerId_), dst);
|
||||||
return msg;
|
return msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,8 +98,8 @@ 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,
|
||||||
util::percentEncode(peerId_, PEER_ID_LENGTH).c_str(),
|
util::percentEncode(peerId_.data(), peerId_.size()).c_str(),
|
||||||
util::toHex(reserved_, RESERVED_LENGTH).c_str());
|
util::toHex(reserved_.data(), reserved_.size()).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool BtHandshakeMessage::isFastExtensionSupported() const
|
bool BtHandshakeMessage::isFastExtensionSupported() const
|
||||||
|
@ -119,12 +116,12 @@ bool BtHandshakeMessage::isDHTEnabled() const { return reserved_[7] & 0x01u; }
|
||||||
|
|
||||||
void BtHandshakeMessage::setInfoHash(const unsigned char* infoHash)
|
void BtHandshakeMessage::setInfoHash(const unsigned char* infoHash)
|
||||||
{
|
{
|
||||||
memcpy(infoHash_, infoHash, INFO_HASH_LENGTH);
|
std::copy_n(infoHash, infoHash_.size(), std::begin(infoHash_));
|
||||||
}
|
}
|
||||||
|
|
||||||
void BtHandshakeMessage::setPeerId(const unsigned char* peerId)
|
void BtHandshakeMessage::setPeerId(const unsigned char* peerId)
|
||||||
{
|
{
|
||||||
memcpy(peerId_, peerId, PEER_ID_LENGTH);
|
std::copy_n(peerId, peerId_.size(), std::begin(peerId_));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace aria2
|
} // namespace aria2
|
||||||
|
|
|
@ -37,21 +37,26 @@
|
||||||
|
|
||||||
#include "SimpleBtMessage.h"
|
#include "SimpleBtMessage.h"
|
||||||
|
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
#include "BtConstants.h"
|
||||||
|
|
||||||
namespace aria2 {
|
namespace aria2 {
|
||||||
|
|
||||||
class BtHandshakeMessage : public SimpleBtMessage {
|
class BtHandshakeMessage : public SimpleBtMessage {
|
||||||
public:
|
public:
|
||||||
static const size_t PSTR_LENGTH = 19;
|
constexpr static size_t PSTR_LENGTH = 19;
|
||||||
static const unsigned char* BT_PSTR;
|
constexpr static size_t RESERVED_LENGTH = 8;
|
||||||
static const size_t RESERVED_LENGTH = 8;
|
constexpr static size_t MESSAGE_LENGTH = 68;
|
||||||
static const size_t MESSAGE_LENGTH = 68;
|
const static unsigned char BT_PSTR[];
|
||||||
|
|
||||||
private:
|
private:
|
||||||
uint8_t pstrlen_;
|
uint8_t pstrlen_;
|
||||||
unsigned char* pstr_;
|
std::array<unsigned char, PSTR_LENGTH> pstr_;
|
||||||
unsigned char* reserved_;
|
std::array<unsigned char, RESERVED_LENGTH> reserved_;
|
||||||
unsigned char* infoHash_;
|
std::array<unsigned char, INFO_HASH_LENGTH> infoHash_;
|
||||||
unsigned char* peerId_;
|
std::array<unsigned char, PEER_ID_LENGTH> peerId_;
|
||||||
|
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
@ -66,17 +71,11 @@ public:
|
||||||
static std::unique_ptr<BtHandshakeMessage> create(const unsigned char* data,
|
static std::unique_ptr<BtHandshakeMessage> create(const unsigned char* data,
|
||||||
size_t dataLength);
|
size_t dataLength);
|
||||||
|
|
||||||
virtual ~BtHandshakeMessage()
|
virtual ~BtHandshakeMessage();
|
||||||
{
|
|
||||||
delete[] pstr_;
|
|
||||||
delete[] reserved_;
|
|
||||||
delete[] infoHash_;
|
|
||||||
delete[] peerId_;
|
|
||||||
}
|
|
||||||
|
|
||||||
static const uint8_t ID = INT8_MAX;
|
static const uint8_t ID = INT8_MAX;
|
||||||
|
|
||||||
static const char NAME[];
|
const static char NAME[];
|
||||||
|
|
||||||
virtual void doReceivedAction() CXX11_OVERRIDE{};
|
virtual void doReceivedAction() CXX11_OVERRIDE{};
|
||||||
|
|
||||||
|
@ -104,15 +103,15 @@ public:
|
||||||
|
|
||||||
uint8_t getPstrlen() const { return pstrlen_; }
|
uint8_t getPstrlen() const { return pstrlen_; }
|
||||||
|
|
||||||
const unsigned char* getPstr() const { return pstr_; }
|
const unsigned char* getPstr() const { return pstr_.data(); }
|
||||||
|
|
||||||
const unsigned char* getReserved() const { return reserved_; }
|
const unsigned char* getReserved() const { return reserved_.data(); }
|
||||||
|
|
||||||
const unsigned char* getInfoHash() const { return infoHash_; }
|
const unsigned char* getInfoHash() const { return infoHash_.data(); }
|
||||||
|
|
||||||
void setInfoHash(const unsigned char* infoHash);
|
void setInfoHash(const unsigned char* infoHash);
|
||||||
|
|
||||||
const unsigned char* getPeerId() const { return peerId_; }
|
const unsigned char* getPeerId() const { return peerId_.data(); }
|
||||||
|
|
||||||
void setPeerId(const unsigned char* peerId);
|
void setPeerId(const unsigned char* peerId);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue