/* */ #ifndef _D_BT_HANDSHAKE_MESSAGE_VALIDATOR_H_ #define _D_BT_HANDSHAKE_MESSAGE_VALIDATOR_H_ #include "BtMessageValidator.h" #include #include "BtHandshakeMessage.h" #include "util.h" #include "StringFormat.h" #include "BtConstants.h" namespace aria2 { class BtHandshakeMessageValidator : public BtMessageValidator { private: const BtHandshakeMessage* _message; unsigned char _infoHash[INFO_HASH_LENGTH]; public: BtHandshakeMessageValidator(const BtHandshakeMessage* message, const unsigned char* infoHash): _message(message) { memcpy(_infoHash, infoHash, sizeof(_infoHash)); } virtual void validate() { if(_message->getPstrlen() != 19) { throw DL_ABORT_EX(StringFormat("invalid handshake pstrlen=%u", _message->getPstrlen()).str()); } if(memcmp(BtHandshakeMessage::BT_PSTR, _message->getPstr(), 19) != 0) { throw DL_ABORT_EX (StringFormat("invalid handshake pstr=%s", util::percentEncode (_message->getPstr(), 19).c_str()).str()); } if(memcmp(_infoHash, _message->getInfoHash(), sizeof(_infoHash)) != 0) { throw DL_ABORT_EX (StringFormat("invalid handshake info hash: expected:%s, actual:%s", util::toHex(_infoHash, sizeof(_infoHash)).c_str(), util::toHex(_message->getInfoHash(), INFO_HASH_LENGTH).c_str()).str()); } } }; typedef SharedHandle BtHandshakeMessageValidatorHandle; } // namespace aria2 #endif // _D_BT_HANDSHAKE_MESSAGE_VALIDATOR_H_