LCOV - code coverage report
Current view: top level - src - net_types.cpp (source / functions) Hit Total Coverage
Test: fuzz_coverage.info Lines: 5 37 13.5 %
Date: 2023-11-12 01:39:15 Functions: 1 4 25.0 %
Branches: 3 99 3.0 %

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

Generated by: LCOV version 1.14