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