1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 };
43 
44 /** All known message types. Keep this in the same order as the list of
45  * messages above and in protocol.h.
46  */
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,
74 };
75 const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
76 
CMessageHeader(const MessageStartChars & pchMessageStartIn)77 CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
78 {
79     memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
80     memset(pchCommand, 0, sizeof(pchCommand));
81     nMessageSize = -1;
82     nChecksum = 0;
83 }
84 
CMessageHeader(const MessageStartChars & pchMessageStartIn,const char * pszCommand,unsigned int nMessageSizeIn)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     nChecksum = 0;
92 }
93 
GetCommand() const94 std::string CMessageHeader::GetCommand() const
95 {
96     return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
97 }
98 
IsValid(const MessageStartChars & pchMessageStartIn) const99 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 
CAddress()131 CAddress::CAddress() : CService()
132 {
133     Init();
134 }
135 
CAddress(CService ipIn,ServiceFlags nServicesIn)136 CAddress::CAddress(CService ipIn, ServiceFlags nServicesIn) : CService(ipIn)
137 {
138     Init();
139     nServices = nServicesIn;
140 }
141 
Init()142 void CAddress::Init()
143 {
144     nServices = NODE_NONE;
145     nTime = 100000000;
146 }
147 
CInv()148 CInv::CInv()
149 {
150     type = 0;
151     hash.SetNull();
152 }
153 
CInv(int typeIn,const uint256 & hashIn)154 CInv::CInv(int typeIn, const uint256& hashIn)
155 {
156     type = typeIn;
157     hash = hashIn;
158 }
159 
operator <(const CInv & a,const CInv & b)160 bool operator<(const CInv& a, const CInv& b)
161 {
162     return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
163 }
164 
GetCommand() const165 std::string CInv::GetCommand() const
166 {
167     std::string cmd;
168     if (type & MSG_WITNESS_FLAG)
169         cmd.append("witness-");
170     int masked = type & MSG_TYPE_MASK;
171     switch (masked)
172     {
173     case MSG_TX:             return cmd.append(NetMsgType::TX);
174     case MSG_BLOCK:          return cmd.append(NetMsgType::BLOCK);
175     case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
176     case MSG_CMPCT_BLOCK:    return cmd.append(NetMsgType::CMPCTBLOCK);
177     default:
178         throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
179     }
180 }
181 
ToString() const182 std::string CInv::ToString() const
183 {
184     return strprintf("%s %s", GetCommand(), hash.ToString());
185 }
186 
getAllNetMessageTypes()187 const std::vector<std::string> &getAllNetMessageTypes()
188 {
189     return allNetMessageTypesVec;
190 }
191