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.
11 # include <arpa/inet.h>
14 // Network ID, previously known as pchMessageStart
15 uint32_t nNetworkID = 0xe5e9e8e4;
17 static const std::vector<const char*> vpszTypeName = { "ERROR", "tx", "block" };
19 CMessageHeader::CMessageHeader() : nMessageSize(std::numeric_limits<uint32_t>::max()), nChecksum(0)
21 nNetworkID = ::nNetworkID;
22 memset(pchCommand, 0, sizeof(pchCommand));
26 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) : nMessageSize(nMessageSizeIn), nChecksum(0)
28 nNetworkID = ::nNetworkID;
29 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
32 std::string CMessageHeader::GetCommand() const
34 if (pchCommand[COMMAND_SIZE-1] == 0)
35 return std::string(pchCommand, pchCommand + strlen(pchCommand));
37 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
40 bool CMessageHeader::IsValid() const
43 if (nNetworkID != ::nNetworkID)
46 // Check the command string for errors
47 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
51 // Must be all zeros after the first zero
52 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
56 else if (*p1 < ' ' || *p1 > 0x7E)
61 if (nMessageSize > MAX_SIZE)
63 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
70 CAddress::CAddress() : CService(), nServices(NODE_NETWORK), nTime(100000000), nLastTry(0) { }
71 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn), nServices(nServicesIn), nTime(100000000), nLastTry(0) { }
72 CInv::CInv() : type(0), hash(0) { }
73 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) { }
74 CInv::CInv(const std::string& strType, const uint256& hashIn) : hash(hashIn)
77 for (i = 1; i < vpszTypeName.size(); ++i)
79 if (strType.compare(vpszTypeName[i]) == 0) {
84 if (i == vpszTypeName.size())
85 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
88 bool operator<(const CInv& a, const CInv& b)
90 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
93 bool CInv::IsKnownType() const
95 return (type >= 1 && type < (int)vpszTypeName.size());
98 const char* CInv::GetCommand() const
101 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
102 return vpszTypeName[type];
105 std::string CInv::ToString() const
107 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());