aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp88
1 files changed, 53 insertions, 35 deletions
diff --git a/src/net.cpp b/src/net.cpp
index c0b39925c6..7ae88b47a0 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -567,42 +567,28 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
nLastRecv = nTimeMicros / 1000000;
nRecvBytes += nBytes;
while (nBytes > 0) {
-
- // get current incomplete message, or create a new one
- if (vRecvMsg.empty() ||
- vRecvMsg.back().complete())
- vRecvMsg.push_back(CNetMessage(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION));
-
- CNetMessage& msg = vRecvMsg.back();
-
// absorb network data
- int handled;
- if (!msg.in_data)
- handled = msg.readHeader(pch, nBytes);
- else
- handled = msg.readData(pch, nBytes);
-
- if (handled < 0)
- return false;
-
- if (msg.in_data && msg.hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
- LogPrint(BCLog::NET, "Oversized message from peer=%i, disconnecting\n", GetId());
- return false;
- }
+ int handled = m_deserializer->Read(pch, nBytes);
+ if (handled < 0) return false;
pch += handled;
nBytes -= handled;
- if (msg.complete()) {
+ if (m_deserializer->Complete()) {
+ // decompose a transport agnostic CNetMessage from the deserializer
+ CNetMessage msg = m_deserializer->GetMessage(Params().MessageStart(), nTimeMicros);
+
//store received bytes per message command
//to prevent a memory DOS, only allow valid commands
- mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.hdr.pchCommand);
+ mapMsgCmdSize::iterator i = mapRecvBytesPerMsgCmd.find(msg.m_command);
if (i == mapRecvBytesPerMsgCmd.end())
i = mapRecvBytesPerMsgCmd.find(NET_MESSAGE_COMMAND_OTHER);
assert(i != mapRecvBytesPerMsgCmd.end());
- i->second += msg.hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
+ i->second += msg.m_raw_message_size;
+
+ // push the message to the process queue,
+ vRecvMsg.push_back(std::move(msg));
- msg.nTime = nTimeMicros;
complete = true;
}
}
@@ -636,8 +622,7 @@ int CNode::GetSendVersion() const
return nSendVersion;
}
-
-int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
+int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
{
// copy data to temporary parsing buffer
unsigned int nRemaining = 24 - nHdrPos;
@@ -658,9 +643,10 @@ int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
return -1;
}
- // reject messages larger than MAX_SIZE
- if (hdr.nMessageSize > MAX_SIZE)
+ // reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
+ if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
return -1;
+ }
// switch state to reading message data
in_data = true;
@@ -668,7 +654,7 @@ int CNetMessage::readHeader(const char *pch, unsigned int nBytes)
return nCopy;
}
-int CNetMessage::readData(const char *pch, unsigned int nBytes)
+int V1TransportDeserializer::readData(const char *pch, unsigned int nBytes)
{
unsigned int nRemaining = hdr.nMessageSize - nDataPos;
unsigned int nCopy = std::min(nRemaining, nBytes);
@@ -685,14 +671,44 @@ int CNetMessage::readData(const char *pch, unsigned int nBytes)
return nCopy;
}
-const uint256& CNetMessage::GetMessageHash() const
+const uint256& V1TransportDeserializer::GetMessageHash() const
{
- assert(complete());
+ assert(Complete());
if (data_hash.IsNull())
hasher.Finalize(data_hash.begin());
return data_hash;
}
+CNetMessage V1TransportDeserializer::GetMessage(const CMessageHeader::MessageStartChars& message_start, int64_t time) {
+ // decompose a single CNetMessage from the TransportDeserializer
+ CNetMessage msg(std::move(vRecv));
+
+ // store state about valid header, netmagic and checksum
+ msg.m_valid_header = hdr.IsValid(message_start);
+ msg.m_valid_netmagic = (memcmp(hdr.pchMessageStart, message_start, CMessageHeader::MESSAGE_START_SIZE) == 0);
+ uint256 hash = GetMessageHash();
+
+ // store command string, payload size
+ msg.m_command = hdr.GetCommand();
+ msg.m_message_size = hdr.nMessageSize;
+ msg.m_raw_message_size = hdr.nMessageSize + CMessageHeader::HEADER_SIZE;
+
+ msg.m_valid_checksum = (memcmp(hash.begin(), hdr.pchChecksum, CMessageHeader::CHECKSUM_SIZE) == 0);
+ if (!msg.m_valid_checksum) {
+ LogPrint(BCLog::NET, "CHECKSUM ERROR (%s, %u bytes), expected %s was %s\n",
+ SanitizeString(msg.m_command), msg.m_message_size,
+ HexStr(hash.begin(), hash.begin()+CMessageHeader::CHECKSUM_SIZE),
+ HexStr(hdr.pchChecksum, hdr.pchChecksum+CMessageHeader::CHECKSUM_SIZE));
+ }
+
+ // store receive time
+ msg.m_time = time;
+
+ // reset the network deserializer (prepare for the next message)
+ Reset();
+ return msg;
+}
+
size_t CConnman::SocketSendData(CNode *pnode) const EXCLUSIVE_LOCKS_REQUIRED(pnode->cs_vSend)
{
auto it = pnode->vSendMsg.begin();
@@ -1344,9 +1360,9 @@ void CConnman::SocketHandler()
size_t nSizeAdded = 0;
auto it(pnode->vRecvMsg.begin());
for (; it != pnode->vRecvMsg.end(); ++it) {
- if (!it->complete())
- break;
- nSizeAdded += it->vRecv.size() + CMessageHeader::HEADER_SIZE;
+ // vRecvMsg contains only completed CNetMessage
+ // the single possible partially deserialized message are held by TransportDeserializer
+ nSizeAdded += it->m_raw_message_size;
}
{
LOCK(pnode->cs_vProcessMsg);
@@ -2676,6 +2692,8 @@ CNode::CNode(NodeId idIn, ServiceFlags nLocalServicesIn, int nMyStartingHeightIn
} else {
LogPrint(BCLog::NET, "Added connection peer=%d\n", id);
}
+
+ m_deserializer = MakeUnique<V1TransportDeserializer>(V1TransportDeserializer(Params().MessageStart(), SER_NETWORK, INIT_PROTO_VERSION));
}
CNode::~CNode()