Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto
2 : // Copyright (c) 2009-2013 The Bitcoin Core developers
3 : // Distributed under the MIT software license, see the accompanying
4 : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 :
6 : #ifndef __cplusplus
7 : #error This header can only be compiled as C++.
8 : #endif
9 :
10 : #ifndef BITCOIN_PROTOCOL_H
11 : #define BITCOIN_PROTOCOL_H
12 :
13 : #include "netbase.h"
14 : #include "serialize.h"
15 : #include "uint256.h"
16 : #include "version.h"
17 :
18 : #include <stdint.h>
19 : #include <string>
20 :
21 : #define MESSAGE_START_SIZE 4
22 :
23 : /** Message header.
24 : * (4) message start.
25 : * (12) command.
26 : * (4) size.
27 : * (4) checksum.
28 : */
29 : class CMessageHeader
30 : {
31 : public:
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);
36 :
37 : std::string GetCommand() const;
38 : bool IsValid(const MessageStartChars& messageStart) const;
39 :
40 42180 : ADD_SERIALIZE_METHODS;
41 :
42 : template <typename Stream, typename Operation>
43 42180 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
44 : {
45 84128 : READWRITE(FLATDATA(pchMessageStart));
46 84128 : READWRITE(FLATDATA(pchCommand));
47 42180 : READWRITE(nMessageSize);
48 42180 : READWRITE(nChecksum);
49 42180 : }
50 :
51 : // TODO: make private (improves encapsulation)
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;
66 : };
67 :
68 : /** nServices flags */
69 : enum {
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.
73 : NODE_NETWORK = (1 << 0),
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),
78 : // NODE_BLOOM means the node is capable and willing to handle bloom-filtered connections.
79 : // Bitcoin Core nodes used to support this by default, without advertising this bit,
80 : // but no longer do as of protocol version 70011 (= NO_BLOOM_VERSION)
81 : NODE_BLOOM = (1 << 2),
82 :
83 : // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
84 : // isn't getting used, or one not being used much, and notify the
85 : // bitcoin-development mailing list. Remember that service bits are just
86 : // unauthenticated advertisements, so your code must be robust against
87 : // collisions and other cases where nodes may be advertising a service they
88 : // do not actually support. Other service bits should be allocated via the
89 : // BIP process.
90 : };
91 :
92 : /** A CService with information about it as peer */
93 : class CAddress : public CService
94 : {
95 : public:
96 : CAddress();
97 : explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK);
98 :
99 : void Init();
100 :
101 1030 : ADD_SERIALIZE_METHODS;
102 :
103 : template <typename Stream, typename Operation>
104 1030 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
105 : {
106 1030 : if (ser_action.ForRead())
107 500 : Init();
108 1030 : if (nType & SER_DISK)
109 0 : READWRITE(nVersion);
110 1030 : if ((nType & SER_DISK) ||
111 0 : (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
112 0 : READWRITE(nTime);
113 1030 : READWRITE(nServices);
114 1030 : READWRITE(*(CService*)this);
115 1030 : }
116 :
117 : // TODO: make private (improves encapsulation)
118 : public:
119 : uint64_t nServices;
120 :
121 : // disk and network only
122 : unsigned int nTime;
123 : };
124 :
125 : /** inv message data */
126 : class CInv
127 : {
128 : public:
129 : CInv();
130 : CInv(int typeIn, const uint256& hashIn);
131 : CInv(const std::string& strType, const uint256& hashIn);
132 :
133 24847 : ADD_SERIALIZE_METHODS;
134 :
135 : template <typename Stream, typename Operation>
136 24847 : inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
137 : {
138 24847 : READWRITE(type);
139 24847 : READWRITE(hash);
140 24847 : }
141 :
142 : friend bool operator<(const CInv& a, const CInv& b);
143 :
144 : bool IsKnownType() const;
145 : const char* GetCommand() const;
146 : std::string ToString() const;
147 :
148 : // TODO: make private (improves encapsulation)
149 : public:
150 : int type;
151 : uint256 hash;
152 : };
153 :
154 : enum {
155 : MSG_TX = 1,
156 : MSG_BLOCK,
157 : // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
158 : // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
159 : MSG_FILTERED_BLOCK,
160 : };
161 :
162 : #endif // BITCOIN_PROTOCOL_H
|