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.h 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2013 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. #ifndef __cplusplus
  6. # error This header can only be compiled as C++.
  7. #endif
  8. #ifndef __INCLUDED_PROTOCOL_H__
  9. #define __INCLUDED_PROTOCOL_H__
  10. #include "netbase.h"
  11. #include "serialize.h"
  12. #include "uint256.h"
  13. #include "version.h"
  14. #include <stdint.h>
  15. #include <string>
  16. #define MESSAGE_START_SIZE 4
  17. /** Message header.
  18. * (4) message start.
  19. * (12) command.
  20. * (4) size.
  21. * (4) checksum.
  22. */
  23. class CMessageHeader
  24. {
  25. public:
  26. CMessageHeader();
  27. CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
  28. std::string GetCommand() const;
  29. bool IsValid() const;
  30. IMPLEMENT_SERIALIZE
  31. (
  32. READWRITE(FLATDATA(pchMessageStart));
  33. READWRITE(FLATDATA(pchCommand));
  34. READWRITE(nMessageSize);
  35. READWRITE(nChecksum);
  36. )
  37. // TODO: make private (improves encapsulation)
  38. public:
  39. enum {
  40. COMMAND_SIZE=12,
  41. MESSAGE_SIZE_SIZE=sizeof(int),
  42. CHECKSUM_SIZE=sizeof(int),
  43. MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
  44. CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE,
  45. HEADER_SIZE=MESSAGE_START_SIZE+COMMAND_SIZE+MESSAGE_SIZE_SIZE+CHECKSUM_SIZE
  46. };
  47. char pchMessageStart[MESSAGE_START_SIZE];
  48. char pchCommand[COMMAND_SIZE];
  49. unsigned int nMessageSize;
  50. unsigned int nChecksum;
  51. };
  52. /** nServices flags */
  53. enum
  54. {
  55. NODE_NETWORK = (1 << 0),
  56. };
  57. /** A CService with information about it as peer */
  58. class CAddress : public CService
  59. {
  60. public:
  61. CAddress();
  62. explicit CAddress(CService ipIn, uint64_t nServicesIn=NODE_NETWORK);
  63. void Init();
  64. IMPLEMENT_SERIALIZE
  65. (
  66. CAddress* pthis = const_cast<CAddress*>(this);
  67. CService* pip = (CService*)pthis;
  68. if (fRead)
  69. pthis->Init();
  70. if (nType & SER_DISK)
  71. READWRITE(nVersion);
  72. if ((nType & SER_DISK) ||
  73. (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
  74. READWRITE(nTime);
  75. READWRITE(nServices);
  76. READWRITE(*pip);
  77. )
  78. void print() const;
  79. // TODO: make private (improves encapsulation)
  80. public:
  81. uint64_t nServices;
  82. // disk and network only
  83. unsigned int nTime;
  84. // memory only
  85. int64_t nLastTry;
  86. };
  87. /** inv message data */
  88. class CInv
  89. {
  90. public:
  91. CInv();
  92. CInv(int typeIn, const uint256& hashIn);
  93. CInv(const std::string& strType, const uint256& hashIn);
  94. IMPLEMENT_SERIALIZE
  95. (
  96. READWRITE(type);
  97. READWRITE(hash);
  98. )
  99. friend bool operator<(const CInv& a, const CInv& b);
  100. bool IsKnownType() const;
  101. const char* GetCommand() const;
  102. std::string ToString() const;
  103. void print() const;
  104. // TODO: make private (improves encapsulation)
  105. public:
  106. int type;
  107. uint256 hash;
  108. };
  109. enum
  110. {
  111. MSG_TX = 1,
  112. MSG_BLOCK,
  113. // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
  114. // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
  115. MSG_FILTERED_BLOCK,
  116. };
  117. #endif // __INCLUDED_PROTOCOL_H__