2021-12-30 14:36:57 -03:00
|
|
|
// Copyright (c) 2009-2021 The Bitcoin Core developers
|
2019-06-20 05:37:51 -04:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
2020-06-06 13:28:47 -04:00
|
|
|
#include <netaddress.h>
|
|
|
|
|
2019-06-20 05:37:51 -04:00
|
|
|
#include <string>
|
2021-03-22 16:05:15 -03:00
|
|
|
#include <type_traits>
|
2019-06-20 05:37:51 -04:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#ifndef BITCOIN_NET_PERMISSIONS_H
|
|
|
|
#define BITCOIN_NET_PERMISSIONS_H
|
2020-06-06 13:28:47 -04:00
|
|
|
|
2020-06-05 08:26:16 -04:00
|
|
|
struct bilingual_str;
|
|
|
|
|
2020-06-06 13:28:47 -04:00
|
|
|
extern const std::vector<std::string> NET_PERMISSIONS_DOC;
|
|
|
|
|
2021-03-22 16:05:15 -03:00
|
|
|
enum class NetPermissionFlags : uint32_t {
|
2021-03-21 18:46:50 -03:00
|
|
|
None = 0,
|
2019-06-20 05:37:51 -04:00
|
|
|
// Can query bloomfilter even if -peerbloomfilters is false
|
2021-03-21 18:46:50 -03:00
|
|
|
BloomFilter = (1U << 1),
|
2019-06-20 05:37:51 -04:00
|
|
|
// Relay and accept transactions from this peer, even if -blocksonly is true
|
2020-09-23 21:00:46 -03:00
|
|
|
// This peer is also not subject to limits on how many transaction INVs are tracked
|
2021-03-21 18:46:50 -03:00
|
|
|
Relay = (1U << 3),
|
2020-01-22 18:02:24 -03:00
|
|
|
// Always relay transactions from this peer, even if already in mempool
|
2019-06-20 05:37:51 -04:00
|
|
|
// Keep parameter interaction: forcerelay implies relay
|
2021-03-21 18:46:50 -03:00
|
|
|
ForceRelay = (1U << 2) | Relay,
|
2020-06-06 11:07:25 -04:00
|
|
|
// Allow getheaders during IBD and block-download after maxuploadtarget limit
|
2021-03-21 18:46:50 -03:00
|
|
|
Download = (1U << 6),
|
2020-06-10 20:11:38 -04:00
|
|
|
// Can't be banned/disconnected/discouraged for misbehavior
|
2021-03-21 18:46:50 -03:00
|
|
|
NoBan = (1U << 4) | Download,
|
2019-06-20 05:37:51 -04:00
|
|
|
// Can query the mempool
|
2021-03-21 18:46:50 -03:00
|
|
|
Mempool = (1U << 5),
|
2021-06-30 14:52:40 -04:00
|
|
|
// Can request addrs without hitting a privacy-preserving cache, and send us
|
|
|
|
// unlimited amounts of addrs.
|
2021-03-21 18:46:50 -03:00
|
|
|
Addr = (1U << 7),
|
2019-06-20 05:37:51 -04:00
|
|
|
|
|
|
|
// True if the user did not specifically set fine grained permissions
|
2021-03-21 18:46:50 -03:00
|
|
|
Implicit = (1U << 31),
|
|
|
|
All = BloomFilter | ForceRelay | Relay | NoBan | Mempool | Download | Addr,
|
2019-06-20 05:37:51 -04:00
|
|
|
};
|
2021-03-22 16:05:15 -03:00
|
|
|
static inline constexpr NetPermissionFlags operator|(NetPermissionFlags a, NetPermissionFlags b)
|
|
|
|
{
|
|
|
|
using t = typename std::underlying_type<NetPermissionFlags>::type;
|
|
|
|
return static_cast<NetPermissionFlags>(static_cast<t>(a) | static_cast<t>(b));
|
|
|
|
}
|
2020-06-06 13:28:47 -04:00
|
|
|
|
2019-06-20 05:37:51 -04:00
|
|
|
class NetPermissions
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NetPermissionFlags m_flags;
|
|
|
|
static std::vector<std::string> ToStrings(NetPermissionFlags flags);
|
2021-05-12 05:04:34 -04:00
|
|
|
static inline bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
|
2019-06-20 05:37:51 -04:00
|
|
|
{
|
2021-03-22 16:05:15 -03:00
|
|
|
using t = typename std::underlying_type<NetPermissionFlags>::type;
|
|
|
|
return (static_cast<t>(flags) & static_cast<t>(f)) == static_cast<t>(f);
|
2019-06-20 05:37:51 -04:00
|
|
|
}
|
|
|
|
static inline void AddFlag(NetPermissionFlags& flags, NetPermissionFlags f)
|
|
|
|
{
|
2021-03-22 16:05:15 -03:00
|
|
|
flags = flags | f;
|
2019-06-20 05:37:51 -04:00
|
|
|
}
|
2021-03-21 18:46:50 -03:00
|
|
|
//! ClearFlag is only called with `f` == NetPermissionFlags::Implicit.
|
2021-04-14 11:10:28 -04:00
|
|
|
//! If that should change in the future, be aware that ClearFlag should not
|
2021-03-21 18:46:50 -03:00
|
|
|
//! be called with a subflag of a multiflag, e.g. NetPermissionFlags::Relay
|
|
|
|
//! or NetPermissionFlags::Download, as that would leave `flags` in an
|
2021-04-14 11:10:28 -04:00
|
|
|
//! invalid state corresponding to none of the existing flags.
|
2019-06-20 05:37:51 -04:00
|
|
|
static inline void ClearFlag(NetPermissionFlags& flags, NetPermissionFlags f)
|
|
|
|
{
|
2021-03-21 18:46:50 -03:00
|
|
|
assert(f == NetPermissionFlags::Implicit);
|
2021-03-22 16:05:15 -03:00
|
|
|
using t = typename std::underlying_type<NetPermissionFlags>::type;
|
|
|
|
flags = static_cast<NetPermissionFlags>(static_cast<t>(flags) & ~static_cast<t>(f));
|
2019-06-20 05:37:51 -04:00
|
|
|
}
|
|
|
|
};
|
2020-06-06 13:28:47 -04:00
|
|
|
|
2019-06-20 05:37:51 -04:00
|
|
|
class NetWhitebindPermissions : public NetPermissions
|
|
|
|
{
|
|
|
|
public:
|
2021-05-19 13:26:56 -04:00
|
|
|
static bool TryParse(const std::string& str, NetWhitebindPermissions& output, bilingual_str& error);
|
2019-06-20 05:37:51 -04:00
|
|
|
CService m_service;
|
|
|
|
};
|
|
|
|
|
|
|
|
class NetWhitelistPermissions : public NetPermissions
|
|
|
|
{
|
|
|
|
public:
|
2021-05-19 13:26:56 -04:00
|
|
|
static bool TryParse(const std::string& str, NetWhitelistPermissions& output, bilingual_str& error);
|
2019-06-20 05:37:51 -04:00
|
|
|
CSubNet m_subnet;
|
|
|
|
};
|
|
|
|
|
2020-01-22 18:02:24 -03:00
|
|
|
#endif // BITCOIN_NET_PERMISSIONS_H
|