/* */ #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" namespace aria2 { class BtHandshakeMessageValidator : public BtMessageValidator { private: const BtHandshakeMessage* message; unsigned char infoHash[20]; public: BtHandshakeMessageValidator(const BtHandshakeMessage* message, const unsigned char* infoHash): message(message) { memcpy(this->infoHash, infoHash, sizeof(this->infoHash)); } virtual bool validate(Errors& error) { // TODO 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::urlencode(message->getPstr(), 19).c_str()).str()); } if(memcmp(infoHash, message->getInfoHash(), 20) != 0) { throw DL_ABORT_EX (StringFormat("invalid handshake info hash: expected:%s, actual:%s", util::toHex(infoHash, 20).c_str(), util::toHex(message->getInfoHash(), 20).c_str()).str()); } return true; } }; typedef SharedHandle BtHandshakeMessageValidatorHandle; } // namespace aria2 #endif // _D_BT_HANDSHAKE_MESSAGE_VALIDATOR_H_