Projects : bitcoin : bitcoin_db_shutdown_checkpoint_calming
1 | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | // Copyright (c) 2011 The Bitcoin developers |
3 | // Distributed under the MIT/X11 software license, see the accompanying |
4 | // file license.txt 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 __INCLUDED_PROTOCOL_H__ |
11 | #define __INCLUDED_PROTOCOL_H__ |
12 | |
13 | #include "serialize.h" |
14 | #include <string> |
15 | #include "uint256.h" |
16 | |
17 | static inline unsigned short GetDefaultPort() |
18 | { |
19 | return 8333; |
20 | } |
21 | |
22 | // |
23 | // Message header |
24 | // (4) message start |
25 | // (12) command |
26 | // (4) size |
27 | // (4) checksum |
28 | |
29 | extern unsigned char pchMessageStart[4]; |
30 | |
31 | class CMessageHeader |
32 | { |
33 | public: |
34 | CMessageHeader(); |
35 | CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn); |
36 | |
37 | std::string GetCommand() const; |
38 | bool IsValid() const; |
39 | |
40 | IMPLEMENT_SERIALIZE |
41 | ( |
42 | READWRITE(FLATDATA(pchMessageStart)); |
43 | READWRITE(FLATDATA(pchCommand)); |
44 | READWRITE(nMessageSize); |
45 | if (nVersion >= 209) |
46 | READWRITE(nChecksum); |
47 | ) |
48 | |
49 | // TODO: make private (improves encapsulation) |
50 | public: |
51 | enum { COMMAND_SIZE=12 }; |
52 | char pchMessageStart[sizeof(::pchMessageStart)]; |
53 | char pchCommand[COMMAND_SIZE]; |
54 | unsigned int nMessageSize; |
55 | unsigned int nChecksum; |
56 | }; |
57 | |
58 | enum |
59 | { |
60 | NODE_NETWORK = (1 << 0), |
61 | }; |
62 | |
63 | class CAddress |
64 | { |
65 | public: |
66 | CAddress(); |
67 | CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK); |
68 | explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK); |
69 | explicit CAddress(const char* pszIn, int portIn, uint64 nServicesIn=NODE_NETWORK); |
70 | explicit CAddress(const char* pszIn, uint64 nServicesIn=NODE_NETWORK); |
71 | explicit CAddress(std::string strIn, int portIn, uint64 nServicesIn=NODE_NETWORK); |
72 | explicit CAddress(std::string strIn, uint64 nServicesIn=NODE_NETWORK); |
73 | |
74 | void Init(); |
75 | |
76 | IMPLEMENT_SERIALIZE |
77 | ( |
78 | if (fRead) |
79 | const_cast<CAddress*>(this)->Init(); |
80 | if (nType & SER_DISK) |
81 | READWRITE(nVersion); |
82 | if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH))) |
83 | READWRITE(nTime); |
84 | READWRITE(nServices); |
85 | READWRITE(FLATDATA(pchReserved)); // for IPv6 |
86 | READWRITE(ip); |
87 | READWRITE(port); |
88 | ) |
89 | |
90 | friend bool operator==(const CAddress& a, const CAddress& b); |
91 | friend bool operator!=(const CAddress& a, const CAddress& b); |
92 | friend bool operator<(const CAddress& a, const CAddress& b); |
93 | |
94 | std::vector<unsigned char> GetKey() const; |
95 | struct sockaddr_in GetSockAddr() const; |
96 | bool IsIPv4() const; |
97 | bool IsRFC1918() const; |
98 | bool IsRFC3927() const; |
99 | bool IsLocal() const; |
100 | bool IsRoutable() const; |
101 | bool IsValid() const; |
102 | unsigned char GetByte(int n) const; |
103 | std::string ToStringIPPort() const; |
104 | std::string ToStringIP() const; |
105 | std::string ToStringPort() const; |
106 | std::string ToString() const; |
107 | void print() const; |
108 | |
109 | // TODO: make private (improves encapsulation) |
110 | public: |
111 | uint64 nServices; |
112 | unsigned char pchReserved[12]; |
113 | unsigned int ip; |
114 | unsigned short port; |
115 | |
116 | // disk and network only |
117 | unsigned int nTime; |
118 | |
119 | // memory only |
120 | unsigned int nLastTry; |
121 | }; |
122 | |
123 | class CInv |
124 | { |
125 | public: |
126 | CInv(); |
127 | CInv(int typeIn, const uint256& hashIn); |
128 | CInv(const std::string& strType, const uint256& hashIn); |
129 | |
130 | IMPLEMENT_SERIALIZE |
131 | ( |
132 | READWRITE(type); |
133 | READWRITE(hash); |
134 | ) |
135 | |
136 | friend bool operator<(const CInv& a, const CInv& b); |
137 | |
138 | bool IsKnownType() const; |
139 | const char* GetCommand() const; |
140 | std::string ToString() const; |
141 | void print() const; |
142 | |
143 | // TODO: make private (improves encapsulation) |
144 | public: |
145 | int type; |
146 | uint256 hash; |
147 | }; |
148 | |
149 | #endif // __INCLUDED_PROTOCOL_H__ |