1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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 char* ppszTypeName[] =
22 CMessageHeader::CMessageHeader()
24 memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
25 memset(pchCommand, 0, sizeof(pchCommand));
31 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
33 memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
34 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
35 nMessageSize = nMessageSizeIn;
39 std::string CMessageHeader::GetCommand() const
41 if (pchCommand[COMMAND_SIZE-1] == 0)
42 return std::string(pchCommand, pchCommand + strlen(pchCommand));
44 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
47 bool CMessageHeader::IsValid() const
50 if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
53 // Check the command string for errors
54 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
58 // Must be all zeros after the first zero
59 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
63 else if (*p1 < ' ' || *p1 > 0x7E)
68 if (nMessageSize > MAX_SIZE)
70 LogPrintf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
79 CAddress::CAddress() : CService()
84 CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
87 nServices = nServicesIn;
92 nServices = NODE_NETWORK;
103 CInv::CInv(int typeIn, const uint256& hashIn)
109 CInv::CInv(const std::string& strType, const uint256& hashIn)
112 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
114 if (strType == ppszTypeName[i])
120 if (i == ARRAYLEN(ppszTypeName))
121 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType));
125 bool operator<(const CInv& a, const CInv& b)
127 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
130 bool CInv::IsKnownType() const
132 return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
135 const char* CInv::GetCommand() const
138 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
139 return ppszTypeName[type];
142 std::string CInv::ToString() const
144 return strprintf("%s %s", GetCommand(), hash.ToString());
147 void CInv::print() const
149 LogPrintf("CInv(%s)\n", ToString());