banman: create and split out banman

Some say he has always been.
This commit is contained in:
Cory Fields
2017-10-05 13:10:58 -04:00
committed by Carl Dong
parent 83c1ea2e5e
commit 4c0d961eb0
10 changed files with 172 additions and 125 deletions

View File

@@ -531,8 +531,9 @@ static UniValue setban(const JSONRPCRequest& request)
+ HelpExampleCli("setban", "\"192.168.0.0/24\" \"add\"")
+ HelpExampleRpc("setban", "\"192.168.0.6\", \"add\", 86400")
);
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
if (!g_banman) {
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
}
CSubNet subNet;
CNetAddr netAddr;
@@ -554,8 +555,9 @@ static UniValue setban(const JSONRPCRequest& request)
if (strCommand == "add")
{
if (isSubnet ? g_connman->IsBanned(subNet) : g_connman->IsBanned(netAddr))
if (isSubnet ? g_banman->IsBanned(subNet) : g_banman->IsBanned(netAddr)) {
throw JSONRPCError(RPC_CLIENT_NODE_ALREADY_ADDED, "Error: IP/Subnet already banned");
}
int64_t banTime = 0; //use standard bantime if not specified
if (!request.params[2].isNull())
@@ -566,17 +568,22 @@ static UniValue setban(const JSONRPCRequest& request)
absolute = true;
if (isSubnet) {
g_connman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute);
g_connman->DisconnectNode(subNet);
g_banman->Ban(subNet, BanReasonManuallyAdded, banTime, absolute);
if (g_connman) {
g_connman->DisconnectNode(subNet);
}
} else {
g_connman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
g_connman->DisconnectNode(netAddr);
g_banman->Ban(netAddr, BanReasonManuallyAdded, banTime, absolute);
if (g_connman) {
g_connman->DisconnectNode(netAddr);
}
}
}
else if(strCommand == "remove")
{
if (!( isSubnet ? g_connman->Unban(subNet) : g_connman->Unban(netAddr) ))
if (!( isSubnet ? g_banman->Unban(subNet) : g_banman->Unban(netAddr) )) {
throw JSONRPCError(RPC_CLIENT_INVALID_IP_OR_SUBNET, "Error: Unban failed. Requested address/subnet was not previously banned.");
}
}
return NullUniValue;
}
@@ -593,11 +600,12 @@ static UniValue listbanned(const JSONRPCRequest& request)
+ HelpExampleRpc("listbanned", "")
);
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
if(!g_banman) {
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
}
banmap_t banMap;
g_connman->GetBanned(banMap);
g_banman->GetBanned(banMap);
UniValue bannedAddresses(UniValue::VARR);
for (const auto& entry : banMap)
@@ -626,10 +634,11 @@ static UniValue clearbanned(const JSONRPCRequest& request)
+ HelpExampleCli("clearbanned", "")
+ HelpExampleRpc("clearbanned", "")
);
if(!g_connman)
throw JSONRPCError(RPC_CLIENT_P2P_DISABLED, "Error: Peer-to-peer functionality missing or disabled");
if (!g_banman) {
throw JSONRPCError(RPC_DATABASE_ERROR, "Error: Ban database not loaded");
}
g_connman->ClearBanned();
g_banman->ClearBanned();
return NullUniValue;
}