CAddrMan: stochastic address manager
[novacoin.git] / src / protocol.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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 "netbase.h"
15 #include "util.h"
16 #include <string>
17 #include "uint256.h"
18
19 extern bool fTestNet;
20 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
21 {
22     return testnet ? 18333 : 8333;
23 }
24
25 //
26 // Message header
27 //  (4) message start
28 //  (12) command
29 //  (4) size
30 //  (4) checksum
31
32 extern unsigned char pchMessageStart[4];
33
34 class CMessageHeader
35 {
36     public:
37         CMessageHeader();
38         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
39
40         std::string GetCommand() const;
41         bool IsValid() const;
42
43         IMPLEMENT_SERIALIZE
44             (
45              READWRITE(FLATDATA(pchMessageStart));
46              READWRITE(FLATDATA(pchCommand));
47              READWRITE(nMessageSize);
48              READWRITE(nChecksum);
49             )
50
51     // TODO: make private (improves encapsulation)
52     public:
53         enum { COMMAND_SIZE=12 };
54         char pchMessageStart[sizeof(::pchMessageStart)];
55         char pchCommand[COMMAND_SIZE];
56         unsigned int nMessageSize;
57         unsigned int nChecksum;
58 };
59
60 enum
61 {
62     NODE_NETWORK = (1 << 0),
63 };
64
65 class CAddress : public CService
66 {
67     public:
68         CAddress();
69         explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
70
71         void Init();
72
73         IMPLEMENT_SERIALIZE
74             (
75              CAddress* pthis = const_cast<CAddress*>(this);
76              CService* pip = (CService*)pthis;
77              if (fRead)
78                  pthis->Init();
79              if (nType & SER_DISK)
80              READWRITE(nVersion);
81              if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
82              READWRITE(nTime);
83              READWRITE(nServices);
84              READWRITE(*pip);
85             )
86
87         void print() const;
88
89     // TODO: make private (improves encapsulation)
90     public:
91         uint64 nServices;
92
93         // disk and network only
94         unsigned int nTime;
95
96         // memory only
97         int64 nLastTry;
98 };
99
100 class CInv
101 {
102     public:
103         CInv();
104         CInv(int typeIn, const uint256& hashIn);
105         CInv(const std::string& strType, const uint256& hashIn);
106
107         IMPLEMENT_SERIALIZE
108         (
109             READWRITE(type);
110             READWRITE(hash);
111         )
112
113         friend bool operator<(const CInv& a, const CInv& b);
114
115         bool IsKnownType() const;
116         const char* GetCommand() const;
117         std::string ToString() const;
118         void print() const;
119
120     // TODO: make private (improves encapsulation)
121     public:
122         int type;
123         uint256 hash;
124 };
125
126 #endif // __INCLUDED_PROTOCOL_H__