LCOV - code coverage report
Current view: top level - src - banman.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 89.2 % 130 116
Test Date: 2024-08-28 04:44:32 Functions: 100.0 % 15 15
Branches: 46.8 % 158 74

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-2010 Satoshi Nakamoto
       2                 :             : // Copyright (c) 2009-2022 The Bitcoin Core developers
       3                 :             : // Distributed under the MIT software license, see the accompanying
       4                 :             : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
       5                 :             : 
       6                 :             : #include <banman.h>
       7                 :             : 
       8                 :             : #include <common/system.h>
       9                 :             : #include <logging.h>
      10                 :             : #include <netaddress.h>
      11                 :             : #include <node/interface_ui.h>
      12                 :             : #include <sync.h>
      13                 :             : #include <util/time.h>
      14                 :             : #include <util/translation.h>
      15                 :             : 
      16                 :             : 
      17                 :         165 : BanMan::BanMan(fs::path ban_file, CClientUIInterface* client_interface, int64_t default_ban_time)
      18   [ +  -  +  - ]:         330 :     : m_client_interface(client_interface), m_ban_db(std::move(ban_file)), m_default_ban_time(default_ban_time)
      19                 :             : {
      20         [ +  - ]:         165 :     LoadBanlist();
      21         [ +  - ]:         165 :     DumpBanlist();
      22                 :         165 : }
      23                 :             : 
      24                 :         165 : BanMan::~BanMan()
      25                 :             : {
      26                 :         165 :     DumpBanlist();
      27                 :         165 : }
      28                 :             : 
      29                 :         165 : void BanMan::LoadBanlist()
      30                 :             : {
      31                 :         165 :     LOCK(m_banned_mutex);
      32                 :             : 
      33   [ -  +  -  -  :         165 :     if (m_client_interface) m_client_interface->InitMessage(_("Loading banlist…").translated);
                   -  - ]
      34                 :             : 
      35                 :         165 :     const auto start{SteadyClock::now()};
      36   [ +  -  +  + ]:         165 :     if (m_ban_db.Read(m_banned)) {
      37         [ +  - ]:           3 :         SweepBanned(); // sweep out unused entries
      38                 :             : 
      39   [ +  -  +  -  :           3 :         LogPrint(BCLog::NET, "Loaded %d banned node addresses/subnets  %dms\n", m_banned.size(),
                   +  - ]
      40                 :             :                  Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
      41                 :             :     } else {
      42         [ +  - ]:         162 :         LogPrintf("Recreating the banlist database\n");
      43         [ +  - ]:         162 :         m_banned = {};
      44                 :         162 :         m_is_dirty = true;
      45                 :             :     }
      46                 :         165 : }
      47                 :             : 
      48                 :         346 : void BanMan::DumpBanlist()
      49                 :             : {
      50   [ +  +  +  - ]:         346 :     static Mutex dump_mutex;
      51                 :         346 :     LOCK(dump_mutex);
      52                 :             : 
      53         [ +  - ]:         346 :     banmap_t banmap;
      54                 :         346 :     {
      55         [ +  - ]:         346 :         LOCK(m_banned_mutex);
      56         [ +  - ]:         346 :         SweepBanned();
      57   [ +  +  +  - ]:         346 :         if (!m_is_dirty) return;
      58         [ +  - ]:         178 :         banmap = m_banned;
      59         [ +  - ]:         178 :         m_is_dirty = false;
      60                 :         168 :     }
      61                 :             : 
      62                 :         178 :     const auto start{SteadyClock::now()};
      63   [ +  -  -  + ]:         178 :     if (!m_ban_db.Write(banmap)) {
      64         [ #  # ]:           0 :         LOCK(m_banned_mutex);
      65         [ #  # ]:           0 :         m_is_dirty = true;
      66                 :           0 :     }
      67                 :             : 
      68   [ +  -  +  -  :         178 :     LogPrint(BCLog::NET, "Flushed %d banned node addresses/subnets to disk  %dms\n", banmap.size(),
                   +  - ]
      69                 :             :              Ticks<std::chrono::milliseconds>(SteadyClock::now() - start));
      70   [ +  -  +  - ]:         692 : }
      71                 :             : 
      72                 :           7 : void BanMan::ClearBanned()
      73                 :             : {
      74                 :           7 :     {
      75                 :           7 :         LOCK(m_banned_mutex);
      76                 :           7 :         m_banned.clear();
      77         [ +  - ]:           7 :         m_is_dirty = true;
      78                 :           7 :     }
      79                 :           7 :     DumpBanlist(); //store banlist to disk
      80         [ -  + ]:           7 :     if (m_client_interface) m_client_interface->BannedListChanged();
      81                 :           7 : }
      82                 :             : 
      83                 :          10 : bool BanMan::IsDiscouraged(const CNetAddr& net_addr)
      84                 :             : {
      85                 :          10 :     LOCK(m_banned_mutex);
      86   [ +  -  +  -  :          10 :     return m_discouraged.contains(net_addr.GetAddrBytes());
                   +  - ]
      87                 :          10 : }
      88                 :             : 
      89                 :           4 : bool BanMan::IsBanned(const CNetAddr& net_addr)
      90                 :             : {
      91                 :           4 :     auto current_time = GetTime();
      92                 :           4 :     LOCK(m_banned_mutex);
      93         [ +  + ]:           4 :     for (const auto& it : m_banned) {
      94                 :           2 :         CSubNet sub_net = it.first;
      95                 :           2 :         CBanEntry ban_entry = it.second;
      96                 :             : 
      97   [ +  -  +  -  :           2 :         if (current_time < ban_entry.nBanUntil && sub_net.Match(net_addr)) {
                   +  - ]
      98                 :           2 :             return true;
      99                 :             :         }
     100                 :           2 :     }
     101                 :             :     return false;
     102                 :           4 : }
     103                 :             : 
     104                 :           5 : bool BanMan::IsBanned(const CSubNet& sub_net)
     105                 :             : {
     106                 :           5 :     auto current_time = GetTime();
     107                 :           5 :     LOCK(m_banned_mutex);
     108         [ +  - ]:           5 :     banmap_t::iterator i = m_banned.find(sub_net);
     109         [ -  + ]:           5 :     if (i != m_banned.end()) {
     110         [ #  # ]:           0 :         CBanEntry ban_entry = (*i).second;
     111         [ #  # ]:           0 :         if (current_time < ban_entry.nBanUntil) {
     112                 :           0 :             return true;
     113                 :             :         }
     114                 :             :     }
     115                 :             :     return false;
     116                 :           5 : }
     117                 :             : 
     118                 :           2 : void BanMan::Ban(const CNetAddr& net_addr, int64_t ban_time_offset, bool since_unix_epoch)
     119                 :             : {
     120                 :           2 :     CSubNet sub_net(net_addr);
     121         [ +  - ]:           2 :     Ban(sub_net, ban_time_offset, since_unix_epoch);
     122                 :           2 : }
     123                 :             : 
     124                 :           4 : void BanMan::Discourage(const CNetAddr& net_addr)
     125                 :             : {
     126                 :           4 :     LOCK(m_banned_mutex);
     127   [ +  -  +  -  :           8 :     m_discouraged.insert(net_addr.GetAddrBytes());
                   +  - ]
     128                 :           4 : }
     129                 :             : 
     130                 :           7 : void BanMan::Ban(const CSubNet& sub_net, int64_t ban_time_offset, bool since_unix_epoch)
     131                 :             : {
     132         [ +  + ]:           7 :     CBanEntry ban_entry(GetTime());
     133                 :             : 
     134                 :           7 :     int64_t normalized_ban_time_offset = ban_time_offset;
     135                 :           7 :     bool normalized_since_unix_epoch = since_unix_epoch;
     136         [ +  + ]:           7 :     if (ban_time_offset <= 0) {
     137                 :           5 :         normalized_ban_time_offset = m_default_ban_time;
     138                 :           5 :         normalized_since_unix_epoch = false;
     139                 :             :     }
     140         [ +  + ]:           7 :     ban_entry.nBanUntil = (normalized_since_unix_epoch ? 0 : GetTime()) + normalized_ban_time_offset;
     141                 :             : 
     142                 :           7 :     {
     143                 :           7 :         LOCK(m_banned_mutex);
     144   [ +  -  +  - ]:           7 :         if (m_banned[sub_net].nBanUntil < ban_entry.nBanUntil) {
     145         [ +  - ]:           7 :             m_banned[sub_net] = ban_entry;
     146         [ +  - ]:           7 :             m_is_dirty = true;
     147                 :             :         } else
     148         [ #  # ]:           0 :             return;
     149                 :           0 :     }
     150         [ -  + ]:           7 :     if (m_client_interface) m_client_interface->BannedListChanged();
     151                 :             : 
     152                 :             :     //store banlist to disk immediately
     153                 :           7 :     DumpBanlist();
     154                 :             : }
     155                 :             : 
     156                 :           1 : bool BanMan::Unban(const CNetAddr& net_addr)
     157                 :             : {
     158                 :           1 :     CSubNet sub_net(net_addr);
     159         [ +  - ]:           1 :     return Unban(sub_net);
     160                 :           1 : }
     161                 :             : 
     162                 :           2 : bool BanMan::Unban(const CSubNet& sub_net)
     163                 :             : {
     164                 :           2 :     {
     165                 :           2 :         LOCK(m_banned_mutex);
     166   [ +  -  -  +  :           2 :         if (m_banned.erase(sub_net) == 0) return false;
                   -  - ]
     167         [ +  - ]:           2 :         m_is_dirty = true;
     168                 :           0 :     }
     169         [ -  + ]:           2 :     if (m_client_interface) m_client_interface->BannedListChanged();
     170                 :           2 :     DumpBanlist(); //store banlist to disk immediately
     171                 :           2 :     return true;
     172                 :             : }
     173                 :             : 
     174                 :          10 : void BanMan::GetBanned(banmap_t& banmap)
     175                 :             : {
     176                 :          10 :     LOCK(m_banned_mutex);
     177                 :             :     // Sweep the banlist so expired bans are not returned
     178         [ +  - ]:          10 :     SweepBanned();
     179   [ +  -  +  - ]:          10 :     banmap = m_banned; //create a thread safe copy
     180                 :          10 : }
     181                 :             : 
     182                 :         359 : void BanMan::SweepBanned()
     183                 :             : {
     184                 :         359 :     AssertLockHeld(m_banned_mutex);
     185                 :             : 
     186                 :         359 :     int64_t now = GetTime();
     187                 :         359 :     bool notify_ui = false;
     188                 :         359 :     banmap_t::iterator it = m_banned.begin();
     189         [ +  + ]:         377 :     while (it != m_banned.end()) {
     190                 :          18 :         CSubNet sub_net = (*it).first;
     191         [ +  - ]:          18 :         CBanEntry ban_entry = (*it).second;
     192   [ +  -  +  -  :          18 :         if (!sub_net.IsValid() || now > ban_entry.nBanUntil) {
                   +  - ]
     193                 :           0 :             m_banned.erase(it++);
     194                 :           0 :             m_is_dirty = true;
     195                 :           0 :             notify_ui = true;
     196   [ #  #  #  #  :           0 :             LogPrint(BCLog::NET, "Removed banned node address/subnet: %s\n", sub_net.ToString());
             #  #  #  # ]
     197                 :             :         } else {
     198                 :          18 :             ++it;
     199                 :             :         }
     200                 :          18 :     }
     201                 :             : 
     202                 :             :     // update UI
     203   [ -  +  -  - ]:         359 :     if (notify_ui && m_client_interface) {
     204                 :           0 :         m_client_interface->BannedListChanged();
     205                 :             :     }
     206                 :         359 : }
        

Generated by: LCOV version 2.0-1