[PATCH, try2] bitcoind: whitelist nodes against banning

[PATCH, try2] bitcoind: whitelist nodes against banning
2013-11-22 20:56 ` Gregory Maxwell
  2013-11-22 21:37 ` Jouke Hofman
  
From: Jeff Garzik @ 2013-11-22 20:49 UTC (permalink / raw)
  To: Bitcoin Dev

[-- Attachment #1: Type: text/plain, Size: 138 bytes --]

Whitelist nodes against banning.

-- 
Jeff Garzik
Bitcoin core developer and open source evangelist
BitPay, Inc.      https://bitpay.com/

[-- Attachment #2: patch.txt --]
[-- Type: text/plain, Size: 6128 bytes --]

commit af7b624763e790075f8bb29a084309e6591e3469
Author: Jeff Garzik <jgarzik@bitpay•com>
Date:   Fri Nov 22 15:43:51 2013 -0500

    Add whitelist, to prevent whitelisted nodes from being banned.
    
    Entry point via RPC, configuration file or command line.

diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp
index a1e7d14..506b370 100644
--- a/src/bitcoinrpc.cpp
+++ b/src/bitcoinrpc.cpp
@@ -233,6 +233,7 @@ static const CRPCCommand vRPCCommands[] =
     { "getpeerinfo",            &getpeerinfo,            true,      false,      false },
     { "ping",                   &ping,                   true,      false,      false },
     { "addnode",                &addnode,                true,      true,       false },
+    { "addwhite",               &addwhite,               true,      true,       false },
     { "getaddednodeinfo",       &getaddednodeinfo,       true,      true,       false },
     { "getnettotals",           &getnettotals,           true,      true,       false },
     { "getdifficulty",          &getdifficulty,          true,      false,      false },
diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h
index 9025ff9..a7c2e2d 100644
--- a/src/bitcoinrpc.h
+++ b/src/bitcoinrpc.h
@@ -162,6 +162,7 @@ extern json_spirit::Value getconnectioncount(const json_spirit::Array& params, b
 extern json_spirit::Value getpeerinfo(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value ping(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value addnode(const json_spirit::Array& params, bool fHelp);
+extern json_spirit::Value addwhite(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value getaddednodeinfo(const json_spirit::Array& params, bool fHelp);
 extern json_spirit::Value getnettotals(const json_spirit::Array& params, bool fHelp);
 
diff --git a/src/init.cpp b/src/init.cpp
index b2e7ddf..75aa301 100644
--- a/src/init.cpp
+++ b/src/init.cpp
@@ -548,6 +548,8 @@ bool AppInit2(boost::thread_group& threadGroup, bool fForceServer)
 
     strWalletFile = GetArg("-wallet", "wallet.dat");
 
+    LoadWhitelist();
+
     // ********************************************************* Step 4: application initialization: dir lock, daemonize, pidfile, debug log
 
     std::string strDataDir = GetDataDir().string();
diff --git a/src/net.cpp b/src/net.cpp
index c547cf3..969bb1b 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -55,6 +55,8 @@ bool fDiscover = true;
 uint64_t nLocalServices = NODE_NETWORK;
 static CCriticalSection cs_mapLocalHost;
 static map<CNetAddr, LocalServiceInfo> mapLocalHost;
+static CCriticalSection cs_mapWhitelist;
+static map<CNetAddr, LocalServiceInfo> mapWhitelist;
 static bool vfReachable[NET_MAX] = {};
 static bool vfLimited[NET_MAX] = {};
 static CNode* pnodeLocalHost = NULL;
@@ -298,6 +300,45 @@ bool IsReachable(const CNetAddr& addr)
     return vfReachable[net] && !vfLimited[net];
 }
 
+// learn a new whitelisted address
+bool AddWhitelist(const CService& addr, int nScore)
+{
+    LogPrintf("AddWhitelist(%s,%i)\n", addr.ToString().c_str(), nScore);
+
+    {
+        LOCK(cs_mapWhitelist);
+        bool fAlready = mapWhitelist.count(addr) > 0;
+        LocalServiceInfo &info = mapWhitelist[addr];
+        if (!fAlready || nScore >= info.nScore) {
+            info.nScore = nScore + (fAlready ? 1 : 0);
+            info.nPort = addr.GetPort();
+        }
+    }
+
+    return true;
+}
+
+/** check whether a given address is potentially local */
+bool IsWhitelisted(const CService& addr)
+{
+    LOCK(cs_mapWhitelist);
+    return mapWhitelist.count(addr) > 0;
+}
+
+void LoadWhitelist()
+{
+    // Connect to specific addresses
+    if (mapArgs.count("-whitelist") && mapMultiArgs["-whitelist"].size() > 0)
+    {
+        BOOST_FOREACH(string strAddr, mapMultiArgs["-whitelist"])
+        {
+            CService serv(strAddr.c_str(), Params().GetDefaultPort(), true);
+            if (serv.IsValid())
+                AddWhitelist(serv, 0);
+        }
+    }
+}
+
 bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet)
 {
     SOCKET hSocket;
@@ -588,6 +629,11 @@ bool CNode::Misbehaving(int howmuch)
         LogPrintf("Warning: Local node %s misbehaving (delta: %d)!\n", addrName.c_str(), howmuch);
         return false;
     }
+    if (IsWhitelisted(addr))
+    {
+        LogPrintf("Warning: Whitelisted node %s misbehaving (delta: %d)!\n", addrName.c_str(), howmuch);
+        return false;
+    }
 
     nMisbehavior += howmuch;
     if (nMisbehavior >= GetArg("-banscore", 100))
diff --git a/src/net.h b/src/net.h
index 278462a..e215255 100644
--- a/src/net.h
+++ b/src/net.h
@@ -87,6 +87,9 @@ bool AddLocal(const CService& addr, int nScore = LOCAL_NONE);
 bool AddLocal(const CNetAddr& addr, int nScore = LOCAL_NONE);
 bool SeenLocal(const CService& addr);
 bool IsLocal(const CService& addr);
+bool AddWhitelist(const CService& addr, int nScore);
+bool IsWhitelisted(const CService& addr);
+void LoadWhitelist();
 bool GetLocal(CService &addr, const CNetAddr *paddrPeer = NULL);
 bool IsReachable(const CNetAddr &addr);
 void SetReachable(enum Network net, bool fFlag = true);
diff --git a/src/rpcnet.cpp b/src/rpcnet.cpp
index 9f8dea8..47af21f 100644
--- a/src/rpcnet.cpp
+++ b/src/rpcnet.cpp
@@ -139,6 +139,26 @@ Value getpeerinfo(const Array& params, bool fHelp)
     return ret;
 }
 
+Value addwhite(const Array& params, bool fHelp)
+{
+    if (fHelp || params.size() != 1)
+        throw runtime_error(
+            "addwhite \"node\"\n"
+            "\nAttempts add a node to the whitelist.\n"
+            "\nArguments:\n"
+            "1. \"node\"     (string, required) The node (see getpeerinfo for nodes)\n"
+            "\nExamples:\n"
+            + HelpExampleCli("addwhite", "\"192.168.0.6:8333\" ")
+            + HelpExampleRpc("addwhite", "\"192.168.0.6:8333\" ")
+        );
+
+    string strNode = params[0].get_str();
+
+    CService serv(strNode.c_str(), Params().GetDefaultPort(), true);
+
+    return AddWhitelist(serv, 0);
+}
+
 Value addnode(const Array& params, bool fHelp)
 {
     string strCommand;

 permalink raw reply	[|nested]
2013-11-22 20:49 [Bitcoin-development] [PATCH, try2] bitcoind: whitelist nodes against banning Jeff Garzik

  2013-11-22 21:01   ` Jeff Garzik
  2013-11-22 21:37 ` Jouke Hofman
  
From: Gregory Maxwell @ 2013-11-22 20:56 UTC (permalink / raw)
  To: Jeff Garzik;  Bitcoin Dev

On Fri, Nov 22, 2013 at 12:49 PM, Jeff Garzik <jgarzik@bitpay•com> wrote:
> Whitelist nodes against banning.

Is there a reason not to have a parallel get rpc to get the current list?



 permalink raw reply	[|nested]
2013-11-22 20:56 ` Gregory Maxwell

  2013-11-22 21:07     ` Warren Togami Jr.
  
From: Jeff Garzik @ 2013-11-22 21:01 UTC (permalink / raw)
  To: Gregory Maxwell;  Bitcoin Dev

On Fri, Nov 22, 2013 at 3:56 PM, Gregory Maxwell <gmaxwell@gmail•com> wrote:
> Is there a reason not to have a parallel get rpc to get the current list?

Easy enough to add.  There had also been requests for an IP blacklist,
which would need associated RPC/config gadgetry.

-- 
Jeff Garzik
Bitcoin core developer and open source evangelist
BitPay, Inc.      https://bitpay.com/



 permalink raw reply	[|nested]
2013-11-22 21:01   ` Jeff Garzik

  
From: Warren Togami Jr. @ 2013-11-22 21:07 UTC (permalink / raw)
  To: Jeff Garzik;  Bitcoin Dev

[-- Attachment #1: Type: text/plain, Size: 1234 bytes --]

https://github.com/bitcoin/bitcoin/pull/2906
There is already a bannode RPC PR.  Last I tried it didn't work though.


On Fri, Nov 22, 2013 at 11:01 AM, Jeff Garzik <jgarzik@bitpay•com> wrote:

> On Fri, Nov 22, 2013 at 3:56 PM, Gregory Maxwell <gmaxwell@gmail•com>
> wrote:
> > Is there a reason not to have a parallel get rpc to get the current list?
>
> Easy enough to add.  There had also been requests for an IP blacklist,
> which would need associated RPC/config gadgetry.
>
> --
> Jeff Garzik
> Bitcoin core developer and open source evangelist
> BitPay, Inc.      https://bitpay.com/
>
>
> ------------------------------------------------------------------------------
> Shape the Mobile Experience: Free Subscription
> Software experts and developers: Be at the forefront of tech innovation.
> Intel(R) Software Adrenaline delivers strategic insight and game-changing
> conversations that shape the rapidly evolving mobile landscape. Sign up
> now.
> http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
> _______________________________________________
> Bitcoin-development mailing list
> Bitcoin-development@lists•sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/bitcoin-development
>

[-- Attachment #2: Type: text/html, Size: 2144 bytes --]

 permalink raw reply	[|nested]
2013-11-22 20:49 [Bitcoin-development] [PATCH, try2] bitcoind: whitelist nodes against banning Jeff Garzik
  2013-11-22 20:56 ` Gregory Maxwell

  
From: Jouke Hofman @ 2013-11-22 21:37 UTC ( / )
  To: bitcoin-development

Thanks a lot! I will run these patches on some nodes tomorrow to see if
it works.

On 22-11-13 21:49, Jeff Garzik wrote:
> Whitelist nodes against banning.
> 
> 
> 
> ------------------------------------------------------------------------------
> Shape the Mobile Experience: Free Subscription
> Software experts and developers: Be at the forefront of tech innovation.
> Intel(R) Software Adrenaline delivers strategic insight and game-changing 
> conversations that shape the rapidly evolving mobile landscape. Sign up now. 
> http://pubads.g.doubleclick.net/gampad/clk?id=63431311&iu=/4140/ostg.clktrk
> 
> 
> 
> _______________________________________________
> Bitcoin-development mailing list
> Bitcoin-development@lists•sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/bitcoin-development
>