Bitcoin Core  0.20.99
P2P Digital Currency
protocol.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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/system.h>
9 #include <util/strencodings.h>
10 
11 #ifndef WIN32
12 # include <arpa/inet.h>
13 #endif
14 
15 static std::atomic<bool> g_initial_block_download_completed(false);
16 
17 namespace NetMsgType {
18 const char *VERSION="version";
19 const char *VERACK="verack";
20 const char *ADDR="addr";
21 const char *INV="inv";
22 const char *GETDATA="getdata";
23 const char *MERKLEBLOCK="merkleblock";
24 const char *GETBLOCKS="getblocks";
25 const char *GETHEADERS="getheaders";
26 const char *TX="tx";
27 const char *HEADERS="headers";
28 const char *BLOCK="block";
29 const char *GETADDR="getaddr";
30 const char *MEMPOOL="mempool";
31 const char *PING="ping";
32 const char *PONG="pong";
33 const char *NOTFOUND="notfound";
34 const char *FILTERLOAD="filterload";
35 const char *FILTERADD="filteradd";
36 const char *FILTERCLEAR="filterclear";
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 *GETCFCHECKPT="getcfcheckpt";
44 const char *CFCHECKPT="cfcheckpt";
45 } // namespace NetMsgType
46 
50 const static std::string allNetMessageTypes[] = {
78 };
80 
81 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
82 {
83  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
84  memset(pchCommand, 0, sizeof(pchCommand));
85  nMessageSize = -1;
86  memset(pchChecksum, 0, CHECKSUM_SIZE);
87 }
88 
89 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
90 {
91  memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
92 
93  // Copy the command name, zero-padding to COMMAND_SIZE bytes
94  size_t i = 0;
95  for (; i < COMMAND_SIZE && pszCommand[i] != 0; ++i) pchCommand[i] = pszCommand[i];
96  assert(pszCommand[i] == 0); // Assert that the command name passed in is not longer than COMMAND_SIZE
97  for (; i < COMMAND_SIZE; ++i) pchCommand[i] = 0;
98 
99  nMessageSize = nMessageSizeIn;
100  memset(pchChecksum, 0, CHECKSUM_SIZE);
101 }
102 
103 std::string CMessageHeader::GetCommand() const
104 {
105  return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
106 }
107 
108 bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
109 {
110  // Check start string
111  if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
112  return false;
113 
114  // Check the command string for errors
115  for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
116  {
117  if (*p1 == 0)
118  {
119  // Must be all zeros after the first zero
120  for (; p1 < pchCommand + COMMAND_SIZE; p1++)
121  if (*p1 != 0)
122  return false;
123  }
124  else if (*p1 < ' ' || *p1 > 0x7E)
125  return false;
126  }
127 
128  // Message size
129  if (nMessageSize > MAX_SIZE)
130  {
131  LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
132  return false;
133  }
134 
135  return true;
136 }
137 
138 
141  return ServiceFlags(NODE_NETWORK_LIMITED | NODE_WITNESS);
142  }
144 }
145 
146 void SetServiceFlagsIBDCache(bool state) {
148 }
149 
151 {
152  type = 0;
153  hash.SetNull();
154 }
155 
156 CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
157 
158 bool operator<(const CInv& a, const CInv& b)
159 {
160  return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
161 }
162 
163 std::string CInv::GetCommand() const
164 {
165  std::string cmd;
166  if (type & MSG_WITNESS_FLAG)
167  cmd.append("witness-");
168  int masked = type & MSG_TYPE_MASK;
169  switch (masked)
170  {
171  case MSG_TX: return cmd.append(NetMsgType::TX);
172  case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
173  case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
174  case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
175  default:
176  throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
177  }
178 }
179 
180 std::string CInv::ToString() const
181 {
182  try {
183  return strprintf("%s %s", GetCommand(), hash.ToString());
184  } catch(const std::out_of_range &) {
185  return strprintf("0x%08x %s", type, hash.ToString());
186  }
187 }
188 
189 const std::vector<std::string> &getAllNetMessageTypes()
190 {
191  return allNetMessageTypesVec;
192 }
const char * PING
The ping message is sent periodically to help confirm that the receiving peer is still connected...
Definition: protocol.cpp:31
const char * FILTERLOAD
The filterload message tells the receiving peer to filter all relayed transactions and requested merk...
Definition: protocol.cpp:34
const char * MERKLEBLOCK
The merkleblock message is a reply to a getdata message which requested a block using the inventory t...
Definition: protocol.cpp:23
const char * BLOCKTXN
Contains a BlockTransactions.
Definition: protocol.cpp:42
static const std::string allNetMessageTypes[]
All known message types.
Definition: protocol.cpp:50
ServiceFlags
nServices flags
Definition: protocol.h:248
std::string GetCommand() const
Definition: protocol.cpp:163
std::string ToString() const
Definition: protocol.cpp:180
const char * GETADDR
The getaddr message requests an addr message from the receiving node, preferably one with lots of IP ...
Definition: protocol.cpp:29
Defined in BIP152.
Definition: protocol.h:371
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1164
inv message data
Definition: protocol.h:378
const char * SENDCMPCT
Contains a 1-byte bool and 8-byte LE version number.
Definition: protocol.cpp:39
static const std::vector< std::string > allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes))
#define ARRAYLEN(array)
Utilities for converting data from/to strings.
Definition: strencodings.h:19
static void LogPrintf(const char *fmt, const Args &... args)
Definition: logging.h:164
void SetServiceFlagsIBDCache(bool state)
Set the current IBD status in order to figure out the desirable service flags.
Definition: protocol.cpp:146
CMessageHeader(const MessageStartChars &pchMessageStartIn)
Definition: protocol.cpp:81
const uint32_t MSG_WITNESS_FLAG
getdata message type flags
Definition: protocol.h:358
const uint32_t MSG_TYPE_MASK
Definition: protocol.h:359
std::string GetCommand() const
Definition: protocol.cpp:103
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:32
bool IsValid(const MessageStartChars &messageStart) const
Definition: protocol.cpp:108
const char * HEADERS
The headers message sends one or more block headers to a node which previously requested certain head...
Definition: protocol.cpp:27
const char * GETCFCHECKPT
getcfcheckpt requests evenly spaced compact filter headers, enabling parallelized download and valida...
Definition: protocol.cpp:43
const char * INV
The inv message (inventory message) transmits one or more inventories of objects known to the transmi...
Definition: protocol.cpp:21
static std::atomic< bool > g_initial_block_download_completed(false)
const char * GETHEADERS
The getheaders message requests a headers message that provides block headers starting from a particu...
Definition: protocol.cpp:25
Bitcoin protocol message types.
Definition: protocol.cpp:17
int type
Definition: protocol.h:391
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:30
const std::vector< std::string > & getAllNetMessageTypes()
Definition: protocol.cpp:189
uint256 hash
Definition: protocol.h:392
const char * ADDR
The addr (IP address) message relays connection information for peers on the network.
Definition: protocol.cpp:20
const char * FILTERCLEAR
The filterclear message tells the receiving peer to remove a previously-set bloom filter...
Definition: protocol.cpp:36
std::string ToString() const
Definition: uint256.cpp:60
const char * NOTFOUND
The notfound message is a reply to a getdata message which requested an object the receiving node doe...
Definition: protocol.cpp:33
const char * BLOCK
The block message transmits a single serialized block.
Definition: protocol.cpp:28
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 * GETBLOCKS
The getblocks message requests an inv message that provides block header hashes starting from a parti...
Definition: protocol.cpp:24
const char * VERACK
The verack message acknowledges a previously-received version message, informing the connecting node ...
Definition: protocol.cpp:19
256-bit opaque blob.
Definition: uint256.h:120
const char * CMPCTBLOCK
Contains a CBlockHeaderAndShortTxIDs object - providing a header and list of "short txids"...
Definition: protocol.cpp:40
static const unsigned int MAX_SIZE
Definition: serialize.h:27
const char * VERSION
The version message provides information about the transmitting node to the receiving node at the beg...
Definition: protocol.cpp:18
void * memcpy(void *a, const void *b, size_t c)
size_t strnlen(const char *start, size_t max_len)
Definition: strnlen.cpp:12
ServiceFlags GetDesirableServiceFlags(ServiceFlags services)
Gets the set of service flags which are "desirable" for a given peer.
Definition: protocol.cpp:139
const char * GETDATA
The getdata message requests one or more data objects from another node.
Definition: protocol.cpp:22
const char * CFCHECKPT
cfcheckpt is a response to a getcfcheckpt request containing a vector of evenly spaced filter headers...
Definition: protocol.cpp:44
CInv()
Definition: protocol.cpp:150
const char * TX
The tx message transmits a single transaction.
Definition: protocol.cpp:26
friend bool operator<(const CInv &a, const CInv &b)
Definition: protocol.cpp:158
Defined in BIP37.
Definition: protocol.h:370
const char * FILTERADD
The filteradd message tells the receiving peer to add a single element to a previously-set bloom filt...
Definition: protocol.cpp:35
const char * GETBLOCKTXN
Contains a BlockTransactionsRequest Peer should respond with "blocktxn" message.
Definition: protocol.cpp:41