2011-08-11 18:14:53 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2012-02-07 17:28:30 +01:00
|
|
|
// Copyright (c) 2009-2012 The Bitcoin developers
|
2011-08-11 18:14:53 +02:00
|
|
|
// Distributed under the MIT/X11 software license, see the accompanying
|
|
|
|
// file license.txt or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#include "protocol.h"
|
2011-08-11 18:40:12 +02:00
|
|
|
#include "util.h"
|
2012-01-03 23:33:31 +01:00
|
|
|
#include "netbase.h"
|
2011-08-11 18:40:12 +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
|
|
|
|
|
2011-08-11 18:49:03 +02:00
|
|
|
static const char* ppszTypeName[] =
|
|
|
|
{
|
|
|
|
"ERROR",
|
|
|
|
"tx",
|
|
|
|
"block",
|
|
|
|
};
|
2011-08-11 18:14:53 +02:00
|
|
|
|
|
|
|
CMessageHeader::CMessageHeader()
|
|
|
|
{
|
|
|
|
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
|
|
|
|
memset(pchCommand, 0, sizeof(pchCommand));
|
|
|
|
pchCommand[1] = 1;
|
|
|
|
nMessageSize = -1;
|
|
|
|
nChecksum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
|
|
|
|
{
|
|
|
|
memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
|
|
|
|
strncpy(pchCommand, pszCommand, COMMAND_SIZE);
|
|
|
|
nMessageSize = nMessageSizeIn;
|
|
|
|
nChecksum = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CMessageHeader::GetCommand() const
|
|
|
|
{
|
|
|
|
if (pchCommand[COMMAND_SIZE-1] == 0)
|
|
|
|
return std::string(pchCommand, pchCommand + strlen(pchCommand));
|
|
|
|
else
|
|
|
|
return std::string(pchCommand, pchCommand + COMMAND_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CMessageHeader::IsValid() const
|
|
|
|
{
|
|
|
|
// Check start string
|
|
|
|
if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-01-03 23:33:31 +01:00
|
|
|
CAddress::CAddress(CService ipIn, uint64 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;
|
|
|
|
nLastTry = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-11 18:49:03 +02:00
|
|
|
CInv::CInv()
|
|
|
|
{
|
|
|
|
type = 0;
|
|
|
|
hash = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
|
|
|
|
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())
|
|
|
|
throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
|
|
|
|
return ppszTypeName[type];
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string CInv::ToString() const
|
|
|
|
{
|
|
|
|
return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
void CInv::print() const
|
|
|
|
{
|
|
|
|
printf("CInv(%s)\n", ToString().c_str());
|
|
|
|
}
|
2012-01-03 23:33:31 +01:00
|
|
|
|