Bitcoin Core  0.19.99
P2P Digital Currency
net_permissions.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2019 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 #include <netbase.h>
7 #include <util/error.h>
8 #include <util/system.h>
9 #include <util/translation.h>
10 
11 // The parse the following format "perm1,perm2@xxxxxx"
12 bool TryParsePermissionFlags(const std::string str, NetPermissionFlags& output, size_t& readen, std::string& error)
13 {
15  const auto atSeparator = str.find('@');
16 
17  // if '@' is not found (ie, "xxxxx"), the caller should apply implicit permissions
18  if (atSeparator == std::string::npos) {
20  readen = 0;
21  }
22  // else (ie, "perm1,perm2@xxxxx"), let's enumerate the permissions by splitting by ',' and calculate the flags
23  else {
24  readen = 0;
25  // permissions == perm1,perm2
26  const auto permissions = str.substr(0, atSeparator);
27  while (readen < permissions.length()) {
28  const auto commaSeparator = permissions.find(',', readen);
29  const auto len = commaSeparator == std::string::npos ? permissions.length() - readen : commaSeparator - readen;
30  // permission == perm1
31  const auto permission = permissions.substr(readen, len);
32  readen += len; // We read "perm1"
33  if (commaSeparator != std::string::npos) readen++; // We read ","
34 
35  if (permission == "bloomfilter" || permission == "bloom") NetPermissions::AddFlag(flags, PF_BLOOMFILTER);
36  else if (permission == "noban") NetPermissions::AddFlag(flags, PF_NOBAN);
37  else if (permission == "forcerelay") NetPermissions::AddFlag(flags, PF_FORCERELAY);
38  else if (permission == "mempool") NetPermissions::AddFlag(flags, PF_MEMPOOL);
39  else if (permission == "all") NetPermissions::AddFlag(flags, PF_ALL);
40  else if (permission == "relay") NetPermissions::AddFlag(flags, PF_RELAY);
41  else if (permission.length() == 0); // Allow empty entries
42  else {
43  error = strprintf(_("Invalid P2P permission: '%s'").translated, permission);
44  return false;
45  }
46  }
47  readen++;
48  }
49 
50  output = flags;
51  error = "";
52  return true;
53 }
54 
56 {
57  std::vector<std::string> strings;
58  if (NetPermissions::HasFlag(flags, PF_BLOOMFILTER)) strings.push_back("bloomfilter");
59  if (NetPermissions::HasFlag(flags, PF_NOBAN)) strings.push_back("noban");
60  if (NetPermissions::HasFlag(flags, PF_FORCERELAY)) strings.push_back("forcerelay");
61  if (NetPermissions::HasFlag(flags, PF_RELAY)) strings.push_back("relay");
62  if (NetPermissions::HasFlag(flags, PF_MEMPOOL)) strings.push_back("mempool");
63  return strings;
64 }
65 
66 bool NetWhitebindPermissions::TryParse(const std::string str, NetWhitebindPermissions& output, std::string& error)
67 {
69  size_t offset;
70  if (!TryParsePermissionFlags(str, flags, offset, error)) return false;
71 
72  const std::string strBind = str.substr(offset);
73  CService addrBind;
74  if (!Lookup(strBind, addrBind, 0, false)) {
75  error = ResolveErrMsg("whitebind", strBind);
76  return false;
77  }
78  if (addrBind.GetPort() == 0) {
79  error = strprintf(_("Need to specify a port with -whitebind: '%s'").translated, strBind);
80  return false;
81  }
82 
83  output.m_flags = flags;
84  output.m_service = addrBind;
85  error = "";
86  return true;
87 }
88 
89 bool NetWhitelistPermissions::TryParse(const std::string str, NetWhitelistPermissions& output, std::string& error)
90 {
92  size_t offset;
93  if (!TryParsePermissionFlags(str, flags, offset, error)) return false;
94 
95  const std::string net = str.substr(offset);
96  CSubNet subnet;
97  LookupSubNet(net, subnet);
98  if (!subnet.IsValid()) {
99  error = strprintf(_("Invalid netmask specified in -whitelist: '%s'").translated, net);
100  return false;
101  }
102 
103  output.m_flags = flags;
104  output.m_subnet = subnet;
105  error = "";
106  return true;
107 }
unsigned short GetPort() const
Definition: netaddress.cpp:666
std::string ResolveErrMsg(const std::string &optname, const std::string &strBind)
Definition: error.cpp:39
static void AddFlag(NetPermissionFlags &flags, NetPermissionFlags f)
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
static bool TryParse(const std::string str, NetWhitebindPermissions &output, std::string &error)
bool LookupSubNet(const std::string &strSubnet, CSubNet &ret)
Parse and resolve a specified subnet string into the appropriate internal representation.
Definition: netbase.cpp:823
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:205
bilingual_str _(const char *psz)
Translation function.
Definition: translation.h:36
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:150
static bool HasFlag(const NetPermissionFlags &flags, NetPermissionFlags f)
NetPermissionFlags
NetPermissionFlags m_flags
int flags
Definition: bitcoin-tx.cpp:509
bool IsValid() const
Definition: netaddress.cpp:883
bool TryParsePermissionFlags(const std::string str, NetPermissionFlags &output, size_t &readen, std::string &error)
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
static bool TryParse(const std::string str, NetWhitelistPermissions &output, std::string &error)
static std::vector< std::string > ToStrings(NetPermissionFlags flags)