06165d87169d453981b9359bb925135180f19c93
[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 <string>
16 #include <limits>
17 #include <vector>
18 #include "uint256.h"
19
20 extern uint32_t nNetworkID;
21 extern bool fTestNet;
22 inline uint16_t GetDefaultPort()
23 {
24     return static_cast<uint16_t>(fTestNet ? 17777 : 7777);
25 }
26
27 /** Message header.
28  * (4) network identifier.
29  * (12) command.
30  * (4) size.
31  * (4) checksum.
32  */
33 class CMessageHeader
34 {
35     public:
36         CMessageHeader();
37         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
38
39         std::string GetCommand() const;
40         bool IsValid() const;
41
42         IMPLEMENT_SERIALIZE
43             (
44              READWRITE(nNetworkID);
45              READWRITE(FLATDATA(pchCommand));
46              READWRITE(nMessageSize);
47              READWRITE(nChecksum);
48             )
49
50     // TODO: make private (improves encapsulation)
51     public:
52         enum {
53             MESSAGE_START_SIZE=4,
54             COMMAND_SIZE=12,
55             MESSAGE_SIZE_SIZE=4,
56             CHECKSUM_SIZE=4,
57
58             MESSAGE_SIZE_OFFSET=MESSAGE_START_SIZE+COMMAND_SIZE,
59             CHECKSUM_OFFSET=MESSAGE_SIZE_OFFSET+MESSAGE_SIZE_SIZE
60         };
61         uint32_t nNetworkID;
62         char pchCommand[COMMAND_SIZE];
63         uint32_t nMessageSize;
64         uint32_t nChecksum;
65 };
66
67 /** nServices flags */
68 enum
69 {
70     NODE_NETWORK = (1 << 0)
71 };
72
73 /** A CService with information about it as peer */
74 class CAddress : public CService
75 {
76     public:
77         CAddress();
78         explicit CAddress(CService ipIn, uint64_t nServicesIn=NODE_NETWORK);
79
80         IMPLEMENT_SERIALIZE
81             (
82              CAddress* pthis = const_cast<CAddress*>(this);
83              CService* pip = (CService*)pthis;
84              if (fRead)
85                  pthis->Init();
86              if (nType & SER_DISK)
87                  READWRITE(nVersion);
88              if ((nType & SER_DISK) ||
89                  (nVersion >= CADDR_TIME_VERSION && !(nType & SER_GETHASH)))
90                  READWRITE(nTime);
91              READWRITE(nServices);
92              READWRITE(*pip);
93             )
94
95     // TODO: make private (improves encapsulation)
96     public:
97         uint64_t nServices;
98
99         // disk and network only
100         unsigned int nTime;
101
102         // memory only
103         int64_t nLastTry;
104 };
105
106 /** inv message data */
107 class CInv
108 {
109     public:
110         CInv();
111         CInv(int typeIn, const uint256& hashIn);
112         CInv(const std::string& strType, const uint256& hashIn);
113
114         IMPLEMENT_SERIALIZE
115         (
116             READWRITE(type);
117             READWRITE(hash);
118         )
119
120         friend bool operator<(const CInv& a, const CInv& b);
121
122         bool IsKnownType() const;
123         const char* GetCommand() const;
124         std::string ToString() const;
125
126     // TODO: make private (improves encapsulation)
127     public:
128         int type;
129         uint256 hash;
130 };
131
132 #endif // __INCLUDED_PROTOCOL_H__