/* */ #ifndef _D_BT_HANDSHAKE_MESSAGE_VALIDATOR_H_ #define _D_BT_HANDSHAKE_MESSAGE_VALIDATOR_H_ #include "BtMessageValidator.h" #include "BtHandshakeMessage.h" #include "Util.h" #include "PeerMessageUtil.h" #include "StringFormat.h" #include 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 DlAbortEx(StringFormat("invalid handshake pstrlen=%u", message->getPstrlen()).str()); } if(memcmp(BtHandshakeMessage::BT_PSTR, message->getPstr(), 19) != 0) { throw DlAbortEx (StringFormat("invalid handshake pstr=%s", Util::urlencode(message->getPstr(), 19).c_str()).str()); } if(memcmp(infoHash, message->getInfoHash(), 20) != 0) { throw DlAbortEx (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_