From 99860de3c9b5809110538e1ab2d4e216d77b5aaf Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 11 Aug 2011 13:41:01 +0200 Subject: Make some global variables less-global (static) Explicitly make these global variables less-global to reduce the maximum scope of this global state. In my experience global variables tend to be a major source of bugs. As such the less accessible they are the less likely they are to be the source of a bug. Signed-off-by: Giel van Schijndel --- src/main.cpp | 4 ++-- src/main.h | 13 ------------- src/net.cpp | 4 ++-- src/net.h | 2 -- 4 files changed, 4 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index f483312065..8b4b3df982 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -22,14 +22,14 @@ set setpwalletRegistered; CCriticalSection cs_main; -map mapTransactions; +static map mapTransactions; CCriticalSection cs_mapTransactions; unsigned int nTransactionsUpdated = 0; map mapNextTx; map mapBlockIndex; uint256 hashGenesisBlock("0x000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f"); -CBigNum bnProofOfWorkLimit(~uint256(0) >> 32); +static CBigNum bnProofOfWorkLimit(~uint256(0) >> 32); const int nTotalBlocksEstimate = 134444; // Conservative estimate of total nr of blocks on main chain const int nInitialBlockThreshold = 120; // Regard blocks up until N-threshold as "initial download" CBlockIndex* pindexGenesisBlock = NULL; diff --git a/src/main.h b/src/main.h index 97a40d3ec9..d4b89225d1 100644 --- a/src/main.h +++ b/src/main.h @@ -54,7 +54,6 @@ static const int fHaveUPnP = false; extern CCriticalSection cs_main; extern std::map mapBlockIndex; extern uint256 hashGenesisBlock; -extern CBigNum bnProofOfWorkLimit; extern CBlockIndex* pindexGenesisBlock; extern int nBestHeight; extern CBigNum bnBestChainWork; @@ -1558,16 +1557,4 @@ public: bool ProcessAlert(); }; - - - - - - - - - - -extern std::map mapTransactions; - #endif diff --git a/src/net.cpp b/src/net.cpp index 3953f021e9..df98457c72 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -50,10 +50,10 @@ bool fClient = false; bool fAllowDNS = false; uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK); CAddress addrLocalHost("0.0.0.0", 0, false, nLocalServices); -CNode* pnodeLocalHost = NULL; +static CNode* pnodeLocalHost = NULL; uint64 nLocalHostNonce = 0; array vnThreadsRunning; -SOCKET hListenSocket = INVALID_SOCKET; +static SOCKET hListenSocket = INVALID_SOCKET; vector vNodes; CCriticalSection cs_vNodes; diff --git a/src/net.h b/src/net.h index 3b098e1a82..52568ef4ce 100644 --- a/src/net.h +++ b/src/net.h @@ -476,10 +476,8 @@ extern bool fClient; extern bool fAllowDNS; extern uint64 nLocalServices; extern CAddress addrLocalHost; -extern CNode* pnodeLocalHost; extern uint64 nLocalHostNonce; extern boost::array vnThreadsRunning; -extern SOCKET hListenSocket; extern std::vector vNodes; extern CCriticalSection cs_vNodes; -- cgit v1.2.3 From e49b83bb1242b37717e5cfabc344b0ff7157484e Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 11 Aug 2011 17:19:36 +0200 Subject: Cleanup makefiles such that diffs to them are smaller Signed-off-by: Giel van Schijndel --- src/makefile.linux-mingw | 37 ++++++++++++++++++++++++++++--------- src/makefile.mingw | 37 ++++++++++++++++++++++++++++--------- src/makefile.osx | 37 ++++++++++++++++++++++++++++--------- src/makefile.unix | 37 ++++++++++++++++++++++++++++--------- src/makefile.vc | 48 +++++++++++++++++++++++++++++++++++------------- 5 files changed, 147 insertions(+), 49 deletions(-) (limited to 'src') diff --git a/src/makefile.linux-mingw b/src/makefile.linux-mingw index e2ef4213aa..c03cb9d2aa 100644 --- a/src/makefile.linux-mingw +++ b/src/makefile.linux-mingw @@ -35,9 +35,28 @@ LIBS= \ DEFS=-D_MT -DWIN32 -D__WXMSW__ -D_WINDOWS -DNOPCH -DUSE_SSL DEBUGFLAGS=-g -D__WXDEBUG__ CFLAGS=-O2 -w -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(DEFS) $(INCLUDEPATHS) -HEADERS=headers.h strlcpy.h serialize.h uint256.h util.h key.h bignum.h base58.h \ - script.h db.h net.h irc.h keystore.h main.h wallet.h rpc.h uibase.h ui.h noui.h \ - crypter.h init.h +HEADERS = \ + base58.h \ + bignum.h \ + crypter.h \ + db.h \ + headers.h \ + init.h \ + irc.h \ + key.h \ + keystore.h \ + main.h \ + net.h \ + noui.h \ + rpc.h \ + script.h \ + serialize.h \ + strlcpy.h \ + ui.h \ + uibase.h \ + uint256.h \ + util.h \ + wallet.h ifdef USE_UPNP INCLUDEPATHS += -I"$(DEPSDIR)/upnpc-exe-win32-20110215" @@ -49,17 +68,17 @@ endif LIBS += -l mingwthrd -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell32 -l comctl32 -l ole32 -l oleaut32 -l uuid -l rpcrt4 -l advapi32 -l ws2_32 -l shlwapi OBJS= \ - obj/util.o \ - obj/script.o \ + obj/crypter.o \ obj/db.o \ - obj/net.o \ + obj/init.o \ obj/irc.o \ obj/keystore.o \ obj/main.o \ - obj/wallet.o \ + obj/net.o \ obj/rpc.o \ - obj/init.o \ - obj/crypter.o \ + obj/script.o \ + obj/util.o \ + obj/wallet.o \ cryptopp/obj/sha.o \ cryptopp/obj/cpu.o diff --git a/src/makefile.mingw b/src/makefile.mingw index 96f81b3067..55cb8a7058 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw @@ -32,9 +32,28 @@ LIBS= \ DEFS=-DWIN32 -D__WXMSW__ -D_WINDOWS -DNOPCH -DUSE_SSL DEBUGFLAGS=-g -D__WXDEBUG__ CFLAGS=-mthreads -O2 -w -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(DEFS) $(INCLUDEPATHS) -HEADERS=headers.h strlcpy.h serialize.h uint256.h util.h key.h bignum.h base58.h \ - script.h db.h net.h irc.h keystore.h main.h wallet.h rpc.h uibase.h ui.h noui.h \ - init.h crypter.h +HEADERS = \ + base58.h \ + bignum.h \ + crypter.h \ + db.h \ + headers.h \ + init.h \ + irc.h \ + key.h \ + keystore.h \ + main.h \ + net.h \ + noui.h \ + rpc.h \ + script.h \ + serialize.h \ + strlcpy.h \ + ui.h \ + uibase.h \ + uint256.h \ + util.h \ + wallet.h ifdef USE_UPNP INCLUDEPATHS += -I"C:\upnpc-exe-win32-20110215" @@ -46,17 +65,17 @@ endif LIBS += -l kernel32 -l user32 -l gdi32 -l comdlg32 -l winspool -l winmm -l shell32 -l comctl32 -l ole32 -l oleaut32 -l uuid -l rpcrt4 -l advapi32 -l ws2_32 -l shlwapi OBJS= \ - obj/util.o \ - obj/script.o \ + obj/crypter.o \ obj/db.o \ - obj/net.o \ + obj/init.o \ obj/irc.o \ obj/keystore.o \ obj/main.o \ - obj/wallet.o \ + obj/net.o \ obj/rpc.o \ - obj/init.o \ - obj/crypter.o \ + obj/script.o \ + obj/util.o \ + obj/wallet.o \ cryptopp/obj/sha.o \ cryptopp/obj/cpu.o diff --git a/src/makefile.osx b/src/makefile.osx index 699911d4e6..7172bcc417 100644 --- a/src/makefile.osx +++ b/src/makefile.osx @@ -32,22 +32,41 @@ DEFS=$(shell $(DEPSDIR)/bin/wx-config --cxxflags) -D__WXMAC_OSX__ -DNOPCH -DMSG_ DEBUGFLAGS=-g -DwxDEBUG_LEVEL=0 # ppc doesn't work because we don't support big-endian CFLAGS=-mmacosx-version-min=10.5 -arch i386 -arch x86_64 -O3 -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(DEFS) $(INCLUDEPATHS) -HEADERS=headers.h strlcpy.h serialize.h uint256.h util.h key.h bignum.h base58.h \ - script.h db.h net.h irc.h keystore.h main.h wallet.h rpc.h uibase.h ui.h noui.h \ - init.h crypter.h +HEADERS = \ + base58.h \ + bignum.h \ + crypter.h \ + db.h \ + headers.h \ + init.h \ + irc.h \ + key.h \ + keystore.h \ + main.h \ + net.h \ + noui.h \ + rpc.h \ + script.h \ + serialize.h \ + strlcpy.h \ + ui.h \ + uibase.h \ + uint256.h \ + util.h \ + wallet.h OBJS= \ - obj/util.o \ - obj/script.o \ + obj/crypter.o \ obj/db.o \ - obj/net.o \ + obj/init.o \ obj/irc.o \ obj/keystore.o \ obj/main.o \ - obj/wallet.o \ + obj/net.o \ obj/rpc.o \ - obj/init.o \ - obj/crypter.o \ + obj/script.o \ + obj/util.o \ + obj/wallet.o \ cryptopp/obj/sha.o \ cryptopp/obj/cpu.o diff --git a/src/makefile.unix b/src/makefile.unix index 4c92797292..7cc72e0ea2 100644 --- a/src/makefile.unix +++ b/src/makefile.unix @@ -38,22 +38,41 @@ LIBS+= \ DEBUGFLAGS=-g -D__WXDEBUG__ CXXFLAGS=-O2 -Wno-invalid-offsetof -Wformat $(DEBUGFLAGS) $(DEFS) -HEADERS=headers.h strlcpy.h serialize.h uint256.h util.h key.h bignum.h base58.h \ - script.h db.h net.h irc.h keystore.h main.h wallet.h rpc.h uibase.h ui.h noui.h \ - init.h crypter.h +HEADERS = \ + base58.h \ + bignum.h \ + crypter.h \ + db.h \ + headers.h \ + init.h \ + irc.h \ + key.h \ + keystore.h \ + main.h \ + net.h \ + noui.h \ + rpc.h \ + script.h \ + serialize.h \ + strlcpy.h \ + ui.h \ + uibase.h \ + uint256.h \ + util.h \ + wallet.h OBJS= \ - obj/util.o \ - obj/script.o \ + obj/crypter.o \ obj/db.o \ - obj/net.o \ + obj/init.o \ obj/irc.o \ obj/keystore.o \ obj/main.o \ - obj/wallet.o \ + obj/net.o \ obj/rpc.o \ - obj/init.o \ - obj/crypter.o \ + obj/script.o \ + obj/util.o \ + obj/wallet.o \ cryptopp/obj/sha.o \ cryptopp/obj/cpu.o diff --git a/src/makefile.vc b/src/makefile.vc index c050deb6ed..edaba80a7b 100644 --- a/src/makefile.vc +++ b/src/makefile.vc @@ -45,21 +45,43 @@ LIBS=$(LIBS) \ DEBUGFLAGS=/Os CFLAGS=/MD /c /nologo /EHsc /GR /Zm300 $(DEBUGFLAGS) $(DEFS) $(INCLUDEPATHS) -HEADERS=headers.h strlcpy.h serialize.h uint256.h util.h key.h bignum.h base58.h \ - script.h db.h net.h irc.h main.h rpc.h uibase.h ui.h noui.h init.h wallet.h keystore.h crypter.h +HEADERS = \ + base58.h \ + bignum.h \ + crypter.h \ + db.h \ + headers.h \ + init.h \ + irc.h \ + key.h \ + keystore.h \ + main.h \ + net.h \ + noui.h \ + rpc.h \ + script.h \ + serialize.h \ + strlcpy.h \ + ui.h \ + uibase.h \ + uint256.h \ + util.h \ + wallet.h OBJS= \ - obj\util.obj \ - obj\script.obj \ - obj\db.obj \ - obj\net.obj \ - obj\irc.obj \ - obj\keystore.obj \ - obj\main.obj \ - obj\wallet.obj \ - obj\rpc.obj \ - obj\init.obj \ - obj\crypter.obj + obj\crypter.o \ + obj\db.o \ + obj\init.o \ + obj\irc.o \ + obj\keystore.o \ + obj\main.o \ + obj\net.o \ + obj\rpc.o \ + obj\script.o \ + obj\util.o \ + obj\wallet.o \ + cryptopp\obj\sha.o \ + cryptopp\obj\cpu.o CRYPTOPP_OBJS= \ cryptopp\obj\sha.obj \ -- cgit v1.2.3 From 82dc6426b4cbf769ace7976c2a40d160b8d76f08 Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 11 Aug 2011 18:12:14 +0200 Subject: Move func 'REF' from util.h to serialize.h util.h doesn't use REF, serialize.h does, creating a dependency of serialize.h on util.h, but util.h already depends on serialize.h. To resolve this circular dependency the function 'REF' has now been moved closer to one of its two points of use. Signed-off-by: Giel van Schijndel --- src/serialize.h | 11 +++++++---- src/util.h | 8 -------- 2 files changed, 7 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/serialize.h b/src/serialize.h index c55775242b..0a31ff557d 100644 --- a/src/serialize.h +++ b/src/serialize.h @@ -63,10 +63,13 @@ static const int VERSION = 32500; static const char* pszSubVer = ""; static const bool VERSION_IS_BETA = true; - - - - +// Used to bypass the rule against non-const reference to temporary +// where it makes sense with wrappers such as CFlatData or CTxDB +template +inline T& REF(const T& val) +{ + return const_cast(val); +} ///////////////////////////////////////////////////////////////// // diff --git a/src/util.h b/src/util.h index 1e4ceb2468..3d7ef108b4 100644 --- a/src/util.h +++ b/src/util.h @@ -67,14 +67,6 @@ typedef unsigned long long uint64; // This is needed because the foreach macro can't get over the comma in pair #define PAIRTYPE(t1, t2) pair -// Used to bypass the rule against non-const reference to temporary -// where it makes sense with wrappers such as CFlatData or CTxDB -template -inline T& REF(const T& val) -{ - return (T&)val; -} - // Align by increasing pointer, must have extra space at end of buffer template T* alignup(T* p) -- cgit v1.2.3 From 507fd9d15baac950df494742d67bcbafdaa4752c Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 11 Aug 2011 18:14:53 +0200 Subject: Start moving protocol-specific code to protocol.[ch]pp Move CMessageHeader from net.h to protocol.[ch]pp, with the implementation in the .cpp compilation unit (compiling once is enough). This commit does *not* and should not modify *any* code, it only moves it from net.h and splits it across protocol.cpp and protocol.hpp. Indentation changes aside the closest thing to a modification of code is the addition of the 'TODO' comment (the execution of which requires code modifications and thus doesn't belong in this commit). Signed-off-by: Giel van Schijndel --- src/main.h | 1 - src/makefile.linux-mingw | 2 + src/makefile.mingw | 2 + src/makefile.osx | 2 + src/makefile.unix | 2 + src/makefile.vc | 2 + src/net.h | 98 +----------------------------------------------- src/protocol.cpp | 61 ++++++++++++++++++++++++++++++ src/protocol.h | 52 +++++++++++++++++++++++++ 9 files changed, 125 insertions(+), 97 deletions(-) create mode 100644 src/protocol.cpp create mode 100644 src/protocol.h (limited to 'src') diff --git a/src/main.h b/src/main.h index d4b89225d1..427067bc94 100644 --- a/src/main.h +++ b/src/main.h @@ -21,7 +21,6 @@ class CKeyItem; class CReserveKey; class CWalletDB; -class CMessageHeader; class CAddress; class CInv; class CRequestTracker; diff --git a/src/makefile.linux-mingw b/src/makefile.linux-mingw index c03cb9d2aa..12f6e212f9 100644 --- a/src/makefile.linux-mingw +++ b/src/makefile.linux-mingw @@ -48,6 +48,7 @@ HEADERS = \ main.h \ net.h \ noui.h \ + protocol.h \ rpc.h \ script.h \ serialize.h \ @@ -75,6 +76,7 @@ OBJS= \ obj/keystore.o \ obj/main.o \ obj/net.o \ + obj/protocol.o \ obj/rpc.o \ obj/script.o \ obj/util.o \ diff --git a/src/makefile.mingw b/src/makefile.mingw index 55cb8a7058..893700b882 100644 --- a/src/makefile.mingw +++ b/src/makefile.mingw @@ -45,6 +45,7 @@ HEADERS = \ main.h \ net.h \ noui.h \ + protocol.h \ rpc.h \ script.h \ serialize.h \ @@ -72,6 +73,7 @@ OBJS= \ obj/keystore.o \ obj/main.o \ obj/net.o \ + obj/protocol.o \ obj/rpc.o \ obj/script.o \ obj/util.o \ diff --git a/src/makefile.osx b/src/makefile.osx index 7172bcc417..48908d9f30 100644 --- a/src/makefile.osx +++ b/src/makefile.osx @@ -45,6 +45,7 @@ HEADERS = \ main.h \ net.h \ noui.h \ + protocol.h \ rpc.h \ script.h \ serialize.h \ @@ -63,6 +64,7 @@ OBJS= \ obj/keystore.o \ obj/main.o \ obj/net.o \ + obj/protocol.o \ obj/rpc.o \ obj/script.o \ obj/util.o \ diff --git a/src/makefile.unix b/src/makefile.unix index 7cc72e0ea2..a4f13ae2c9 100644 --- a/src/makefile.unix +++ b/src/makefile.unix @@ -51,6 +51,7 @@ HEADERS = \ main.h \ net.h \ noui.h \ + protocol.h \ rpc.h \ script.h \ serialize.h \ @@ -69,6 +70,7 @@ OBJS= \ obj/keystore.o \ obj/main.o \ obj/net.o \ + obj/protocol.o \ obj/rpc.o \ obj/script.o \ obj/util.o \ diff --git a/src/makefile.vc b/src/makefile.vc index edaba80a7b..a5437bcf5c 100644 --- a/src/makefile.vc +++ b/src/makefile.vc @@ -58,6 +58,7 @@ HEADERS = \ main.h \ net.h \ noui.h \ + protocol.h \ rpc.h \ script.h \ serialize.h \ @@ -77,6 +78,7 @@ OBJS= \ obj\main.o \ obj\net.o \ obj\rpc.o \ + obj\protocol.o \ obj\script.o \ obj\util.o \ obj\wallet.o \ diff --git a/src/net.h b/src/net.h index 52568ef4ce..7a4706d53b 100644 --- a/src/net.h +++ b/src/net.h @@ -14,7 +14,8 @@ #include #endif -class CMessageHeader; +#include "protocol.h" + class CAddress; class CAddrDB; class CInv; @@ -54,101 +55,6 @@ bool BindListenPort(std::string& strError=REF(std::string())); void StartNode(void* parg); bool StopNode(); - - - - - - - -// -// Message header -// (4) message start -// (12) command -// (4) size -// (4) checksum - -extern unsigned char pchMessageStart[4]; - -class CMessageHeader -{ -public: - enum { COMMAND_SIZE=12 }; - char pchMessageStart[sizeof(::pchMessageStart)]; - char pchCommand[COMMAND_SIZE]; - unsigned int nMessageSize; - unsigned int nChecksum; - - CMessageHeader() - { - memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); - memset(pchCommand, 0, sizeof(pchCommand)); - pchCommand[1] = 1; - nMessageSize = -1; - nChecksum = 0; - } - - CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) - { - memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); - strncpy(pchCommand, pszCommand, COMMAND_SIZE); - nMessageSize = nMessageSizeIn; - nChecksum = 0; - } - - IMPLEMENT_SERIALIZE - ( - READWRITE(FLATDATA(pchMessageStart)); - READWRITE(FLATDATA(pchCommand)); - READWRITE(nMessageSize); - if (nVersion >= 209) - READWRITE(nChecksum); - ) - - std::string GetCommand() - { - if (pchCommand[COMMAND_SIZE-1] == 0) - return std::string(pchCommand, pchCommand + strlen(pchCommand)); - else - return std::string(pchCommand, pchCommand + COMMAND_SIZE); - } - - bool IsValid() - { - // Check start string - if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0) - return false; - - // Check the command string for errors - for (char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++) - { - if (*p1 == 0) - { - // Must be all zeros after the first zero - for (; p1 < pchCommand + COMMAND_SIZE; p1++) - if (*p1 != 0) - return false; - } - else if (*p1 < ' ' || *p1 > 0x7E) - return false; - } - - // Message size - if (nMessageSize > MAX_SIZE) - { - printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize); - return false; - } - - return true; - } -}; - - - - - - static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; class CAddress diff --git a/src/protocol.cpp b/src/protocol.cpp new file mode 100644 index 0000000000..0bb1da93e0 --- /dev/null +++ b/src/protocol.cpp @@ -0,0 +1,61 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +#include "protocol.h" + +CMessageHeader::CMessageHeader() +{ + memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); + memset(pchCommand, 0, sizeof(pchCommand)); + pchCommand[1] = 1; + nMessageSize = -1; + nChecksum = 0; +} + +CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) +{ + memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); + strncpy(pchCommand, pszCommand, COMMAND_SIZE); + nMessageSize = nMessageSizeIn; + nChecksum = 0; +} + +std::string CMessageHeader::GetCommand() const +{ + if (pchCommand[COMMAND_SIZE-1] == 0) + return std::string(pchCommand, pchCommand + strlen(pchCommand)); + else + return std::string(pchCommand, pchCommand + COMMAND_SIZE); +} + +bool CMessageHeader::IsValid() const +{ + // Check start string + if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0) + return false; + + // Check the command string for errors + for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++) + { + if (*p1 == 0) + { + // Must be all zeros after the first zero + for (; p1 < pchCommand + COMMAND_SIZE; p1++) + if (*p1 != 0) + return false; + } + else if (*p1 < ' ' || *p1 > 0x7E) + return false; + } + + // Message size + if (nMessageSize > MAX_SIZE) + { + printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize); + return false; + } + + return true; +} diff --git a/src/protocol.h b/src/protocol.h new file mode 100644 index 0000000000..b5baeb2a68 --- /dev/null +++ b/src/protocol.h @@ -0,0 +1,52 @@ +// Copyright (c) 2009-2010 Satoshi Nakamoto +// Copyright (c) 2011 The Bitcoin developers +// Distributed under the MIT/X11 software license, see the accompanying +// file license.txt or http://www.opensource.org/licenses/mit-license.php. + +#ifndef __cplusplus +# error This header can only be compiled as C++. +#endif + +#ifndef __INCLUDED_PROTOCOL_H__ +#define __INCLUDED_PROTOCOL_H__ + +#include "serialize.h" +#include + +// +// Message header +// (4) message start +// (12) command +// (4) size +// (4) checksum + +extern unsigned char pchMessageStart[4]; + +class CMessageHeader +{ + public: + CMessageHeader(); + CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn); + + std::string GetCommand() const; + bool IsValid() const; + + IMPLEMENT_SERIALIZE + ( + READWRITE(FLATDATA(pchMessageStart)); + READWRITE(FLATDATA(pchCommand)); + READWRITE(nMessageSize); + if (nVersion >= 209) + READWRITE(nChecksum); + ) + + // TODO: make private (improves encapsulation) + public: + enum { COMMAND_SIZE=12 }; + char pchMessageStart[sizeof(::pchMessageStart)]; + char pchCommand[COMMAND_SIZE]; + unsigned int nMessageSize; + unsigned int nChecksum; +}; + +#endif // __INCLUDED_PROTOCOL_H__ -- cgit v1.2.3 From 33e28c9948336784324cf5c7248ce608b93dbfde Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 11 Aug 2011 18:40:12 +0200 Subject: Move CAddress to protocol.[ch]pp This commit does *not* and should not modify *any* code, it only moves it from net.h and splits it across protocol.cpp and protocol.hpp. Signed-off-by: Giel van Schijndel --- src/net.h | 225 ------------------------------------------------------- src/protocol.cpp | 190 ++++++++++++++++++++++++++++++++++++++++++++++ src/protocol.h | 71 ++++++++++++++++++ 3 files changed, 261 insertions(+), 225 deletions(-) (limited to 'src') diff --git a/src/net.h b/src/net.h index 7a4706d53b..66f3949b86 100644 --- a/src/net.h +++ b/src/net.h @@ -16,7 +16,6 @@ #include "protocol.h" -class CAddress; class CAddrDB; class CInv; class CRequestTracker; @@ -29,15 +28,7 @@ extern int nConnectTimeout; inline unsigned int ReceiveBufferSize() { return 1000*GetArg("-maxreceivebuffer", 10*1000); } inline unsigned int SendBufferSize() { return 1000*GetArg("-maxsendbuffer", 10*1000); } -inline unsigned short GetDefaultPort() { return fTestNet ? 18333 : 8333; } static const unsigned int PUBLISH_HOPS = 5; -enum -{ - NODE_NETWORK = (1 << 0), -}; - - - bool ConnectSocket(const CAddress& addrConnect, SOCKET& hSocketRet, int nTimeout=nConnectTimeout); bool Lookup(const char *pszName, std::vector& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); @@ -55,222 +46,6 @@ bool BindListenPort(std::string& strError=REF(std::string())); void StartNode(void* parg); bool StopNode(); -static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; - -class CAddress -{ -public: - uint64 nServices; - unsigned char pchReserved[12]; - unsigned int ip; - unsigned short port; - - // disk and network only - unsigned int nTime; - - // memory only - unsigned int nLastTry; - - CAddress() - { - Init(); - } - - CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK) - { - Init(); - ip = ipIn; - port = htons(portIn == 0 ? GetDefaultPort() : portIn); - nServices = nServicesIn; - } - - explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK) - { - Init(); - ip = sockaddr.sin_addr.s_addr; - port = sockaddr.sin_port; - nServices = nServicesIn; - } - - explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) - { - Init(); - Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn); - } - - explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) - { - Init(); - Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true); - } - - explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) - { - Init(); - Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn); - } - - explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK) - { - Init(); - Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true); - } - - void Init() - { - nServices = NODE_NETWORK; - memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); - ip = INADDR_NONE; - port = htons(GetDefaultPort()); - nTime = 100000000; - nLastTry = 0; - } - - IMPLEMENT_SERIALIZE - ( - if (fRead) - const_cast(this)->Init(); - if (nType & SER_DISK) - READWRITE(nVersion); - if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH))) - READWRITE(nTime); - READWRITE(nServices); - READWRITE(FLATDATA(pchReserved)); // for IPv6 - READWRITE(ip); - READWRITE(port); - ) - - friend inline bool operator==(const CAddress& a, const CAddress& b) - { - return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 && - a.ip == b.ip && - a.port == b.port); - } - - friend inline bool operator!=(const CAddress& a, const CAddress& b) - { - return (!(a == b)); - } - - friend inline bool operator<(const CAddress& a, const CAddress& b) - { - int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)); - if (ret < 0) - return true; - else if (ret == 0) - { - if (ntohl(a.ip) < ntohl(b.ip)) - return true; - else if (a.ip == b.ip) - return ntohs(a.port) < ntohs(b.port); - } - return false; - } - - std::vector GetKey() const - { - CDataStream ss; - ss.reserve(18); - ss << FLATDATA(pchReserved) << ip << port; - - #if defined(_MSC_VER) && _MSC_VER < 1300 - return std::vector((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]); - #else - return std::vector(ss.begin(), ss.end()); - #endif - } - - struct sockaddr_in GetSockAddr() const - { - struct sockaddr_in sockaddr; - memset(&sockaddr, 0, sizeof(sockaddr)); - sockaddr.sin_family = AF_INET; - sockaddr.sin_addr.s_addr = ip; - sockaddr.sin_port = port; - return sockaddr; - } - - bool IsIPv4() const - { - return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0); - } - - bool IsRFC1918() const - { - return IsIPv4() && (GetByte(3) == 10 || - (GetByte(3) == 192 && GetByte(2) == 168) || - (GetByte(3) == 172 && - (GetByte(2) >= 16 && GetByte(2) <= 31))); - } - - bool IsRFC3927() const - { - return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254); - } - - bool IsLocal() const - { - return IsIPv4() && (GetByte(3) == 127 || - GetByte(3) == 0); - } - - bool IsRoutable() const - { - return IsValid() && - !(IsRFC1918() || IsRFC3927() || IsLocal()); - } - - bool IsValid() const - { - // Clean up 3-byte shifted addresses caused by garbage in size field - // of addr messages from versions before 0.2.9 checksum. - // Two consecutive addr messages look like this: - // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26... - // so if the first length field is garbled, it reads the second batch - // of addr misaligned by 3 bytes. - if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0) - return false; - - return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX)); - } - - unsigned char GetByte(int n) const - { - return ((unsigned char*)&ip)[3-n]; - } - - std::string ToStringIPPort() const - { - return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); - } - - std::string ToStringIP() const - { - return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0)); - } - - std::string ToStringPort() const - { - return strprintf("%u", ntohs(port)); - } - - std::string ToString() const - { - return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); - } - - void print() const - { - printf("CAddress(%s)\n", ToString().c_str()); - } -}; - - - - - - - enum { MSG_TX = 1, diff --git a/src/protocol.cpp b/src/protocol.cpp index 0bb1da93e0..8d2dbfdd2d 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -4,6 +4,17 @@ // file license.txt or http://www.opensource.org/licenses/mit-license.php. #include "protocol.h" +#include "util.h" + +#ifndef __WXMSW__ +# include +#endif + +// Prototypes from net.h, but that header (currently) stinks, can't #include it without breaking things +bool Lookup(const char *pszName, std::vector& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); +bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); + +static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; CMessageHeader::CMessageHeader() { @@ -59,3 +70,182 @@ bool CMessageHeader::IsValid() const return true; } + +CAddress::CAddress() +{ + Init(); +} + +CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn) +{ + Init(); + ip = ipIn; + port = htons(portIn == 0 ? GetDefaultPort() : portIn); + nServices = nServicesIn; +} + +CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn) +{ + Init(); + ip = sockaddr.sin_addr.s_addr; + port = sockaddr.sin_port; + nServices = nServicesIn; +} + +CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn) +{ + Init(); + Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn); +} + +CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn) +{ + Init(); + Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true); +} + +CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn) +{ + Init(); + Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn); +} + +CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn) +{ + Init(); + Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true); +} + +void CAddress::Init() +{ + nServices = NODE_NETWORK; + memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); + ip = INADDR_NONE; + port = htons(GetDefaultPort()); + nTime = 100000000; + nLastTry = 0; +} + +bool operator==(const CAddress& a, const CAddress& b) +{ + return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 && + a.ip == b.ip && + a.port == b.port); +} + +bool operator!=(const CAddress& a, const CAddress& b) +{ + return (!(a == b)); +} + +bool operator<(const CAddress& a, const CAddress& b) +{ + int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)); + if (ret < 0) + return true; + else if (ret == 0) + { + if (ntohl(a.ip) < ntohl(b.ip)) + return true; + else if (a.ip == b.ip) + return ntohs(a.port) < ntohs(b.port); + } + return false; +} + +std::vector CAddress::GetKey() const +{ + CDataStream ss; + ss.reserve(18); + ss << FLATDATA(pchReserved) << ip << port; + + #if defined(_MSC_VER) && _MSC_VER < 1300 + return std::vector((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]); + #else + return std::vector(ss.begin(), ss.end()); + #endif +} + +struct sockaddr_in CAddress::GetSockAddr() const +{ + struct sockaddr_in sockaddr; + memset(&sockaddr, 0, sizeof(sockaddr)); + sockaddr.sin_family = AF_INET; + sockaddr.sin_addr.s_addr = ip; + sockaddr.sin_port = port; + return sockaddr; +} + +bool CAddress::IsIPv4() const +{ + return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0); +} + +bool CAddress::IsRFC1918() const +{ + return IsIPv4() && (GetByte(3) == 10 || + (GetByte(3) == 192 && GetByte(2) == 168) || + (GetByte(3) == 172 && + (GetByte(2) >= 16 && GetByte(2) <= 31))); +} + +bool CAddress::IsRFC3927() const +{ + return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254); +} + +bool CAddress::IsLocal() const +{ + return IsIPv4() && (GetByte(3) == 127 || + GetByte(3) == 0); +} + +bool CAddress::IsRoutable() const +{ + return IsValid() && + !(IsRFC1918() || IsRFC3927() || IsLocal()); +} + +bool CAddress::IsValid() const +{ + // Clean up 3-byte shifted addresses caused by garbage in size field + // of addr messages from versions before 0.2.9 checksum. + // Two consecutive addr messages look like this: + // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26... + // so if the first length field is garbled, it reads the second batch + // of addr misaligned by 3 bytes. + if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0) + return false; + + return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX)); +} + +unsigned char CAddress::GetByte(int n) const +{ + return ((unsigned char*)&ip)[3-n]; +} + +std::string CAddress::ToStringIPPort() const +{ + return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); +} + +std::string CAddress::ToStringIP() const +{ + return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0)); +} + +std::string CAddress::ToStringPort() const +{ + return strprintf("%u", ntohs(port)); +} + +std::string CAddress::ToString() const +{ + return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); +} + +void CAddress::print() const +{ + printf("CAddress(%s)\n", ToString().c_str()); +} diff --git a/src/protocol.h b/src/protocol.h index b5baeb2a68..009f67dfa1 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -13,6 +13,12 @@ #include "serialize.h" #include +extern bool fTestNet; +static inline unsigned short GetDefaultPort(const bool testnet = fTestNet) +{ + return testnet ? 18333 : 8333; +} + // // Message header // (4) message start @@ -49,4 +55,69 @@ class CMessageHeader unsigned int nChecksum; }; +enum +{ + NODE_NETWORK = (1 << 0), +}; + +class CAddress +{ + public: + CAddress(); + CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK); + explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK); + explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK); + explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK); + explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK); + explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK); + + void Init(); + + IMPLEMENT_SERIALIZE + ( + if (fRead) + const_cast(this)->Init(); + if (nType & SER_DISK) + READWRITE(nVersion); + if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH))) + READWRITE(nTime); + READWRITE(nServices); + READWRITE(FLATDATA(pchReserved)); // for IPv6 + READWRITE(ip); + READWRITE(port); + ) + + friend bool operator==(const CAddress& a, const CAddress& b); + friend bool operator!=(const CAddress& a, const CAddress& b); + friend bool operator<(const CAddress& a, const CAddress& b); + + std::vector GetKey() const; + struct sockaddr_in GetSockAddr() const; + bool IsIPv4() const; + bool IsRFC1918() const; + bool IsRFC3927() const; + bool IsLocal() const; + bool IsRoutable() const; + bool IsValid() const; + unsigned char GetByte(int n) const; + std::string ToStringIPPort() const; + std::string ToStringIP() const; + std::string ToStringPort() const; + std::string ToString() const; + void print() const; + + // TODO: make private (improves encapsulation) + public: + uint64 nServices; + unsigned char pchReserved[12]; + unsigned int ip; + unsigned short port; + + // disk and network only + unsigned int nTime; + + // memory only + unsigned int nLastTry; +}; + #endif // __INCLUDED_PROTOCOL_H__ -- cgit v1.2.3 From e4dde849ae5544383703ef2d73592677e6c528ad Mon Sep 17 00:00:00 2001 From: Giel van Schijndel Date: Thu, 11 Aug 2011 18:49:03 +0200 Subject: Move CInv to protocol.[ch]pp This commit does *not* and should not modify *any* code, it only moves it from net.h and splits it across protocol.cpp and protocol.hpp. Signed-off-by: Giel van Schijndel --- src/net.h | 80 -------------------------------------------------------- src/protocol.cpp | 61 ++++++++++++++++++++++++++++++++++++++++++ src/protocol.h | 27 +++++++++++++++++++ 3 files changed, 88 insertions(+), 80 deletions(-) (limited to 'src') diff --git a/src/net.h b/src/net.h index 66f3949b86..efac1f452a 100644 --- a/src/net.h +++ b/src/net.h @@ -17,7 +17,6 @@ #include "protocol.h" class CAddrDB; -class CInv; class CRequestTracker; class CNode; class CBlockIndex; @@ -52,85 +51,6 @@ enum MSG_BLOCK, }; -static const char* ppszTypeName[] = -{ - "ERROR", - "tx", - "block", -}; - -class CInv -{ -public: - int type; - uint256 hash; - - CInv() - { - type = 0; - hash = 0; - } - - CInv(int typeIn, const uint256& hashIn) - { - type = typeIn; - hash = hashIn; - } - - CInv(const std::string& strType, const uint256& hashIn) - { - int i; - for (i = 1; i < ARRAYLEN(ppszTypeName); i++) - { - if (strType == ppszTypeName[i]) - { - type = i; - break; - } - } - if (i == ARRAYLEN(ppszTypeName)) - throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str())); - hash = hashIn; - } - - IMPLEMENT_SERIALIZE - ( - READWRITE(type); - READWRITE(hash); - ) - - friend inline bool operator<(const CInv& a, const CInv& b) - { - return (a.type < b.type || (a.type == b.type && a.hash < b.hash)); - } - - bool IsKnownType() const - { - return (type >= 1 && type < ARRAYLEN(ppszTypeName)); - } - - const char* GetCommand() const - { - if (!IsKnownType()) - throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type)); - return ppszTypeName[type]; - } - - std::string ToString() const - { - return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str()); - } - - void print() const - { - printf("CInv(%s)\n", ToString().c_str()); - } -}; - - - - - class CRequestTracker { public: diff --git a/src/protocol.cpp b/src/protocol.cpp index 8d2dbfdd2d..48784b9cfb 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -15,6 +15,12 @@ bool Lookup(const char *pszName, std::vector& vaddr, int nServices, in bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; +static const char* ppszTypeName[] = +{ + "ERROR", + "tx", + "block", +}; CMessageHeader::CMessageHeader() { @@ -249,3 +255,58 @@ void CAddress::print() const { printf("CAddress(%s)\n", ToString().c_str()); } + +CInv::CInv() +{ + type = 0; + hash = 0; +} + +CInv::CInv(int typeIn, const uint256& hashIn) +{ + type = typeIn; + hash = hashIn; +} + +CInv::CInv(const std::string& strType, const uint256& hashIn) +{ + int i; + for (i = 1; i < ARRAYLEN(ppszTypeName); i++) + { + if (strType == ppszTypeName[i]) + { + type = i; + break; + } + } + if (i == ARRAYLEN(ppszTypeName)) + throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str())); + hash = hashIn; +} + +bool operator<(const CInv& a, const CInv& b) +{ + return (a.type < b.type || (a.type == b.type && a.hash < b.hash)); +} + +bool CInv::IsKnownType() const +{ + return (type >= 1 && type < ARRAYLEN(ppszTypeName)); +} + +const char* CInv::GetCommand() const +{ + if (!IsKnownType()) + throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type)); + return ppszTypeName[type]; +} + +std::string CInv::ToString() const +{ + return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str()); +} + +void CInv::print() const +{ + printf("CInv(%s)\n", ToString().c_str()); +} diff --git a/src/protocol.h b/src/protocol.h index 009f67dfa1..53d3eef4d5 100644 --- a/src/protocol.h +++ b/src/protocol.h @@ -12,6 +12,7 @@ #include "serialize.h" #include +#include "uint256.h" extern bool fTestNet; static inline unsigned short GetDefaultPort(const bool testnet = fTestNet) @@ -120,4 +121,30 @@ class CAddress unsigned int nLastTry; }; +class CInv +{ + public: + CInv(); + CInv(int typeIn, const uint256& hashIn); + CInv(const std::string& strType, const uint256& hashIn); + + IMPLEMENT_SERIALIZE + ( + READWRITE(type); + READWRITE(hash); + ) + + friend bool operator<(const CInv& a, const CInv& b); + + bool IsKnownType() const; + const char* GetCommand() const; + std::string ToString() const; + void print() const; + + // TODO: make private (improves encapsulation) + public: + int type; + uint256 hash; +}; + #endif // __INCLUDED_PROTOCOL_H__ -- cgit v1.2.3