@@ -605,6 +605,7 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
605605 // absorb network data
606606 int handled = m_deserializer->Read (pch, nBytes);
607607 if (handled < 0 ) {
608+ // Serious header problem, disconnect from the peer.
608609 return false ;
609610 }
610611
@@ -616,6 +617,7 @@ bool CNode::ReceiveMsgBytes(const char *pch, unsigned int nBytes, bool& complete
616617 uint32_t out_err_raw_size{0 };
617618 Optional<CNetMessage> result{m_deserializer->GetMessage (time, out_err_raw_size)};
618619 if (!result) {
620+ // Message deserialization failed. Drop the message but don't disconnect the peer.
619621 // store the size of the corrupt message
620622 mapRecvBytesPerMsgCmd.find (NET_MESSAGE_COMMAND_OTHER)->second += out_err_raw_size;
621623 continue ;
@@ -657,11 +659,19 @@ int V1TransportDeserializer::readHeader(const char *pch, unsigned int nBytes)
657659 hdrbuf >> hdr;
658660 }
659661 catch (const std::exception&) {
662+ LogPrint (BCLog::NET, " HEADER ERROR - UNABLE TO DESERIALIZE, peer=%d\n " , m_node_id);
663+ return -1 ;
664+ }
665+
666+ // Check start string, network magic
667+ if (memcmp (hdr.pchMessageStart , m_chain_params.MessageStart (), CMessageHeader::MESSAGE_START_SIZE) != 0 ) {
668+ LogPrint (BCLog::NET, " HEADER ERROR - MESSAGESTART (%s, %u bytes), received %s, peer=%d\n " , hdr.GetCommand (), hdr.nMessageSize , HexStr (hdr.pchMessageStart ), m_node_id);
660669 return -1 ;
661670 }
662671
663672 // reject messages larger than MAX_SIZE or MAX_PROTOCOL_MESSAGE_LENGTH
664673 if (hdr.nMessageSize > MAX_SIZE || hdr.nMessageSize > MAX_PROTOCOL_MESSAGE_LENGTH) {
674+ LogPrint (BCLog::NET, " HEADER ERROR - SIZE (%s, %u bytes), peer=%d\n " , hdr.GetCommand (), hdr.nMessageSize , m_node_id);
665675 return -1 ;
666676 }
667677
@@ -701,10 +711,6 @@ Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::mic
701711 // decompose a single CNetMessage from the TransportDeserializer
702712 Optional<CNetMessage> msg (std::move (vRecv));
703713
704- // store state about valid header, netmagic and checksum
705- msg->m_valid_header = hdr.IsValid (m_chain_params.MessageStart ());
706- msg->m_valid_netmagic = (memcmp (hdr.pchMessageStart , m_chain_params.MessageStart (), CMessageHeader::MESSAGE_START_SIZE) == 0 );
707-
708714 // store command string, time, and sizes
709715 msg->m_command = hdr.GetCommand ();
710716 msg->m_time = time;
@@ -716,6 +722,7 @@ Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::mic
716722 // We just received a message off the wire, harvest entropy from the time (and the message checksum)
717723 RandAddEvent (ReadLE32 (hash.begin ()));
718724
725+ // Check checksum and header command string
719726 if (memcmp (hash.begin (), hdr.pchChecksum , CMessageHeader::CHECKSUM_SIZE) != 0 ) {
720727 LogPrint (BCLog::NET, " CHECKSUM ERROR (%s, %u bytes), expected %s was %s, peer=%d\n " ,
721728 SanitizeString (msg->m_command ), msg->m_message_size ,
@@ -724,6 +731,11 @@ Optional<CNetMessage> V1TransportDeserializer::GetMessage(const std::chrono::mic
724731 m_node_id);
725732 out_err_raw_size = msg->m_raw_message_size ;
726733 msg = nullopt ;
734+ } else if (!hdr.IsCommandValid ()) {
735+ LogPrint (BCLog::NET, " HEADER ERROR - COMMAND (%s, %u bytes), peer=%d\n " ,
736+ hdr.GetCommand (), msg->m_message_size , m_node_id);
737+ out_err_raw_size = msg->m_raw_message_size ;
738+ msg = nullopt ;
727739 }
728740
729741 // Always reset the network deserializer (prepare for the next message)
0 commit comments