Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
net_types.cpp
Go to the documentation of this file.
1// Copyright (c) 2021-present The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <net_types.h>
6
7#include <netaddress.h>
8#include <netbase.h>
9#include <univalue.h>
10#include <util/log.h>
11
12#include <string>
13#include <utility>
14#include <vector>
15
16static const char* BANMAN_JSON_VERSION_KEY{"version"};
17
19 : nVersion(json[BANMAN_JSON_VERSION_KEY].getInt<int>()),
20 nCreateTime(json["ban_created"].getInt<int64_t>()),
21 nBanUntil(json["banned_until"].getInt<int64_t>())
22{
23}
24
26{
29 json.pushKV("ban_created", nCreateTime);
30 json.pushKV("banned_until", nBanUntil);
31 return json;
32}
33
34static const char* BANMAN_JSON_ADDR_KEY = "address";
35
43{
45 for (const auto& it : bans) {
46 const auto& address = it.first;
47 const auto& ban_entry = it.second;
48 UniValue j = ban_entry.ToJson();
49 j.pushKV(BANMAN_JSON_ADDR_KEY, address.ToString());
50 bans_json.push_back(std::move(j));
51 }
52 return bans_json;
53}
54
63{
64 for (const auto& ban_entry_json : bans_json.getValues()) {
65 const int version{ban_entry_json[BANMAN_JSON_VERSION_KEY].getInt<int>()};
66 if (version != CBanEntry::CURRENT_VERSION) {
67 LogInfo("Dropping entry with unknown version (%s) from ban list\n", version);
68 continue;
69 }
70 const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
71 const CSubNet subnet{LookupSubNet(subnet_str)};
72 if (!subnet.IsValid()) {
73 LogInfo("Dropping entry with unparseable address or subnet (%s) from ban list\n", subnet_str);
74 continue;
75 }
76 bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
77 }
78}
Definition net_types.h:18
CBanEntry()=default
int64_t nCreateTime
Definition net_types.h:22
UniValue ToJson() const
Generate a JSON representation of this ban entry.
Definition net_types.cpp:25
int nVersion
Definition net_types.h:21
int64_t nBanUntil
Definition net_types.h:23
static constexpr int CURRENT_VERSION
Definition net_types.h:20
void pushKV(std::string key, UniValue val)
Definition univalue.cpp:125
#define LogInfo(...)
Definition log.h:136
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
Definition net_types.cpp:62
static const char * BANMAN_JSON_ADDR_KEY
Definition net_types.cpp:34
static const char * BANMAN_JSON_VERSION_KEY
Definition net_types.cpp:16
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
Definition net_types.cpp:42
std::map< CSubNet, CBanEntry > banmap_t
Definition net_types.h:44
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition netbase.cpp:831
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82