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