Skip to content

Commit d2b8e0a

Browse files
committed
net: make CMessageHeader a dumb storage class
1 parent cc24eff commit d2b8e0a

File tree

6 files changed

+17
-19
lines changed

6 files changed

+17
-19
lines changed

src/chainparams.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717

1818
#include <vector>
1919

20-
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
21-
2220
struct CDNSSeedData {
2321
std::string name, host;
2422
bool supportsServiceBitsFiltering;
@@ -62,7 +60,7 @@ class CChainParams
6260
};
6361

6462
const Consensus::Params& GetConsensus() const { return consensus; }
65-
const MessageStartChars& MessageStart() const { return pchMessageStart; }
63+
const CMessageHeader::MessageStartChars& MessageStart() const { return pchMessageStart; }
6664
int GetDefaultPort() const { return nDefaultPort; }
6765

6866
const CBlock& GenesisBlock() const { return genesis; }
@@ -93,7 +91,7 @@ class CChainParams
9391
std::string strNetworkID;
9492
CBlock genesis;
9593
Consensus::Params consensus;
96-
MessageStartChars pchMessageStart;
94+
CMessageHeader::MessageStartChars pchMessageStart;
9795
int nDefaultPort;
9896
std::vector<CDNSSeedData> vSeeds;
9997
std::vector<unsigned char> base58Prefixes[MAX_BASE58_TYPES];

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6117,7 +6117,7 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman, std::atomic<bool>& interru
61176117

61186118
// Read header
61196119
CMessageHeader& hdr = msg.hdr;
6120-
if (!hdr.IsValid()) {
6120+
if (!hdr.IsValid(Params().MessageStart())) {
61216121
LogPrintf("PROCESSMESSAGE: ERRORS IN HEADER %s peer=%d\n", SanitizeString(hdr.GetCommand()), pfrom->id);
61226122
continue;
61236123
}

src/net.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ bool CNode::ReceiveMsgBytes(const char* pch, unsigned int nBytes, bool& complete
709709
// get current incomplete message, or create a new one
710710
if (vRecvMsg.empty() ||
711711
vRecvMsg.back().complete())
712-
vRecvMsg.push_back(CNetMessage(SER_NETWORK, nRecvVersion));
712+
vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, nRecvVersion));
713713

714714
CNetMessage& msg = vRecvMsg.back();
715715

@@ -2533,7 +2533,7 @@ void CNode::BeginMessage(const char* pszCommand) EXCLUSIVE_LOCK_FUNCTION(cs_vSen
25332533
{
25342534
ENTER_CRITICAL_SECTION(cs_vSend);
25352535
assert(ssSend.size() == 0);
2536-
ssSend << CMessageHeader(pszCommand, 0);
2536+
ssSend << CMessageHeader(Params().MessageStart(), pszCommand, 0);
25372537
LogPrint(BCLog::NET, "sending: %s ", SanitizeString(pszCommand));
25382538
}
25392539

src/net.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ class CNetMessage
503503

504504
int64_t nTime; // time (in microseconds) of message receipt.
505505

506-
CNetMessage(int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), vRecv(nTypeIn, nVersionIn)
507-
{
506+
CNetMessage(const CMessageHeader::MessageStartChars& pchMessageStartIn, int nTypeIn, int nVersionIn) : hdrbuf(nTypeIn, nVersionIn), hdr(pchMessageStartIn), vRecv(nTypeIn, nVersionIn) {
508507
hdrbuf.resize(24);
509508
in_data = false;
510509
nHdrPos = 0;

src/protocol.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
#include "protocol.h"
99

10-
#include "chainparams.h"
1110
#include "util.h"
1211
#include "utilstrencodings.h"
1312

@@ -120,17 +119,17 @@ const static std::string allNetMessageTypes[] = {
120119
};
121120
const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes + ARRAYLEN(allNetMessageTypes));
122121

123-
CMessageHeader::CMessageHeader()
122+
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
124123
{
125-
memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
124+
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
126125
memset(pchCommand, 0, sizeof(pchCommand));
127126
nMessageSize = -1;
128127
nChecksum = 0;
129128
}
130129

131-
CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
130+
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
132131
{
133-
memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
132+
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
134133
memset(pchCommand, 0, sizeof(pchCommand));
135134
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
136135
nMessageSize = nMessageSizeIn;
@@ -142,10 +141,10 @@ std::string CMessageHeader::GetCommand() const
142141
return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
143142
}
144143

145-
bool CMessageHeader::IsValid() const
144+
bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
146145
{
147146
// Check start string
148-
if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
147+
if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
149148
return false;
150149

151150
// Check the command string for errors

src/protocol.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@
3131
class CMessageHeader
3232
{
3333
public:
34-
CMessageHeader();
35-
CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
34+
typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
35+
36+
CMessageHeader(const MessageStartChars& pchMessageStartIn);
37+
CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
3638

3739
std::string GetCommand() const;
38-
bool IsValid() const;
40+
bool IsValid(const MessageStartChars& messageStart) const;
3941

4042
ADD_SERIALIZE_METHODS;
4143

0 commit comments

Comments
 (0)