Bitcoin ABC  0.28.12
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 // 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") {
80  } else if (permission.length() == 0) {
81  // Allow empty entries
82  } else {
83  error =
84  strprintf(_("Invalid P2P permission: '%s'"), permission);
85  return false;
86  }
87  }
88  readen++;
89  }
90 
91  output = flags;
92  error = Untranslated("");
93  return true;
94 }
95 
96 } // namespace
97 
99  std::vector<std::string> strings;
101  strings.push_back("bloomfilter");
102  }
104  strings.push_back("noban");
105  }
107  strings.push_back("forcerelay");
108  }
110  strings.push_back("relay");
111  }
113  strings.push_back("mempool");
114  }
116  strings.push_back("download");
117  }
119  strings.push_back("addr");
120  }
123  strings.push_back("bypass_proof_request_limits");
124  }
125  return strings;
126 }
127 
128 bool NetWhitebindPermissions::TryParse(const std::string &str,
129  NetWhitebindPermissions &output,
130  bilingual_str &error) {
132  size_t offset;
133  if (!TryParsePermissionFlags(str, flags, offset, error)) {
134  return false;
135  }
136 
137  const std::string strBind = str.substr(offset);
138  CService addrBind;
139  if (!Lookup(strBind, addrBind, 0, false)) {
140  error = ResolveErrMsg("whitebind", strBind);
141  return false;
142  }
143  if (addrBind.GetPort() == 0) {
144  error = strprintf(_("Need to specify a port with -whitebind: '%s'"),
145  strBind);
146  return false;
147  }
148 
149  output.m_flags = flags;
150  output.m_service = addrBind;
151  error = Untranslated("");
152  return true;
153 }
154 
155 bool NetWhitelistPermissions::TryParse(const std::string &str,
156  NetWhitelistPermissions &output,
157  bilingual_str &error) {
159  size_t offset;
160  if (!TryParsePermissionFlags(str, flags, offset, error)) {
161  return false;
162  }
163 
164  const std::string net = str.substr(offset);
165  CSubNet subnet;
166  LookupSubNet(net, subnet);
167  if (!subnet.IsValid()) {
168  error =
169  strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net);
170  return false;
171  }
172 
173  output.m_flags = flags;
174  output.m_subnet = subnet;
175  error = Untranslated("");
176  return true;
177 }
int flags
Definition: bitcoin-tx.cpp:533
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:545
uint16_t GetPort() const
bool IsValid() const
NetPermissionFlags m_flags
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
static std::vector< std::string > ToStrings(NetPermissionFlags flags)
static bool HasFlag(NetPermissionFlags flags, NetPermissionFlags f)
static bool TryParse(const std::string &str, NetWhitebindPermissions &output, bilingual_str &error)
static bool TryParse(const std::string &str, NetWhitelistPermissions &output, bilingual_str &error)
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: error.cpp:42
const std::vector< std::string > NET_PERMISSIONS_DOC
NetPermissionFlags
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret, DNSLookupFn dns_lookup_function)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:786
bool Lookup(const std::string &name, std::vector< CService > &vAddr, uint16_t portDefault, bool fAllowLookup, unsigned int nMaxSolutions, DNSLookupFn dns_lookup_function)
Resolve a service string to its corresponding service.
Definition: netbase.cpp:222
Bilingual messages:
Definition: translation.h:17
bool error(const char *fmt, const Args &...args)
Definition: system.h:45
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1202
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:68
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36