1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 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.
15 # include <arpa/inet.h>
18 bool fTestNet = false;
20 static const char* ppszTypeName[] =
27 unsigned char pchMessageStart[4] = { 0xe4, 0xe8, 0xe9, 0xe5 };
29 CMessageHeader::CMessageHeader()
31 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
32 memset(pchCommand, 0, sizeof(pchCommand));
38 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
40 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
41 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
42 nMessageSize = nMessageSizeIn;
46 std::string CMessageHeader::GetCommand() const
48 if (pchCommand[COMMAND_SIZE-1] == 0)
49 return std::string(pchCommand, pchCommand + strlen(pchCommand));
51 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
54 bool CMessageHeader::IsValid() const
57 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
60 // Check the command string for errors
61 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
65 // Must be all zeros after the first zero
66 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
70 else if (*p1 < ' ' || *p1 > 0x7E)
75 if (nMessageSize > MAX_SIZE)
77 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
86 CAddress::CAddress() : CService()
91 CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
94 nServices = nServicesIn;
99 nServices = NODE_NETWORK;
103 void CAddress::print() const
105 printf("CAddress(%s)\n", ToString().c_str());
114 CInv::CInv(int typeIn, const uint256& hashIn)
120 CInv::CInv(const std::string& strType, const uint256& hashIn)
123 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
125 if (strType == ppszTypeName[i])
131 if (i == ARRAYLEN(ppszTypeName))
132 throw std::out_of_range("CInv::CInv(string, uint256) : unknown type");
136 bool operator<(const CInv& a, const CInv& b)
138 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
141 bool CInv::IsKnownType() const
143 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
146 const char* CInv::GetCommand() const
149 throw std::out_of_range("CInv::GetCommand() : unknown type");
150 return ppszTypeName[type];
153 std::string CInv::ToString() const
158 void CInv::print() const