PRCYCoin  2.0.0.7rc1
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 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.
5 
6 #include "protocol.h"
7 
8 #include "chainparams.h"
9 #include "util.h"
10 #include "utilstrencodings.h"
11 
12 #ifndef WIN32
13 #include <arpa/inet.h>
14 #endif
15 
16 namespace NetMsgType {
17 const char *VERSION="version";
18 const char *VERACK="verack";
19 const char *ADDR="addr";
20 const char *INV="inv";
21 const char *GETDATA="getdata";
22 const char *MERKLEBLOCK="merkleblock";
23 const char *GETBLOCKS="getblocks";
24 const char *GETHEADERS="getheaders";
25 const char *TX="tx";
26 const char *DSC="dsc";
27 const char *DSF="dsf";
28 const char *DSQ="dsq";
29 const char *DSR="dsr";
30 const char *DSTX="dstx";
31 const char *DSEE="dsee";
32 const char *DSEG="dseg";
33 const char *DSEEP="dseep";
34 const char *DSSU="dssu";
35 const char *HEADERS="headers";
36 const char *BLOCK="block";
37 const char *GETADDR="getaddr";
38 const char *MEMPOOL="mempool";
39 const char *PING="ping";
40 const char *PONG="pong";
41 const char *ALERT="alert";
42 const char *NOTFOUND="notfound";
43 const char *FILTERLOAD="filterload";
44 const char *FILTERADD="filteradd";
45 const char *FILTERCLEAR="filterclear";
46 const char *REJECT="reject";
47 const char *SENDHEADERS="sendheaders";
48 const char *IX="ix";
49 const char *IXLOCKVOTE="txlvote";
50 const char *MNBROADCAST="mnb";
51 const char *MNPING="mnp";
52 const char *MNWINNER="mnw";
53 const char *GETMNWINNERS="mnget";
54 const char *BUDGETPROPOSAL="mprop";
55 const char *BUDGETVOTE="mvote";
56 const char *BUDGETVOTESYNC="mnvs";
57 const char *FINALBUDGET="fbs";
58 const char *FINALBUDGETVOTE="fbvote";
59 const char *SYNCSTATUSCOUNT="ssc";
60 };
61 
62 static const char* ppszTypeName[] =
63 {
64  "ERROR", // Should never occur
76  "filtered block", // Should never occur
87 };
88 
92 const static std::string allNetMessageTypes[] = {
135 };
136 const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
137 
139 {
140  memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
141  memset(pchCommand, 0, sizeof(pchCommand));
142  nMessageSize = -1;
143  nChecksum = 0;
144 }
145 
146 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
147 {
148  memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
149  memset(pchCommand, 0, sizeof(pchCommand));
150  strncpy(pchCommand, pszCommand, COMMAND_SIZE);
151  nMessageSize = nMessageSizeIn;
152  nChecksum = 0;
153 }
154 
155 std::string CMessageHeader::GetCommand() const
156 {
157  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
158 }
159 
161 {
162  // Check start string
163  if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
164  return false;
165 
166  // Check the command string for errors
167  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++) {
168  if (*p1 == 0) {
169  // Must be all zeros after the first zero
170  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
171  if (*p1 != 0)
172  return false;
173  } else if (*p1 < ' ' || *p1 > 0x7E)
174  return false;
175  }
176 
177  // Message size
178  if (nMessageSize > MAX_SIZE) {
179  LogPrintf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
180  return false;
181  }
182 
183  return true;
184 }
185 
186 
188 {
189  Init();
190 }
191 
193 {
194  Init();
195  nServices = nServicesIn;
196 }
197 
199 {
201  nTime = 100000000;
202 }
203 
205 {
206  type = 0;
207  hash.SetNull();
208 }
209 
210 CInv::CInv(int typeIn, const uint256& hashIn)
211 {
212  type = typeIn;
213  hash = hashIn;
214 }
215 
216 CInv::CInv(const std::string& strType, const uint256& hashIn)
217 {
218  unsigned int i;
219  for (i = 1; i < ARRAYLEN(ppszTypeName); i++) {
220  if (strType == ppszTypeName[i]) {
221  type = i;
222  break;
223  }
224  }
225  if (i == ARRAYLEN(ppszTypeName))
226  LogPrint(BCLog::NET, "CInv::CInv(string, uint256) : unknown type '%s'", strType);
227  hash = hashIn;
228 }
229 
230 bool operator<(const CInv& a, const CInv& b)
231 {
232  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
233 }
234 
235 bool CInv::IsKnownType() const
236 {
237  return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
238 }
239 
241  return (type >= 6);
242 }
243 
244 const char* CInv::GetCommand() const
245 {
246  if (!IsKnownType()) {
247  LogPrint(BCLog::NET, "CInv::GetCommand() : type=%d unknown type", type);
248  return "UNKNOWN";
249  }
250 
251  return ppszTypeName[type];
252 }
253 
254 std::string CInv::ToString() const
255 {
256  return strprintf("%s %s", GetCommand(), hash.ToString());
257 }
258 
259 const std::vector<std::string> &getAllNetMessageTypes()
260 {
261  return allNetMessageTypesVec;
262 }
ARRAYLEN
#define ARRAYLEN(array)
Definition: utilstrencodings.h:21
CMessageHeader::pchCommand
char pchCommand[COMMAND_SIZE]
Definition: protocol.h:61
CService
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:133
NetMsgType
Bitcoin protocol message types.
Definition: protocol.cpp:16
NetMsgType::DSR
const char * DSR
Definition: protocol.cpp:29
NetMsgType::DSEE
const char * DSEE
Definition: protocol.cpp:31
CInv::type
int type
Definition: protocol.h:383
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
NetMsgType::FINALBUDGET
const char * FINALBUDGET
The finalbudget message is used to broadcast or relay finalized budget metadata to connected peers.
Definition: protocol.cpp:57
CAddress::CAddress
CAddress()
Definition: protocol.cpp:187
NetMsgType::PING
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected.
Definition: protocol.cpp:39
NetMsgType::FILTERLOAD
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:43
NetMsgType::IX
const char * IX
The ix message transmits a single SwiftX transaction.
Definition: protocol.cpp:48
operator<
bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:230
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:15
CAddress::nTime
unsigned int nTime
Definition: protocol.h:354
ServiceFlags
ServiceFlags
nServices flags
Definition: protocol.h:296
CMessageHeader::GetCommand
std::string GetCommand() const
Definition: protocol.cpp:155
CAddress::nServices
ServiceFlags nServices
Definition: protocol.h:351
CInv
inv message data
Definition: protocol.h:358
chainparams.h
NetMsgType::PONG
const char * PONG
The pong message replies to a ping message, proving to the pinging node that the ponging node is stil...
Definition: protocol.cpp:40
NetMsgType::MNWINNER
const char * MNWINNER
The mnwinner message is used to relay and distribute consensus for masternode payout ordering.
Definition: protocol.cpp:52
NetMsgType::BUDGETVOTESYNC
const char * BUDGETVOTESYNC
The budgetvotesync message is used to request budget vote data from connected peers.
Definition: protocol.cpp:56
CInv::IsMasterNodeType
bool IsMasterNodeType() const
Definition: protocol.cpp:240
protocol.h
CInv::GetCommand
const char * GetCommand() const
Definition: protocol.cpp:244
NetMsgType::INV
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:20
NetMsgType::GETHEADERS
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:24
NetMsgType::DSEG
const char * DSEG
Definition: protocol.cpp:32
CMessageHeader::nChecksum
unsigned int nChecksum
Definition: protocol.h:63
NetMsgType::IXLOCKVOTE
const char * IXLOCKVOTE
The ixlockvote message is used to reach consensus for SwiftX transaction locks.
Definition: protocol.cpp:49
NetMsgType::HEADERS
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:35
CAddress::Init
void Init()
Definition: protocol.cpp:198
LogPrintf
#define LogPrintf(...)
Definition: logging.h:147
NetMsgType::GETMNWINNERS
const char * GETMNWINNERS
The getmnwinners message is used to request winning masternode data from connected peers.
Definition: protocol.cpp:53
NetMsgType::FINALBUDGETVOTE
const char * FINALBUDGETVOTE
The finalbudgetvote message is used to broadcast or relay finalized budget votes to connected peers.
Definition: protocol.cpp:58
strnlen
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
NetMsgType::FILTERCLEAR
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter.
Definition: protocol.cpp:45
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
NetMsgType::SENDHEADERS
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:47
LogPrint
#define LogPrint(category,...)
Definition: logging.h:162
NetMsgType::REJECT
const char * REJECT
The reject message informs the receiving node that one of its previous messages has been rejected.
Definition: protocol.cpp:46
NetMsgType::DSQ
const char * DSQ
Definition: protocol.cpp:28
CInv::IsKnownType
bool IsKnownType() const
Definition: protocol.cpp:235
MESSAGE_START_SIZE
#define MESSAGE_START_SIZE
Definition: protocol.h:21
NetMsgType::GETBLOCKS
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:23
NetMsgType::DSSU
const char * DSSU
Definition: protocol.cpp:34
CMessageHeader::CMessageHeader
CMessageHeader()
Definition: protocol.cpp:138
NetMsgType::GETDATA
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:21
NetMsgType::NOTFOUND
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:42
NODE_NONE
@ NODE_NONE
Definition: protocol.h:298
NetMsgType::ADDR
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:19
NetMsgType::DSF
const char * DSF
Definition: protocol.cpp:27
NetMsgType::BUDGETPROPOSAL
const char * BUDGETPROPOSAL
The budgetproposal message is used to broadcast or relay budget proposal metadata to connected peers.
Definition: protocol.cpp:54
NetMsgType::DSC
const char * DSC
We are keeping these for now.
Definition: protocol.cpp:26
base_uint::SetNull
void SetNull()
Definition: arith_uint256.h:308
NetMsgType::BLOCK
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:36
strprintf
#define strprintf
Definition: tinyformat.h:1056
NetMsgType::MEMPOOL
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:38
NetMsgType::ALERT
const char * ALERT
The alert message warns nodes of problems that may affect them or the rest of the network.
Definition: protocol.cpp:41
CInv::CInv
CInv()
Definition: protocol.cpp:204
NetMsgType::TX
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:25
CInv::hash
uint256 hash
Definition: protocol.h:384
NetMsgType::BUDGETVOTE
const char * BUDGETVOTE
The budgetvote message is used to broadcast or relay budget proposal votes to connected peers.
Definition: protocol.cpp:55
NetMsgType::DSTX
const char * DSTX
Definition: protocol.cpp:30
CMessageHeader::pchMessageStart
char pchMessageStart[MESSAGE_START_SIZE]
Definition: protocol.h:60
Params
const CChainParams & Params()
Return the currently selected parameters.
Definition: chainparams.cpp:463
NetMsgType::SYNCSTATUSCOUNT
const char * SYNCSTATUSCOUNT
The syncstatuscount message is used to track the layer 2 syncing process.
Definition: protocol.cpp:59
getAllNetMessageTypes
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:259
NetMsgType::VERACK
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:18
CMessageHeader::nMessageSize
unsigned int nMessageSize
Definition: protocol.h:62
NetMsgType::FILTERADD
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:44
utilstrencodings.h
CMessageHeader::IsValid
bool IsValid() const
Definition: protocol.cpp:160
BCLog::NET
@ NET
Definition: logging.h:40
NetMsgType::VERSION
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:17
NetMsgType::MNPING
const char * MNPING
The mnping message is used to ensure a masternode is still active.
Definition: protocol.cpp:51
NetMsgType::DSEEP
const char * DSEEP
Definition: protocol.cpp:33
util.h
NetMsgType::MNBROADCAST
const char * MNBROADCAST
The mnbroadcast message is used to broadcast masternode startup data to connected peers.
Definition: protocol.cpp:50
CInv::ToString
std::string ToString() const
Definition: protocol.cpp:254
NetMsgType::GETADDR
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:37
NetMsgType::MERKLEBLOCK
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:22
CMessageHeader::COMMAND_SIZE
@ COMMAND_SIZE
Definition: protocol.h:52
base_uint::ToString
std::string ToString() const
Definition: arith_uint256.cpp:199