Replace several network protocol version numbers with named constants
[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 extern unsigned char pchMessageStart[4];
27
28 /** Message header.
29  * (4) message start.
30  * (12) command.
31  * (4) size.
32  * (4) checksum.
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 /** nServices flags */
61 enum
62 {
63     NODE_NETWORK = (1 << 0),
64 };
65
66 /** A CService with information about it as peer */
67 class CAddress : public CService
68 {
69     public:
70         CAddress();
71         explicit CAddress(CService ipIn, uint64 nServicesIn=NODE_NETWORK);
72
73         void Init();
74
75         IMPLEMENT_SERIALIZE
76             (
77              CAddress* pthis = const_cast<CAddress*>(this);
78              CService* pip = (CService*)pthis;
79              if (fRead)
80                  pthis->Init();
81              if (nType & SER_DISK)
82                  READWRITE(nVersion);
83              if ((nType & SER_DISK) ||
84                  (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
85                  READWRITE(nTime);
86              READWRITE(nServices);
87              READWRITE(*pip);
88             )
89
90         void print() const;
91
92     // TODO: make private (improves encapsulation)
93     public:
94         uint64 nServices;
95
96         // disk and network only
97         unsigned int nTime;
98
99         // memory only
100         int64 nLastTry;
101 };
102
103 /** inv message data */
104 class CInv
105 {
106     public:
107         CInv();
108         CInv(int typeIn, const uint256& hashIn);
109         CInv(const std::string& strType, const uint256& hashIn);
110
111         IMPLEMENT_SERIALIZE
112         (
113             READWRITE(type);
114             READWRITE(hash);
115         )
116
117         friend bool operator<(const CInv& a, const CInv& b);
118
119         bool IsKnownType() const;
120         const char* GetCommand() const;
121         std::string ToString() const;
122         void print() const;
123
124     // TODO: make private (improves encapsulation)
125     public:
126         int type;
127         uint256 hash;
128 };
129
130 #endif // __INCLUDED_PROTOCOL_H__