/bitcoin/src/net_permissions.h
Line | Count | Source |
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 | | static inline constexpr NetPermissionFlags operator|(NetPermissionFlags a, NetPermissionFlags b) |
50 | 44.3k | { |
51 | 44.3k | using t = std::underlying_type_t<NetPermissionFlags>; |
52 | 44.3k | return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b)); |
53 | 44.3k | } Unexecuted instantiation: bitcoind.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: init.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: mapport.cpp:operator|(NetPermissionFlags, NetPermissionFlags) net.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Line | Count | Source | 50 | 44.3k | { | 51 | 44.3k | using t = std::underlying_type_t<NetPermissionFlags>; | 52 | 44.3k | return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b)); | 53 | 44.3k | } |
Unexecuted instantiation: net_processing.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: context.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: eviction.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: interfaces.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: peerman_args.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: transaction.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: txdownloadman_impl.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: txreconciliation.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: blockchain.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: mempool.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: mining.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: node.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: server_util.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: torcontrol.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: txorphanage.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: txrequest.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: headerssync.cpp:operator|(NetPermissionFlags, NetPermissionFlags) Unexecuted instantiation: net_permissions.cpp:operator|(NetPermissionFlags, NetPermissionFlags) |
54 | | |
55 | | class NetPermissions |
56 | | { |
57 | | public: |
58 | | NetPermissionFlags m_flags; |
59 | | static std::vector<std::string> ToStrings(NetPermissionFlags flags); |
60 | | static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f) |
61 | 84.1M | { |
62 | 84.1M | using t = std::underlying_type_t<NetPermissionFlags>; |
63 | 84.1M | return (static_cast<t>(flags) & static_cast<t>(f)) == static_cast<t>(f); |
64 | 84.1M | } |
65 | | static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f) |
66 | 44.3k | { |
67 | 44.3k | flags = flags | f; |
68 | 44.3k | } |
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 | | static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f) |
75 | 0 | { |
76 | 0 | assert(f == NetPermissionFlags::Implicit); Branch (76:9): [True: 0, False: 0]
|
77 | 0 | using t = std::underlying_type_t<NetPermissionFlags>; |
78 | 0 | flags = static_cast<NetPermissionFlags>(static_cast<t>(flags) & ~static_cast<t>(f)); |
79 | 0 | } |
80 | | }; |
81 | | |
82 | | 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 | | 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 |