Skip to content

Commit 2252fb9

Browse files
committed
[net] extend core functionallity for ban/unban/listban
1 parent 9849c66 commit 2252fb9

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

src/net.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -458,16 +458,31 @@ bool CNode::IsBanned(CNetAddr ip)
458458
return fResult;
459459
}
460460

461-
bool CNode::Ban(const CNetAddr &addr) {
461+
bool CNode::Ban(const CNetAddr &addr, int64_t bantimeoffset) {
462462
int64_t banTime = GetTime()+GetArg("-bantime", 60*60*24); // Default 24-hour ban
463-
{
464-
LOCK(cs_setBanned);
465-
if (setBanned[addr] < banTime)
466-
setBanned[addr] = banTime;
467-
}
463+
if (bantimeoffset > 0)
464+
banTime = GetTime()+bantimeoffset;
465+
466+
LOCK(cs_setBanned);
467+
if (setBanned[addr] < banTime)
468+
setBanned[addr] = banTime;
469+
468470
return true;
469471
}
470472

473+
bool CNode::Unban(const CNetAddr &addr) {
474+
LOCK(cs_setBanned);
475+
if (setBanned.erase(addr))
476+
return true;
477+
return false;
478+
}
479+
480+
void CNode::GetBanned(std::map<CNetAddr, int64_t> &banMap)
481+
{
482+
LOCK(cs_setBanned);
483+
banMap = setBanned; //create a thread safe copy
484+
}
485+
471486

472487
std::vector<CSubNet> CNode::vWhitelistedRange;
473488
CCriticalSection CNode::cs_vWhitelistedRange;

src/net.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,10 @@ class CNode
606606
// new code.
607607
static void ClearBanned(); // needed for unit testing
608608
static bool IsBanned(CNetAddr ip);
609-
static bool Ban(const CNetAddr &ip);
609+
static bool Ban(const CNetAddr &ip, int64_t bantimeoffset = 0);
610+
static bool Unban(const CNetAddr &ip);
611+
static void GetBanned(std::map<CNetAddr, int64_t> &banmap);
612+
610613
void copyStats(CNodeStats &stats);
611614

612615
static bool IsWhitelistedRange(const CNetAddr &ip);

0 commit comments

Comments
 (0)