Bitcoin Core
32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
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
16
static
const
char
*
BANMAN_JSON_VERSION_KEY
{
"version"
};
17
18
CBanEntry::CBanEntry
(
const
UniValue
& json)
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
25
UniValue
CBanEntry::ToJson
()
const
26
{
27
UniValue
json(
UniValue::VOBJ
);
28
json.pushKV(
BANMAN_JSON_VERSION_KEY
,
nVersion
);
29
json.pushKV(
"ban_created"
,
nCreateTime
);
30
json.pushKV(
"banned_until"
,
nBanUntil
);
31
return
json;
32
}
33
34
static
const
char
*
BANMAN_JSON_ADDR_KEY
=
"address"
;
35
42
UniValue
BanMapToJson
(
const
banmap_t
&
bans
)
43
{
44
UniValue
bans_json
(
UniValue::VARR
);
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
62
void
BanMapFromJson
(
const
UniValue
&
bans_json
,
banmap_t
&
bans
)
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
}
CBanEntry
Definition
net_types.h:18
CBanEntry::CBanEntry
CBanEntry()=default
CBanEntry::nCreateTime
int64_t nCreateTime
Definition
net_types.h:22
CBanEntry::ToJson
UniValue ToJson() const
Generate a JSON representation of this ban entry.
Definition
net_types.cpp:25
CBanEntry::nVersion
int nVersion
Definition
net_types.h:21
CBanEntry::nBanUntil
int64_t nBanUntil
Definition
net_types.h:23
CBanEntry::CURRENT_VERSION
static constexpr int CURRENT_VERSION
Definition
net_types.h:20
CSubNet
Definition
netaddress.h:481
UniValue
Definition
univalue.h:22
UniValue::VOBJ
@ VOBJ
Definition
univalue.h:24
UniValue::VARR
@ VARR
Definition
univalue.h:24
UniValue::pushKV
void pushKV(std::string key, UniValue val)
Definition
univalue.cpp:125
log.h
LogInfo
#define LogInfo(...)
Definition
log.h:136
BanMapFromJson
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
Definition
net_types.cpp:62
BANMAN_JSON_ADDR_KEY
static const char * BANMAN_JSON_ADDR_KEY
Definition
net_types.cpp:34
BANMAN_JSON_VERSION_KEY
static const char * BANMAN_JSON_VERSION_KEY
Definition
net_types.cpp:16
BanMapToJson
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
Definition
net_types.cpp:42
net_types.h
banmap_t
std::map< CSubNet, CBanEntry > banmap_t
Definition
net_types.h:44
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:831
netbase.h
univalue.h
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:82
Generated on Sat Nov 14 2026 20:00:27 for Bitcoin Core by
1.9.8