Skip to content

Commit 07d8c7b

Browse files
theuniFuzzbawls
authored andcommitted
net: don't send any messages before handshake or after fdisconnect
Also, send reject messages earlier in SendMessages(), so that disconnections are processed earlier. These changes combined should ensure that no message is ever sent after fDisconnect is set.
1 parent 9adfc7f commit 07d8c7b

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/main.cpp

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6200,8 +6200,8 @@ bool ProcessMessages(CNode* pfrom, CConnman& connman, std::atomic<bool>& interru
62006200
bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsgProc)
62016201
{
62026202
{
6203-
// Don't send anything until we get their version message
6204-
if (pto->nVersion == 0)
6203+
// Don't send anything until we get its version message
6204+
if (pto->nVersion == 0 || pto->fDisconnect)
62056205
return true;
62066206

62076207
//
@@ -6237,6 +6237,27 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
62376237
if (!lockMain)
62386238
return true;
62396239

6240+
CNodeState& state = *State(pto->GetId());
6241+
6242+
for (const CBlockReject& reject : state.rejects)
6243+
connman.PushMessage(pto, NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
6244+
state.rejects.clear();
6245+
6246+
if (state.fShouldBan) {
6247+
state.fShouldBan = false;
6248+
if (pto->fWhitelisted)
6249+
LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
6250+
else {
6251+
pto->fDisconnect = true;
6252+
if (pto->addr.IsLocal())
6253+
LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
6254+
else {
6255+
connman.Ban(pto->addr, BanReasonNodeMisbehaving);
6256+
}
6257+
return true;
6258+
}
6259+
}
6260+
62406261
// Address refresh broadcast
62416262
int64_t nNow = GetTimeMicros();
62426263
if (!IsInitialBlockDownload() && pto->nNextLocalAddrSend < nNow) {
@@ -6267,25 +6288,6 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
62676288
connman.PushMessage(pto, NetMsgType::ADDR, vAddr);
62686289
}
62696290

6270-
CNodeState& state = *State(pto->GetId());
6271-
if (state.fShouldBan) {
6272-
if (pto->fWhitelisted)
6273-
LogPrintf("Warning: not punishing whitelisted peer %s!\n", pto->addr.ToString());
6274-
else {
6275-
pto->fDisconnect = true;
6276-
if (pto->addr.IsLocal())
6277-
LogPrintf("Warning: not banning local peer %s!\n", pto->addr.ToString());
6278-
else {
6279-
connman.Ban(pto->addr, BanReasonNodeMisbehaving);
6280-
}
6281-
}
6282-
state.fShouldBan = false;
6283-
}
6284-
6285-
for (const CBlockReject& reject : state.rejects)
6286-
connman.PushMessage(pto, NetMsgType::REJECT, (std::string)NetMsgType::BLOCK, reject.chRejectCode, reject.strRejectReason, reject.hashBlock);
6287-
state.rejects.clear();
6288-
62896291
// Start block sync
62906292
if (pindexBestHeader == NULL)
62916293
pindexBestHeader = chainActive.Tip();
@@ -6364,6 +6366,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
63646366
// should only happen during initial block download.
63656367
LogPrintf("Peer=%d is stalling block download, disconnecting\n", pto->id);
63666368
pto->fDisconnect = true;
6369+
return true;
63676370
}
63686371
// In case there is a block that has been in flight from this peer for (2 + 0.5 * N) times the block interval
63696372
// (with N the number of validated blocks that were in flight at the time it was requested), disconnect due to
@@ -6373,6 +6376,7 @@ bool SendMessages(CNode* pto, CConnman& connman, std::atomic<bool>& interruptMsg
63736376
if (!pto->fDisconnect && state.vBlocksInFlight.size() > 0 && state.vBlocksInFlight.front().nTime < nNow - 500000 * Params().GetConsensus().nTargetSpacing * (4 + state.vBlocksInFlight.front().nValidatedQueuedBefore)) {
63746377
LogPrintf("Timeout downloading block %s from peer=%d, disconnecting\n", state.vBlocksInFlight.front().hash.ToString(), pto->id);
63756378
pto->fDisconnect = true;
6379+
return true;
63766380
}
63776381

63786382
//

0 commit comments

Comments
 (0)