Bitcoin ABC  0.24.10
P2P Digital Currency
net_permissions.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2018 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 <net_permissions.h>
6 
7 #include <netbase.h>
8 #include <util/error.h>
9 #include <util/system.h>
10 #include <util/translation.h>
11 
12 const std::vector<std::string> NET_PERMISSIONS_DOC{
13  "bloomfilter (allow requesting BIP37 filtered blocks and transactions)",
14  "noban (do not ban for misbehavior; implies download)",
15  "forcerelay (relay transactions that are already in the mempool; implies "
16  "relay)",
17  "relay (relay even in -blocksonly mode, and unlimited transaction "
18  "announcements)",
19  "mempool (allow requesting BIP35 mempool contents)",
20  "download (allow getheaders during IBD, no disconnect after "
21  "maxuploadtarget limit)",
22  "bypass_proof_request_limits (experimental, bypass the limits on avalanche "
23  "proof downloads)",
24  "addr (responses to GETADDR avoid hitting the cache and contain random "
25  "records with the most up-to-date info)"};
26 
27 namespace {
28 
29 // The parse the following format "perm1,perm2@xxxxxx"
30 bool TryParsePermissionFlags(const std::string str, NetPermissionFlags &output,
31  size_t &readen, bilingual_str &error) {
33  const auto atSeparator = str.find('@');
34 
35  // if '@' is not found (ie, "xxxxx"), the caller should apply implicit
36  // permissions
37  if (atSeparator == std::string::npos) {
39  readen = 0;
40  }
41  // else (ie, "perm1,perm2@xxxxx"), let's enumerate the permissions by
42  // splitting by ',' and calculate the flags
43  else {
44  readen = 0;
45  // permissions == perm1,perm2
46  const auto permissions = str.substr(0, atSeparator);
47  while (readen < permissions.length()) {
48  const auto commaSeparator = permissions.find(',', readen);
49  const auto len = commaSeparator == std::string::npos
50  ? permissions.length() - readen
51  : commaSeparator - readen;
52  // permission == perm1
53  const auto permission = permissions.substr(readen, len);
54  // We read "perm1"
55  readen += len;
56  if (commaSeparator != std::string::npos) {
57  // We read ","
58  readen++;
59  }
60 
61  if (permission == "bloomfilter" || permission == "bloom") {
63  } else if (permission == "noban") {
65  } else if (permission == "forcerelay") {
67  } else if (permission == "mempool") {
69  } else if (permission == "download") {
71  } else if (permission == "all") {
73  } else if (permission == "relay") {
75  } else if (permission == "addr") {
77  } else if (permission == "bypass_proof_request_limits") {
79  } else if (permission.length() == 0) {
80  // Allow empty entries
81  } else {
82  error =
83  strprintf(_("Invalid P2P permission: '%s'"), permission);
84  return false;
85  }
86  }
87  readen++;
88  }
89 
90  output = flags;
91  error = Untranslated("");
92  return true;
93 }
94 
95 } // namespace
96 
98  std::vector<std::string> strings;
100  strings.push_back("bloomfilter");
101  }
103  strings.push_back("noban");
104  }
106  strings.push_back("forcerelay");
107  }
109  strings.push_back("relay");
110  }
112  strings.push_back("mempool");
113  }
115  strings.push_back("download");
116  }
118  strings.push_back("addr");
119  }
121  strings.push_back("bypass_proof_request_limits");
122  }
123  return strings;
124 }
125 
126 bool NetWhitebindPermissions::TryParse(const std::string str,
127  NetWhitebindPermissions &output,
128  bilingual_str &error) {
130  size_t offset;
131  if (!TryParsePermissionFlags(str, flags, offset, error)) {
132  return false;
133  }
134 
135  const std::string strBind = str.substr(offset);
136  CService addrBind;
137  if (!Lookup(strBind, addrBind, 0, false)) {
138  error = ResolveErrMsg("whitebind", strBind);
139  return false;
140  }
141  if (addrBind.GetPort() == 0) {
142  error = strprintf(_("Need to specify a port with -whitebind: '%s'"),
143  strBind);
144  return false;
145  }
146 
147  output.m_flags = flags;
148  output.m_service = addrBind;
149  error = Untranslated("");
150  return true;
151 }
152 
153 bool NetWhitelistPermissions::TryParse(const std::string str,
154  NetWhitelistPermissions &output,
155  bilingual_str &error) {
157  size_t offset;
158  if (!TryParsePermissionFlags(str, flags, offset, error)) {
159  return false;
160  }
161 
162  const std::string net = str.substr(offset);
163  CSubNet subnet;
164  LookupSubNet(net, subnet);
165  if (!subnet.IsValid()) {
166  error =
167  strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net);
168  return false;
169  }
170 
171  output.m_flags = flags;
172  output.m_subnet = subnet;
173  error = Untranslated("");
174  return true;
175 }
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:514
_
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
PF_DOWNLOAD
@ PF_DOWNLOAD
Definition: net_permissions.h:30
NetPermissions::ToStrings
static std::vector< std::string > ToStrings(NetPermissionFlags flags)
Definition: net_permissions.cpp:97
PF_FORCERELAY
@ PF_FORCERELAY
Definition: net_permissions.h:27
flags
int flags
Definition: bitcoin-tx.cpp:532
PF_ALL
@ PF_ALL
Definition: net_permissions.h:43
PF_NONE
@ PF_NONE
Definition: net_permissions.h:18
NET_PERMISSIONS_DOC
const std::vector< std::string > NET_PERMISSIONS_DOC
Definition: net_permissions.cpp:12
bilingual_str
Bilingual messages:
Definition: translation.h:17
PF_RELAY
@ PF_RELAY
Definition: net_permissions.h:24
ResolveErrMsg
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: error.cpp:42
PF_BYPASS_PROOF_REQUEST_LIMITS
@ PF_BYPASS_PROOF_REQUEST_LIMITS
Definition: net_permissions.h:39
NetWhitelistPermissions::m_subnet
CSubNet m_subnet
Definition: net_permissions.h:76
NetPermissions::HasFlag
static bool HasFlag(const NetPermissionFlags &flags, NetPermissionFlags f)
Definition: net_permissions.h:51
NetWhitelistPermissions
Definition: net_permissions.h:72
Untranslated
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
Lookup
bool Lookup(const std::string &name, std::vector< CService > &vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:232
NetPermissionFlags
NetPermissionFlags
Definition: net_permissions.h:17
NetWhitebindPermissions::m_service
CService m_service
Definition: net_permissions.h:69
NetWhitebindPermissions::TryParse
static bool TryParse(const std::string str, NetWhitebindPermissions &output, bilingual_str &error)
Definition: net_permissions.cpp:126
error.h
CSubNet
Definition: netaddress.h:465
CService::GetPort
uint16_t GetPort() const
Definition: netaddress.cpp:937
system.h
strprintf
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
PF_BLOOMFILTER
@ PF_BLOOMFILTER
Definition: net_permissions.h:20
NetPermissions::AddFlag
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
Definition: net_permissions.h:55
translation.h
NetPermissions::m_flags
NetPermissionFlags m_flags
Definition: net_permissions.h:49
PF_MEMPOOL
@ PF_MEMPOOL
Definition: net_permissions.h:34
PF_ADDR
@ PF_ADDR
Definition: net_permissions.h:36
CSubNet::IsValid
bool IsValid() const
Definition: netaddress.cpp:1158
netbase.h
error
bool error(const char *fmt, const Args &... args)
Definition: system.h:48
PF_NOBAN
@ PF_NOBAN
Definition: net_permissions.h:32
PF_ISIMPLICIT
@ PF_ISIMPLICIT
Definition: net_permissions.h:42
NetWhitebindPermissions
Definition: net_permissions.h:65
NetWhitelistPermissions::TryParse
static bool TryParse(const std::string str, NetWhitelistPermissions &output, bilingual_str &error)
Definition: net_permissions.cpp:153
LookupSubNet
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:903
net_permissions.h