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 // The message start string is designed to be unlikely to occur in normal data.
15 // The characters are rarely used upper ascii, not valid as UTF-8, and produce
16 // a large 4-byte int at any alignment.
18 static unsigned char pchMessageStartTestNew[4] = { 0xcd, 0xf2, 0xc0, 0xef };
19 static unsigned char pchMessageStartPPCoin[4] = { 0xe4, 0xe8, 0xe9, 0xe5 };
21 void GetMessageStart(unsigned char pchMessageStart[], bool fPersistent)
24 memcpy(pchMessageStart, pchMessageStartTestNew, sizeof(pchMessageStartTestNew));
26 memcpy(pchMessageStart, pchMessageStartPPCoin, sizeof(pchMessageStartPPCoin));
29 static const char* ppszTypeName[] =
36 CMessageHeader::CMessageHeader()
38 GetMessageStart(pchMessageStart);
39 memset(pchCommand, 0, sizeof(pchCommand));
45 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
47 GetMessageStart(pchMessageStart);
48 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
49 nMessageSize = nMessageSizeIn;
53 std::string CMessageHeader::GetCommand() const
55 if (pchCommand[COMMAND_SIZE-1] == 0)
56 return std::string(pchCommand, pchCommand + strlen(pchCommand));
58 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
61 bool CMessageHeader::IsValid() const
64 unsigned char pchMessageStartProtocol[4];
65 GetMessageStart(pchMessageStartProtocol);
66 if (memcmp(pchMessageStart, pchMessageStartProtocol, sizeof(pchMessageStart)) != 0)
69 // Check the command string for errors
70 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
74 // Must be all zeros after the first zero
75 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
79 else if (*p1 < ' ' || *p1 > 0x7E)
84 if (nMessageSize > MAX_SIZE)
86 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
95 CAddress::CAddress() : CService()
100 CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
103 nServices = nServicesIn;
106 void CAddress::Init()
108 nServices = NODE_NETWORK;
119 CInv::CInv(int typeIn, const uint256& hashIn)
125 CInv::CInv(const std::string& strType, const uint256& hashIn)
128 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
130 if (strType == ppszTypeName[i])
136 if (i == ARRAYLEN(ppszTypeName))
137 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
141 bool operator<(const CInv& a, const CInv& b)
143 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
146 bool CInv::IsKnownType() const
148 return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
151 const char* CInv::GetCommand() const
154 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
155 return ppszTypeName[type];
158 std::string CInv::ToString() const
160 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
163 void CInv::print() const
165 printf("CInv(%s)\n", ToString().c_str());