1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Copyright (c) 2013 NovaCoin Developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
12 # include <arpa/inet.h>
15 // The message start string is designed to be unlikely to occur in normal data.
16 // The characters are rarely used upper ascii, not valid as UTF-8, and produce
17 // a large 4-byte int at any alignment.
19 static unsigned char pchMessageStartTestNew[4] = { 0xcd, 0xf2, 0xc0, 0xef };
20 static unsigned char pchMessageStartPPCoin[4] = { 0xe4, 0xe8, 0xe9, 0xe5 };
22 void GetMessageStart(unsigned char pchMessageStart[], bool fPersistent)
25 memcpy(pchMessageStart, pchMessageStartTestNew, sizeof(pchMessageStartTestNew));
27 memcpy(pchMessageStart, pchMessageStartPPCoin, sizeof(pchMessageStartPPCoin));
30 static const char* ppszTypeName[] =
37 CMessageHeader::CMessageHeader()
39 GetMessageStart(pchMessageStart);
40 memset(pchCommand, 0, sizeof(pchCommand));
46 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
48 GetMessageStart(pchMessageStart);
49 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
50 nMessageSize = nMessageSizeIn;
54 std::string CMessageHeader::GetCommand() const
56 if (pchCommand[COMMAND_SIZE-1] == 0)
57 return std::string(pchCommand, pchCommand + strlen(pchCommand));
59 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
62 bool CMessageHeader::IsValid() const
65 unsigned char pchMessageStartProtocol[4];
66 GetMessageStart(pchMessageStartProtocol);
67 if (memcmp(pchMessageStart, pchMessageStartProtocol, sizeof(pchMessageStart)) != 0)
70 // Check the command string for errors
71 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
75 // Must be all zeros after the first zero
76 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
80 else if (*p1 < ' ' || *p1 > 0x7E)
85 if (nMessageSize > MAX_SIZE)
87 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
96 CAddress::CAddress() : CService()
101 CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
104 nServices = nServicesIn;
107 void CAddress::Init()
109 nServices = NODE_NETWORK;
120 CInv::CInv(int typeIn, const uint256& hashIn)
126 CInv::CInv(const std::string& strType, const uint256& hashIn)
129 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
131 if (strType == ppszTypeName[i])
137 if (i == ARRAYLEN(ppszTypeName))
138 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
142 bool operator<(const CInv& a, const CInv& b)
144 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
147 bool CInv::IsKnownType() const
149 return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
152 const char* CInv::GetCommand() const
155 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
156 return ppszTypeName[type];
159 std::string CInv::ToString() const
161 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
164 void CInv::print() const
166 printf("CInv(%s)\n", ToString().c_str());