net_permissions.h raw

   1  // Copyright (c) 2009-2022 The Limenka 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 LIMENKA_NET_PERMISSIONS_H
  13  #define LIMENKA_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      // Try harder to evict an existing connection if needed to open
  44      // an inbound slot from this peer
  45      ForceInbound = (1U << 10) | NoBan,
  46  
  47      // Can query compact filters even if -peerblockfilters is false
  48      BlockFilters = (1U << 8),
  49      // Used to avoid an error when All is used to set BlockFilters
  50      BlockFilters_Explicit = BlockFilters | (1U << 9),
  51  
  52      // True if the user did not specifically set fine-grained permissions with
  53      // the -whitebind or -whitelist configuration options.
  54      Implicit = (1U << 31),
  55      All = BloomFilter | ForceRelay | Relay | NoBan | Mempool | Download | Addr | BlockFilters | ForceInbound,
  56  };
  57  static inline constexpr NetPermissionFlags operator|(NetPermissionFlags a, NetPermissionFlags b)
  58  {
  59      using t = typename std::underlying_type<NetPermissionFlags>::type;
  60      return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b));
  61  }
  62  
  63  class NetPermissions
  64  {
  65  public:
  66      NetPermissionFlags m_flags;
  67      static std::vector<std::string> ToStrings(NetPermissionFlags flags);
  68      static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
  69      {
  70          using t = typename std::underlying_type<NetPermissionFlags>::type;
  71          return (static_cast<t>(flags) & static_cast<t>(f)) == static_cast<t>(f);
  72      }
  73      static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f)
  74      {
  75          flags = flags | f;
  76      }
  77      //! ClearFlag is only called with `f` == NetPermissionFlags::Implicit.
  78      //! If that should change in the future, be aware that ClearFlag should not
  79      //! be called with a subflag of a multiflag, e.g. NetPermissionFlags::Relay
  80      //! or NetPermissionFlags::Download, as that would leave `flags` in an
  81      //! invalid state corresponding to none of the existing flags.
  82      static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f)
  83      {
  84          assert(f == NetPermissionFlags::Implicit);
  85          using t = typename std::underlying_type<NetPermissionFlags>::type;
  86          flags = static_cast<NetPermissionFlags>(static_cast<t>(flags) & ~static_cast<t>(f));
  87      }
  88  };
  89  
  90  class NetWhitebindPermissions : public NetPermissions
  91  {
  92  public:
  93      static bool TryParse(const std::string& str, NetWhitebindPermissions& output, bilingual_str& error);
  94      CService m_service;
  95  };
  96  
  97  class NetWhitelistPermissions : public NetPermissions
  98  {
  99  public:
 100      static bool TryParse(const std::string& str, NetWhitelistPermissions& output, ConnectionDirection& output_connection_direction, bilingual_str& error);
 101      CSubNet m_subnet;
 102  };
 103  
 104  #endif // LIMENKA_NET_PERMISSIONS_H
 105