Dash Core  0.12.2.1
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
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.
5 
6 #include "protocol.h"
7 
8 #include "util.h"
9 #include "utilstrencodings.h"
10 
11 #ifndef WIN32
12 # include <arpa/inet.h>
13 #endif
14 
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 *ALERT="alert";
32 const char *NOTFOUND="notfound";
33 const char *FILTERLOAD="filterload";
34 const char *FILTERADD="filteradd";
35 const char *FILTERCLEAR="filterclear";
36 const char *REJECT="reject";
37 const char *SENDHEADERS="sendheaders";
38 // Dash message types
39 const char *TXLOCKREQUEST="ix";
40 const char *TXLOCKVOTE="txlvote";
41 const char *SPORK="spork";
42 const char *GETSPORKS="getsporks";
43 const char *MASTERNODEPAYMENTVOTE="mnw";
44 const char *MASTERNODEPAYMENTBLOCK="mnwb";
45 const char *MASTERNODEPAYMENTSYNC="mnget";
46 const char *MNBUDGETSYNC="mnvs"; // depreciated since 12.1
47 const char *MNBUDGETVOTE="mvote"; // depreciated since 12.1
48 const char *MNBUDGETPROPOSAL="mprop"; // depreciated since 12.1
49 const char *MNBUDGETFINAL="fbs"; // depreciated since 12.1
50 const char *MNBUDGETFINALVOTE="fbvote"; // depreciated since 12.1
51 const char *MNQUORUM="mn quorum"; // not implemented
52 const char *MNANNOUNCE="mnb";
53 const char *MNPING="mnp";
54 const char *DSACCEPT="dsa";
55 const char *DSVIN="dsi";
56 const char *DSFINALTX="dsf";
57 const char *DSSIGNFINALTX="dss";
58 const char *DSCOMPLETE="dsc";
59 const char *DSSTATUSUPDATE="dssu";
60 const char *DSTX="dstx";
61 const char *DSQUEUE="dsq";
62 const char *DSEG="dseg";
63 const char *SYNCSTATUSCOUNT="ssc";
64 const char *MNGOVERNANCESYNC="govsync";
65 const char *MNGOVERNANCEOBJECT="govobj";
66 const char *MNGOVERNANCEOBJECTVOTE="govobjvote";
67 const char *MNVERIFY="mnv";
68 };
69 
70 static const char* ppszTypeName[] =
71 {
72  "ERROR", // Should never occur
75  "filtered block", // Should never occur
76  // Dash message types
77  // NOTE: include non-implmented here, we must keep this list in sync with enum in protocol.h
82  NetMsgType::MASTERNODEPAYMENTBLOCK, // reusing, was MNSCANERROR previousely, was NOT used in 12.0, we need this for inv
83  NetMsgType::MNBUDGETVOTE, // depreciated since 12.1
84  NetMsgType::MNBUDGETPROPOSAL, // depreciated since 12.1
85  NetMsgType::MNBUDGETFINAL, // depreciated since 12.1
86  NetMsgType::MNBUDGETFINALVOTE, // depreciated since 12.1
87  NetMsgType::MNQUORUM, // not implemented
94 };
95 
99 const static std::string allNetMessageTypes[] = {
122  // Dash message types
123  // NOTE: do NOT include non-implmented here, we want them to be "Unknown command" in ProcessMessage()
129  // NetMsgType::MASTERNODEPAYMENTBLOCK, // there is no message for this, only inventory
147 };
149 
150 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
151 {
152  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
153  memset(pchCommand, 0, sizeof(pchCommand));
154  nMessageSize = -1;
155  memset(pchChecksum, 0, CHECKSUM_SIZE);
156 }
157 
158 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
159 {
160  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
161  memset(pchCommand, 0, sizeof(pchCommand));
162  strncpy(pchCommand, pszCommand, COMMAND_SIZE);
163  nMessageSize = nMessageSizeIn;
164  memset(pchChecksum, 0, CHECKSUM_SIZE);
165 }
166 
167 std::string CMessageHeader::GetCommand() const
168 {
169  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
170 }
171 
172 bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
173 {
174  // Check start string
175  if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
176  return false;
177 
178  // Check the command string for errors
179  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
180  {
181  if (*p1 == 0)
182  {
183  // Must be all zeros after the first zero
184  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
185  if (*p1 != 0)
186  return false;
187  }
188  else if (*p1 < ' ' || *p1 > 0x7E)
189  return false;
190  }
191 
192  // Message size
193  if (nMessageSize > MAX_SIZE)
194  {
195  LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
196  return false;
197  }
198 
199  return true;
200 }
201 
202 
203 
205 {
206  Init();
207 }
208 
210 {
211  Init();
212  nServices = nServicesIn;
213 }
214 
216 {
218  nTime = 100000000;
219 }
220 
222 {
223  type = 0;
224  hash.SetNull();
225 }
226 
227 CInv::CInv(int typeIn, const uint256& hashIn)
228 {
229  type = typeIn;
230  hash = hashIn;
231 }
232 
233 CInv::CInv(const std::string& strType, const uint256& hashIn)
234 {
235  unsigned int i;
236  for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
237  {
238  if (strType == ppszTypeName[i])
239  {
240  type = i;
241  break;
242  }
243  }
244  if (i == ARRAYLEN(ppszTypeName))
245  throw std::out_of_range(strprintf("CInv::CInv(string, uint256): unknown type '%s'", strType));
246  hash = hashIn;
247 }
248 
249 bool operator<(const CInv& a, const CInv& b)
250 {
251  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
252 }
253 
254 bool CInv::IsKnownType() const
255 {
256  return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
257 }
258 
259 const char* CInv::GetCommand() const
260 {
261  if (!IsKnownType())
262  throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
263  return ppszTypeName[type];
264 }
265 
266 std::string CInv::ToString() const
267 {
268  try {
269  return strprintf("%s %s", GetCommand(), hash.ToString());
270  } catch(const std::out_of_range &) {
271  return strprintf("0x%08x %s", type, hash.ToString());
272  }
273 }
274 
275 const std::vector<std::string> &getAllNetMessageTypes()
276 {
277  return allNetMessageTypesVec;
278 }
const char * MNPING
Definition: protocol.cpp:53
const char * ADDR
Definition: protocol.cpp:18
const char * VERACK
Definition: protocol.cpp:17
const char * DSACCEPT
Definition: protocol.cpp:54
static const std::string allNetMessageTypes[]
Definition: protocol.cpp:99
ServiceFlags
Definition: protocol.h:253
const char * DSTX
Definition: protocol.cpp:60
void SetNull()
Definition: uint256.h:41
static const char * ppszTypeName[]
Definition: protocol.cpp:70
const char * MASTERNODEPAYMENTVOTE
Definition: protocol.cpp:43
#define MESSAGE_START_SIZE
Definition: protocol.h:21
const char * DSQUEUE
Definition: protocol.cpp:61
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:62
#define strprintf
Definition: tinyformat.h:1011
const char * MASTERNODEPAYMENTBLOCK
Definition: protocol.cpp:44
uint256 hash
Definition: protocol.h:339
const char * NOTFOUND
Definition: protocol.cpp:32
static const std::vector< std::string > allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes))
const char * BLOCK
Definition: protocol.cpp:26
const char * INV
Definition: protocol.cpp:19
const char * DSFINALTX
Definition: protocol.cpp:56
const char * MEMPOOL
Definition: protocol.cpp:28
const char * TX
Definition: protocol.cpp:24
const char * MNQUORUM
Definition: protocol.cpp:51
const char * SYNCSTATUSCOUNT
Definition: protocol.cpp:63
bool IsKnownType() const
Definition: protocol.cpp:254
const char * HEADERS
Definition: protocol.cpp:25
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:172
const char * FILTERLOAD
Definition: protocol.cpp:33
const char * MNVERIFY
Definition: protocol.cpp:67
const char * FILTERADD
Definition: protocol.cpp:34
const char * MNBUDGETSYNC
Definition: protocol.cpp:46
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:63
uint32_t nMessageSize
Definition: protocol.h:64
const char * GETBLOCKS
Definition: protocol.cpp:22
const char * REJECT
Definition: protocol.cpp:36
const char * DSEG
Definition: protocol.cpp:62
const char * TXLOCKVOTE
Definition: protocol.cpp:40
const char * SENDHEADERS
Definition: protocol.cpp:37
const char * GetCommand() const
Definition: protocol.cpp:259
const char * MNGOVERNANCESYNC
Definition: protocol.cpp:64
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:150
const char * ALERT
Definition: protocol.cpp:31
std::string GetCommand() const
Definition: protocol.cpp:167
#define LogPrintf(...)
Definition: util.h:98
CInv()
Definition: protocol.cpp:221
const char * GETSPORKS
Definition: protocol.cpp:42
bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:249
const char * MNBUDGETVOTE
Definition: protocol.cpp:47
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:275
const char * GETDATA
Definition: protocol.cpp:20
const char * FILTERCLEAR
Definition: protocol.cpp:35
std::string ToString() const
Definition: uint256.cpp:65
const char * PONG
Definition: protocol.cpp:30
ServiceFlags nServices
Definition: protocol.h:307
const char * MNGOVERNANCEOBJECT
Definition: protocol.cpp:65
const char * VERSION
Definition: protocol.cpp:16
const char * SPORK
Definition: protocol.cpp:41
const char * DSSIGNFINALTX
Definition: protocol.cpp:57
const char * MNBUDGETPROPOSAL
Definition: protocol.cpp:48
std::string ToString() const
Definition: protocol.cpp:266
const char * GETHEADERS
Definition: protocol.cpp:23
const char * TXLOCKREQUEST
Definition: protocol.cpp:39
const char * MNGOVERNANCEOBJECTVOTE
Definition: protocol.cpp:66
unsigned int nTime
Definition: protocol.h:310
#define ARRAYLEN(array)
const char * GETADDR
Definition: protocol.cpp:27
const char * MNBUDGETFINALVOTE
Definition: protocol.cpp:50
static const unsigned int MAX_SIZE
Definition: serialize.h:26
void * memcpy(void *a, const void *b, size_t c)
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
const char * PING
Definition: protocol.cpp:29
const char * MNBUDGETFINAL
Definition: protocol.cpp:49
int type
Definition: protocol.h:338
Definition: protocol.h:314
const char * MERKLEBLOCK
Definition: protocol.cpp:21
const char * MASTERNODEPAYMENTSYNC
Definition: protocol.cpp:45
const char * DSVIN
Definition: protocol.cpp:55
const char * DSCOMPLETE
Definition: protocol.cpp:58
const char * DSSTATUSUPDATE
Definition: protocol.cpp:59
const char * MNANNOUNCE
Definition: protocol.cpp:52
void Init()
Definition: protocol.cpp:215
uint8_t pchChecksum[CHECKSUM_SIZE]
Definition: protocol.h:65