1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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.
9 #include "utilstrencodings.h"
12 # include <arpa/inet.h>
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";
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
44 /** All known message types. Keep this in the same order as the list of
45 * messages above and in protocol.h.
47 const static std::string allNetMessageTypes
[] = {
53 NetMsgType::MERKLEBLOCK
,
54 NetMsgType::GETBLOCKS
,
55 NetMsgType::GETHEADERS
,
64 NetMsgType::FILTERLOAD
,
65 NetMsgType::FILTERADD
,
66 NetMsgType::FILTERCLEAR
,
68 NetMsgType::SENDHEADERS
,
69 NetMsgType::FEEFILTER
,
70 NetMsgType::SENDCMPCT
,
71 NetMsgType::CMPCTBLOCK
,
72 NetMsgType::GETBLOCKTXN
,
75 const static std::vector
<std::string
> allNetMessageTypesVec(allNetMessageTypes
, allNetMessageTypes
+ARRAYLEN(allNetMessageTypes
));
77 CMessageHeader::CMessageHeader(const MessageStartChars
& pchMessageStartIn
)
79 memcpy(pchMessageStart
, pchMessageStartIn
, MESSAGE_START_SIZE
);
80 memset(pchCommand
, 0, sizeof(pchCommand
));
82 memset(pchChecksum
, 0, CHECKSUM_SIZE
);
85 CMessageHeader::CMessageHeader(const MessageStartChars
& pchMessageStartIn
, const char* pszCommand
, unsigned int nMessageSizeIn
)
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
);
94 std::string
CMessageHeader::GetCommand() const
96 return std::string(pchCommand
, pchCommand
+ strnlen(pchCommand
, COMMAND_SIZE
));
99 bool CMessageHeader::IsValid(const MessageStartChars
& pchMessageStartIn
) const
101 // Check start string
102 if (memcmp(pchMessageStart
, pchMessageStartIn
, MESSAGE_START_SIZE
) != 0)
105 // Check the command string for errors
106 for (const char* p1
= pchCommand
; p1
< pchCommand
+ COMMAND_SIZE
; p1
++)
110 // Must be all zeros after the first zero
111 for (; p1
< pchCommand
+ COMMAND_SIZE
; p1
++)
115 else if (*p1
< ' ' || *p1
> 0x7E)
120 if (nMessageSize
> MAX_SIZE
)
122 LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize
);
131 CAddress::CAddress() : CService()
136 CAddress::CAddress(CService ipIn
, ServiceFlags nServicesIn
) : CService(ipIn
)
139 nServices
= nServicesIn
;
142 void CAddress::Init()
144 nServices
= NODE_NONE
;
154 CInv::CInv(int typeIn
, const uint256
& hashIn
) : type(typeIn
), hash(hashIn
) {}
156 bool operator<(const CInv
& a
, const CInv
& b
)
158 return (a
.type
< b
.type
|| (a
.type
== b
.type
&& a
.hash
< b
.hash
));
161 std::string
CInv::GetCommand() const
164 if (type
& MSG_WITNESS_FLAG
)
165 cmd
.append("witness-");
166 int masked
= type
& MSG_TYPE_MASK
;
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
);
174 throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type
));
178 std::string
CInv::ToString() const
181 return strprintf("%s %s", GetCommand(), hash
.ToString());
182 } catch(const std::out_of_range
&) {
183 return strprintf("0x%08x %s", type
, hash
.ToString());
187 const std::vector
<std::string
> &getAllNetMessageTypes()
189 return allNetMessageTypesVec
;