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 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels 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. namespace NetMsgType {
  12. const char *VERSION="version";
  13. const char *VERACK="verack";
  14. const char *ADDR="addr";
  15. const char *INV="inv";
  16. const char *GETDATA="getdata";
  17. const char *MERKLEBLOCK="merkleblock";
  18. const char *GETBLOCKS="getblocks";
  19. const char *GETHEADERS="getheaders";
  20. const char *TX="tx";
  21. const char *HEADERS="headers";
  22. const char *BLOCK="block";
  23. const char *GETADDR="getaddr";
  24. const char *MEMPOOL="mempool";
  25. const char *PING="ping";
  26. const char *PONG="pong";
  27. const char *NOTFOUND="notfound";
  28. const char *FILTERLOAD="filterload";
  29. const char *FILTERADD="filteradd";
  30. const char *FILTERCLEAR="filterclear";
  31. const char *REJECT="reject";
  32. const char *SENDHEADERS="sendheaders";
  33. const char *FEEFILTER="feefilter";
  34. const char *SENDCMPCT="sendcmpct";
  35. const char *CMPCTBLOCK="cmpctblock";
  36. const char *GETBLOCKTXN="getblocktxn";
  37. const char *BLOCKTXN="blocktxn";
  38. } // namespace NetMsgType
  39. /** All known message types. Keep this in the same order as the list of
  40. * messages above and in protocol.h.
  41. */
  42. const static std::string allNetMessageTypes[] = {
  43. NetMsgType::VERSION,
  44. NetMsgType::VERACK,
  45. NetMsgType::ADDR,
  46. NetMsgType::INV,
  47. NetMsgType::GETDATA,
  48. NetMsgType::MERKLEBLOCK,
  49. NetMsgType::GETBLOCKS,
  50. NetMsgType::GETHEADERS,
  51. NetMsgType::TX,
  52. NetMsgType::HEADERS,
  53. NetMsgType::BLOCK,
  54. NetMsgType::GETADDR,
  55. NetMsgType::MEMPOOL,
  56. NetMsgType::PING,
  57. NetMsgType::PONG,
  58. NetMsgType::NOTFOUND,
  59. NetMsgType::FILTERLOAD,
  60. NetMsgType::FILTERADD,
  61. NetMsgType::FILTERCLEAR,
  62. NetMsgType::REJECT,
  63. NetMsgType::SENDHEADERS,
  64. NetMsgType::FEEFILTER,
  65. NetMsgType::SENDCMPCT,
  66. NetMsgType::CMPCTBLOCK,
  67. NetMsgType::GETBLOCKTXN,
  68. NetMsgType::BLOCKTXN,
  69. };
  70. const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
  71. CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn)
  72. {
  73. memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
  74. memset(pchCommand, 0, sizeof(pchCommand));
  75. nMessageSize = -1;
  76. memset(pchChecksum, 0, CHECKSUM_SIZE);
  77. }
  78. CMessageHeader::CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn)
  79. {
  80. memcpy(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE);
  81. memset(pchCommand, 0, sizeof(pchCommand));
  82. strncpy(pchCommand, pszCommand, COMMAND_SIZE);
  83. nMessageSize = nMessageSizeIn;
  84. memset(pchChecksum, 0, CHECKSUM_SIZE);
  85. }
  86. std::string CMessageHeader::GetCommand() const
  87. {
  88. return std::string(pchCommand, pchCommand + strnlen(pchCommand, COMMAND_SIZE));
  89. }
  90. bool CMessageHeader::IsValid(const MessageStartChars& pchMessageStartIn) const
  91. {
  92. // Check start string
  93. if (memcmp(pchMessageStart, pchMessageStartIn, MESSAGE_START_SIZE) != 0)
  94. return false;
  95. // Check the command string for errors
  96. for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
  97. {
  98. if (*p1 == 0)
  99. {
  100. // Must be all zeros after the first zero
  101. for (; p1 < pchCommand + COMMAND_SIZE; p1++)
  102. if (*p1 != 0)
  103. return false;
  104. }
  105. else if (*p1 < ' ' || *p1 > 0x7E)
  106. return false;
  107. }
  108. // Message size
  109. if (nMessageSize > MAX_SIZE)
  110. {
  111. LogPrintf("CMessageHeader::IsValid(): (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand(), nMessageSize);
  112. return false;
  113. }
  114. return true;
  115. }
  116. CAddress::CAddress() : CService()
  117. {
  118. Init();
  119. }
  120. CAddress::CAddress(CService ipIn, ServiceFlags nServicesIn) : CService(ipIn)
  121. {
  122. Init();
  123. nServices = nServicesIn;
  124. }
  125. void CAddress::Init()
  126. {
  127. nServices = NODE_NONE;
  128. nTime = 100000000;
  129. }
  130. CInv::CInv()
  131. {
  132. type = 0;
  133. hash.SetNull();
  134. }
  135. CInv::CInv(int typeIn, const uint256& hashIn) : type(typeIn), hash(hashIn) {}
  136. bool operator<(const CInv& a, const CInv& b)
  137. {
  138. return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
  139. }
  140. std::string CInv::GetCommand() const
  141. {
  142. std::string cmd;
  143. if (type & MSG_WITNESS_FLAG)
  144. cmd.append("witness-");
  145. int masked = type & MSG_TYPE_MASK;
  146. switch (masked)
  147. {
  148. case MSG_TX: return cmd.append(NetMsgType::TX);
  149. case MSG_BLOCK: return cmd.append(NetMsgType::BLOCK);
  150. case MSG_FILTERED_BLOCK: return cmd.append(NetMsgType::MERKLEBLOCK);
  151. case MSG_CMPCT_BLOCK: return cmd.append(NetMsgType::CMPCTBLOCK);
  152. default:
  153. throw std::out_of_range(strprintf("CInv::GetCommand(): type=%d unknown type", type));
  154. }
  155. }
  156. std::string CInv::ToString() const
  157. {
  158. try {
  159. return strprintf("%s %s", GetCommand(), hash.ToString());
  160. } catch(const std::out_of_range &) {
  161. return strprintf("0x%08x %s", type, hash.ToString());
  162. }
  163. }
  164. const std::vector<std::string> &getAllNetMessageTypes()
  165. {
  166. return allNetMessageTypesVec;
  167. }