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 license.txt or http://www.opensource.org/licenses/mit-license.php.
11 # include <arpa/inet.h>
14 static const char* ppszTypeName[] =
21 CMessageHeader::CMessageHeader()
23 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
24 memset(pchCommand, 0, sizeof(pchCommand));
30 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
32 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
33 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
34 nMessageSize = nMessageSizeIn;
38 std::string CMessageHeader::GetCommand() const
40 if (pchCommand[COMMAND_SIZE-1] == 0)
41 return std::string(pchCommand, pchCommand + strlen(pchCommand));
43 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
46 bool CMessageHeader::IsValid() const
49 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
52 // Check the command string for errors
53 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
57 // Must be all zeros after the first zero
58 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
62 else if (*p1 < ' ' || *p1 > 0x7E)
67 if (nMessageSize > MAX_SIZE)
69 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
78 CAddress::CAddress() : CService()
83 CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
86 nServices = nServicesIn;
91 nServices = NODE_NETWORK;
102 CInv::CInv(int typeIn, const uint256& hashIn)
108 CInv::CInv(const std::string& strType, const uint256& hashIn)
111 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
113 if (strType == ppszTypeName[i])
119 if (i == ARRAYLEN(ppszTypeName))
120 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
124 bool operator<(const CInv& a, const CInv& b)
126 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
129 bool CInv::IsKnownType() const
131 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
134 const char* CInv::GetCommand() const
137 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
138 return ppszTypeName[type];
141 std::string CInv::ToString() const
143 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
146 void CInv::print() const
148 printf("CInv(%s)\n", ToString().c_str());