net: Drop only invalid entries when reading banlist.json

Currently all entries in the file are dropped. Fix that by only dropping the invalid ones
This commit is contained in:
MarcoFalke
2021-06-28 16:41:11 +02:00
parent 9015d11842
commit faa6c3d44c
3 changed files with 57 additions and 4 deletions

View File

@@ -4,12 +4,16 @@
#include <net_types.h>
#include <logging.h>
#include <netaddress.h>
#include <netbase.h>
#include <univalue.h>
static const char* BANMAN_JSON_VERSION_KEY{"version"};
CBanEntry::CBanEntry(const UniValue& json)
: nVersion(json["version"].get_int()), nCreateTime(json["ban_created"].get_int64()),
: nVersion(json[BANMAN_JSON_VERSION_KEY].get_int()),
nCreateTime(json["ban_created"].get_int64()),
nBanUntil(json["banned_until"].get_int64())
{
}
@@ -17,7 +21,7 @@ CBanEntry::CBanEntry(const UniValue& json)
UniValue CBanEntry::ToJson() const
{
UniValue json(UniValue::VOBJ);
json.pushKV("version", nVersion);
json.pushKV(BANMAN_JSON_VERSION_KEY, nVersion);
json.pushKV("ban_created", nCreateTime);
json.pushKV("banned_until", nBanUntil);
return json;
@@ -54,11 +58,16 @@ UniValue BanMapToJson(const banmap_t& bans)
void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)
{
for (const auto& ban_entry_json : bans_json.getValues()) {
const int version{ban_entry_json[BANMAN_JSON_VERSION_KEY].get_int()};
if (version != CBanEntry::CURRENT_VERSION) {
LogPrintf("Dropping entry with unknown version (%s) from ban list\n", version);
continue;
}
CSubNet subnet;
const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
if (!LookupSubNet(subnet_str, subnet)) {
throw std::runtime_error(
strprintf("Cannot parse banned address or subnet: %s", subnet_str));
LogPrintf("Dropping entry with unparseable address or subnet (%s) from ban list\n", subnet_str);
continue;
}
bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
}