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