Bitcoin ABC  0.22.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 namespace {
13 
14 // The parse the following format "perm1,[email protected]"
15 bool TryParsePermissionFlags(const std::string str, NetPermissionFlags &output,
16  size_t &readen, bilingual_str &error) {
18  const auto atSeparator = str.find('@');
19 
20  // if '@' is not found (ie, "xxxxx"), the caller should apply implicit
21  // permissions
22  if (atSeparator == std::string::npos) {
24  readen = 0;
25  }
26  // else (ie, "perm1,[email protected]"), let's enumerate the permissions by
27  // splitting by ',' and calculate the flags
28  else {
29  readen = 0;
30  // permissions == perm1,perm2
31  const auto permissions = str.substr(0, atSeparator);
32  while (readen < permissions.length()) {
33  const auto commaSeparator = permissions.find(',', readen);
34  const auto len = commaSeparator == std::string::npos
35  ? permissions.length() - readen
36  : commaSeparator - readen;
37  // permission == perm1
38  const auto permission = permissions.substr(readen, len);
39  // We read "perm1"
40  readen += len;
41  if (commaSeparator != std::string::npos) {
42  // We read ","
43  readen++;
44  }
45 
46  if (permission == "bloomfilter" || permission == "bloom") {
48  } else if (permission == "noban") {
50  } else if (permission == "forcerelay") {
52  } else if (permission == "mempool") {
54  } else if (permission == "all") {
56  } else if (permission == "relay") {
58  } else if (permission.length() == 0) {
59  // Allow empty entries
60  } else {
61  error =
62  strprintf(_("Invalid P2P permission: '%s'"), permission);
63  return false;
64  }
65  }
66  readen++;
67  }
68 
69  output = flags;
70  error = Untranslated("");
71  return true;
72 }
73 
74 } // namespace
75 
76 std::vector<std::string> NetPermissions::ToStrings(NetPermissionFlags flags) {
77  std::vector<std::string> strings;
79  strings.push_back("bloomfilter");
80  }
81  if (NetPermissions::HasFlag(flags, PF_NOBAN)) {
82  strings.push_back("noban");
83  }
85  strings.push_back("forcerelay");
86  }
87  if (NetPermissions::HasFlag(flags, PF_RELAY)) {
88  strings.push_back("relay");
89  }
90  if (NetPermissions::HasFlag(flags, PF_MEMPOOL)) {
91  strings.push_back("mempool");
92  }
93  return strings;
94 }
95 
96 bool NetWhitebindPermissions::TryParse(const std::string str,
98  bilingual_str &error) {
100  size_t offset;
101  if (!TryParsePermissionFlags(str, flags, offset, error)) {
102  return false;
103  }
104 
105  const std::string strBind = str.substr(offset);
106  CService addrBind;
107  if (!Lookup(strBind, addrBind, 0, false)) {
108  error = ResolveErrMsg("whitebind", strBind);
109  return false;
110  }
111  if (addrBind.GetPort() == 0) {
112  error = strprintf(_("Need to specify a port with -whitebind: '%s'"),
113  strBind);
114  return false;
115  }
116 
117  output.m_flags = flags;
118  output.m_service = addrBind;
119  error = Untranslated("");
120  return true;
121 }
122 
123 bool NetWhitelistPermissions::TryParse(const std::string str,
124  NetWhitelistPermissions &output,
125  bilingual_str &error) {
127  size_t offset;
128  if (!TryParsePermissionFlags(str, flags, offset, error)) {
129  return false;
130  }
131 
132  const std::string net = str.substr(offset);
133  CSubNet subnet;
134  LookupSubNet(net, subnet);
135  if (!subnet.IsValid()) {
136  error =
137  strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net);
138  return false;
139  }
140 
141  output.m_flags = flags;
142  output.m_subnet = subnet;
143  error = Untranslated("");
144  return true;
145 }
unsigned short GetPort() const
Definition: netaddress.cpp:692
bilingual_str ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: error.cpp:41
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
Bilingual messages:
Definition: translation.h:17
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1201
bilingual_str Untranslated(std::string original)
Mark a bilingual_str as untranslated.
Definition: translation.h:36
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:890
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:219
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:55
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:179
static bool HasFlag(const NetPermissionFlags &flags, NetPermissionFlags f)
NetPermissionFlags
NetPermissionFlags m_flags
int flags
Definition: bitcoin-tx.cpp:529
bool IsValid() const
Definition: netaddress.cpp:930
static bool TryParse(const std::string str, NetWhitebindPermissions &output, bilingual_str &error)
bool error(const char *fmt, const Args &... args)
Definition: system.h:47
static bool TryParse(const std::string str, NetWhitelistPermissions &output, bilingual_str &error)
static std::vector< std::string > ToStrings(NetPermissionFlags flags)