Move CInv to protocol.[ch]pp
[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 "serialize.h"
14 #include <string>
15 #include "uint256.h"
16
17 extern bool fTestNet;
18 static inline unsigned short GetDefaultPort(const bool testnet = fTestNet)
19 {
20     return testnet ? 18333 : 8333;
21 }
22
23 //
24 // Message header
25 //  (4) message start
26 //  (12) command
27 //  (4) size
28 //  (4) checksum
29
30 extern unsigned char pchMessageStart[4];
31
32 class CMessageHeader
33 {
34     public:
35         CMessageHeader();
36         CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn);
37
38         std::string GetCommand() const;
39         bool IsValid() const;
40
41         IMPLEMENT_SERIALIZE
42             (
43              READWRITE(FLATDATA(pchMessageStart));
44              READWRITE(FLATDATA(pchCommand));
45              READWRITE(nMessageSize);
46              if (nVersion >= 209)
47              READWRITE(nChecksum);
48             )
49
50     // TODO: make private (improves encapsulation)
51     public:
52         enum { COMMAND_SIZE=12 };
53         char pchMessageStart[sizeof(::pchMessageStart)];
54         char pchCommand[COMMAND_SIZE];
55         unsigned int nMessageSize;
56         unsigned int nChecksum;
57 };
58
59 enum
60 {
61     NODE_NETWORK = (1 << 0),
62 };
63
64 class CAddress
65 {
66     public:
67         CAddress();
68         CAddress(unsigned int ipIn, unsigned short portIn=0, uint64 nServicesIn=NODE_NETWORK);
69         explicit CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn=NODE_NETWORK);
70         explicit CAddress(const char* pszIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
71         explicit CAddress(const char* pszIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
72         explicit CAddress(std::string strIn, int portIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
73         explicit CAddress(std::string strIn, bool fNameLookup = false, uint64 nServicesIn=NODE_NETWORK);
74
75         void Init();
76
77         IMPLEMENT_SERIALIZE
78             (
79              if (fRead)
80              const_cast<CAddress*>(this)->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(FLATDATA(pchReserved)); // for IPv6
87              READWRITE(ip);
88              READWRITE(port);
89             )
90
91         friend bool operator==(const CAddress& a, const CAddress& b);
92         friend bool operator!=(const CAddress& a, const CAddress& b);
93         friend bool operator<(const CAddress& a, const CAddress& b);
94
95         std::vector<unsigned char> GetKey() const;
96         struct sockaddr_in GetSockAddr() const;
97         bool IsIPv4() const;
98         bool IsRFC1918() const;
99         bool IsRFC3927() const;
100         bool IsLocal() const;
101         bool IsRoutable() const;
102         bool IsValid() const;
103         unsigned char GetByte(int n) const;
104         std::string ToStringIPPort() const;
105         std::string ToStringIP() const;
106         std::string ToStringPort() const;
107         std::string ToString() const;
108         void print() const;
109
110     // TODO: make private (improves encapsulation)
111     public:
112         uint64 nServices;
113         unsigned char pchReserved[12];
114         unsigned int ip;
115         unsigned short port;
116
117         // disk and network only
118         unsigned int nTime;
119
120         // memory only
121         unsigned int nLastTry;
122 };
123
124 class CInv
125 {
126     public:
127         CInv();
128         CInv(int typeIn, const uint256& hashIn);
129         CInv(const std::string& strType, const uint256& hashIn);
130
131         IMPLEMENT_SERIALIZE
132         (
133             READWRITE(type);
134             READWRITE(hash);
135         )
136
137         friend bool operator<(const CInv& a, const CInv& b);
138
139         bool IsKnownType() const;
140         const char* GetCommand() const;
141         std::string ToString() const;
142         void print() const;
143
144     // TODO: make private (improves encapsulation)
145     public:
146         int type;
147         uint256 hash;
148 };
149
150 #endif // __INCLUDED_PROTOCOL_H__