LCOV - code coverage report
Current view: top level - src - net_permissions.cpp (source / functions) Hit Total Coverage
Test: coverage.lcov Lines: 82 82 100.0 %
Date: 2020-10-08 11:22:10 Functions: 4 4 100.0 %
Legend: Modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed

Not modified by patch:
Lines: hit not hit | Branches: + taken - not taken # not executed
Branches: 0 0 -

           Branch data     Line data    Source code
#       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                 :            : const std::vector<std::string> NET_PERMISSIONS_DOC{
#      12                 :            :     "bloomfilter (allow requesting BIP37 filtered blocks and transactions)",
#      13                 :            :     "noban (do not ban for misbehavior; implies download)",
#      14                 :            :     "forcerelay (relay transactions that are already in the mempool; implies relay)",
#      15                 :            :     "relay (relay even in -blocksonly mode, and unlimited transaction announcements)",
#      16                 :            :     "mempool (allow requesting BIP35 mempool contents)",
#      17                 :            :     "download (allow getheaders during IBD, no disconnect after maxuploadtarget limit)",
#      18                 :            :     "addr (responses to GETADDR avoid hitting the cache and contain random records with the most up-to-date info)"
#      19                 :            : };
#      20                 :            : 
#      21                 :            : namespace {
#      22                 :            : 
#      23                 :            : // The parse the following format "perm1,perm2@xxxxxx"
#      24                 :            : bool TryParsePermissionFlags(const std::string str, NetPermissionFlags& output, size_t& readen, bilingual_str& error)
#      25                 :        101 : {
#      26                 :        101 :     NetPermissionFlags flags = PF_NONE;
#      27                 :        101 :     const auto atSeparator = str.find('@');
#      28                 :            : 
#      29                 :            :     // if '@' is not found (ie, "xxxxx"), the caller should apply implicit permissions
#      30                 :        101 :     if (atSeparator == std::string::npos) {
#      31                 :         14 :         NetPermissions::AddFlag(flags, PF_ISIMPLICIT);
#      32                 :         14 :         readen = 0;
#      33                 :         14 :     }
#      34                 :            :     // else (ie, "perm1,perm2@xxxxx"), let's enumerate the permissions by splitting by ',' and calculate the flags
#      35                 :         87 :     else {
#      36                 :         87 :         readen = 0;
#      37                 :            :         // permissions == perm1,perm2
#      38                 :         87 :         const auto permissions = str.substr(0, atSeparator);
#      39                 :        217 :         while (readen < permissions.length()) {
#      40                 :        133 :             const auto commaSeparator = permissions.find(',', readen);
#      41                 :         80 :             const auto len = commaSeparator == std::string::npos ? permissions.length() - readen : commaSeparator - readen;
#      42                 :            :             // permission == perm1
#      43                 :        133 :             const auto permission = permissions.substr(readen, len);
#      44                 :        133 :             readen += len; // We read "perm1"
#      45                 :        133 :             if (commaSeparator != std::string::npos) readen++; // We read ","
#      46                 :            : 
#      47                 :        133 :             if (permission == "bloomfilter" || permission == "bloom") NetPermissions::AddFlag(flags, PF_BLOOMFILTER);
#      48                 :        112 :             else if (permission == "noban") NetPermissions::AddFlag(flags, PF_NOBAN);
#      49                 :         45 :             else if (permission == "forcerelay") NetPermissions::AddFlag(flags, PF_FORCERELAY);
#      50                 :         30 :             else if (permission == "mempool") NetPermissions::AddFlag(flags, PF_MEMPOOL);
#      51                 :         26 :             else if (permission == "download") NetPermissions::AddFlag(flags, PF_DOWNLOAD);
#      52                 :         25 :             else if (permission == "all") NetPermissions::AddFlag(flags, PF_ALL);
#      53                 :         22 :             else if (permission == "relay") NetPermissions::AddFlag(flags, PF_RELAY);
#      54                 :         11 :             else if (permission == "addr") NetPermissions::AddFlag(flags, PF_ADDR);
#      55                 :         11 :             else if (permission.length() == 0); // Allow empty entries
#      56                 :          3 :             else {
#      57                 :          3 :                 error = strprintf(_("Invalid P2P permission: '%s'"), permission);
#      58                 :          3 :                 return false;
#      59                 :          3 :             }
#      60                 :        133 :         }
#      61                 :         84 :         readen++;
#      62                 :         84 :     }
#      63                 :            : 
#      64                 :         98 :     output = flags;
#      65                 :         98 :     error = Untranslated("");
#      66                 :         98 :     return true;
#      67                 :        101 : }
#      68                 :            : 
#      69                 :            : }
#      70                 :            : 
#      71                 :            : std::vector<std::string> NetPermissions::ToStrings(NetPermissionFlags flags)
#      72                 :       6972 : {
#      73                 :       6972 :     std::vector<std::string> strings;
#      74                 :       6972 :     if (NetPermissions::HasFlag(flags, PF_BLOOMFILTER)) strings.push_back("bloomfilter");
#      75                 :       6972 :     if (NetPermissions::HasFlag(flags, PF_NOBAN)) strings.push_back("noban");
#      76                 :       6972 :     if (NetPermissions::HasFlag(flags, PF_FORCERELAY)) strings.push_back("forcerelay");
#      77                 :       6972 :     if (NetPermissions::HasFlag(flags, PF_RELAY)) strings.push_back("relay");
#      78                 :       6972 :     if (NetPermissions::HasFlag(flags, PF_MEMPOOL)) strings.push_back("mempool");
#      79                 :       6972 :     if (NetPermissions::HasFlag(flags, PF_DOWNLOAD)) strings.push_back("download");
#      80                 :       6972 :     if (NetPermissions::HasFlag(flags, PF_ADDR)) strings.push_back("addr");
#      81                 :       6972 :     return strings;
#      82                 :       6972 : }
#      83                 :            : 
#      84                 :            : bool NetWhitebindPermissions::TryParse(const std::string str, NetWhitebindPermissions& output, bilingual_str& error)
#      85                 :         32 : {
#      86                 :         32 :     NetPermissionFlags flags;
#      87                 :         32 :     size_t offset;
#      88                 :         32 :     if (!TryParsePermissionFlags(str, flags, offset, error)) return false;
#      89                 :            : 
#      90                 :         30 :     const std::string strBind = str.substr(offset);
#      91                 :         30 :     CService addrBind;
#      92                 :         30 :     if (!Lookup(strBind, addrBind, 0, false)) {
#      93                 :          5 :         error = ResolveErrMsg("whitebind", strBind);
#      94                 :          5 :         return false;
#      95                 :          5 :     }
#      96                 :         25 :     if (addrBind.GetPort() == 0) {
#      97                 :          2 :         error = strprintf(_("Need to specify a port with -whitebind: '%s'"), strBind);
#      98                 :          2 :         return false;
#      99                 :          2 :     }
#     100                 :            : 
#     101                 :         23 :     output.m_flags = flags;
#     102                 :         23 :     output.m_service = addrBind;
#     103                 :         23 :     error = Untranslated("");
#     104                 :         23 :     return true;
#     105                 :         23 : }
#     106                 :            : 
#     107                 :            : bool NetWhitelistPermissions::TryParse(const std::string str, NetWhitelistPermissions& output, bilingual_str& error)
#     108                 :         69 : {
#     109                 :         69 :     NetPermissionFlags flags;
#     110                 :         69 :     size_t offset;
#     111                 :         69 :     if (!TryParsePermissionFlags(str, flags, offset, error)) return false;
#     112                 :            : 
#     113                 :         68 :     const std::string net = str.substr(offset);
#     114                 :         68 :     CSubNet subnet;
#     115                 :         68 :     LookupSubNet(net, subnet);
#     116                 :         68 :     if (!subnet.IsValid()) {
#     117                 :          3 :         error = strprintf(_("Invalid netmask specified in -whitelist: '%s'"), net);
#     118                 :          3 :         return false;
#     119                 :          3 :     }
#     120                 :            : 
#     121                 :         65 :     output.m_flags = flags;
#     122                 :         65 :     output.m_subnet = subnet;
#     123                 :         65 :     error = Untranslated("");
#     124                 :         65 :     return true;
#     125                 :         65 : }

Generated by: LCOV version 1.14