1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
12 // Network ID, previously known as pchMessageStart
13 uint32_t nNetworkID = 0xe5e9e8e4;
15 static const vector<const char*> vpszTypeName = { "ERROR", "tx", "block" };
17 CMessageHeader::CMessageHeader() : nNetworkID(::nNetworkID), nMessageSize(numeric_limits<uint32_t>::max()), nChecksum(0)
19 fill(begin(pchCommand), end(pchCommand), '\0');
23 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) : nNetworkID(::nNetworkID), nMessageSize(nMessageSizeIn), nChecksum(0)
25 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
28 string CMessageHeader::GetCommand() const
30 if (pchCommand[COMMAND_SIZE-1] == 0)
31 return string(pchCommand, pchCommand + strlen(pchCommand));
33 return string(pchCommand, pchCommand + COMMAND_SIZE);
36 bool CMessageHeader::IsValid() const
39 if (nNetworkID != ::nNetworkID)
42 // Check the command string for errors
43 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
47 // Must be all zeros after the first zero
48 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
52 else if (*p1 < ' ' || *p1 > 0x7E)
57 if (nMessageSize > MAX_SIZE)
59 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
66 CAddress::CAddress() : CService(), nServices(NODE_NETWORK), nTime(100000000), nLastTry(0) { }
67 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn), nServices(nServicesIn), nTime(100000000), nLastTry(0) { }
69 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) { }
70 CInv::CInv(const string& strType, const uint256& hashIn) : hash(hashIn)
73 for (i = 1; i < vpszTypeName.size(); ++i)
75 if (strType.compare(vpszTypeName[i]) == 0) {
80 if (i == vpszTypeName.size())
81 throw out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
84 bool operator<(const CInv& a, const CInv& b)
86 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
89 bool CInv::IsKnownType() const
91 return (type >= 1 && type < (int)vpszTypeName.size());
94 const char* CInv::GetCommand() const
97 throw out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
98 return vpszTypeName[type];
101 string CInv::ToString() const
103 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());