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 static const std::vector<const char*> vpszTypeName = { "ERROR", "tx", "block" };
16 CMessageHeader::CMessageHeader() : nMessageSize(std::numeric_limits<uint32_t>::max()), nChecksum(0)
18 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
19 memset(pchCommand, 0, sizeof(pchCommand));
23 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) : nMessageSize(nMessageSizeIn), nChecksum(0)
25 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
26 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
29 std::string CMessageHeader::GetCommand() const
31 if (pchCommand[COMMAND_SIZE-1] == 0)
32 return std::string(pchCommand, pchCommand + strlen(pchCommand));
34 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
37 bool CMessageHeader::IsValid() const
40 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
43 // Check the command string for errors
44 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
48 // Must be all zeros after the first zero
49 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
53 else if (*p1 < ' ' || *p1 > 0x7E)
58 if (nMessageSize > MAX_SIZE)
60 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
67 CAddress::CAddress() : CService(), nServices(NODE_NETWORK), nTime(100000000), nLastTry(0) { }
68 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn), nServices(nServicesIn), nTime(100000000), nLastTry(0) { }
69 CInv::CInv() : type(0), hash(0) { }
70 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) { }
71 CInv::CInv(const std::string& strType, const uint256& hashIn) : hash(hashIn)
74 for (i = 1; i < vpszTypeName.size(); ++i)
76 if (strType.compare(vpszTypeName[i]) == 0) {
81 if (i == vpszTypeName.size())
82 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
85 bool operator<(const CInv& a, const CInv& b)
87 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
90 bool CInv::IsKnownType() const
92 return (type >= 1 && type < (int)vpszTypeName.size());
95 const char* CInv::GetCommand() const
98 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
99 return vpszTypeName[type];
102 std::string CInv::ToString() const
104 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());