You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

protocol.cpp 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2014 The Bitcoin developers
  3. // Distributed under the MIT/X11 software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "protocol.h"
  6. #include "chainparams.h"
  7. #include "util.h"
  8. #include "utilstrencodings.h"
  9. #ifndef WIN32
  10. # include <arpa/inet.h>
  11. #endif
  12. static const char* ppszTypeName[] =
  13. {
  14. "ERROR",
  15. "tx",
  16. "block",
  17. "filtered block"
  18. };
  19. CMessageHeader::CMessageHeader()
  20. {
  21. memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
  22. memset(pchCommand, 0, sizeof(pchCommand));
  23. nMessageSize = -1;
  24. nChecksum = 0;
  25. }
  26. CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
  27. {
  28. memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
  29. memset(pchCommand, 0, sizeof(pchCommand));
  30. strncpy(pchCommand, pszCommand, COMMAND_SIZE);
  31. nMessageSize = nMessageSizeIn;
  32. nChecksum = 0;
  33. }
  34. std::string CMessageHeader::GetCommand() const
  35. {
  36. return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
  37. }
  38. bool CMessageHeader::IsValid() const
  39. {
  40. // Check start string
  41. if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
  42. return false;
  43. // Check the command string for errors
  44. for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
  45. {
  46. if (*p1 == 0)
  47. {
  48. // Must be all zeros after the first zero
  49. for (; p1 < pchCommand + COMMAND_SIZE; p1++)
  50. if (*p1 != 0)
  51. return false;
  52. }
  53. else if (*p1 < ' ' || *p1 > 0x7E)
  54. return false;
  55. }
  56. // Message size
  57. if (nMessageSize > MAX_SIZE)
  58. {
  59. LogPrintf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
  60. return false;
  61. }
  62. return true;
  63. }
  64. CAddress::CAddress() : CService()
  65. {
  66. Init();
  67. }
  68. CAddress::CAddress(CService ipIn, uint64_t nServicesIn) : CService(ipIn)
  69. {
  70. Init();
  71. nServices = nServicesIn;
  72. }
  73. void CAddress::Init()
  74. {
  75. nServices = NODE_NETWORK;
  76. nTime = 100000000;
  77. nLastTry = 0;
  78. }
  79. CInv::CInv()
  80. {
  81. type = 0;
  82. hash = 0;
  83. }
  84. CInv::CInv(int typeIn, const uint256& hashIn)
  85. {
  86. type = typeIn;
  87. hash = hashIn;
  88. }
  89. CInv::CInv(const std::string& strType, const uint256& hashIn)
  90. {
  91. unsigned int i;
  92. for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
  93. {
  94. if (strType == ppszTypeName[i])
  95. {
  96. type = i;
  97. break;
  98. }
  99. }
  100. if (i == ARRAYLEN(ppszTypeName))
  101. throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType));
  102. hash = hashIn;
  103. }
  104. bool operator<(const CInv& a, const CInv& b)
  105. {
  106. return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
  107. }
  108. bool CInv::IsKnownType() const
  109. {
  110. return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
  111. }
  112. const char* CInv::GetCommand() const
  113. {
  114. if (!IsKnownType())
  115. throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
  116. return ppszTypeName[type];
  117. }
  118. std::string CInv::ToString() const
  119. {
  120. return strprintf("%s %s", GetCommand(), hash.ToString());
  121. }