Raven Core  3.0.0
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 The Bitcoin Core developers
3 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "protocol.h"
8 
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 *HEADERS="headers";
27 const char *BLOCK="block";
28 const char *GETADDR="getaddr";
29 const char *MEMPOOL="mempool";
30 const char *PING="ping";
31 const char *PONG="pong";
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 const char *FEEFILTER="feefilter";
39 const char *SENDCMPCT="sendcmpct";
40 const char *CMPCTBLOCK="cmpctblock";
41 const char *GETBLOCKTXN="getblocktxn";
42 const char *BLOCKTXN="blocktxn";
43 const char *GETASSETDATA="getassetdata";
44 const char *ASSETDATA="assetdata";
45 const char *ASSETNOTFOUND ="asstnotfound";
46 } // namespace NetMsgType
47 
51 const static std::string allNetMessageTypes[] = {
81 };
82 const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
83 
84 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
85 {
86  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
87  memset(pchCommand, 0, sizeof(pchCommand));
88  nMessageSize = -1;
89  memset(pchChecksum, 0, CHECKSUM_SIZE);
90 }
91 
92 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
93 {
94  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
95  memset(pchCommand, 0, sizeof(pchCommand));
96  strncpy(pchCommand, pszCommand, COMMAND_SIZE);
97  nMessageSize = nMessageSizeIn;
98  memset(pchChecksum, 0, CHECKSUM_SIZE);
99 }
100 
101 std::string CMessageHeader::GetCommand() const
102 {
103  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
104 }
105 
106 bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
107 {
108  // Check start string
109  if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
110  return false;
111 
112  // Check the command string for errors
113  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
114  {
115  if (*p1 == 0)
116  {
117  // Must be all zeros after the first zero
118  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
119  if (*p1 != 0)
120  return false;
121  }
122  else if (*p1 < ' ' || *p1 > 0x7E)
123  return false;
124  }
125 
126  // Message size
127  if (nMessageSize > MAX_SIZE)
128  {
129  LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
130  return false;
131  }
132 
133  return true;
134 }
135 
136 
137 
139 {
140  Init();
141 }
142 
144 {
145  Init();
146  nServices = nServicesIn;
147 }
148 
150 {
152  nTime = 100000000;
153 }
154 
156 {
157  type = 0;
158  hash.SetNull();
159 }
160 
161 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
162 
163 bool operator<(const CInv& a, const CInv& b)
164 {
165  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
166 }
167 
168 std::string CInv::GetCommand() const
169 {
170  std::string cmd;
171  if (type & MSG_WITNESS_FLAG)
172  cmd.append("witness-");
173  int masked = type & MSG_TYPE_MASK;
174  switch (masked)
175  {
176  case MSG_TX: return cmd.append(NetMsgType::TX);
177  case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
178  case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
179  case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
180  default:
181  throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
182  }
183 }
184 
185 std::string CInv::ToString() const
186 {
187  try {
188  return strprintf("%s %s", GetCommand(), hash.ToString());
189  } catch(const std::out_of_range &) {
190  return strprintf("0x%08x %s", type, hash.ToString());
191  }
192 }
193 
194 const std::vector<std::string> &getAllNetMessageTypes()
195 {
196  return allNetMessageTypesVec;
197 }
198 
200 {
201  name = "";
202 }
203 
204 CInvAsset::CInvAsset(std::string strName) : name(strName){}
205 
206 bool operator<(const CInvAsset& a, const CInvAsset& b)
207 {
208  return a.name < b.name;
209 }
210 
211 std::string CInvAsset::ToString() const
212 {
213  return strprintf("%s %s", "CInvAsset for asset: ", name);
214 }
215 
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:30
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:33
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
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
ServiceFlags
nServices flags
Definition: protocol.h:271
const char * ASSETNOTFOUND
The asstnotfound message is a reply to a getassetdata message which requested an object the receiving...
Definition: protocol.cpp:45
std::string GetCommand() const
Definition: protocol.cpp:168
std::string ToString() const
Definition: protocol.cpp:185
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:28
void Init()
Definition: protocol.cpp:149
Defined in BIP152.
Definition: protocol.h:390
#define strprintf
Definition: tinyformat.h:1054
const char * GETASSETDATA
Contains a AssetDataRequest.
Definition: protocol.cpp:43
inv message data
Definition: protocol.h:397
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:39
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:84
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:376
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:377
std::string GetCommand() const
Definition: protocol.cpp:101
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:31
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:106
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:26
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:20
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:24
#define LogPrintf(...)
Definition: util.h:149
Raven protocol message types.
Definition: protocol.cpp:16
int type
Definition: protocol.h:419
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:141
friend bool operator<(const CInvAsset &a, const CInvAsset &b)
Definition: protocol.cpp:206
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:37
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:29
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:194
uint256 hash
Definition: protocol.h:420
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:19
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:35
std::string ToString() const
Definition: uint256.cpp:63
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:32
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:27
const char * FEEFILTER
The feefilter message tells the receiving peer not to inv us any txs which do not meet the specified ...
Definition: protocol.cpp:38
const char * REJECT
The reject message informs the receiving node that one of its previous messages has been rejected...
Definition: protocol.cpp:36
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:23
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:18
256-bit opaque blob.
Definition: uint256.h:123
unsigned int nTime
Definition: protocol.h:372
std::string name
Definition: protocol.h:443
ServiceFlags nServices
Definition: protocol.h:369
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:40
#define ARRAYLEN(array)
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:17
void * memcpy(void *a, const void *b, size_t c)
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:13
inv message data
Definition: protocol.h:424
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:21
CInv()
Definition: protocol.cpp:155
const char * ASSETDATA
Contains a AssetData Sent in response to a "getassetdata" message.
Definition: protocol.cpp:44
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:25
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:163
Defined in BIP37.
Definition: protocol.h:389
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:34
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:41
std::string ToString() const
Definition: protocol.cpp:211