]>
Commit | Line | Data |
---|---|---|
507fd9d1 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
db0e8ccd | 2 | // Copyright (c) 2009-2013 The Bitcoin developers |
507fd9d1 | 3 | // Distributed under the MIT/X11 software license, see the accompanying |
3a25a2b9 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
507fd9d1 GS |
5 | |
6 | #ifndef __cplusplus | |
20e01b1a | 7 | #error This header can only be compiled as C++. |
507fd9d1 GS |
8 | #endif |
9 | ||
84738627 PJ |
10 | #ifndef BITCOIN_PROTOCOL_H |
11 | #define BITCOIN_PROTOCOL_H | |
507fd9d1 | 12 | |
67a42f92 | 13 | #include "netbase.h" |
51ed9ec9 | 14 | #include "serialize.h" |
e4dde849 | 15 | #include "uint256.h" |
ccc84e09 | 16 | #include "version.h" |
507fd9d1 | 17 | |
51ed9ec9 BD |
18 | #include <stdint.h> |
19 | #include <string> | |
20 | ||
c8c52de3 | 21 | #define MESSAGE_START_SIZE 4 |
22 | ||
6b8de05d PW |
23 | /** Message header. |
24 | * (4) message start. | |
25 | * (12) command. | |
26 | * (4) size. | |
27 | * (4) checksum. | |
28 | */ | |
507fd9d1 GS |
29 | class CMessageHeader |
30 | { | |
20e01b1a PW |
31 | public: |
32 | CMessageHeader(); | |
33 | CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn); | |
507fd9d1 | 34 | |
20e01b1a PW |
35 | std::string GetCommand() const; |
36 | bool IsValid() const; | |
507fd9d1 | 37 | |
20e01b1a | 38 | ADD_SERIALIZE_METHODS; |
3d796f89 | 39 | |
20e01b1a PW |
40 | template <typename Stream, typename Operation> |
41 | inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) | |
42 | { | |
43 | READWRITE(FLATDATA(pchMessageStart)); | |
44 | READWRITE(FLATDATA(pchCommand)); | |
45 | READWRITE(nMessageSize); | |
46 | READWRITE(nChecksum); | |
47 | } | |
507fd9d1 GS |
48 | |
49 | // TODO: make private (improves encapsulation) | |
20e01b1a PW |
50 | public: |
51 | enum { | |
52 | COMMAND_SIZE = 12, | |
53 | MESSAGE_SIZE_SIZE = sizeof(int), | |
54 | CHECKSUM_SIZE = sizeof(int), | |
55 | ||
56 | MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE, | |
57 | CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE, | |
58 | HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE | |
59 | }; | |
60 | char pchMessageStart[MESSAGE_START_SIZE]; | |
61 | char pchCommand[COMMAND_SIZE]; | |
62 | unsigned int nMessageSize; | |
63 | unsigned int nChecksum; | |
507fd9d1 GS |
64 | }; |
65 | ||
6b8de05d | 66 | /** nServices flags */ |
20e01b1a | 67 | enum { |
33e28c99 | 68 | NODE_NETWORK = (1 << 0), |
38405ac1 PT |
69 | |
70 | // Bits 24-31 are reserved for temporary experiments. Just pick a bit that | |
71 | // isn't getting used, or one not being used much, and notify the | |
72 | // bitcoin-development mailing list. Remember that service bits are just | |
73 | // unauthenticated advertisements, so your code must be robust against | |
74 | // collisions and other cases where nodes may be advertising a service they | |
75 | // do not actually support. Other service bits should be allocated via the | |
76 | // BIP process. | |
33e28c99 GS |
77 | }; |
78 | ||
6b8de05d | 79 | /** A CService with information about it as peer */ |
67a42f92 | 80 | class CAddress : public CService |
33e28c99 | 81 | { |
20e01b1a PW |
82 | public: |
83 | CAddress(); | |
84 | explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK); | |
85 | ||
86 | void Init(); | |
87 | ||
88 | ADD_SERIALIZE_METHODS; | |
89 | ||
90 | template <typename Stream, typename Operation> | |
91 | inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) | |
92 | { | |
93 | if (ser_action.ForRead()) | |
94 | Init(); | |
95 | if (nType & SER_DISK) | |
96 | READWRITE(nVersion); | |
97 | if ((nType & SER_DISK) || | |
98 | (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) | |
99 | READWRITE(nTime); | |
100 | READWRITE(nServices); | |
101 | READWRITE(*(CService*)this); | |
102 | } | |
33e28c99 | 103 | |
33e28c99 | 104 | // TODO: make private (improves encapsulation) |
20e01b1a PW |
105 | public: |
106 | uint64_t nServices; | |
33e28c99 | 107 | |
20e01b1a PW |
108 | // disk and network only |
109 | unsigned int nTime; | |
33e28c99 | 110 | |
20e01b1a PW |
111 | // memory only |
112 | int64_t nLastTry; | |
33e28c99 GS |
113 | }; |
114 | ||
6b8de05d | 115 | /** inv message data */ |
e4dde849 GS |
116 | class CInv |
117 | { | |
20e01b1a PW |
118 | public: |
119 | CInv(); | |
120 | CInv(int typeIn, const uint256& hashIn); | |
121 | CInv(const std::string& strType, const uint256& hashIn); | |
e4dde849 | 122 | |
20e01b1a | 123 | ADD_SERIALIZE_METHODS; |
3d796f89 | 124 | |
20e01b1a PW |
125 | template <typename Stream, typename Operation> |
126 | inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) | |
127 | { | |
128 | READWRITE(type); | |
129 | READWRITE(hash); | |
130 | } | |
e4dde849 | 131 | |
20e01b1a | 132 | friend bool operator<(const CInv& a, const CInv& b); |
e4dde849 | 133 | |
20e01b1a PW |
134 | bool IsKnownType() const; |
135 | const char* GetCommand() const; | |
136 | std::string ToString() const; | |
e4dde849 GS |
137 | |
138 | // TODO: make private (improves encapsulation) | |
20e01b1a PW |
139 | public: |
140 | int type; | |
141 | uint256 hash; | |
e4dde849 GS |
142 | }; |
143 | ||
20e01b1a | 144 | enum { |
f3a84c3a LD |
145 | MSG_TX = 1, |
146 | MSG_BLOCK, | |
b02ddbed MC |
147 | // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however, |
148 | // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata. | |
149 | MSG_FILTERED_BLOCK, | |
f3a84c3a LD |
150 | }; |
151 | ||
84738627 | 152 | #endif // BITCOIN_PROTOCOL_H |