P2PServer: sanity check message id

pull/271/head
SChernykh 2023-07-04 16:47:14 +02:00
parent d6cb0ee8a0
commit 8d9f16c387
2 changed files with 19 additions and 11 deletions

View File

@ -1418,7 +1418,14 @@ bool P2PServer::P2PClient::on_read(char* data, uint32_t size)
uint32_t bytes_read; uint32_t bytes_read;
do { do {
MessageId id = static_cast<MessageId>(buf[0]); if (buf[0] > static_cast<uint8_t>(MessageId::LAST)) {
LOGWARN(5, "peer " << static_cast<char*>(m_addrString) << " sent an unknown message id " << buf[0]);
ban(DEFAULT_BAN_TIME);
server->remove_peer_from_list(this);
return false;
}
const MessageId id = static_cast<MessageId>(buf[0]);
// Peer must complete the handshake challenge before sending any other messages // Peer must complete the handshake challenge before sending any other messages
if (!m_handshakeComplete && (id != m_expectedMessage)) { if (!m_handshakeComplete && (id != m_expectedMessage)) {

View File

@ -45,16 +45,17 @@ class P2PServer : public TCPServer
{ {
public: public:
enum class MessageId { enum class MessageId {
HANDSHAKE_CHALLENGE = 0, HANDSHAKE_CHALLENGE,
HANDSHAKE_SOLUTION = 1, HANDSHAKE_SOLUTION,
LISTEN_PORT = 2, LISTEN_PORT,
BLOCK_REQUEST = 3, BLOCK_REQUEST,
BLOCK_RESPONSE = 4, BLOCK_RESPONSE,
BLOCK_BROADCAST = 5, BLOCK_BROADCAST,
PEER_LIST_REQUEST = 6, PEER_LIST_REQUEST,
PEER_LIST_RESPONSE = 7, PEER_LIST_RESPONSE,
BLOCK_BROADCAST_COMPACT = 8, BLOCK_BROADCAST_COMPACT,
BLOCK_NOTIFY = 9, BLOCK_NOTIFY,
LAST = BLOCK_NOTIFY,
}; };
explicit P2PServer(p2pool *pool); explicit P2PServer(p2pool *pool);