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::string forfill[] = { "ERROR", "tx", "block" }; //TODO: Replace with initializer list constructor when c++11 comes
15 static const std::vector<std::string> vpszTypeName(forfill, forfill + 3);
17 CMessageHeader::CMessageHeader() : nMessageSize(std::numeric_limits<uint32_t>::max()), nChecksum(0)
19 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
20 memset(pchCommand, 0, sizeof(pchCommand));
24 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) : nMessageSize(nMessageSizeIn), nChecksum(0)
26 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
27 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
30 std::string CMessageHeader::GetCommand() const
32 if (pchCommand[COMMAND_SIZE-1] == 0)
33 return std::string(pchCommand, pchCommand + strlen(pchCommand));
35 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
38 bool CMessageHeader::IsValid() const
41 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
44 // Check the command string for errors
45 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
49 // Must be all zeros after the first zero
50 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
54 else if (*p1 < ' ' || *p1 > 0x7E)
59 if (nMessageSize > MAX_SIZE)
61 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
68 CAddress::CAddress() : CService(), nServices(NODE_NETWORK), nTime(100000000), nLastTry(0) { }
69 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn), nServices(nServicesIn), nTime(100000000), nLastTry(0) { }
70 CInv::CInv() : type(0), hash(0) { }
71 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) { }
72 CInv::CInv(const std::string& strType, const uint256& hashIn) : hash(hashIn)
75 for (i = 1; i < vpszTypeName.size(); ++i)
77 if (strType.compare(vpszTypeName[i]) == 0) {
82 if (i == vpszTypeName.size())
83 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
86 bool operator<(const CInv& a, const CInv& b)
88 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
91 bool CInv::IsKnownType() const
93 return (type >= 1 && type < (int)vpszTypeName.size());
96 const char* CInv::GetCommand() const
99 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
100 return vpszTypeName[type].c_str();
103 std::string CInv::ToString() const
105 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());