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 // Public testnet message start
19 // unsigned char pchMessageStartTestBitcoin[4] = { 0xfa, 0xbf, 0xb5, 0xda };
20 static unsigned char pchMessageStartTestOld[4] = { 0xdb, 0xe1, 0xf2, 0xf6 };
21 static unsigned char pchMessageStartTestNew[4] = { 0xcb, 0xf2, 0xc0, 0xef };
22 static unsigned int nMessageStartTestSwitchTime = 1346200000;
24 // PPCoin message start (switch from Bitcoin's in v0.2)
25 static unsigned char pchMessageStartBitcoin[4] = { 0xf9, 0xbe, 0xb4, 0xd9 };
26 static unsigned char pchMessageStartPPCoin[4] = { 0xe6, 0xe8, 0xe9, 0xe5 };
27 static unsigned int nMessageStartSwitchTime = 1347300000;
29 void GetMessageStart(unsigned char pchMessageStart[], bool fPersistent)
32 memcpy(pchMessageStart, (fPersistent || GetAdjustedTime() > nMessageStartTestSwitchTime)? pchMessageStartTestNew : pchMessageStartTestOld, sizeof(pchMessageStartTestNew));
34 memcpy(pchMessageStart, (fPersistent || GetAdjustedTime() > nMessageStartSwitchTime)? pchMessageStartPPCoin : pchMessageStartBitcoin, sizeof(pchMessageStartPPCoin));
37 static const char* ppszTypeName[] =
44 CMessageHeader::CMessageHeader()
46 GetMessageStart(pchMessageStart);
47 memset(pchCommand, 0, sizeof(pchCommand));
53 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
55 GetMessageStart(pchMessageStart);
56 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
57 nMessageSize = nMessageSizeIn;
61 std::string CMessageHeader::GetCommand() const
63 if (pchCommand[COMMAND_SIZE-1] == 0)
64 return std::string(pchCommand, pchCommand + strlen(pchCommand));
66 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
69 bool CMessageHeader::IsValid() const
72 unsigned char pchMessageStartProtocol[4];
73 GetMessageStart(pchMessageStartProtocol);
74 if (memcmp(pchMessageStart, pchMessageStartProtocol, sizeof(pchMessageStart)) != 0)
77 // Check the command string for errors
78 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
82 // Must be all zeros after the first zero
83 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
87 else if (*p1 < ' ' || *p1 > 0x7E)
92 if (nMessageSize > MAX_SIZE)
94 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
103 CAddress::CAddress() : CService()
108 CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
111 nServices = nServicesIn;
114 void CAddress::Init()
116 nServices = NODE_NETWORK;
127 CInv::CInv(int typeIn, const uint256& hashIn)
133 CInv::CInv(const std::string& strType, const uint256& hashIn)
136 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
138 if (strType == ppszTypeName[i])
144 if (i == ARRAYLEN(ppszTypeName))
145 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
149 bool operator<(const CInv& a, const CInv& b)
151 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
154 bool CInv::IsKnownType() const
156 return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
159 const char* CInv::GetCommand() const
162 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
163 return ppszTypeName[type];
166 std::string CInv::ToString() const
168 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
171 void CInv::print() const
173 printf("CInv(%s)\n", ToString().c_str());