Introduce enum ServiceFlags for service flags
[bitcoinplatinum.git] / src / protocol.cpp
blob422ef6f6365129a0fc328684d767b961ab320d79
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "protocol.h"
8 #include "util.h"
9 #include "utilstrencodings.h"
11 #ifndef WIN32
12 # include <arpa/inet.h>
13 #endif
15 namespace NetMsgType {
16 const char *VERSION="version";
17 const char *VERACK="verack";
18 const char *ADDR="addr";
19 const char *INV="inv";
20 const char *GETDATA="getdata";
21 const char *MERKLEBLOCK="merkleblock";
22 const char *GETBLOCKS="getblocks";
23 const char *GETHEADERS="getheaders";
24 const char *TX="tx";
25 const char *HEADERS="headers";
26 const char *BLOCK="block";
27 const char *GETADDR="getaddr";
28 const char *MEMPOOL="mempool";
29 const char *PING="ping";
30 const char *PONG="pong";
31 const char *NOTFOUND="notfound";
32 const char *FILTERLOAD="filterload";
33 const char *FILTERADD="filteradd";
34 const char *FILTERCLEAR="filterclear";
35 const char *REJECT="reject";
36 const char *SENDHEADERS="sendheaders";
37 const char *FEEFILTER="feefilter";
40 static const char* ppszTypeName[] =
42 "ERROR", // Should never occur
43 NetMsgType::TX,
44 NetMsgType::BLOCK,
45 "filtered block" // Should never occur
48 /** All known message types. Keep this in the same order as the list of
49 * messages above and in protocol.h.
51 const static std::string allNetMessageTypes[] = {
52 NetMsgType::VERSION,
53 NetMsgType::VERACK,
54 NetMsgType::ADDR,
55 NetMsgType::INV,
56 NetMsgType::GETDATA,
57 NetMsgType::MERKLEBLOCK,
58 NetMsgType::GETBLOCKS,
59 NetMsgType::GETHEADERS,
60 NetMsgType::TX,
61 NetMsgType::HEADERS,
62 NetMsgType::BLOCK,
63 NetMsgType::GETADDR,
64 NetMsgType::MEMPOOL,
65 NetMsgType::PING,
66 NetMsgType::PONG,
67 NetMsgType::NOTFOUND,
68 NetMsgType::FILTERLOAD,
69 NetMsgType::FILTERADD,
70 NetMsgType::FILTERCLEAR,
71 NetMsgType::REJECT,
72 NetMsgType::SENDHEADERS,
73 NetMsgType::FEEFILTER
75 const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
77 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
79 memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
80 memset(pchCommand, 0, sizeof(pchCommand));
81 nMessageSize = -1;
82 nChecksum = 0;
85 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
87 memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
88 memset(pchCommand, 0, sizeof(pchCommand));
89 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
90 nMessageSize = nMessageSizeIn;
91 nChecksum = 0;
94 std::string CMessageHeader::GetCommand() const
96 return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
99 bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
101 // Check start string
102 if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
103 return false;
105 // Check the command string for errors
106 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
108 if (*p1 == 0)
110 // Must be all zeros after the first zero
111 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
112 if (*p1 != 0)
113 return false;
115 else if (*p1 < ' ' || *p1 > 0x7E)
116 return false;
119 // Message size
120 if (nMessageSize > MAX_SIZE)
122 LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
123 return false;
126 return true;
131 CAddress::CAddress() : CService()
133 Init();
136 CAddress::CAddress(CService ipIn, ServiceFlags nServicesIn) : CService(ipIn)
138 Init();
139 nServices = nServicesIn;
142 void CAddress::Init()
144 nServices = NODE_NONE;
145 nTime = 100000000;
148 CInv::CInv()
150 type = 0;
151 hash.SetNull();
154 CInv::CInv(int typeIn, const uint256& hashIn)
156 type = typeIn;
157 hash = hashIn;
160 CInv::CInv(const std::string& strType, const uint256& hashIn)
162 unsigned int i;
163 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
165 if (strType == ppszTypeName[i])
167 type = i;
168 break;
171 if (i == ARRAYLEN(ppszTypeName))
172 throw std::out_of_range(strprintf("CInv::CInv(string, uint256): unknown type '%s'", strType));
173 hash = hashIn;
176 bool operator<(const CInv& a, const CInv& b)
178 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
181 bool CInv::IsKnownType() const
183 return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
186 const char* CInv::GetCommand() const
188 if (!IsKnownType())
189 throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
190 return ppszTypeName[type];
193 std::string CInv::ToString() const
195 return strprintf("%s %s", GetCommand(), hash.ToString());
198 const std::vector<std::string> &getAllNetMessageTypes()
200 return allNetMessageTypesVec;