Update License in File Headers
[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 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 __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) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
84              READWRITE(nTime);
85              READWRITE(nServices);
86              READWRITE(*pip);
87             )
88
89         void print() const;
90
91     // TODO: make private (improves encapsulation)
92     public:
93         uint64 nServices;
94
95         // disk and network only
96         unsigned int nTime;
97
98         // memory only
99         int64 nLastTry;
100 };
101
102 /** inv message data */
103 class CInv
104 {
105     public:
106         CInv();
107         CInv(int typeIn, const uint256& hashIn);
108         CInv(const std::string& strType, const uint256& hashIn);
109
110         IMPLEMENT_SERIALIZE
111         (
112             READWRITE(type);
113             READWRITE(hash);
114         )
115
116         friend bool operator<(const CInv& a, const CInv& b);
117
118         bool IsKnownType() const;
119         const char* GetCommand() const;
120         std::string ToString() const;
121         void print() const;
122
123     // TODO: make private (improves encapsulation)
124     public:
125         int type;
126         uint256 hash;
127 };
128
129 #endif // __INCLUDED_PROTOCOL_H__