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.3KB

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