選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

protocol.cpp 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2012 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 "util.h"
  7. #include "netbase.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()
  19. {
  20. memcpy(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE);
  21. memset(pchCommand, 0, sizeof(pchCommand));
  22. pchCommand[1] = 1;
  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. strncpy(pchCommand, pszCommand, COMMAND_SIZE);
  30. nMessageSize = nMessageSizeIn;
  31. nChecksum = 0;
  32. }
  33. std::string CMessageHeader::GetCommand() const
  34. {
  35. if (pchCommand[COMMAND_SIZE-1] == 0)
  36. return std::string(pchCommand, pchCommand + strlen(pchCommand));
  37. else
  38. return std::string(pchCommand, pchCommand + COMMAND_SIZE);
  39. }
  40. bool CMessageHeader::IsValid() const
  41. {
  42. // Check start string
  43. if (memcmp(pchMessageStart, Params().MessageStart(), MESSAGE_START_SIZE) != 0)
  44. return false;
  45. // Check the command string for errors
  46. for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
  47. {
  48. if (*p1 == 0)
  49. {
  50. // Must be all zeros after the first zero
  51. for (; p1 < pchCommand + COMMAND_SIZE; p1++)
  52. if (*p1 != 0)
  53. return false;
  54. }
  55. else if (*p1 < ' ' || *p1 > 0x7E)
  56. return false;
  57. }
  58. // Message size
  59. if (nMessageSize > MAX_SIZE)
  60. {
  61. printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
  62. return false;
  63. }
  64. return true;
  65. }
  66. CAddress::CAddress() : CService()
  67. {
  68. Init();
  69. }
  70. CAddress::CAddress(CService ipIn, uint64 nServicesIn) : CService(ipIn)
  71. {
  72. Init();
  73. nServices = nServicesIn;
  74. }
  75. void CAddress::Init()
  76. {
  77. nServices = NODE_NETWORK;
  78. nTime = 100000000;
  79. nLastTry = 0;
  80. }
  81. CInv::CInv()
  82. {
  83. type = 0;
  84. hash = 0;
  85. }
  86. CInv::CInv(int typeIn, const uint256& hashIn)
  87. {
  88. type = typeIn;
  89. hash = hashIn;
  90. }
  91. CInv::CInv(const std::string& strType, const uint256& hashIn)
  92. {
  93. unsigned int i;
  94. for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
  95. {
  96. if (strType == ppszTypeName[i])
  97. {
  98. type = i;
  99. break;
  100. }
  101. }
  102. if (i == ARRAYLEN(ppszTypeName))
  103. throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
  104. hash = hashIn;
  105. }
  106. bool operator<(const CInv& a, const CInv& b)
  107. {
  108. return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
  109. }
  110. bool CInv::IsKnownType() const
  111. {
  112. return (type >= 1 && type < (int)ARRAYLEN(ppszTypeName));
  113. }
  114. const char* CInv::GetCommand() const
  115. {
  116. if (!IsKnownType())
  117. throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
  118. return ppszTypeName[type];
  119. }
  120. std::string CInv::ToString() const
  121. {
  122. return strprintf("%s %s", GetCommand(), hash.ToString().c_str());
  123. }
  124. void CInv::print() const
  125. {
  126. printf("CInv(%s)\n", ToString().c_str());
  127. }