Skip to content

Commit 08a12e0

Browse files
theuniFuzzbawls
authored andcommitted
net: log an error rather than asserting if send version is misused
Also cleaned up the comments and moved from the header to the .cpp so that logging headers aren't needed from net.h
1 parent cd8b82c commit 08a12e0

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

src/net.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -743,6 +743,32 @@ bool CNode::ReceiveMsgBytes(const char* pch, unsigned int nBytes, bool& complete
743743
return true;
744744
}
745745

746+
void CNode::SetSendVersion(int nVersionIn)
747+
{
748+
// Send version may only be changed in the version message, and
749+
// only one version message is allowed per session. We can therefore
750+
// treat this value as const and even atomic as long as it's only used
751+
// once a version message has been successfully processed. Any attempt to
752+
// set this twice is an error.
753+
if (nSendVersion != 0) {
754+
error("Send version already set for node: %i. Refusing to change from %i to %i", id, nSendVersion, nVersionIn);
755+
} else {
756+
nSendVersion = nVersionIn;
757+
}
758+
}
759+
760+
int CNode::GetSendVersion() const
761+
{
762+
// The send version should always be explicitly set to
763+
// INIT_PROTO_VERSION rather than using this value until SetSendVersion
764+
// has been called.
765+
if (nSendVersion == 0) {
766+
error("Requesting unset send version for node: %i. Using %i", id, INIT_PROTO_VERSION);
767+
return INIT_PROTO_VERSION;
768+
}
769+
return nSendVersion;
770+
}
771+
746772
int CNetMessage::readHeader(const char* pch, unsigned int nBytes)
747773
{
748774
// copy data to temporary parsing buffer

src/net.h

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -684,25 +684,8 @@ class CNode
684684
{
685685
return nRecvVersion;
686686
}
687-
void SetSendVersion(int nVersionIn)
688-
{
689-
// Send version may only be changed in the version message, and
690-
// only one version message is allowed per session. We can therefore
691-
// treat this value as const and even atomic as long as it's only used
692-
// once the handshake is complete. Any attempt to set this twice is an
693-
// error.
694-
assert(nSendVersion == 0);
695-
nSendVersion = nVersionIn;
696-
}
697-
698-
int GetSendVersion() const
699-
{
700-
// The send version should always be explicitly set to
701-
// INIT_PROTO_VERSION rather than using this value until the handshake
702-
// is complete.
703-
assert(nSendVersion != 0);
704-
return nSendVersion;
705-
}
687+
void SetSendVersion(int nVersionIn);
688+
int GetSendVersion() const;
706689

707690
CNode* AddRef()
708691
{

0 commit comments

Comments
 (0)