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