Branch data Line data Source code
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 : 1 : CBanEntry::CBanEntry(const UniValue& json)
19 [ + - + - ]: 1 : : nVersion(json[BANMAN_JSON_VERSION_KEY].getInt<int>()),
20 [ + - + - ]: 1 : nCreateTime(json["ban_created"].getInt<int64_t>()),
21 [ + - + - ]: 1 : nBanUntil(json["banned_until"].getInt<int64_t>())
22 : : {
23 : 1 : }
24 : :
25 : 7 : UniValue CBanEntry::ToJson() const
26 : : {
27 : 7 : UniValue json(UniValue::VOBJ);
28 [ + - + - : 14 : json.pushKV(BANMAN_JSON_VERSION_KEY, nVersion);
+ - ]
29 [ + - + - : 14 : json.pushKV("ban_created", nCreateTime);
+ - ]
30 [ + - + - : 14 : json.pushKV("banned_until", nBanUntil);
+ - ]
31 : 7 : return json;
32 : 0 : }
33 : :
34 : : static const char* BANMAN_JSON_ADDR_KEY = "address";
35 : :
36 : : /**
37 : : * Convert a `banmap_t` object to a JSON array.
38 : : * @param[in] bans Bans list to convert.
39 : : * @return a JSON array, similar to the one returned by the `listbanned` RPC. Suitable for
40 : : * passing to `BanMapFromJson()`.
41 : : */
42 : 211 : UniValue BanMapToJson(const banmap_t& bans)
43 : : {
44 : 211 : UniValue bans_json(UniValue::VARR);
45 [ + + ]: 218 : for (const auto& it : bans) {
46 : 7 : const auto& address = it.first;
47 : 7 : const auto& ban_entry = it.second;
48 [ + - ]: 7 : UniValue j = ban_entry.ToJson();
49 [ + - + - : 14 : j.pushKV(BANMAN_JSON_ADDR_KEY, address.ToString());
+ - + - ]
50 [ + - ]: 7 : bans_json.push_back(std::move(j));
51 : 7 : }
52 : 211 : return bans_json;
53 : 0 : }
54 : :
55 : : /**
56 : : * Convert a JSON array to a `banmap_t` object.
57 : : * @param[in] bans_json JSON to convert, must be as returned by `BanMapToJson()`.
58 : : * @param[out] bans Bans list to create from the JSON.
59 : : * @throws std::runtime_error if the JSON does not have the expected fields or they contain
60 : : * unparsable values.
61 : : */
62 : 3 : void BanMapFromJson(const UniValue& bans_json, banmap_t& bans)
63 : : {
64 [ + + ]: 6 : for (const auto& ban_entry_json : bans_json.getValues()) {
65 [ + - + - ]: 3 : const int version{ban_entry_json[BANMAN_JSON_VERSION_KEY].getInt<int>()};
66 [ + + ]: 3 : if (version != CBanEntry::CURRENT_VERSION) {
67 : 1 : LogInfo("Dropping entry with unknown version (%s) from ban list\n", version);
68 : 1 : continue;
69 : : }
70 [ + - + - ]: 2 : const auto& subnet_str = ban_entry_json[BANMAN_JSON_ADDR_KEY].get_str();
71 : 2 : const CSubNet subnet{LookupSubNet(subnet_str)};
72 [ + - + + ]: 2 : if (!subnet.IsValid()) {
73 [ + - ]: 1 : LogInfo("Dropping entry with unparseable address or subnet (%s) from ban list\n", subnet_str);
74 : 1 : continue;
75 : : }
76 [ + - + - ]: 1 : bans.insert_or_assign(subnet, CBanEntry{ban_entry_json});
77 : 2 : }
78 : 3 : }
|