Bitcoin Core 22.99.0
P2P Digital Currency
net_types.cpp
Go to the documentation of this file.
1// Copyright (c) 2021 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
12 : nVersion(json["version"].get_int()), nCreateTime(json["ban_created"].get_int64()),
13 nBanUntil(json["banned_until"].get_int64())
14{
15}
16
18{
20 json.pushKV("version", nVersion);
21 json.pushKV("ban_created", nCreateTime);
22 json.pushKV("banned_until", nBanUntil);
23 return json;
24}
25
26static const char* BANMAN_JSON_ADDR_KEY = "address";
27
35{
36 UniValue bans_json(UniValue::VARR);
37 for (const auto& it : bans) {
38 const auto& address = it.first;
39 const auto& ban_entry = it.second;
40 UniValue j = ban_entry.ToJson();
41 j.pushKV(BANMAN_JSON_ADDR_KEY, address.ToString());
42 bans_json.push_back(j);
43 }
44 return bans_json;
45}
46
54void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)
55{
56 for (const auto& ban_entry_json : bans_json.getValues()) {
57 CSubNet subnet;
58 const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
59 if (!LookupSubNet(subnet_str, subnet)) {
60 throw std::runtime_error(
61 strprintf("Cannot parse banned address or subnet: %s", subnet_str));
62 }
63 bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
64 }
65}
Definition: net_types.h:15
int64_t nCreateTime
Definition: net_types.h:19
UniValue ToJson() const
Generate a JSON representation of this ban entry.
Definition: net_types.cpp:17
CBanEntry()
Definition: net_types.h:22
int nVersion
Definition: net_types.h:18
int64_t nBanUntil
Definition: net_types.h:20
@ VOBJ
Definition: univalue.h:19
@ VARR
Definition: univalue.h:19
const std::vector< UniValue > & getValues() const
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
char const * json() noexcept
Template to generate JSON data.
void BanMapFromJson(const UniValue &bans_json, banmap_t &bans)
Convert a JSON array to a banmap_t object.
Definition: net_types.cpp:54
static const char * BANMAN_JSON_ADDR_KEY
Definition: net_types.cpp:26
UniValue BanMapToJson(const banmap_t &bans)
Convert a banmap_t object to a JSON array.
Definition: net_types.cpp:34
std::map< CSubNet, CBanEntry > banmap_t
Definition: net_types.h:41
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret, DNSLookupFn dns_lookup_function)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:679
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164