Browse Source

net: make Ban/Unban/ClearBan functionality consistent

- Ban/Unban/ClearBan call uiInterface.BannedListChanged() as necessary
- Ban/Unban/ClearBan sync to disk if the operation is user-invoked
- Mark node for disconnection automatically when banning
- Lock cs_vNodes while setting disconnected
- Don't spin in a tight loop while setting disconnected
0.13
Cory Fields 9 years ago
parent
commit
8b8f87714d
  1. 32
      src/net.cpp
  2. 5
      src/qt/rpcconsole.cpp
  3. 10
      src/rpc/net.cpp

32
src/net.cpp

@ -462,11 +462,15 @@ CCriticalSection CNode::cs_setBanned; @@ -462,11 +462,15 @@ CCriticalSection CNode::cs_setBanned;
bool CNode::setBannedIsDirty;
void CNode::ClearBanned()
{
{
LOCK(cs_setBanned);
setBanned.clear();
setBannedIsDirty = true;
}
DumpBanlist(); //store banlist to disk
uiInterface.BannedListChanged();
}
bool CNode::IsBanned(CNetAddr ip)
{
@ -516,12 +520,26 @@ void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t banti @@ -516,12 +520,26 @@ void CNode::Ban(const CSubNet& subNet, const BanReason &banReason, int64_t banti
}
banEntry.nBanUntil = (sinceUnixEpoch ? 0 : GetTime() )+bantimeoffset;
{
LOCK(cs_setBanned);
if (setBanned[subNet].nBanUntil < banEntry.nBanUntil)
if (setBanned[subNet].nBanUntil < banEntry.nBanUntil) {
setBanned[subNet] = banEntry;
setBannedIsDirty = true;
}
else
return;
}
uiInterface.BannedListChanged();
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes) {
if (subNet.Match((CNetAddr)pnode->addr))
pnode->fDisconnect = true;
}
}
if(banReason == BanReasonManuallyAdded)
DumpBanlist(); //store banlist to disk immediately if user requested ban
}
bool CNode::Unban(const CNetAddr &addr) {
CSubNet subNet(addr);
@ -529,13 +547,15 @@ bool CNode::Unban(const CNetAddr &addr) { @@ -529,13 +547,15 @@ bool CNode::Unban(const CNetAddr &addr) {
}
bool CNode::Unban(const CSubNet &subNet) {
LOCK(cs_setBanned);
if (setBanned.erase(subNet))
{
LOCK(cs_setBanned);
if (!setBanned.erase(subNet))
return false;
setBannedIsDirty = true;
return true;
}
return false;
uiInterface.BannedListChanged();
DumpBanlist(); //store banlist to disk immediately
return true;
}
void CNode::GetBanned(banmap_t &banMap)

5
src/qt/rpcconsole.cpp

@ -885,15 +885,13 @@ void RPCConsole::banSelectedNode(int bantime) @@ -885,15 +885,13 @@ void RPCConsole::banSelectedNode(int bantime)
// Get currently selected peer address
QString strNode = GUIUtil::getEntryData(ui->peerWidget, 0, PeerTableModel::Address);
// Find possible nodes, ban it and clear the selected node
if (CNode *bannedNode = FindNode(strNode.toStdString())) {
if (FindNode(strNode.toStdString())) {
std::string nStr = strNode.toStdString();
std::string addr;
int port = 0;
SplitHostPort(nStr, port, addr);
CNode::Ban(CNetAddr(addr), BanReasonManuallyAdded, bantime);
bannedNode->fDisconnect = true;
DumpBanlist();
clearSelectedNode();
clientModel->getBanTableModel()->refresh();
@ -912,7 +910,6 @@ void RPCConsole::unbanSelectedNode() @@ -912,7 +910,6 @@ void RPCConsole::unbanSelectedNode()
if (possibleSubnet.IsValid())
{
CNode::Unban(possibleSubnet);
DumpBanlist();
clientModel->getBanTableModel()->refresh();
}
}

10
src/rpc/net.cpp

@ -565,20 +565,12 @@ UniValue setban(const UniValue& params, bool fHelp) @@ -565,20 +565,12 @@ UniValue setban(const UniValue& params, bool fHelp)
absolute = true;
isSubnet ? CNode::Ban(subNet, BanReasonManuallyAdded, banTime, absolute) : CNode::Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
//disconnect possible nodes
while(CNode *bannedNode = (isSubnet ? FindNode(subNet) : FindNode(netAddr)))
bannedNode->fDisconnect = true;
}
else if(strCommand == "remove")
{
if (!( isSubnet ? CNode::Unban(subNet) : CNode::Unban(netAddr) ))
throw JSONRPCError(RPC_MISC_ERROR, "Error: Unban failed");
}
DumpBanlist(); //store banlist to disk
uiInterface.BannedListChanged();
return NullUniValue;
}
@ -624,8 +616,6 @@ UniValue clearbanned(const UniValue& params, bool fHelp) @@ -624,8 +616,6 @@ UniValue clearbanned(const UniValue& params, bool fHelp)
);
CNode::ClearBanned();
DumpBanlist(); //store banlist to disk
uiInterface.BannedListChanged();
return NullUniValue;
}

Loading…
Cancel
Save