-
+ 8D8E7740244CD3B3BCE21DC5A394E1F8D7AA0B220C8E85D23DA61BB4E92C7CDE2C857AA27F86840411BDD83A9CBFAC64D3562E8EE30DBA53DCA4EB641A88C69B
bitcoin/src/protocol.cpp
(0 . 0)(1 . 343)
18532 // /****************************\
18533 // * EXPERIMENTAL BRANCH. *
18534 // * FOR LABORATORY USE ONLY. *
18535 // ********************************
18536 // ************
18537 // **************
18538 // ****************
18539 // **** **** ****
18540 // *** *** ***
18541 // *** *** ***
18542 // *** * * **
18543 // ******** ********
18544 // ******* ******
18545 // *** **
18546 // * ******* **
18547 // ** * * * * *
18548 // ** * * ***
18549 // **** * * * * ****
18550 // **** *** * * ** ***
18551 // **** ********* ******
18552 // ******* ***** *******
18553 // ********* ****** **
18554 // ** ****** ******
18555 // ** ******* **
18556 // ** ******* ***
18557 // **** ******** ************
18558 // ************ ************
18559 // ******** *******
18560 // ****** ****
18561 // *** ***
18562 // ********************************
18563 // Copyright (c) 2009-2010 Satoshi Nakamoto
18564 // Copyright (c) 2011 The Bitcoin developers
18565 // Distributed under the MIT/X11 software license, see the accompanying
18566 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
18567
18568 #include "protocol.h"
18569 #include "util.h"
18570
18571 #ifndef WIN32
18572 # include <arpa/inet.h>
18573 #endif
18574
18575 // Prototypes from net.h, but that header (currently) stinks, can't #include it without breaking things
18576 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
18577 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
18578
18579 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
18580 static const char* ppszTypeName[] =
18581 {
18582 "ERROR",
18583 "tx",
18584 "block",
18585 };
18586
18587 CMessageHeader::CMessageHeader()
18588 {
18589 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
18590 memset(pchCommand, 0, sizeof(pchCommand));
18591 pchCommand[1] = 1;
18592 nMessageSize = -1;
18593 nChecksum = 0;
18594 }
18595
18596 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
18597 {
18598 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
18599 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
18600 nMessageSize = nMessageSizeIn;
18601 nChecksum = 0;
18602 }
18603
18604 std::string CMessageHeader::GetCommand() const
18605 {
18606 if (pchCommand[COMMAND_SIZE-1] == 0)
18607 return std::string(pchCommand, pchCommand + strlen(pchCommand));
18608 else
18609 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
18610 }
18611
18612 bool CMessageHeader::IsValid() const
18613 {
18614 // Check start string
18615 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
18616 return false;
18617
18618 // Check the command string for errors
18619 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
18620 {
18621 if (*p1 == 0)
18622 {
18623 // Must be all zeros after the first zero
18624 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
18625 if (*p1 != 0)
18626 return false;
18627 }
18628 else if (*p1 < ' ' || *p1 > 0x7E)
18629 return false;
18630 }
18631
18632 // Message size
18633 if (nMessageSize > MAX_SIZE)
18634 {
18635 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
18636 return false;
18637 }
18638
18639 return true;
18640 }
18641
18642 CAddress::CAddress()
18643 {
18644 Init();
18645 }
18646
18647 CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
18648 {
18649 Init();
18650 ip = ipIn;
18651 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
18652 nServices = nServicesIn;
18653 }
18654
18655 CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
18656 {
18657 Init();
18658 ip = sockaddr.sin_addr.s_addr;
18659 port = sockaddr.sin_port;
18660 nServices = nServicesIn;
18661 }
18662
18663 CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn)
18664 {
18665 Init();
18666 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
18667 }
18668
18669 CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn)
18670 {
18671 Init();
18672 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
18673 }
18674
18675 CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn)
18676 {
18677 Init();
18678 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
18679 }
18680
18681 CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn)
18682 {
18683 Init();
18684 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
18685 }
18686
18687 void CAddress::Init()
18688 {
18689 nServices = NODE_NETWORK;
18690 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
18691 ip = INADDR_NONE;
18692 port = htons(GetDefaultPort());
18693 nTime = 100000000;
18694 nLastTry = 0;
18695 }
18696
18697 bool operator==(const CAddress& a, const CAddress& b)
18698 {
18699 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
18700 a.ip == b.ip &&
18701 a.port == b.port);
18702 }
18703
18704 bool operator!=(const CAddress& a, const CAddress& b)
18705 {
18706 return (!(a == b));
18707 }
18708
18709 bool operator<(const CAddress& a, const CAddress& b)
18710 {
18711 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
18712 if (ret < 0)
18713 return true;
18714 else if (ret == 0)
18715 {
18716 if (ntohl(a.ip) < ntohl(b.ip))
18717 return true;
18718 else if (a.ip == b.ip)
18719 return ntohs(a.port) < ntohs(b.port);
18720 }
18721 return false;
18722 }
18723
18724 std::vector<unsigned char> CAddress::GetKey() const
18725 {
18726 CDataStream ss;
18727 ss.reserve(18);
18728 ss << FLATDATA(pchReserved) << ip << port;
18729
18730 #if defined(_MSC_VER) && _MSC_VER < 1300
18731 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
18732 #else
18733 return std::vector<unsigned char>(ss.begin(), ss.end());
18734 #endif
18735 }
18736
18737 struct sockaddr_in CAddress::GetSockAddr() const
18738 {
18739 struct sockaddr_in sockaddr;
18740 memset(&sockaddr, 0, sizeof(sockaddr));
18741 sockaddr.sin_family = AF_INET;
18742 sockaddr.sin_addr.s_addr = ip;
18743 sockaddr.sin_port = port;
18744 return sockaddr;
18745 }
18746
18747 bool CAddress::IsIPv4() const
18748 {
18749 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
18750 }
18751
18752 bool CAddress::IsRFC1918() const
18753 {
18754 return IsIPv4() && (GetByte(3) == 10 ||
18755 (GetByte(3) == 192 && GetByte(2) == 168) ||
18756 (GetByte(3) == 172 &&
18757 (GetByte(2) >= 16 && GetByte(2) <= 31)));
18758 }
18759
18760 bool CAddress::IsRFC3927() const
18761 {
18762 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
18763 }
18764
18765 bool CAddress::IsLocal() const
18766 {
18767 return IsIPv4() && (GetByte(3) == 127 ||
18768 GetByte(3) == 0);
18769 }
18770
18771 bool CAddress::IsRoutable() const
18772 {
18773 return IsValid() &&
18774 !(IsRFC1918() || IsRFC3927() || IsLocal());
18775 }
18776
18777 bool CAddress::IsValid() const
18778 {
18779 // Clean up 3-byte shifted addresses caused by garbage in size field
18780 // of addr messages from versions before 0.2.9 checksum.
18781 // Two consecutive addr messages look like this:
18782 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
18783 // so if the first length field is garbled, it reads the second batch
18784 // of addr misaligned by 3 bytes.
18785 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
18786 return false;
18787
18788 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
18789 }
18790
18791 unsigned char CAddress::GetByte(int n) const
18792 {
18793 return ((unsigned char*)&ip)[3-n];
18794 }
18795
18796 std::string CAddress::ToStringIPPort() const
18797 {
18798 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
18799 }
18800
18801 std::string CAddress::ToStringIP() const
18802 {
18803 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
18804 }
18805
18806 std::string CAddress::ToStringPort() const
18807 {
18808 return strprintf("%u", ntohs(port));
18809 }
18810
18811 std::string CAddress::ToString() const
18812 {
18813 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
18814 }
18815
18816 void CAddress::print() const
18817 {
18818 printf("CAddress(%s)\n", ToString().c_str());
18819 }
18820
18821 CInv::CInv()
18822 {
18823 type = 0;
18824 hash = 0;
18825 }
18826
18827 CInv::CInv(int typeIn, const uint256& hashIn)
18828 {
18829 type = typeIn;
18830 hash = hashIn;
18831 }
18832
18833 CInv::CInv(const std::string& strType, const uint256& hashIn)
18834 {
18835 int i;
18836 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
18837 {
18838 if (strType == ppszTypeName[i])
18839 {
18840 type = i;
18841 break;
18842 }
18843 }
18844 if (i == ARRAYLEN(ppszTypeName))
18845 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
18846 hash = hashIn;
18847 }
18848
18849 bool operator<(const CInv& a, const CInv& b)
18850 {
18851 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
18852 }
18853
18854 bool CInv::IsKnownType() const
18855 {
18856 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
18857 }
18858
18859 const char* CInv::GetCommand() const
18860 {
18861 if (!IsKnownType())
18862 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
18863 return ppszTypeName[type];
18864 }
18865
18866 std::string CInv::ToString() const
18867 {
18868 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
18869 }
18870
18871 void CInv::print() const
18872 {
18873 printf("CInv(%s)\n", ToString().c_str());
18874 }