2011-08-11 09:14:53 -07:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2014-12-16 17:47:57 -08:00
|
|
|
// Copyright (c) 2009-2014 The Bitcoin Core developers
|
2014-12-12 20:09:33 -08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2019-07-18 07:16:09 -07:00
|
|
|
// file COPYING or https://www.opensource.org/licenses/mit-license.php .
|
2011-08-11 09:14:53 -07:00
|
|
|
|
|
|
|
#include "protocol.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
|
2011-08-11 09:40:12 -07:00
|
|
|
#include "util.h"
|
2014-09-24 20:32:36 -07:00
|
|
|
#include "utilstrencodings.h"
|
2013-04-12 22:13:08 -07:00
|
|
|
|
2011-10-07 08:02:21 -07:00
|
|
|
#ifndef WIN32
|
2011-08-11 09:40:12 -07:00
|
|
|
# include <arpa/inet.h>
|
|
|
|
#endif
|
|
|
|
|
2014-10-27 17:24:31 -07:00
|
|
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
|
2011-08-11 09:14:53 -07:00
|
|
|
{
|
2014-10-27 17:24:31 -07:00
|
|
|
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
|
2011-08-11 09:14:53 -07:00
|
|
|
memset(pchCommand, 0, sizeof(pchCommand));
|
|
|
|
nMessageSize = -1;
|
2016-09-27 05:05:24 -07:00
|
|
|
memset(pchChecksum, 0, CHECKSUM_SIZE);
|
2011-08-11 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 17:24:31 -07:00
|
|
|
CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
|
2011-08-11 09:14:53 -07:00
|
|
|
{
|
2014-10-27 17:24:31 -07:00
|
|
|
memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
|
2014-09-16 09:13:05 -07:00
|
|
|
memset(pchCommand, 0, sizeof(pchCommand));
|
2011-08-11 09:14:53 -07:00
|
|
|
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
|
|
|
|
nMessageSize = nMessageSizeIn;
|
2016-09-27 05:05:24 -07:00
|
|
|
memset(pchChecksum, 0, CHECKSUM_SIZE);
|
2011-08-11 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMessageHeader::GetCommand() const
|
|
|
|
{
|
2014-09-16 09:13:05 -07:00
|
|
|
return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
|
2011-08-11 09:14:53 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 17:24:31 -07:00
|
|
|
bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
|
2011-08-11 09:14:53 -07:00
|
|
|
{
|
|
|
|
// Check start string
|
2014-10-27 17:24:31 -07:00
|
|
|
if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
|
2011-08-11 09:14:53 -07: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 02:44:25 -08:00
|
|
|
LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
|
2011-08-11 09:14:53 -07:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2011-08-11 09:40:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-01-03 14:33:31 -08:00
|
|
|
CAddress::CAddress() : CService()
|
2011-08-11 09:40:12 -07:00
|
|
|
{
|
|
|
|
Init();
|
|
|
|
}
|
|
|
|
|
2013-04-12 22:13:08 -07:00
|
|
|
CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
|
2011-08-11 09:40:12 -07:00
|
|
|
{
|
|
|
|
Init();
|
2012-01-03 14:33:31 -08:00
|
|
|
nServices = nServicesIn;
|
2011-08-11 09:40:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAddress::Init()
|
|
|
|
{
|
|
|
|
nServices = NODE_NETWORK;
|
|
|
|
nTime = 100000000;
|
|
|
|
}
|
|
|
|
|
2011-08-11 09:49:03 -07:00
|
|
|
CInv::CInv()
|
|
|
|
{
|
|
|
|
type = 0;
|
2014-12-15 00:11:16 -08:00
|
|
|
hash.SetNull();
|
2021-09-02 17:37:16 -07:00
|
|
|
hashAux.SetNull();
|
2011-08-11 09:49:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
CInv::CInv(int typeIn, const uint256& hashIn)
|
|
|
|
{
|
2021-09-02 17:37:16 -07:00
|
|
|
assert(typeIn != MSG_WTX);
|
2011-08-11 09:49:03 -07:00
|
|
|
type = typeIn;
|
|
|
|
hash = hashIn;
|
2021-09-02 17:37:16 -07:00
|
|
|
if (typeIn == MSG_TX) {
|
|
|
|
hashAux = uint256S("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
|
|
|
|
} else {
|
|
|
|
hashAux.SetNull();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CInv::CInv(int typeIn, const uint256& hashIn, const uint256& hashAuxIn)
|
|
|
|
{
|
|
|
|
type = typeIn;
|
|
|
|
hash = hashIn;
|
|
|
|
hashAux = hashAuxIn;
|
2011-08-11 09:49:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator<(const CInv& a, const CInv& b)
|
|
|
|
{
|
2021-09-02 17:37:16 -07:00
|
|
|
return (a.type < b.type ||
|
|
|
|
(a.type == b.type && a.hash < b.hash) ||
|
|
|
|
(a.type == b.type && a.hash == b.hash && a.hashAux < b.hashAux));
|
2011-08-11 09:49:03 -07:00
|
|
|
}
|
|
|
|
|
2016-04-19 11:33:59 -07:00
|
|
|
std::string CInv::GetCommand() const
|
2011-08-11 09:49:03 -07:00
|
|
|
{
|
2016-04-19 11:33:59 -07:00
|
|
|
std::string cmd;
|
2016-01-10 23:55:11 -08:00
|
|
|
switch (type)
|
|
|
|
{
|
2016-04-19 11:33:59 -07:00
|
|
|
case MSG_TX: return cmd.append("tx");
|
|
|
|
case MSG_BLOCK: return cmd.append("block");
|
2021-09-02 17:37:16 -07:00
|
|
|
// WTX is not a message type, just an inv type
|
|
|
|
case MSG_WTX: return cmd.append("wtx");
|
2016-04-19 11:33:59 -07:00
|
|
|
case MSG_FILTERED_BLOCK: return cmd.append("merkleblock");
|
2016-01-10 23:55:11 -08:00
|
|
|
default:
|
2015-01-08 02:44:25 -08:00
|
|
|
throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
|
2016-01-10 23:55:11 -08:00
|
|
|
}
|
2011-08-11 09:49:03 -07:00
|
|
|
}
|
|
|
|
|
2021-09-02 17:37:16 -07:00
|
|
|
std::vector<unsigned char> CInv::GetWideHash() const
|
|
|
|
{
|
|
|
|
assert(type != MSG_BLOCK);
|
|
|
|
if (type == MSG_TX) {
|
|
|
|
for (auto byte : hashAux) {
|
|
|
|
assert(byte == 0xff);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
std::vector<unsigned char> vData(hash.begin(), hash.end());
|
|
|
|
vData.insert(vData.end(), hashAux.begin(), hashAux.end());
|
|
|
|
return vData;
|
|
|
|
}
|
|
|
|
|
2011-08-11 09:49:03 -07:00
|
|
|
std::string CInv::ToString() const
|
|
|
|
{
|
2021-09-02 17:37:16 -07:00
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case MSG_WTX:
|
|
|
|
return strprintf("%s(%s, %s)", GetCommand(), hash.ToString(), hashAux.ToString());
|
|
|
|
default:
|
|
|
|
return strprintf("%s %s", GetCommand(), hash.ToString());
|
|
|
|
}
|
2011-08-11 09:49:03 -07:00
|
|
|
}
|