Use standard C99 (and Qt) types for 64-bit integers
[novacoin.git] / src / protocol.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2011 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 <stdint.h>
14
15 #include "serialize.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              if (nVersion >= 209)
49              READWRITE(nChecksum);
50             )
51
52     // TODO: make private (improves encapsulation)
53     public:
54         enum { COMMAND_SIZE=12 };
55         char pchMessageStart[sizeof(::pchMessageStart)];
56         char pchCommand[COMMAND_SIZE];
57         unsigned int nMessageSize;
58         unsigned int nChecksum;
59 };
60
61 enum
62 {
63     NODE_NETWORK = (1 << 0),
64 };
65
66 class CAddress
67 {
68     public:
69         CAddress();
70         CAddress(unsigned int ipIn, unsigned short portIn=0, uint64_t nServicesIn=NODE_NETWORK);
71         explicit CAddress(const struct sockaddr_in& sockaddr, uint64_t nServicesIn=NODE_NETWORK);
72         explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64_t nServicesIn=NODE_NETWORK);
73         explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64_t nServicesIn=NODE_NETWORK);
74         explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64_t nServicesIn=NODE_NETWORK);
75         explicit CAddress(std::string strIn, bool fNameLookup = false, uint64_t nServicesIn=NODE_NETWORK);
76
77         void Init();
78
79         IMPLEMENT_SERIALIZE
80             (
81              if (fRead)
82              const_cast<CAddress*>(this)->Init();
83              if (nType & SER_DISK)
84              READWRITE(nVersion);
85              if ((nType & SER_DISK) || (nVersion >= 31402 && !(nType & SER_GETHASH)))
86              READWRITE(nTime);
87              READWRITE(nServices);
88              READWRITE(FLATDATA(pchReserved)); // for IPv6
89              READWRITE(ip);
90              READWRITE(port);
91             )
92
93         friend bool operator==(const CAddress& a, const CAddress& b);
94         friend bool operator!=(const CAddress& a, const CAddress& b);
95         friend bool operator<(const CAddress& a, const CAddress& b);
96
97         std::vector<unsigned char> GetKey() const;
98         struct sockaddr_in GetSockAddr() const;
99         bool IsIPv4() const;
100         bool IsRFC1918() const;
101         bool IsRFC3927() const;
102         bool IsLocal() const;
103         bool IsRoutable() const;
104         bool IsValid() const;
105         unsigned char GetByte(int n) const;
106         std::string ToStringIPPort() const;
107         std::string ToStringIP() const;
108         std::string ToStringPort() const;
109         std::string ToString() const;
110         void print() const;
111
112     // TODO: make private (improves encapsulation)
113     public:
114         uint64_t nServices;
115         unsigned char pchReserved[12];
116         unsigned int ip;
117         unsigned short port;
118
119         // disk and network only
120         unsigned int nTime;
121
122         // memory only
123         unsigned int nLastTry;
124 };
125
126 class CInv
127 {
128     public:
129         CInv();
130         CInv(int typeIn, const uint256& hashIn);
131         CInv(const std::string& strType, const uint256& hashIn);
132
133         IMPLEMENT_SERIALIZE
134         (
135             READWRITE(type);
136             READWRITE(hash);
137         )
138
139         friend bool operator<(const CInv& a, const CInv& b);
140
141         bool IsKnownType() const;
142         const char* GetCommand() const;
143         std::string ToString() const;
144         void print() const;
145
146     // TODO: make private (improves encapsulation)
147     public:
148         int type;
149         uint256 hash;
150 };
151
152 #endif // __INCLUDED_PROTOCOL_H__