Merge #8822: net: Consistent checksum handling
305087b
net: Hardcode protocol sizes and use fixed-size types (Wladimir J. van der Laan)41e58fa
net: Consistent checksum handling (Wladimir J. van der Laan)
This commit is contained in:
commit
9bc6a6bd7b
4 changed files with 14 additions and 15 deletions
|
@ -6253,11 +6253,12 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman)
|
||||||
// Checksum
|
// Checksum
|
||||||
CDataStream& vRecv = msg.vRecv;
|
CDataStream& vRecv = msg.vRecv;
|
||||||
uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
|
uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
|
||||||
unsigned int nChecksum = ReadLE32((unsigned char*)&hash);
|
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
|
||||||
if (nChecksum != hdr.nChecksum)
|
|
||||||
{
|
{
|
||||||
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR nChecksum=%08x hdr.nChecksum=%08x\n", __func__,
|
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
|
||||||
SanitizeString(strCommand), nMessageSize, nChecksum, hdr.nChecksum);
|
SanitizeString(strCommand), nMessageSize,
|
||||||
|
HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
|
||||||
|
HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2661,10 +2661,8 @@ void CNode::EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend)
|
||||||
|
|
||||||
// Set the checksum
|
// Set the checksum
|
||||||
uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
|
uint256 hash = Hash(ssSend.begin() + CMessageHeader::HEADER_SIZE, ssSend.end());
|
||||||
unsigned int nChecksum = 0;
|
assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + CMessageHeader::CHECKSUM_SIZE);
|
||||||
memcpy(&nChecksum, &hash, sizeof(nChecksum));
|
memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], hash.begin(), CMessageHeader::CHECKSUM_SIZE);
|
||||||
assert(ssSend.size () >= CMessageHeader::CHECKSUM_OFFSET + sizeof(nChecksum));
|
|
||||||
memcpy((char*)&ssSend[CMessageHeader::CHECKSUM_OFFSET], &nChecksum, sizeof(nChecksum));
|
|
||||||
|
|
||||||
LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
|
LogPrint("net", "(%d bytes) peer=%d\n", nSize, id);
|
||||||
|
|
||||||
|
|
|
@ -79,7 +79,7 @@ CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
|
||||||
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
|
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
|
||||||
memset(pchCommand, 0, sizeof(pchCommand));
|
memset(pchCommand, 0, sizeof(pchCommand));
|
||||||
nMessageSize = -1;
|
nMessageSize = -1;
|
||||||
nChecksum = 0;
|
memset(pchChecksum, 0, CHECKSUM_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
|
||||||
|
@ -88,7 +88,7 @@ CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const
|
||||||
memset(pchCommand, 0, sizeof(pchCommand));
|
memset(pchCommand, 0, sizeof(pchCommand));
|
||||||
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
|
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
|
||||||
nMessageSize = nMessageSizeIn;
|
nMessageSize = nMessageSizeIn;
|
||||||
nChecksum = 0;
|
memset(pchChecksum, 0, CHECKSUM_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string CMessageHeader::GetCommand() const
|
std::string CMessageHeader::GetCommand() const
|
||||||
|
|
|
@ -45,15 +45,15 @@ public:
|
||||||
READWRITE(FLATDATA(pchMessageStart));
|
READWRITE(FLATDATA(pchMessageStart));
|
||||||
READWRITE(FLATDATA(pchCommand));
|
READWRITE(FLATDATA(pchCommand));
|
||||||
READWRITE(nMessageSize);
|
READWRITE(nMessageSize);
|
||||||
READWRITE(nChecksum);
|
READWRITE(FLATDATA(pchChecksum));
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: make private (improves encapsulation)
|
// TODO: make private (improves encapsulation)
|
||||||
public:
|
public:
|
||||||
enum {
|
enum {
|
||||||
COMMAND_SIZE = 12,
|
COMMAND_SIZE = 12,
|
||||||
MESSAGE_SIZE_SIZE = sizeof(int),
|
MESSAGE_SIZE_SIZE = 4,
|
||||||
CHECKSUM_SIZE = sizeof(int),
|
CHECKSUM_SIZE = 4,
|
||||||
|
|
||||||
MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE,
|
MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE,
|
||||||
CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE,
|
CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE,
|
||||||
|
@ -61,8 +61,8 @@ public:
|
||||||
};
|
};
|
||||||
char pchMessageStart[MESSAGE_START_SIZE];
|
char pchMessageStart[MESSAGE_START_SIZE];
|
||||||
char pchCommand[COMMAND_SIZE];
|
char pchCommand[COMMAND_SIZE];
|
||||||
unsigned int nMessageSize;
|
uint32_t nMessageSize;
|
||||||
unsigned int nChecksum;
|
uint8_t pchChecksum[CHECKSUM_SIZE];
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue