Skip to content

Commit

Permalink
net: Break disconnecting out of Ban()
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
theuni authored and dongcarl committed Jan 16, 2019
1 parent f71c2ea commit 7cc2b9f
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 25 deletions.
7 changes: 7 additions & 0 deletions src/interfaces/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ class NodeImpl : public Node
}
return false;
}
bool disconnect(const CNetAddr& net_addr) override
{
if (g_connman) {
return g_connman->DisconnectNode(net_addr);
}
return false;
}
bool disconnect(NodeId id) override
{
if (g_connman) {
Expand Down
5 changes: 4 additions & 1 deletion src/interfaces/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ class Node
//! Unban node.
virtual bool unban(const CSubNet& ip) = 0;

//! Disconnect node.
//! Disconnect node by address.
virtual bool disconnect(const CNetAddr& net_addr) = 0;

//! Disconnect node by id.
virtual bool disconnect(NodeId id) = 0;

//! Get total bytes recv.
Expand Down
26 changes: 19 additions & 7 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,6 @@ void CConnman::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t ba
}
if(clientInterface)
clientInterface->BannedListChanged();
{
LOCK(cs_vNodes);
for (CNode* pnode : vNodes) {
if (subNet.Match(static_cast<CNetAddr>(pnode->addr)))
pnode->fDisconnect = true;
}
}
if(banReason == BanReasonManuallyAdded)
DumpBanlist(); //store banlist to disk immediately if user requested ban
}
Expand Down Expand Up @@ -2643,6 +2636,25 @@ bool CConnman::DisconnectNode(const std::string& strNode)
}
return false;
}

bool CConnman::DisconnectNode(const CSubNet& subnet)
{
bool disconnected = false;
LOCK(cs_vNodes);
for (CNode* pnode : vNodes) {
if (subnet.Match(pnode->addr)) {
pnode->fDisconnect = true;
disconnected = true;
}
}
return disconnected;
}

bool CConnman::DisconnectNode(const CNetAddr& addr)
{
return DisconnectNode(CSubNet(addr));
}

bool CConnman::DisconnectNode(NodeId id)
{
LOCK(cs_vNodes);
Expand Down
2 changes: 2 additions & 0 deletions src/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ class CConnman
size_t GetNodeCount(NumConnections num);
void GetNodeStats(std::vector<CNodeStats>& vstats);
bool DisconnectNode(const std::string& node);
bool DisconnectNode(const CSubNet& subnet);
bool DisconnectNode(const CNetAddr& addr);
bool DisconnectNode(NodeId id);

ServiceFlags GetLocalServices() const;
Expand Down
14 changes: 7 additions & 7 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2961,14 +2961,14 @@ static bool SendRejectsAndCheckIfBanned(CNode* pnode, CConnman* connman, bool en
LogPrintf("Warning: not punishing whitelisted peer %s!\n", pnode->addr.ToString());
else if (pnode->m_manual_connection)
LogPrintf("Warning: not punishing manually-connected peer %s!\n", pnode->addr.ToString());
else {
else if (pnode->addr.IsLocal()) {
// Disconnect but don't ban _this_ local node
LogPrintf("Warning: disconnecting but not banning local peer %s!\n", pnode->addr.ToString());
pnode->fDisconnect = true;
if (pnode->addr.IsLocal())
LogPrintf("Warning: not banning local peer %s!\n", pnode->addr.ToString());
else
{
connman->Ban(pnode->addr, BanReasonNodeMisbehaving);
}
} else {
// Disconnect and ban all nodes sharing the address
connman->Ban(pnode->addr, BanReasonNodeMisbehaving);
connman->DisconnectNode(pnode->addr);
}
return true;
}
Expand Down
18 changes: 9 additions & 9 deletions src/qt/rpcconsole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1216,16 +1216,16 @@ void RPCConsole::banSelectedNode(int bantime)
// Get currently selected peer address
NodeId id = nodes.at(i).data().toLongLong();

// Get currently selected peer address
int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(id);
if(detailNodeRow < 0)
return;

// Find possible nodes, ban it and clear the selected node
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
if(stats) {
// Get currently selected peer address
int detailNodeRow = clientModel->getPeerTableModel()->getRowByNodeId(id);
if (detailNodeRow < 0) return;

// Find possible nodes, ban it and clear the selected node
const CNodeCombinedStats *stats = clientModel->getPeerTableModel()->getNodeStats(detailNodeRow);
if (stats) {
m_node.ban(stats->nodeStats.addr, BanReasonManuallyAdded, bantime);
}
m_node.disconnect(stats->nodeStats.addr);
}
}
clearSelectedNode();
clientModel->getBanTableModel()->refresh();
Expand Down
8 changes: 7 additions & 1 deletion src/rpc/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,13 @@ static UniValue setban(const JSONRPCRequest& request)
if (request.params[3].isTrue())
absolute = true;

isSubnet ? g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute) : g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
if (isSubnet) {
g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute);
g_connman->DisconnectNode(subNet);
} else {
g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
g_connman->DisconnectNode(netAddr);
}
}
else if(strCommand == "remove")
{
Expand Down

0 comments on commit 7cc2b9f

Please sign in to comment.