Fabcoin Core  0.16.2
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 *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";
38 const char *SENDCMPCT="sendcmpct";
39 const char *CMPCTBLOCK="cmpctblock";
40 const char *GETBLOCKTXN="getblocktxn";
41 const char *BLOCKTXN="blocktxn";
42 } // namespace NetMsgType
43 
47 const static std::string allNetMessageTypes[] = {
74 };
75 const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
76 
77 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
78 {
79  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
80  memset(pchCommand, 0, sizeof(pchCommand));
81  nMessageSize = -1;
82  memset(pchChecksum, 0, CHECKSUM_SIZE);
83 }
84 
85 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
86 {
87  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
88  memset(pchCommand, 0, sizeof(pchCommand));
89  strncpy(pchCommand, pszCommand, COMMAND_SIZE);
90  nMessageSize = nMessageSizeIn;
91  memset(pchChecksum, 0, CHECKSUM_SIZE);
92 }
93 
94 std::string CMessageHeader::GetCommand() const
95 {
96  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
97 }
98 
99 bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
100 {
101  // Check start string
102  if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
103  return false;
104 
105  // Check the command string for errors
106  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
107  {
108  if (*p1 == 0)
109  {
110  // Must be all zeros after the first zero
111  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
112  if (*p1 != 0)
113  return false;
114  }
115  else if (*p1 < ' ' || *p1 > 0x7E)
116  return false;
117  }
118 
119  // Message size
120  if (nMessageSize > MAX_SIZE)
121  {
122  LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
123  return false;
124  }
125 
126  return true;
127 }
128 
129 
130 
132 {
133  Init();
134 }
135 
137 {
138  Init();
139  nServices = nServicesIn;
140 }
141 
143 {
145  nTime = 100000000;
146 }
147 
149 {
150  type = 0;
151  hash.SetNull();
152 }
153 
154 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
155 
156 bool operator<(const CInv& a, const CInv& b)
157 {
158  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
159 }
160 
161 std::string CInv::GetCommand() const
162 {
163  std::string cmd;
164  if (type & MSG_WITNESS_FLAG)
165  cmd.append("witness-");
166  int masked = type & MSG_TYPE_MASK;
167  switch (masked)
168  {
169  case MSG_TX: return cmd.append(NetMsgType::TX);
170  case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
171  case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
172  case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
173  default:
174  throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
175  }
176 }
177 
178 std::string CInv::ToString() const
179 {
180  try {
181  return strprintf("%s %s", GetCommand(), hash.ToString());
182  } catch(const std::out_of_range &) {
183  return strprintf("0x%08x %s", type, hash.ToString());
184  }
185 }
186 
187 const std::vector<std::string> &getAllNetMessageTypes()
188 {
189  return allNetMessageTypesVec;
190 }
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:29
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:32
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:21
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:41
ServiceFlags
nServices flags
Definition: protocol.h:249
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:27
void Init()
Definition: protocol.cpp:142
Defined in BIP152.
Definition: protocol.h:331
#define strprintf
Definition: tinyformat.h:1054
inv message data
Definition: protocol.h:338
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:38
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:77
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:317
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:318
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:30
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:99
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:25
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:19
std::string ToString() const
Definition: uint256.cpp:95
#define a(i)
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:23
#define LogPrintf(...)
Definition: util.h:153
Fabcoin protocol message types.
Definition: protocol.cpp:15
int type
Definition: protocol.h:360
A combination of a network address (CNetAddr) and a (TCP) port.
Definition: netaddress.h:140
const char * SENDHEADERS
Indicates that a node prefers to receive new block announcements via a "headers" message rather than ...
Definition: protocol.cpp:36
const char * MEMPOOL
The mempool message requests the TXIDs of transactions that the receiving node has verified as valid ...
Definition: protocol.cpp:28
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:187
uint256 hash
Definition: protocol.h:361
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:18
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:34
std::string GetCommand() const
Definition: protocol.cpp:94
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:31
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:26
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:37
#define b(i, j)
const char * REJECT
The reject message informs the receiving node that one of its previous messages has been rejected...
Definition: protocol.cpp:35
const char * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:22
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:17
256-bit opaque blob.
Definition: uint256.h:132
unsigned int nTime
Definition: protocol.h:313
ServiceFlags nServices
Definition: protocol.h:310
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:39
#define ARRAYLEN(array)
PlatformStyle::TableColorType type
Definition: rpcconsole.cpp:61
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:16
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 * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:20
CInv()
Definition: protocol.cpp:148
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:24
std::string ToString() const
Definition: protocol.cpp:178
std::string GetCommand() const
Definition: protocol.cpp:161
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:156
Defined in BIP37.
Definition: protocol.h:330
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:33
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:40