LCOV - code coverage report
Current view: top level - src - net_permissions.h (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 100.0 % 15 15
Test Date: 2024-11-04 05:10:19 Functions: - 0 0
Branches: 56.7 % 104 59

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2009-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 <netaddress.h>
       6                 :             : #include <netbase.h>
       7                 :             : 
       8                 :             : #include <string>
       9                 :             : #include <type_traits>
      10                 :             : #include <vector>
      11                 :             : 
      12                 :             : #ifndef BITCOIN_NET_PERMISSIONS_H
      13                 :             : #define BITCOIN_NET_PERMISSIONS_H
      14                 :             : 
      15                 :             : struct bilingual_str;
      16                 :             : 
      17                 :             : extern const std::vector<std::string> NET_PERMISSIONS_DOC;
      18                 :             : 
      19                 :             : /** Default for -whitelistrelay. */
      20                 :             : constexpr bool DEFAULT_WHITELISTRELAY = true;
      21                 :             : /** Default for -whitelistforcerelay. */
      22                 :             : constexpr bool DEFAULT_WHITELISTFORCERELAY = false;
      23                 :             : 
      24                 :             : enum class NetPermissionFlags : uint32_t {
      25                 :             :     None = 0,
      26                 :             :     // Can query bloomfilter even if -peerbloomfilters is false
      27                 :             :     BloomFilter = (1U << 1),
      28                 :             :     // Relay and accept transactions from this peer, even if -blocksonly is true
      29                 :             :     // This peer is also not subject to limits on how many transaction INVs are tracked
      30                 :             :     Relay = (1U << 3),
      31                 :             :     // Always relay transactions from this peer, even if already in mempool
      32                 :             :     // Keep parameter interaction: forcerelay implies relay
      33                 :             :     ForceRelay = (1U << 2) | Relay,
      34                 :             :     // Allow getheaders during IBD and block-download after maxuploadtarget limit
      35                 :             :     Download = (1U << 6),
      36                 :             :     // Can't be banned/disconnected/discouraged for misbehavior
      37                 :             :     NoBan = (1U << 4) | Download,
      38                 :             :     // Can query the mempool
      39                 :             :     Mempool = (1U << 5),
      40                 :             :     // Can request addrs without hitting a privacy-preserving cache, and send us
      41                 :             :     // unlimited amounts of addrs.
      42                 :             :     Addr = (1U << 7),
      43                 :             : 
      44                 :             :     // True if the user did not specifically set fine-grained permissions with
      45                 :             :     // the -whitebind or -whitelist configuration options.
      46                 :             :     Implicit = (1U << 31),
      47                 :             :     All = BloomFilter | ForceRelay | Relay | NoBan | Mempool | Download | Addr,
      48                 :             : };
      49                 :         470 : static inline constexpr NetPermissionFlags operator|(NetPermissionFlags a, NetPermissionFlags b)
      50                 :             : {
      51                 :         470 :     using t = typename std::underlying_type<NetPermissionFlags>::type;
      52                 :         470 :     return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b));
      53                 :             : }
      54                 :             : 
      55                 :             : class NetPermissions
      56                 :             : {
      57                 :             : public:
      58                 :             :     NetPermissionFlags m_flags;
      59                 :             :     static std::vector<std::string> ToStrings(NetPermissionFlags flags);
      60                 :     1016904 :     static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
      61                 :             :     {
      62                 :     1016904 :         using t = typename std::underlying_type<NetPermissionFlags>::type;
      63   [ +  +  +  +  :      940110 :         return (static_cast<t>(flags) & static_cast<t>(f)) == static_cast<t>(f);
          +  +  +  +  +  
             +  +  +  +  
           + ][ +  +  -  
          -  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  +  
          +  +  -  +  +  
          -  -  +  +  -  
          +  +  +  +  +  
           +  +  + ][ -  
          -  +  +  +  +  
          +  -  +  -  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
             #  #  #  #  
                      # ]
      64                 :             :     }
      65                 :         479 :     static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f)
      66                 :             :     {
      67         [ +  - ]:         963 :         flags = flags | f;
      68                 :         465 :     }
      69                 :             :     //! ClearFlag is only called with `f` == NetPermissionFlags::Implicit.
      70                 :             :     //! If that should change in the future, be aware that ClearFlag should not
      71                 :             :     //! be called with a subflag of a multiflag, e.g. NetPermissionFlags::Relay
      72                 :             :     //! or NetPermissionFlags::Download, as that would leave `flags` in an
      73                 :             :     //! invalid state corresponding to none of the existing flags.
      74                 :           6 :     static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f)
      75                 :             :     {
      76                 :           6 :         assert(f == NetPermissionFlags::Implicit);
      77                 :           6 :         using t = typename std::underlying_type<NetPermissionFlags>::type;
      78         [ +  + ]:           6 :         flags = static_cast<NetPermissionFlags>(static_cast<t>(flags) & ~static_cast<t>(f));
      79                 :             :     }
      80                 :             : };
      81                 :             : 
      82 [ +  - ][ +  -  :          10 : class NetWhitebindPermissions : public NetPermissions
          +  -  +  -  +  
                -  +  - ]
      83                 :             : {
      84                 :             : public:
      85                 :             :     static bool TryParse(const std::string& str, NetWhitebindPermissions& output, bilingual_str& error);
      86                 :             :     CService m_service;
      87                 :             : };
      88                 :             : 
      89         [ +  - ]:        1414 : class NetWhitelistPermissions : public NetPermissions
      90                 :             : {
      91                 :             : public:
      92                 :             :     static bool TryParse(const std::string& str, NetWhitelistPermissions& output, ConnectionDirection& output_connection_direction, bilingual_str& error);
      93                 :             :     CSubNet m_subnet;
      94                 :             : };
      95                 :             : 
      96                 :             : #endif // BITCOIN_NET_PERMISSIONS_H
        

Generated by: LCOV version 2.0-1