X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=src%2Fprotocol.cpp;h=e33ede58c6b1984a562f044ada246ceefaf65542;hb=3176e0f244d929669aa3e1d81e0787d82d9150d3;hp=8d2dbfdd2d76b6de1d45a962686b2879614640d4;hpb=33e28c9948336784324cf5c7248ce608b93dbfde;p=novacoin.git diff --git a/src/protocol.cpp b/src/protocol.cpp index 8d2dbfd..e33ede5 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -1,24 +1,42 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2011 The Bitcoin developers +// Copyright (c) 2009-2012 The Bitcoin developers +// Copyright (c) 2013 NovaCoin Developers // Distributed under the MIT/X11 software license, see the accompanying -// file license.txt or http://www.opensource.org/licenses/mit-license.php. +// file COPYING or http://www.opensource.org/licenses/mit-license.php. #include "protocol.h" #include "util.h" +#include "netbase.h" -#ifndef __WXMSW__ +#ifndef WIN32 # include #endif -// Prototypes from net.h, but that header (currently) stinks, can't #include it without breaking things -bool Lookup(const char *pszName, std::vector& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); -bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false); +// The message start string is designed to be unlikely to occur in normal data. +// The characters are rarely used upper ascii, not valid as UTF-8, and produce +// a large 4-byte int at any alignment. -static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; +static unsigned char pchMessageStartTestNew[4] = { 0xcd, 0xf2, 0xc0, 0xef }; +static unsigned char pchMessageStartPPCoin[4] = { 0xe4, 0xe8, 0xe9, 0xe5 }; + +void GetMessageStart(unsigned char pchMessageStart[], bool fPersistent) +{ + if (fTestNet) + memcpy(pchMessageStart, pchMessageStartTestNew, sizeof(pchMessageStartTestNew)); + else + memcpy(pchMessageStart, pchMessageStartPPCoin, sizeof(pchMessageStartPPCoin)); +} + +static const char* ppszTypeName[] = +{ + "ERROR", + "tx", + "block", +}; CMessageHeader::CMessageHeader() { - memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); + GetMessageStart(pchMessageStart); memset(pchCommand, 0, sizeof(pchCommand)); pchCommand[1] = 1; nMessageSize = -1; @@ -27,7 +45,7 @@ CMessageHeader::CMessageHeader() CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn) { - memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)); + GetMessageStart(pchMessageStart); strncpy(pchCommand, pszCommand, COMMAND_SIZE); nMessageSize = nMessageSizeIn; nChecksum = 0; @@ -44,7 +62,9 @@ std::string CMessageHeader::GetCommand() const bool CMessageHeader::IsValid() const { // Check start string - if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0) + unsigned char pchMessageStartProtocol[4]; + GetMessageStart(pchMessageStartProtocol); + if (memcmp(pchMessageStart, pchMessageStartProtocol, sizeof(pchMessageStart)) != 0) return false; // Check the command string for errors @@ -71,181 +91,78 @@ bool CMessageHeader::IsValid() const return true; } -CAddress::CAddress() -{ - Init(); -} -CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn) -{ - Init(); - ip = ipIn; - port = htons(portIn == 0 ? GetDefaultPort() : portIn); - nServices = nServicesIn; -} -CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn) +CAddress::CAddress() : CService() { Init(); - ip = sockaddr.sin_addr.s_addr; - port = sockaddr.sin_port; - nServices = nServicesIn; } -CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn) +CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn) { Init(); - Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn); -} - -CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn) -{ - Init(); - Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true); -} - -CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn) -{ - Init(); - Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn); -} - -CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn) -{ - Init(); - Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true); + nServices = nServicesIn; } void CAddress::Init() { nServices = NODE_NETWORK; - memcpy(pchReserved, pchIPv4, sizeof(pchReserved)); - ip = INADDR_NONE; - port = htons(GetDefaultPort()); nTime = 100000000; nLastTry = 0; } -bool operator==(const CAddress& a, const CAddress& b) +CInv::CInv() { - return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 && - a.ip == b.ip && - a.port == b.port); + type = 0; + hash = 0; } -bool operator!=(const CAddress& a, const CAddress& b) +CInv::CInv(int typeIn, const uint256& hashIn) { - return (!(a == b)); + type = typeIn; + hash = hashIn; } -bool operator<(const CAddress& a, const CAddress& b) +CInv::CInv(const std::string& strType, const uint256& hashIn) { - int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)); - if (ret < 0) - return true; - else if (ret == 0) + unsigned int i; + for (i = 1; i < ARRAYLEN(ppszTypeName); i++) { - if (ntohl(a.ip) < ntohl(b.ip)) - return true; - else if (a.ip == b.ip) - return ntohs(a.port) < ntohs(b.port); + if (strType == ppszTypeName[i]) + { + type = i; + break; + } } - return false; + if (i == ARRAYLEN(ppszTypeName)) + throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str())); + hash = hashIn; } -std::vector CAddress::GetKey() const +bool operator<(const CInv& a, const CInv& b) { - CDataStream ss; - ss.reserve(18); - ss << FLATDATA(pchReserved) << ip << port; - - #if defined(_MSC_VER) && _MSC_VER < 1300 - return std::vector((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]); - #else - return std::vector(ss.begin(), ss.end()); - #endif -} - -struct sockaddr_in CAddress::GetSockAddr() const -{ - struct sockaddr_in sockaddr; - memset(&sockaddr, 0, sizeof(sockaddr)); - sockaddr.sin_family = AF_INET; - sockaddr.sin_addr.s_addr = ip; - sockaddr.sin_port = port; - return sockaddr; + return (a.type < b.type || (a.type == b.type && a.hash < b.hash)); } -bool CAddress::IsIPv4() const +bool CInv::IsKnownType() const { - return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0); + return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName)); } -bool CAddress::IsRFC1918() const +const char* CInv::GetCommand() const { - return IsIPv4() && (GetByte(3) == 10 || - (GetByte(3) == 192 && GetByte(2) == 168) || - (GetByte(3) == 172 && - (GetByte(2) >= 16 && GetByte(2) <= 31))); + if (!IsKnownType()) + throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type)); + return ppszTypeName[type]; } -bool CAddress::IsRFC3927() const +std::string CInv::ToString() const { - return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254); -} - -bool CAddress::IsLocal() const -{ - return IsIPv4() && (GetByte(3) == 127 || - GetByte(3) == 0); -} - -bool CAddress::IsRoutable() const -{ - return IsValid() && - !(IsRFC1918() || IsRFC3927() || IsLocal()); -} - -bool CAddress::IsValid() const -{ - // Clean up 3-byte shifted addresses caused by garbage in size field - // of addr messages from versions before 0.2.9 checksum. - // Two consecutive addr messages look like this: - // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26... - // so if the first length field is garbled, it reads the second batch - // of addr misaligned by 3 bytes. - if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0) - return false; - - return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX)); + return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str()); } -unsigned char CAddress::GetByte(int n) const +void CInv::print() const { - return ((unsigned char*)&ip)[3-n]; + printf("CInv(%s)\n", ToString().c_str()); } -std::string CAddress::ToStringIPPort() const -{ - return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); -} - -std::string CAddress::ToStringIP() const -{ - return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0)); -} - -std::string CAddress::ToStringPort() const -{ - return strprintf("%u", ntohs(port)); -} - -std::string CAddress::ToString() const -{ - return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port)); -} - -void CAddress::print() const -{ - printf("CAddress(%s)\n", ToString().c_str()); -}