]>
Commit | Line | Data |
---|---|---|
507fd9d1 | 1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
f914f1a7 | 2 | // Copyright (c) 2009-2013 The Bitcoin Core developers |
78253fcb | 3 | // Distributed under the MIT 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 | 31 | public: |
eec37136 CF |
32 | typedef unsigned char MessageStartChars[MESSAGE_START_SIZE]; |
33 | ||
34 | CMessageHeader(const MessageStartChars& pchMessageStartIn); | |
35 | CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn); | |
507fd9d1 | 36 | |
20e01b1a | 37 | std::string GetCommand() const; |
eec37136 | 38 | bool IsValid(const MessageStartChars& messageStart) const; |
507fd9d1 | 39 | |
20e01b1a | 40 | ADD_SERIALIZE_METHODS; |
3d796f89 | 41 | |
20e01b1a PW |
42 | template <typename Stream, typename Operation> |
43 | inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) | |
44 | { | |
45 | READWRITE(FLATDATA(pchMessageStart)); | |
46 | READWRITE(FLATDATA(pchCommand)); | |
47 | READWRITE(nMessageSize); | |
48 | READWRITE(nChecksum); | |
49 | } | |
507fd9d1 GS |
50 | |
51 | // TODO: make private (improves encapsulation) | |
20e01b1a PW |
52 | public: |
53 | enum { | |
54 | COMMAND_SIZE = 12, | |
55 | MESSAGE_SIZE_SIZE = sizeof(int), | |
56 | CHECKSUM_SIZE = sizeof(int), | |
57 | ||
58 | MESSAGE_SIZE_OFFSET = MESSAGE_START_SIZE + COMMAND_SIZE, | |
59 | CHECKSUM_OFFSET = MESSAGE_SIZE_OFFSET + MESSAGE_SIZE_SIZE, | |
60 | HEADER_SIZE = MESSAGE_START_SIZE + COMMAND_SIZE + MESSAGE_SIZE_SIZE + CHECKSUM_SIZE | |
61 | }; | |
62 | char pchMessageStart[MESSAGE_START_SIZE]; | |
63 | char pchCommand[COMMAND_SIZE]; | |
64 | unsigned int nMessageSize; | |
65 | unsigned int nChecksum; | |
507fd9d1 GS |
66 | }; |
67 | ||
6b8de05d | 68 | /** nServices flags */ |
20e01b1a | 69 | enum { |
5983a4e5 MH |
70 | // NODE_NETWORK means that the node is capable of serving the block chain. It is currently |
71 | // set by all Bitcoin Core nodes, and is unset by SPV clients or other peers that just want | |
72 | // network services but don't provide them. | |
33e28c99 | 73 | NODE_NETWORK = (1 << 0), |
5983a4e5 MH |
74 | // NODE_GETUTXO means the node is capable of responding to the getutxo protocol request. |
75 | // Bitcoin Core does not support this but a patch set called Bitcoin XT does. | |
76 | // See BIP 64 for details on how this is implemented. | |
77 | NODE_GETUTXO = (1 << 1), | |
38405ac1 PT |
78 | |
79 | // Bits 24-31 are reserved for temporary experiments. Just pick a bit that | |
80 | // isn't getting used, or one not being used much, and notify the | |
81 | // bitcoin-development mailing list. Remember that service bits are just | |
82 | // unauthenticated advertisements, so your code must be robust against | |
83 | // collisions and other cases where nodes may be advertising a service they | |
84 | // do not actually support. Other service bits should be allocated via the | |
85 | // BIP process. | |
33e28c99 GS |
86 | }; |
87 | ||
6b8de05d | 88 | /** A CService with information about it as peer */ |
67a42f92 | 89 | class CAddress : public CService |
33e28c99 | 90 | { |
20e01b1a PW |
91 | public: |
92 | CAddress(); | |
93 | explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK); | |
94 | ||
95 | void Init(); | |
96 | ||
97 | ADD_SERIALIZE_METHODS; | |
98 | ||
99 | template <typename Stream, typename Operation> | |
100 | inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) | |
101 | { | |
102 | if (ser_action.ForRead()) | |
103 | Init(); | |
104 | if (nType & SER_DISK) | |
105 | READWRITE(nVersion); | |
106 | if ((nType & SER_DISK) || | |
107 | (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH))) | |
108 | READWRITE(nTime); | |
109 | READWRITE(nServices); | |
110 | READWRITE(*(CService*)this); | |
111 | } | |
33e28c99 | 112 | |
33e28c99 | 113 | // TODO: make private (improves encapsulation) |
20e01b1a PW |
114 | public: |
115 | uint64_t nServices; | |
33e28c99 | 116 | |
20e01b1a PW |
117 | // disk and network only |
118 | unsigned int nTime; | |
33e28c99 GS |
119 | }; |
120 | ||
6b8de05d | 121 | /** inv message data */ |
e4dde849 GS |
122 | class CInv |
123 | { | |
20e01b1a PW |
124 | public: |
125 | CInv(); | |
126 | CInv(int typeIn, const uint256& hashIn); | |
127 | CInv(const std::string& strType, const uint256& hashIn); | |
e4dde849 | 128 | |
20e01b1a | 129 | ADD_SERIALIZE_METHODS; |
3d796f89 | 130 | |
20e01b1a PW |
131 | template <typename Stream, typename Operation> |
132 | inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion) | |
133 | { | |
134 | READWRITE(type); | |
135 | READWRITE(hash); | |
136 | } | |
e4dde849 | 137 | |
20e01b1a | 138 | friend bool operator<(const CInv& a, const CInv& b); |
e4dde849 | 139 | |
20e01b1a PW |
140 | bool IsKnownType() const; |
141 | const char* GetCommand() const; | |
142 | std::string ToString() const; | |
e4dde849 GS |
143 | |
144 | // TODO: make private (improves encapsulation) | |
20e01b1a PW |
145 | public: |
146 | int type; | |
147 | uint256 hash; | |
e4dde849 GS |
148 | }; |
149 | ||
20e01b1a | 150 | enum { |
f3a84c3a LD |
151 | MSG_TX = 1, |
152 | MSG_BLOCK, | |
b02ddbed MC |
153 | // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however, |
154 | // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata. | |
155 | MSG_FILTERED_BLOCK, | |
f3a84c3a LD |
156 | }; |
157 | ||
84738627 | 158 | #endif // BITCOIN_PROTOCOL_H |