Skip to content

Commit 7a8c251

Browse files
committed
net: Disallow sending messages until the version handshake is complete
This is a change in behavior, though it's much more sane now than before.
1 parent 12752af commit 7a8c251

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,6 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
12771277
// Change version
12781278
pfrom->SetSendVersion(nSendVersion);
12791279
pfrom->nVersion = nVersion;
1280-
pfrom->fSuccessfullyConnected = true;
12811280

12821281
if((nServices & NODE_WITNESS))
12831282
{
@@ -1387,6 +1386,7 @@ bool static ProcessMessage(CNode* pfrom, std::string strCommand, CDataStream& vR
13871386
nCMPCTBLOCKVersion = 1;
13881387
connman.PushMessage(pfrom, msgMaker.Make(NetMsgType::SENDCMPCT, fAnnounceUsingCMPCTBLOCK, nCMPCTBLOCKVersion));
13891388
}
1389+
pfrom->fSuccessfullyConnected = true;
13901390
}
13911391

13921392

@@ -2725,8 +2725,8 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
27252725
{
27262726
const Consensus::Params& consensusParams = Params().GetConsensus();
27272727
{
2728-
// Don't send anything until we get its version message
2729-
if (pto->nVersion == 0 || pto->fDisconnect)
2728+
// Don't send anything until the version handshake is complete
2729+
if (!pto->fSuccessfullyConnected || pto->fDisconnect)
27302730
return true;
27312731

27322732
// If we get here, the outgoing message serialization version is set and can't change.

src/test/DoS_tests.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
5555
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
5656
GetNodeSignals().InitializeNode(&dummyNode1, *connman);
5757
dummyNode1.nVersion = 1;
58+
dummyNode1.fSuccessfullyConnected = true;
5859
Misbehaving(dummyNode1.GetId(), 100); // Should get banned
5960
SendMessages(&dummyNode1, *connman, interruptDummy);
6061
BOOST_CHECK(connman->IsBanned(addr1));
@@ -65,6 +66,7 @@ BOOST_AUTO_TEST_CASE(DoS_banning)
6566
dummyNode2.SetSendVersion(PROTOCOL_VERSION);
6667
GetNodeSignals().InitializeNode(&dummyNode2, *connman);
6768
dummyNode2.nVersion = 1;
69+
dummyNode2.fSuccessfullyConnected = true;
6870
Misbehaving(dummyNode2.GetId(), 50);
6971
SendMessages(&dummyNode2, *connman, interruptDummy);
7072
BOOST_CHECK(!connman->IsBanned(addr2)); // 2 not banned yet...
@@ -85,6 +87,7 @@ BOOST_AUTO_TEST_CASE(DoS_banscore)
8587
dummyNode1.SetSendVersion(PROTOCOL_VERSION);
8688
GetNodeSignals().InitializeNode(&dummyNode1, *connman);
8789
dummyNode1.nVersion = 1;
90+
dummyNode1.fSuccessfullyConnected = true;
8891
Misbehaving(dummyNode1.GetId(), 100);
8992
SendMessages(&dummyNode1, *connman, interruptDummy);
9093
BOOST_CHECK(!connman->IsBanned(addr1));
@@ -110,6 +113,7 @@ BOOST_AUTO_TEST_CASE(DoS_bantime)
110113
dummyNode.SetSendVersion(PROTOCOL_VERSION);
111114
GetNodeSignals().InitializeNode(&dummyNode, *connman);
112115
dummyNode.nVersion = 1;
116+
dummyNode.fSuccessfullyConnected = true;
113117

114118
Misbehaving(dummyNode.GetId(), 100);
115119
SendMessages(&dummyNode, *connman, interruptDummy);

0 commit comments

Comments
 (0)