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.
7 #error This header can only be compiled as C++.
10 #ifndef BITCOIN_PROTOCOL_H
11 #define BITCOIN_PROTOCOL_H
14 #include "serialize.h"
21 #define MESSAGE_START_SIZE 4
32 typedef unsigned char MessageStartChars[MESSAGE_START_SIZE];
34 CMessageHeader(const MessageStartChars& pchMessageStartIn);
35 CMessageHeader(const MessageStartChars& pchMessageStartIn, const char* pszCommand, unsigned int nMessageSizeIn);
37 std::string GetCommand() const;
38 bool IsValid(const MessageStartChars& messageStart) const;
40 ADD_SERIALIZE_METHODS;
42 template <typename Stream, typename Operation>
43 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
45 READWRITE(FLATDATA(pchMessageStart));
46 READWRITE(FLATDATA(pchCommand));
47 READWRITE(nMessageSize);
51 // TODO: make private (improves encapsulation)
55 MESSAGE_SIZE_SIZE = sizeof(int),
56 CHECKSUM_SIZE = sizeof(int),
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
62 char pchMessageStart[MESSAGE_START_SIZE];
63 char pchCommand[COMMAND_SIZE];
64 unsigned int nMessageSize;
65 unsigned int nChecksum;
68 /** nServices flags */
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),
75 // Bits 24-31 are reserved for temporary experiments. Just pick a bit that
76 // isn't getting used, or one not being used much, and notify the
77 // bitcoin-development mailing list. Remember that service bits are just
78 // unauthenticated advertisements, so your code must be robust against
79 // collisions and other cases where nodes may be advertising a service they
80 // do not actually support. Other service bits should be allocated via the
84 /** A CService with information about it as peer */
85 class CAddress : public CService
89 explicit CAddress(CService ipIn, uint64_t nServicesIn = NODE_NETWORK);
93 ADD_SERIALIZE_METHODS;
95 template <typename Stream, typename Operation>
96 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
98 if (ser_action.ForRead())
100 if (nType & SER_DISK)
102 if ((nType & SER_DISK) ||
103 (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
105 READWRITE(nServices);
106 READWRITE(*(CService*)this);
109 // TODO: make private (improves encapsulation)
113 // disk and network only
117 /** inv message data */
122 CInv(int typeIn, const uint256& hashIn);
123 CInv(const std::string& strType, const uint256& hashIn);
125 ADD_SERIALIZE_METHODS;
127 template <typename Stream, typename Operation>
128 inline void SerializationOp(Stream& s, Operation ser_action, int nType, int nVersion)
134 friend bool operator<(const CInv& a, const CInv& b);
136 bool IsKnownType() const;
137 const char* GetCommand() const;
138 std::string ToString() const;
140 // TODO: make private (improves encapsulation)
149 // Nodes may always request a MSG_FILTERED_BLOCK in a getdata, however,
150 // MSG_FILTERED_BLOCK should not appear in any invs except as a part of getdata.
154 #endif // BITCOIN_PROTOCOL_H