diff options
-rw-r--r-- | qa/rpc-tests/util.py | 2 | ||||
-rw-r--r-- | src/hash.cpp | 4 | ||||
-rw-r--r-- | src/init.cpp | 3 | ||||
-rw-r--r-- | src/main.cpp | 41 | ||||
-rw-r--r-- | src/net.cpp | 161 | ||||
-rw-r--r-- | src/net.h | 4 | ||||
-rw-r--r-- | src/qt/bitcoingui.cpp | 6 | ||||
-rw-r--r-- | src/qt/bitcoingui.h | 1 | ||||
-rw-r--r-- | src/qt/optionsdialog.cpp | 7 | ||||
-rw-r--r-- | src/qt/optionsdialog.h | 2 | ||||
-rw-r--r-- | src/random.cpp | 5 | ||||
-rw-r--r-- | src/random.h | 2 | ||||
-rw-r--r-- | src/wallet.cpp | 6 |
13 files changed, 82 insertions, 162 deletions
diff --git a/qa/rpc-tests/util.py b/qa/rpc-tests/util.py index c895eb1619..e5383b6c57 100644 --- a/qa/rpc-tests/util.py +++ b/qa/rpc-tests/util.py @@ -225,7 +225,7 @@ def gather_inputs(from_node, amount_needed): total_in += t["amount"] inputs.append({ "txid" : t["txid"], "vout" : t["vout"], "address" : t["address"] } ) if total_in < amount_needed: - raise RuntimeError("Insufficient funds: need %d, have %d"%(amount+fee*2, total_in)) + raise RuntimeError("Insufficient funds: need %d, have %d"%(amount_needed, total_in)) return (total_in, inputs) def make_change(from_node, amount_in, amount_out, fee): diff --git a/src/hash.cpp b/src/hash.cpp index 29376b45aa..2cca06ae23 100644 --- a/src/hash.cpp +++ b/src/hash.cpp @@ -1,3 +1,7 @@ +// Copyright (c) 2013-2014 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + #include "hash.h" inline uint32_t ROTL32(uint32_t x, int8_t r) diff --git a/src/init.cpp b/src/init.cpp index 22ec80e17b..b290d54158 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -574,6 +574,9 @@ bool AppInit2(boost::thread_group& threadGroup) // to protect privacy, do not listen by default if a default proxy server is specified if (SoftSetBoolArg("-listen", false)) LogPrintf("AppInit2 : parameter interaction: -proxy set -> setting -listen=0\n"); + // to protect privacy, do not discover addresses by default + if (SoftSetBoolArg("-discover", false)) + LogPrintf("AppInit2 : parameter interaction: -proxy set -> setting -discover=0\n"); } if (!GetBoolArg("-listen", true)) { diff --git a/src/main.cpp b/src/main.cpp index 4aa49531b3..2bff781bfa 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3478,12 +3478,6 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, else pfrom->fRelayTxes = true; - if (pfrom->fInbound && addrMe.IsRoutable()) - { - pfrom->addrLocal = addrMe; - SeenLocal(addrMe); - } - // Disconnect if we connected to ourself if (nNonce == nLocalHostNonce && nNonce > 1) { @@ -3492,6 +3486,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, return true; } + pfrom->addrLocal = addrMe; + if (pfrom->fInbound && addrMe.IsRoutable()) + { + SeenLocal(addrMe); + } + // Be shy and don't send version until we hear if (pfrom->fInbound) pfrom->PushVersion(); @@ -3512,7 +3512,12 @@ bool static ProcessMessage(CNode* pfrom, string strCommand, CDataStream& vRecv, { CAddress addr = GetLocalAddress(&pfrom->addr); if (addr.IsRoutable()) + { + pfrom->PushAddress(addr); + } else if (IsPeerAddrLocalGood(pfrom)) { + addr.SetIP(pfrom->addrLocal); pfrom->PushAddress(addr); + } } // Get recent addresses @@ -4375,24 +4380,18 @@ bool SendMessages(CNode* pto, bool fSendTrickle) static int64_t nLastRebroadcast; if (!IsInitialBlockDownload() && (GetTime() - nLastRebroadcast > 24 * 60 * 60)) { + LOCK(cs_vNodes); + BOOST_FOREACH(CNode* pnode, vNodes) { - LOCK(cs_vNodes); - BOOST_FOREACH(CNode* pnode, vNodes) - { - // Periodically clear setAddrKnown to allow refresh broadcasts - if (nLastRebroadcast) - pnode->setAddrKnown.clear(); + // Periodically clear setAddrKnown to allow refresh broadcasts + if (nLastRebroadcast) + pnode->setAddrKnown.clear(); - // Rebroadcast our address - if (fListen) - { - CAddress addr = GetLocalAddress(&pnode->addr); - if (addr.IsRoutable()) - pnode->PushAddress(addr); - } - } + // Rebroadcast our address + AdvertizeLocal(pnode); } - nLastRebroadcast = GetTime(); + if (!vNodes.empty()) + nLastRebroadcast = GetTime(); } // diff --git a/src/net.cpp b/src/net.cpp index 5ceb82cf8b..a66875a894 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -142,16 +142,19 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer) } // get best local address for a particular peer as a CAddress +// Otherwise, return the unroutable 0.0.0.0 but filled in with +// the normal parameters, since the IP may be changed to a useful +// one by discovery. CAddress GetLocalAddress(const CNetAddr *paddrPeer) { - CAddress ret(CService("0.0.0.0",0),0); + CAddress ret(CService("0.0.0.0",GetListenPort()),0); CService addr; if (GetLocal(addr, paddrPeer)) { ret = CAddress(addr); - ret.nServices = nLocalServices; - ret.nTime = GetAdjustedTime(); } + ret.nServices = nLocalServices; + ret.nTime = GetAdjustedTime(); return ret; } @@ -205,21 +208,38 @@ bool RecvLine(SOCKET hSocket, string& strLine) } } -// used when scores of local addresses may have changed -// pushes better local address to peers -void static AdvertizeLocal() +int GetnScore(const CService& addr) { - LOCK(cs_vNodes); - BOOST_FOREACH(CNode* pnode, vNodes) + LOCK(cs_mapLocalHost); + if (mapLocalHost.count(addr) == LOCAL_NONE) + return 0; + return mapLocalHost[addr].nScore; +} + +// Is our peer's addrLocal potentially useful as an external IP source? +bool IsPeerAddrLocalGood(CNode *pnode) +{ + return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() && + !IsLimited(pnode->addrLocal.GetNetwork()); +} + +// pushes our own address to a peer +void AdvertizeLocal(CNode *pnode) +{ + if (fListen && pnode->fSuccessfullyConnected) { - if (pnode->fSuccessfullyConnected) + CAddress addrLocal = GetLocalAddress(&pnode->addr); + // If discovery is enabled, sometimes give our peer the address it + // tells us that it sees us as in case it has a better idea of our + // address than we do. + if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() || + GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0)) { - CAddress addrLocal = GetLocalAddress(&pnode->addr); - if (addrLocal.IsRoutable() && (CService)addrLocal != (CService)pnode->addrLocal) - { - pnode->PushAddress(addrLocal); - pnode->addrLocal = addrLocal; - } + addrLocal.SetIP(pnode->addrLocal); + } + if (addrLocal.IsRoutable()) + { + pnode->PushAddress(addrLocal); } } } @@ -257,8 +277,6 @@ bool AddLocal(const CService& addr, int nScore) SetReachable(addr.GetNetwork()); } - AdvertizeLocal(); - return true; } @@ -296,12 +314,10 @@ bool SeenLocal(const CService& addr) return false; mapLocalHost[addr].nScore++; } - - AdvertizeLocal(); - return true; } + /** check whether a given address is potentially local */ bool IsLocal(const CService& addr) { @@ -323,114 +339,12 @@ bool IsReachable(const CNetAddr& addr) return IsReachable(net); } -bool GetMyExternalIP2(const CService& addrConnect, const char* pszGet, const char* pszKeyword, CNetAddr& ipRet) -{ - SOCKET hSocket; - if (!ConnectSocket(addrConnect, hSocket)) - return error("GetMyExternalIP() : connection to %s failed", addrConnect.ToString()); - - send(hSocket, pszGet, strlen(pszGet), MSG_NOSIGNAL); - - string strLine; - while (RecvLine(hSocket, strLine)) - { - if (strLine.empty()) // HTTP response is separated from headers by blank line - { - while (true) - { - if (!RecvLine(hSocket, strLine)) - { - CloseSocket(hSocket); - return false; - } - if (pszKeyword == NULL) - break; - if (strLine.find(pszKeyword) != string::npos) - { - strLine = strLine.substr(strLine.find(pszKeyword) + strlen(pszKeyword)); - break; - } - } - CloseSocket(hSocket); - if (strLine.find("<") != string::npos) - strLine = strLine.substr(0, strLine.find("<")); - strLine = strLine.substr(strspn(strLine.c_str(), " \t\n\r")); - while (strLine.size() > 0 && isspace(strLine[strLine.size()-1])) - strLine.resize(strLine.size()-1); - CService addr(strLine,0,true); - LogPrintf("GetMyExternalIP() received [%s] %s\n", strLine, addr.ToString()); - if (!addr.IsValid() || !addr.IsRoutable()) - return false; - ipRet.SetIP(addr); - return true; - } - } - CloseSocket(hSocket); - return error("GetMyExternalIP() : connection closed"); -} - -bool GetMyExternalIP(CNetAddr& ipRet) -{ - CService addrConnect; - const char* pszGet; - const char* pszKeyword; - - for (int nLookup = 0; nLookup <= 1; nLookup++) - for (int nHost = 1; nHost <= 1; nHost++) - { - // We should be phasing out our use of sites like these. If we need - // replacements, we should ask for volunteers to put this simple - // php file on their web server that prints the client IP: - // <?php echo $_SERVER["REMOTE_ADDR"]; ?> - if (nHost == 1) - { - addrConnect = CService("91.198.22.70", 80); // checkip.dyndns.org - - if (nLookup == 1) - { - CService addrIP("checkip.dyndns.org", 80, true); - if (addrIP.IsValid()) - addrConnect = addrIP; - } - - pszGet = "GET / HTTP/1.1\r\n" - "Host: checkip.dyndns.org\r\n" - "User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)\r\n" - "Connection: close\r\n" - "\r\n"; - - pszKeyword = "Address:"; - } - - if (GetMyExternalIP2(addrConnect, pszGet, pszKeyword, ipRet)) - return true; - } - - return false; -} - -void ThreadGetMyExternalIP() -{ - CNetAddr addrLocalHost; - if (GetMyExternalIP(addrLocalHost)) - { - LogPrintf("GetMyExternalIP() returned %s\n", addrLocalHost.ToStringIP()); - AddLocal(addrLocalHost, LOCAL_HTTP); - } -} - - - - - void AddressCurrentlyConnected(const CService& addr) { addrman.Connected(addr); } - - uint64_t CNode::nTotalBytesRecv = 0; uint64_t CNode::nTotalBytesSent = 0; CCriticalSection CNode::cs_totalBytesRecv; @@ -1687,9 +1601,6 @@ void static Discover(boost::thread_group& threadGroup) } #endif - // Don't use external IPv4 discovery, when -onlynet="IPv6" - if (!IsLimited(NET_IPV4)) - threadGroup.create_thread(boost::bind(&TraceThread<void (*)()>, "ext-ip", &ThreadGetMyExternalIP)); } void StartNode(boost::thread_group& threadGroup) @@ -60,7 +60,6 @@ unsigned int SendBufferSize(); void AddOneShot(std::string strDest); bool RecvLine(SOCKET hSocket, std::string& strLine); -bool GetMyExternalIP(CNetAddr& ipRet); void AddressCurrentlyConnected(const CService& addr); CNode* FindNode(const CNetAddr& ip); CNode* FindNode(const std::string& addrName); @@ -96,12 +95,13 @@ enum LOCAL_IF, // address a local interface listens on LOCAL_BIND, // address explicit bound to LOCAL_UPNP, // address reported by UPnP - LOCAL_HTTP, // address reported by whatismyip.com and similar LOCAL_MANUAL, // address explicitly specified (-externalip=) LOCAL_MAX }; +bool IsPeerAddrLocalGood(CNode *pnode); +void AdvertizeLocal(CNode *pnode); void SetLimited(enum Network net, bool fLimited = true); bool IsLimited(enum Network net); bool IsLimited(const CNetAddr& addr); diff --git a/src/qt/bitcoingui.cpp b/src/qt/bitcoingui.cpp index 77cfdceef0..450b83a8c6 100644 --- a/src/qt/bitcoingui.cpp +++ b/src/qt/bitcoingui.cpp @@ -103,9 +103,9 @@ BitcoinGUI::BitcoinGUI(const NetworkStyle *networkStyle, QWidget *parent) : QString windowTitle = tr("Bitcoin Core") + " - "; #ifdef ENABLE_WALLET /* if compiled with wallet support, -disablewallet can still disable the wallet */ - bool enableWallet = !GetBoolArg("-disablewallet", false); + enableWallet = !GetBoolArg("-disablewallet", false); #else - bool enableWallet = false; + enableWallet = false; #endif // ENABLE_WALLET if(enableWallet) { @@ -554,7 +554,7 @@ void BitcoinGUI::optionsClicked() if(!clientModel || !clientModel->getOptionsModel()) return; - OptionsDialog dlg(this); + OptionsDialog dlg(this, enableWallet); dlg.setModel(clientModel->getOptionsModel()); dlg.exec(); } diff --git a/src/qt/bitcoingui.h b/src/qt/bitcoingui.h index 35b36811c4..662ef9d9e8 100644 --- a/src/qt/bitcoingui.h +++ b/src/qt/bitcoingui.h @@ -64,6 +64,7 @@ public: bool setCurrentWallet(const QString& name); void removeAllWallets(); #endif // ENABLE_WALLET + bool enableWallet; protected: void changeEvent(QEvent *e); diff --git a/src/qt/optionsdialog.cpp b/src/qt/optionsdialog.cpp index 67be174d55..f5a0759c92 100644 --- a/src/qt/optionsdialog.cpp +++ b/src/qt/optionsdialog.cpp @@ -30,7 +30,7 @@ #include <QMessageBox> #include <QTimer> -OptionsDialog::OptionsDialog(QWidget *parent) : +OptionsDialog::OptionsDialog(QWidget *parent, bool enableWallet) : QDialog(parent), ui(new Ui::OptionsDialog), model(0), @@ -66,6 +66,11 @@ OptionsDialog::OptionsDialog(QWidget *parent) : ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWindow)); #endif + /* remove Wallet tab in case of -disablewallet */ + if (!enableWallet) { + ui->tabWidget->removeTab(ui->tabWidget->indexOf(ui->tabWallet)); + } + /* Display elements init */ QDir translations(":translations"); ui->lang->addItem(QString("(") + tr("default") + QString(")"), QVariant("")); diff --git a/src/qt/optionsdialog.h b/src/qt/optionsdialog.h index 108609610d..794a39590c 100644 --- a/src/qt/optionsdialog.h +++ b/src/qt/optionsdialog.h @@ -24,7 +24,7 @@ class OptionsDialog : public QDialog Q_OBJECT public: - explicit OptionsDialog(QWidget *parent); + explicit OptionsDialog(QWidget *parent, bool enableWallet); ~OptionsDialog(); void setModel(OptionsModel *model); diff --git a/src/random.cpp b/src/random.cpp index 998e7dfb08..fc9505ae73 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -82,13 +82,12 @@ void RandAddSeedPerfmon() #endif } -bool GetRandBytes(unsigned char* buf, int num) +void GetRandBytes(unsigned char* buf, int num) { if (RAND_bytes(buf, num) != 1) { LogPrintf("%s: OpenSSL RAND_bytes() failed with error: %s\n", __func__, ERR_error_string(ERR_get_error(), NULL)); - return false; + assert(false); } - return true; } uint64_t GetRand(uint64_t nMax) diff --git a/src/random.h b/src/random.h index 161ebe8986..ec73d910c4 100644 --- a/src/random.h +++ b/src/random.h @@ -19,7 +19,7 @@ void RandAddSeedPerfmon(); /** * Functions to gather random data via the OpenSSL PRNG */ -bool GetRandBytes(unsigned char* buf, int num); +void GetRandBytes(unsigned char* buf, int num); uint64_t GetRand(uint64_t nMax); int GetRandInt(int nMax); uint256 GetRandHash(); diff --git a/src/wallet.cpp b/src/wallet.cpp index d392149dbb..ec439c5aad 100644 --- a/src/wallet.cpp +++ b/src/wallet.cpp @@ -422,15 +422,13 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase) RandAddSeedPerfmon(); vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE); - if (!GetRandBytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE)) - return false; + GetRandBytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE); CMasterKey kMasterKey; RandAddSeedPerfmon(); kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE); - if (!GetRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE)) - return false; + GetRandBytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE); CCrypter crypter; int64_t nStartTime = GetTimeMillis(); |