/* */ #include "BtHandshakeMessage.h" #include #include "util.h" #include "BtConstants.h" #include "a2functional.h" namespace aria2 { const std::string BtHandshakeMessage::NAME("handshake"); const unsigned char* BtHandshakeMessage::BT_PSTR = reinterpret_cast("BitTorrent protocol"); BtHandshakeMessage::BtHandshakeMessage():SimpleBtMessage(ID, NAME) { init(); } BtHandshakeMessage::BtHandshakeMessage(const unsigned char* infoHash, const unsigned char* peerId): SimpleBtMessage(ID, NAME) { init(); memcpy(this->infoHash, infoHash, INFO_HASH_LENGTH); memcpy(this->peerId, peerId, PEER_ID_LENGTH); } void BtHandshakeMessage::init() { this->pstrlen = 19; pstr = new unsigned char[PSTR_LENGTH]; reserved = new unsigned char[RESERVED_LENGTH]; infoHash = new unsigned char[INFO_HASH_LENGTH]; peerId = new unsigned char[PEER_ID_LENGTH]; memcpy(this->pstr, BT_PSTR, PSTR_LENGTH); memset(this->reserved, 0, RESERVED_LENGTH); // fast extension this->reserved[7] |= 0x04; // extended messaging this->reserved[5] |= 0x10; } SharedHandle BtHandshakeMessage::create(const unsigned char* data, size_t dataLength) { SharedHandle message(new BtHandshakeMessage()); message->pstrlen = data[0]; memcpy(message->pstr, &data[1], PSTR_LENGTH); memcpy(message->reserved, &data[20], RESERVED_LENGTH); memcpy(message->infoHash, &data[28], INFO_HASH_LENGTH); memcpy(message->peerId, &data[48], PEER_ID_LENGTH); return message; } unsigned char* BtHandshakeMessage::createMessage() { unsigned char* msg = new unsigned char[MESSAGE_LENGTH]; msg[0] = pstrlen; memcpy(msg+1, pstr, PSTR_LENGTH); memcpy(msg+20, reserved, RESERVED_LENGTH); memcpy(msg+28, infoHash, INFO_HASH_LENGTH); memcpy(msg+48, peerId, PEER_ID_LENGTH); return msg; } size_t BtHandshakeMessage::getMessageLength() { return MESSAGE_LENGTH; } std::string BtHandshakeMessage::toString() const { return strconcat(NAME, " peerId=", util::percentEncode(peerId, PEER_ID_LENGTH), ", reserved=", util::toHex(reserved, RESERVED_LENGTH)); } bool BtHandshakeMessage::isFastExtensionSupported() const { return reserved[7]&0x04; } bool BtHandshakeMessage::isExtendedMessagingEnabled() const { return reserved[5]&0x10; } bool BtHandshakeMessage::isDHTEnabled() const { return reserved[7]&0x01; } void BtHandshakeMessage::setInfoHash(const unsigned char* infoHash) { memcpy(this->infoHash, infoHash, INFO_HASH_LENGTH); } void BtHandshakeMessage::setPeerId(const unsigned char* peerId) { memcpy(this->peerId, peerId, PEER_ID_LENGTH); } } // namespace aria2