2011-08-11 18:14:53 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2015-12-13 14:51:43 +01:00
|
|
|
// Copyright (c) 2009-2015 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 16:02:28 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2011-08-11 18:14:53 +02:00
|
|
|
|
|
|
|
#include "protocol.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2011-08-11 18:40:12 +02:00
|
|
|
#include "util.h"
|
2014-09-25 05:32:36 +02:00
|
|
|
#include "utilstrencodings.h"
|
2013-04-13 07:13:08 +02:00
|
|
|
|
2011-10-07 17:02:21 +02:00
|
|
|
#ifndef WIN32
|
2011-08-11 18:40:12 +02:00
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2015-12-07 15:31:32 +01:00
|
|
|
namespace NetMsgType {
|
|
|
|
const char *VERSION="version";
|
|
|
|
const char *VERACK="verack";
|
|
|
|
const char *ADDR="addr";
|
|
|
|
const char *INV="inv";
|
|
|
|
const char *GETDATA="getdata";
|
|
|
|
const char *MERKLEBLOCK="merkleblock";
|
|
|
|
const char *GETBLOCKS="getblocks";
|
|
|
|
const char *GETHEADERS="getheaders";
|
|
|
|
const char *TX="tx";
|
|
|
|
const char *HEADERS="headers";
|
|
|
|
const char *BLOCK="block";
|
|
|
|
const char *GETADDR="getaddr";
|
|
|
|
const char *MEMPOOL="mempool";
|
|
|
|
const char *PING="ping";
|
|
|
|
const char *PONG="pong";
|
|
|
|
const char *ALERT="alert";
|
|
|
|
const char *NOTFOUND="notfound";
|
|
|
|
const char *FILTERLOAD="filterload";
|
|
|
|
const char *FILTERADD="filteradd";
|
|
|
|
const char *FILTERCLEAR="filterclear";
|
|
|
|
const char *REJECT="reject";
|
|
|
|
const char *SENDHEADERS="sendheaders";
|
2016-02-17 23:18:57 +01:00
|
|
|
// Dash message types
|
2016-09-15 22:07:01 +02:00
|
|
|
const char *TXLOCKREQUEST="ix";
|
|
|
|
const char *TXLOCKVOTE="txlvote";
|
2016-02-17 23:18:57 +01:00
|
|
|
const char *SPORK="spork";
|
|
|
|
const char *GETSPORKS="getsporks";
|
2016-09-21 16:45:29 +02:00
|
|
|
const char *MASTERNODEPAYMENTVOTE="mnw";
|
|
|
|
const char *MASTERNODEPAYMENTBLOCK="mnwb";
|
|
|
|
const char *MASTERNODEPAYMENTSYNC="mnget";
|
2016-08-05 18:25:03 +02:00
|
|
|
const char *MNBUDGETSYNC="mnvs"; // depreciated since 12.1
|
|
|
|
const char *MNBUDGETVOTE="mvote"; // depreciated since 12.1
|
|
|
|
const char *MNBUDGETPROPOSAL="mprop"; // depreciated since 12.1
|
|
|
|
const char *MNBUDGETFINAL="fbs"; // depreciated since 12.1
|
|
|
|
const char *MNBUDGETFINALVOTE="fbvote"; // depreciated since 12.1
|
2016-05-29 13:46:39 +02:00
|
|
|
const char *MNQUORUM="mn quorum"; // not implemented
|
2016-02-17 23:18:57 +01:00
|
|
|
const char *MNANNOUNCE="mnb";
|
|
|
|
const char *MNPING="mnp";
|
|
|
|
const char *DSACCEPT="dsa";
|
|
|
|
const char *DSVIN="dsi";
|
|
|
|
const char *DSFINALTX="dsf";
|
|
|
|
const char *DSSIGNFINALTX="dss";
|
|
|
|
const char *DSCOMPLETE="dsc";
|
|
|
|
const char *DSSTATUSUPDATE="dssu";
|
|
|
|
const char *DSTX="dstx";
|
|
|
|
const char *DSQUEUE="dsq";
|
|
|
|
const char *DSEG="dseg";
|
|
|
|
const char *SYNCSTATUSCOUNT="ssc";
|
2016-08-05 18:25:03 +02:00
|
|
|
const char *MNGOVERNANCESYNC="govsync";
|
|
|
|
const char *MNGOVERNANCEOBJECT="govobj";
|
|
|
|
const char *MNGOVERNANCEOBJECTVOTE="govobjvote";
|
2016-10-20 23:11:30 +02:00
|
|
|
const char *MNVERIFY="mnv";
|
2015-12-07 15:31:32 +01:00
|
|
|
};
|
|
|
|
|
2011-08-11 18:49:03 +02:00
|
|
|
static const char* ppszTypeName[] =
|
|
|
|
{
|
2015-12-07 15:31:32 +01:00
|
|
|
"ERROR", // Should never occur
|
|
|
|
NetMsgType::TX,
|
|
|
|
NetMsgType::BLOCK,
|
2016-02-17 23:18:57 +01:00
|
|
|
"filtered block", // Should never occur
|
2016-08-05 18:25:03 +02:00
|
|
|
// Dash message types
|
|
|
|
// NOTE: include non-implmented here, we must keep this list in sync with enum in protocol.h
|
2016-09-15 22:07:01 +02:00
|
|
|
NetMsgType::TXLOCKREQUEST,
|
|
|
|
NetMsgType::TXLOCKVOTE,
|
2016-02-17 23:18:57 +01:00
|
|
|
NetMsgType::SPORK,
|
2016-09-21 16:45:29 +02:00
|
|
|
NetMsgType::MASTERNODEPAYMENTVOTE,
|
|
|
|
NetMsgType::MASTERNODEPAYMENTBLOCK, // reusing, was MNSCANERROR previousely, was NOT used in 12.0, we need this for inv
|
2016-08-05 18:25:03 +02:00
|
|
|
NetMsgType::MNBUDGETVOTE, // depreciated since 12.1
|
|
|
|
NetMsgType::MNBUDGETPROPOSAL, // depreciated since 12.1
|
|
|
|
NetMsgType::MNBUDGETFINAL, // depreciated since 12.1
|
|
|
|
NetMsgType::MNBUDGETFINALVOTE, // depreciated since 12.1
|
2016-05-29 13:46:39 +02:00
|
|
|
NetMsgType::MNQUORUM, // not implemented
|
2016-02-17 23:18:57 +01:00
|
|
|
NetMsgType::MNANNOUNCE,
|
|
|
|
NetMsgType::MNPING,
|
2016-08-05 18:25:03 +02:00
|
|
|
NetMsgType::DSTX,
|
|
|
|
NetMsgType::MNGOVERNANCEOBJECT,
|
2016-10-20 23:11:30 +02:00
|
|
|
NetMsgType::MNGOVERNANCEOBJECTVOTE,
|
|
|
|
NetMsgType::MNVERIFY,
|
2011-08-11 18:49:03 +02:00
|
|
|
};
|
2011-08-11 18:14:53 +02:00
|
|
|
|
2015-12-07 15:31:32 +01:00
|
|
|
/** All known message types. Keep this in the same order as the list of
|
|
|
|
* messages above and in protocol.h.
|
|
|
|
*/
|
|
|
|
const static std::string allNetMessageTypes[] = {
|
|
|
|
NetMsgType::VERSION,
|
|
|
|
NetMsgType::VERACK,
|
|
|
|
NetMsgType::ADDR,
|
|
|
|
NetMsgType::INV,
|
|
|
|
NetMsgType::GETDATA,
|
|
|
|
NetMsgType::MERKLEBLOCK,
|
|
|
|
NetMsgType::GETBLOCKS,
|
|
|
|
NetMsgType::GETHEADERS,
|
|
|
|
NetMsgType::TX,
|
|
|
|
NetMsgType::HEADERS,
|
|
|
|
NetMsgType::BLOCK,
|
|
|
|
NetMsgType::GETADDR,
|
|
|
|
NetMsgType::MEMPOOL,
|
|
|
|
NetMsgType::PING,
|
|
|
|
NetMsgType::PONG,
|
|
|
|
NetMsgType::ALERT,
|
|
|
|
NetMsgType::NOTFOUND,
|
|
|
|
NetMsgType::FILTERLOAD,
|
|
|
|
NetMsgType::FILTERADD,
|
|
|
|
NetMsgType::FILTERCLEAR,
|
|
|
|
NetMsgType::REJECT,
|
2016-02-17 23:18:57 +01:00
|
|
|
NetMsgType::SENDHEADERS,
|
2016-08-05 18:25:03 +02:00
|
|
|
// Dash message types
|
|
|
|
// NOTE: do NOT include non-implmented here, we want them to be "Unknown command" in ProcessMessage()
|
2016-09-15 22:07:01 +02:00
|
|
|
NetMsgType::TXLOCKREQUEST,
|
|
|
|
NetMsgType::TXLOCKVOTE,
|
2016-02-17 23:18:57 +01:00
|
|
|
NetMsgType::SPORK,
|
|
|
|
NetMsgType::GETSPORKS,
|
2016-09-21 16:45:29 +02:00
|
|
|
NetMsgType::MASTERNODEPAYMENTVOTE,
|
|
|
|
// NetMsgType::MASTERNODEPAYMENTBLOCK, // there is no message for this, only inventory
|
|
|
|
NetMsgType::MASTERNODEPAYMENTSYNC,
|
2016-02-17 23:18:57 +01:00
|
|
|
NetMsgType::MNANNOUNCE,
|
|
|
|
NetMsgType::MNPING,
|
|
|
|
NetMsgType::DSACCEPT,
|
|
|
|
NetMsgType::DSVIN,
|
|
|
|
NetMsgType::DSFINALTX,
|
|
|
|
NetMsgType::DSSIGNFINALTX,
|
|
|
|
NetMsgType::DSCOMPLETE,
|
|
|
|
NetMsgType::DSSTATUSUPDATE,
|
|
|
|
NetMsgType::DSTX,
|
|
|
|
NetMsgType::DSQUEUE,
|
|
|
|
NetMsgType::DSEG,
|
2016-08-05 18:25:03 +02:00
|
|
|
NetMsgType::SYNCSTATUSCOUNT,
|
|
|
|
NetMsgType::MNGOVERNANCESYNC,
|
|
|
|
NetMsgType::MNGOVERNANCEOBJECT,
|
2016-10-20 23:11:30 +02:00
|
|
|
NetMsgType::MNGOVERNANCEOBJECTVOTE,
|
|
|
|
NetMsgType::MNVERIFY,
|
2011-08-11 18:49:03 +02:00
|
|
|
};
|
2015-12-07 15:31:32 +01:00
|
|
|
const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
|
2011-08-11 18:14:53 +02:00
|
|
|
|
2014-10-28 01:24:31 +01:00
|
|
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
|
2011-08-11 18:14:53 +02:00
|
|
|
{
|
2014-10-28 01:24:31 +01:00
|
|
|
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
|
2011-08-11 18:14:53 +02:00
|
|
|
memset(pchCommand, 0, sizeof(pchCommand));
|
|
|
|
nMessageSize = -1;
|
|
|
|
nChecksum = 0;
|
|
|
|
}
|
|
|
|
|
2014-10-28 01:24:31 +01:00
|
|
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
|
2011-08-11 18:14:53 +02:00
|
|
|
{
|
2014-10-28 01:24:31 +01:00
|
|
|
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
|
2014-09-16 18:13:05 +02:00
|
|
|
memset(pchCommand, 0, sizeof(pchCommand));
|
2011-08-11 18:14:53 +02:00
|
|
|
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
|
|
|
|
nMessageSize = nMessageSizeIn;
|
|
|
|
nChecksum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMessageHeader::GetCommand() const
|
|
|
|
{
|
2014-09-16 18:13:05 +02:00
|
|
|
return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
|
2011-08-11 18:14:53 +02:00
|
|
|
}
|
|
|
|
|
2014-10-28 01:24:31 +01:00
|
|
|
bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
|
2011-08-11 18:14:53 +02:00
|
|
|
{
|
|
|
|
// Check start string
|
2014-10-28 01:24:31 +01:00
|
|
|
if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
|
2011-08-11 18:14:53 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Check the command string for errors
|
|
|
|
for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
|
|
|
|
{
|
|
|
|
if (*p1 == 0)
|
|
|
|
{
|
|
|
|
// Must be all zeros after the first zero
|
|
|
|
for (; p1 < pchCommand + COMMAND_SIZE; p1++)
|
|
|
|
if (*p1 != 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
else if (*p1 < ' ' || *p1 > 0x7E)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message size
|
|
|
|
if (nMessageSize > MAX_SIZE)
|
|
|
|
{
|
2015-01-08 11:44:25 +01:00
|
|
|
LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
|
2011-08-11 18:14:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-11 18:40:12 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
CAddress::CAddress() : CService()
|
2011-08-11 18:40:12 +02:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2013-04-13 07:13:08 +02:00
|
|
|
CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
|
2011-08-11 18:40:12 +02:00
|
|
|
{
|
|
|
|
Init();
|
2012-01-03 23:33:31 +01:00
|
|
|
nServices = nServicesIn;
|
2011-08-11 18:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAddress::Init()
|
|
|
|
{
|
|
|
|
nServices = NODE_NETWORK;
|
|
|
|
nTime = 100000000;
|
|
|
|
}
|
|
|
|
|
2011-08-11 18:49:03 +02:00
|
|
|
CInv::CInv()
|
|
|
|
{
|
|
|
|
type = 0;
|
2014-12-15 09:11:16 +01:00
|
|
|
hash.SetNull();
|
2011-08-11 18:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CInv::CInv(int typeIn, const uint256& hashIn)
|
|
|
|
{
|
|
|
|
type = typeIn;
|
|
|
|
hash = hashIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
CInv::CInv(const std::string& strType, const uint256& hashIn)
|
|
|
|
{
|
2012-04-15 22:52:09 +02:00
|
|
|
unsigned int i;
|
2011-08-11 18:49:03 +02:00
|
|
|
for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
|
|
|
|
{
|
|
|
|
if (strType == ppszTypeName[i])
|
|
|
|
{
|
|
|
|
type = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == ARRAYLEN(ppszTypeName))
|
2016-08-11 19:32:52 +02:00
|
|
|
throw std::out_of_range(strprintf("CInv::CInv(string, uint256): unknown type '%s'", strType));
|
2011-08-11 18:49:03 +02:00
|
|
|
hash = hashIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const CInv& a, const CInv& b)
|
|
|
|
{
|
|
|
|
return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CInv::IsKnownType() const
|
|
|
|
{
|
2012-04-22 19:51:16 +02:00
|
|
|
return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
|
2011-08-11 18:49:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const char* CInv::GetCommand() const
|
|
|
|
{
|
|
|
|
if (!IsKnownType())
|
2016-08-11 19:32:52 +02:00
|
|
|
throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
|
2011-08-11 18:49:03 +02:00
|
|
|
return ppszTypeName[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CInv::ToString() const
|
|
|
|
{
|
2014-01-16 16:15:27 +01:00
|
|
|
return strprintf("%s %s", GetCommand(), hash.ToString());
|
2011-08-11 18:49:03 +02:00
|
|
|
}
|
2015-12-07 15:31:32 +01:00
|
|
|
|
|
|
|
const std::vector<std::string> &getAllNetMessageTypes()
|
|
|
|
{
|
|
|
|
return allNetMessageTypesVec;
|
|
|
|
}
|