Skip to content

Commit

Permalink
Hash P2P messages as they are received instead of at process-time
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBlueMatt committed Oct 30, 2016
1 parent d2143dc commit fe1dc62
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main.cpp
Expand Up @@ -6350,7 +6350,7 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman)

// Checksum
CDataStream& vRecv = msg.vRecv;
uint256 hash = Hash(vRecv.begin(), vRecv.begin() + nMessageSize);
const uint256& hash = msg.GetMessageHash();
if (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) != 0)
{
LogPrintf("%s(%s, %u bytes): CHECKSUM ERROR expected %s was %s\n", __func__,
Expand Down
9 changes: 9 additions & 0 deletions src/net.cpp
Expand Up @@ -758,12 +758,21 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes)
vRecv.resize(std::min(hdr.nMessageSize, nDataPos + nCopy + 256 * 1024));
}

hasher.Write((const unsigned char*)pch, nCopy);
memcpy(&vRecv[nDataPos], pch, nCopy);
nDataPos += nCopy;

return nCopy;
}

const uint256& CNetMessage::GetMessageHash() const
{
assert(complete());
if (data_hash.IsNull())
hasher.Finalize(data_hash.begin());
return data_hash;
}




Expand Down
5 changes: 5 additions & 0 deletions src/net.h
Expand Up @@ -512,6 +512,9 @@ class CNodeStats


class CNetMessage {
private:
mutable CHash256 hasher;
mutable uint256 data_hash;
public:
bool in_data; // parsing header (false) or data (true)

Expand Down Expand Up @@ -539,6 +542,8 @@ class CNetMessage {
return (hdr.nMessageSize == nDataPos);
}

const uint256& GetMessageHash() const;

void SetVersion(int nVersionIn)
{
hdrbuf.SetVersion(nVersionIn);
Expand Down

0 comments on commit fe1dc62

Please sign in to comment.