Skip to content

Commit 7cc2b9f

Browse files
theunidongcarl
authored andcommitted
net: Break disconnecting out of Ban()
These are separate events which need to be carried out by separate subsystems. This also cleans up some whitespace and tabs in qt to avoid getting flagged by the linter. Current behavior is preserved.
1 parent f71c2ea commit 7cc2b9f

File tree

7 files changed

+55
-25
lines changed

7 files changed

+55
-25
lines changed

src/interfaces/node.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ class NodeImpl : public Node
144144
}
145145
return false;
146146
}
147+
bool disconnect(const CNetAddr& net_addr) override
148+
{
149+
if (g_connman) {
150+
return g_connman->DisconnectNode(net_addr);
151+
}
152+
return false;
153+
}
147154
bool disconnect(NodeId id) override
148155
{
149156
if (g_connman) {

src/interfaces/node.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ class Node
113113
//! Unban node.
114114
virtual bool unban(const CSubNet& ip) = 0;
115115

116-
//! Disconnect node.
116+
//! Disconnect node by address.
117+
virtual bool disconnect(const CNetAddr& net_addr) = 0;
118+
119+
//! Disconnect node by id.
117120
virtual bool disconnect(NodeId id) = 0;
118121

119122
//! Get total bytes recv.

src/net.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,6 @@ void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t ba
554554
}
555555
if(clientInterface)
556556
clientInterface->BannedListChanged();
557-
{
558-
LOCK(cs_vNodes);
559-
for (CNode* pnode : vNodes) {
560-
if (subNet.Match(static_cast<CNetAddr>(pnode->addr)))
561-
pnode->fDisconnect = true;
562-
}
563-
}
564557
if(banReason == BanReasonManuallyAdded)
565558
DumpBanlist(); //store banlist to disk immediately if user requested ban
566559
}
@@ -2643,6 +2636,25 @@ bool CConnman::DisconnectNode(const std::string& strNode)
26432636
}
26442637
return false;
26452638
}
2639+
2640+
bool CConnman::DisconnectNode(const CSubNet& subnet)
2641+
{
2642+
bool disconnected = false;
2643+
LOCK(cs_vNodes);
2644+
for (CNode* pnode : vNodes) {
2645+
if (subnet.Match(pnode->addr)) {
2646+
pnode->fDisconnect = true;
2647+
disconnected = true;
2648+
}
2649+
}
2650+
return disconnected;
2651+
}
2652+
2653+
bool CConnman::DisconnectNode(const CNetAddr& addr)
2654+
{
2655+
return DisconnectNode(CSubNet(addr));
2656+
}
2657+
26462658
bool CConnman::DisconnectNode(NodeId id)
26472659
{
26482660
LOCK(cs_vNodes);

src/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,8 @@ class CConnman
282282
size_t GetNodeCount(NumConnections num);
283283
void GetNodeStats(std::vector<CNodeStats>& vstats);
284284
bool DisconnectNode(const std::string& node);
285+
bool DisconnectNode(const CSubNet& subnet);
286+
bool DisconnectNode(const CNetAddr& addr);
285287
bool DisconnectNode(NodeId id);
286288

287289
ServiceFlags GetLocalServices() const;

src/net_processing.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2961,14 +2961,14 @@ static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman, bool en
29612961
LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode->addr.ToString());
29622962
else if (pnode->m_manual_connection)
29632963
LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode->addr.ToString());
2964-
else {
2964+
else if (pnode->addr.IsLocal()) {
2965+
// Disconnect but don't ban _this_ local node
2966+
LogPrintf("Warning: disconnecting but not banning local peer %s!\n", pnode->addr.ToString());
29652967
pnode->fDisconnect = true;
2966-
if (pnode->addr.IsLocal())
2967-
LogPrintf("Warning: not banning local peer %s!\n", pnode->addr.ToString());
2968-
else
2969-
{
2970-
connman->Ban(pnode->addr, BanReasonNodeMisbehaving);
2971-
}
2968+
} else {
2969+
// Disconnect and ban all nodes sharing the address
2970+
connman->Ban(pnode->addr, BanReasonNodeMisbehaving);
2971+
connman->DisconnectNode(pnode->addr);
29722972
}
29732973
return true;
29742974
}

src/qt/rpcconsole.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,16 +1216,16 @@ void RPCConsole::banSelectedNode(int bantime)
12161216
// Get currently selected peer address
12171217
NodeId id = nodes.at(i).data().toLongLong();
12181218

1219-
// Get currently selected peer address
1220-
int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(id);
1221-
if(detailNodeRow < 0)
1222-
return;
1223-
1224-
// Find possible nodes, ban it and clear the selected node
1225-
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
1226-
if(stats) {
1219+
// Get currently selected peer address
1220+
int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(id);
1221+
if (detailNodeRow < 0) return;
1222+
1223+
// Find possible nodes, ban it and clear the selected node
1224+
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
1225+
if (stats) {
12271226
m_node.ban(stats->nodeStats.addr, BanReasonManuallyAdded, bantime);
1228-
}
1227+
m_node.disconnect(stats->nodeStats.addr);
1228+
}
12291229
}
12301230
clearSelectedNode();
12311231
clientModel->getBanTableModel()->refresh();

src/rpc/net.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,13 @@ static UniValue setban(const JSONRPCRequest& request)
565565
if (request.params[3].isTrue())
566566
absolute = true;
567567

568-
isSubnet ? g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute) : g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
568+
if (isSubnet) {
569+
g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute);
570+
g_connman->DisconnectNode(subNet);
571+
} else {
572+
g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
573+
g_connman->DisconnectNode(netAddr);
574+
}
569575
}
570576
else if(strCommand == "remove")
571577
{

0 commit comments

Comments
 (0)