[tests] Fix incorrect memory_cleanse(…) call in crypto_tests.cpp
[bitcoinplatinum.git] / src / protocol.cpp
blobda87e40091f65088ce6fff620cb27884bbbbae22
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.
6 #include "protocol.h"
8 #include "util.h"
9 #include "utilstrencodings.h"
11 #ifndef WIN32
12 # include <arpa/inet.h>
13 #endif
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
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[] = {
48 NetMsgType::VERSION,
49 NetMsgType::VERACK,
50 NetMsgType::ADDR,
51 NetMsgType::INV,
52 NetMsgType::GETDATA,
53 NetMsgType::MERKLEBLOCK,
54 NetMsgType::GETBLOCKS,
55 NetMsgType::GETHEADERS,
56 NetMsgType::TX,
57 NetMsgType::HEADERS,
58 NetMsgType::BLOCK,
59 NetMsgType::GETADDR,
60 NetMsgType::MEMPOOL,
61 NetMsgType::PING,
62 NetMsgType::PONG,
63 NetMsgType::NOTFOUND,
64 NetMsgType::FILTERLOAD,
65 NetMsgType::FILTERADD,
66 NetMsgType::FILTERCLEAR,
67 NetMsgType::REJECT,
68 NetMsgType::SENDHEADERS,
69 NetMsgType::FEEFILTER,
70 NetMsgType::SENDCMPCT,
71 NetMsgType::CMPCTBLOCK,
72 NetMsgType::GETBLOCKTXN,
73 NetMsgType::BLOCKTXN,
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));
81 nMessageSize = -1;
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)
103 return false;
105 // Check the command string for errors
106 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
108 if (*p1 == 0)
110 // Must be all zeros after the first zero
111 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
112 if (*p1 != 0)
113 return false;
115 else if (*p1 < ' ' || *p1 > 0x7E)
116 return false;
119 // Message size
120 if (nMessageSize > MAX_SIZE)
122 LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
123 return false;
126 return true;
131 CAddress::CAddress() : CService()
133 Init();
136 CAddress::CAddress(CService ipIn, ServiceFlags nServicesIn) : CService(ipIn)
138 Init();
139 nServices = nServicesIn;
142 void CAddress::Init()
144 nServices = NODE_NONE;
145 nTime = 100000000;
148 CInv::CInv()
150 type = 0;
151 hash.SetNull();
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
163 std::string cmd;
164 if (type & MSG_WITNESS_FLAG)
165 cmd.append("witness-");
166 int masked = type & MSG_TYPE_MASK;
167 switch (masked)
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));
178 std::string CInv::ToString() const
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());
187 const std::vector<std::string> &getAllNetMessageTypes()
189 return allNetMessageTypesVec;