Bitcoin Core
28.99.0
P2P Digital Currency
src
net_types.cpp
Go to the documentation of this file.
1
// Copyright (c) 2021-2022 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 <
logging.h
>
8
#include <
netaddress.h
>
9
#include <
netbase.h
>
10
#include <
univalue.h
>
11
12
static
const
char
*
BANMAN_JSON_VERSION_KEY
{
"version"
};
13
14
CBanEntry::CBanEntry
(
const
UniValue
&
json
)
15
: nVersion(
json
[
BANMAN_JSON_VERSION_KEY
].getInt<int>()),
16
nCreateTime(
json
[
"ban_created"
].getInt<int64_t>()),
17
nBanUntil(
json
[
"banned_until"
].getInt<int64_t>())
18
{
19
}
20
21
UniValue
CBanEntry::ToJson
()
const
22
{
23
UniValue
json
(
UniValue::VOBJ
);
24
json
.pushKV(
BANMAN_JSON_VERSION_KEY
,
nVersion
);
25
json
.pushKV(
"ban_created"
,
nCreateTime
);
26
json
.pushKV(
"banned_until"
,
nBanUntil
);
27
return
json
;
28
}
29
30
static
const
char
*
BANMAN_JSON_ADDR_KEY
=
"address"
;
31
38
UniValue
BanMapToJson
(
const
banmap_t
& bans)
39
{
40
UniValue
bans_json(
UniValue::VARR
);
41
for
(
const
auto
& it : bans) {
42
const
auto
& address = it.first;
43
const
auto
& ban_entry = it.second;
44
UniValue
j = ban_entry.ToJson();
45
j.
pushKV
(
BANMAN_JSON_ADDR_KEY
, address.ToString());
46
bans_json.
push_back
(std::move(j));
47
}
48
return
bans_json;
49
}
50
58
void
BanMapFromJson
(
const
UniValue
& bans_json,
banmap_t
& bans)
59
{
60
for
(
const
auto
& ban_entry_json : bans_json.
getValues
()) {
61
const
int
version{ban_entry_json[
BANMAN_JSON_VERSION_KEY
].getInt<
int
>()};
62
if
(version !=
CBanEntry::CURRENT_VERSION
) {
63
LogPrintf
(
"Dropping entry with unknown version (%s) from ban list\n"
, version);
64
continue
;
65
}
66
const
auto
& subnet_str = ban_entry_json[
BANMAN_JSON_ADDR_KEY
].get_str();
67
const
CSubNet
subnet{
LookupSubNet
(subnet_str)};
68
if
(!subnet.IsValid()) {
69
LogPrintf
(
"Dropping entry with unparseable address or subnet (%s) from ban list\n"
, subnet_str);
70
continue
;
71
}
72
bans.insert_or_assign(subnet,
CBanEntry
{ban_entry_json});
73
}
74
}
CBanEntry
Definition:
net_types.h:15
CBanEntry::CBanEntry
CBanEntry()=default
CBanEntry::nCreateTime
int64_t nCreateTime
Definition:
net_types.h:19
CBanEntry::ToJson
UniValue ToJson() const
Generate a JSON representation of this ban entry.
Definition:
net_types.cpp:21
CBanEntry::nVersion
int nVersion
Definition:
net_types.h:18
CBanEntry::nBanUntil
int64_t nBanUntil
Definition:
net_types.h:20
CBanEntry::CURRENT_VERSION
static constexpr int CURRENT_VERSION
Definition:
net_types.h:17
CSubNet
Definition:
netaddress.h:480
UniValue
Definition:
univalue.h:22
UniValue::push_back
void push_back(UniValue val)
Definition:
univalue.cpp:104
UniValue::VOBJ
@ VOBJ
Definition:
univalue.h:24
UniValue::VARR
@ VARR
Definition:
univalue.h:24
UniValue::getValues
const std::vector< UniValue > & getValues() const
Definition:
univalue_get.cpp:53
UniValue::pushKV
void pushKV(std::string key, UniValue val)
Definition:
univalue.cpp:126
LogPrintf
#define LogPrintf(...)
Definition:
logging.h:266
ankerl::nanobench::templates::json
char const * json() noexcept
Template to generate JSON data.
BanMapFromJson
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
Definition:
net_types.cpp:58
BANMAN_JSON_ADDR_KEY
static const char * BANMAN_JSON_ADDR_KEY
Definition:
net_types.cpp:30
BANMAN_JSON_VERSION_KEY
static const char * BANMAN_JSON_VERSION_KEY
Definition:
net_types.cpp:12
BanMapToJson
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
Definition:
net_types.cpp:38
net_types.h
banmap_t
std::map< CSubNet, CBanEntry > banmap_t
Definition:
net_types.h:41
netaddress.h
LookupSubNet
CSubNet LookupSubNet(const std::string &subnet_str)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition:
netbase.cpp:756
netbase.h
logging.h
univalue.h
Generated on Wed Dec 18 2024 20:00:10 for Bitcoin Core by
1.9.4