Update License in File Headers
[novacoin.git] / src / protocol.cpp
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 #include "protocol.h"
7 #include "util.h"
8
9 #ifndef __WXMSW__
10 # include <arpa/inet.h>
11 # include <netinet/in.h>
12 #endif
13
14 // Prototypes from net.h, but that header (currently) stinks, can't #include it without breaking things
15 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
16 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
17
18 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
19 static const char* ppszTypeName[] =
20 {
21     "ERROR",
22     "tx",
23     "block",
24 };
25
26 CMessageHeader::CMessageHeader()
27 {
28     memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
29     memset(pchCommand, 0, sizeof(pchCommand));
30     pchCommand[1] = 1;
31     nMessageSize = -1;
32     nChecksum = 0;
33 }
34
35 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
36 {
37     memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
38     strncpy(pchCommand, pszCommand, COMMAND_SIZE);
39     nMessageSize = nMessageSizeIn;
40     nChecksum = 0;
41 }
42
43 std::string CMessageHeader::GetCommand() const
44 {
45     if (pchCommand[COMMAND_SIZE-1] == 0)
46         return std::string(pchCommand, pchCommand + strlen(pchCommand));
47     else
48         return std::string(pchCommand, pchCommand + COMMAND_SIZE);
49 }
50
51 bool CMessageHeader::IsValid() const
52 {
53     // Check start string
54     if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
55         return false;
56
57     // Check the command string for errors
58     for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
59     {
60         if (*p1 == 0)
61         {
62             // Must be all zeros after the first zero
63             for (; p1 < pchCommand + COMMAND_SIZE; p1++)
64                 if (*p1 != 0)
65                     return false;
66         }
67         else if (*p1 < ' ' || *p1 > 0x7E)
68             return false;
69     }
70
71     // Message size
72     if (nMessageSize > MAX_SIZE)
73     {
74         printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
75         return false;
76     }
77
78     return true;
79 }
80
81 CAddress::CAddress()
82 {
83     Init();
84 }
85
86 CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
87 {
88     Init();
89     ip = ipIn;
90     port = htons(portIn == 0 ? GetDefaultPort() : portIn);
91     nServices = nServicesIn;
92 }
93
94 CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
95 {
96     Init();
97     ip = sockaddr.sin_addr.s_addr;
98     port = sockaddr.sin_port;
99     nServices = nServicesIn;
100 }
101
102 CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn)
103 {
104     Init();
105     Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
106 }
107
108 CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn)
109 {
110     Init();
111     Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
112 }
113
114 CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn)
115 {
116     Init();
117     Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
118 }
119
120 CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn)
121 {
122     Init();
123     Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
124 }
125
126 void CAddress::Init()
127 {
128     nServices = NODE_NETWORK;
129     memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
130     ip = INADDR_NONE;
131     port = htons(GetDefaultPort());
132     nTime = 100000000;
133     nLastTry = 0;
134 }
135
136 bool operator==(const CAddress& a, const CAddress& b)
137 {
138     return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
139             a.ip   == b.ip &&
140             a.port == b.port);
141 }
142
143 bool operator!=(const CAddress& a, const CAddress& b)
144 {
145     return (!(a == b));
146 }
147
148 bool operator<(const CAddress& a, const CAddress& b)
149 {
150     int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
151     if (ret < 0)
152         return true;
153     else if (ret == 0)
154     {
155         if (ntohl(a.ip) < ntohl(b.ip))
156             return true;
157         else if (a.ip == b.ip)
158             return ntohs(a.port) < ntohs(b.port);
159     }
160     return false;
161 }
162
163 std::vector<unsigned char> CAddress::GetKey() const
164 {
165     CDataStream ss;
166     ss.reserve(18);
167     ss << FLATDATA(pchReserved) << ip << port;
168
169     #if defined(_MSC_VER) && _MSC_VER < 1300
170     return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
171     #else
172     return std::vector<unsigned char>(ss.begin(), ss.end());
173     #endif
174 }
175
176 struct sockaddr_in CAddress::GetSockAddr() const
177 {
178     struct sockaddr_in sockaddr;
179     memset(&sockaddr, 0, sizeof(sockaddr));
180     sockaddr.sin_family = AF_INET;
181     sockaddr.sin_addr.s_addr = ip;
182     sockaddr.sin_port = port;
183     return sockaddr;
184 }
185
186 bool CAddress::IsIPv4() const
187 {
188     return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
189 }
190
191 bool CAddress::IsRFC1918() const
192 {
193   return IsIPv4() && (GetByte(3) == 10 ||
194     (GetByte(3) == 192 && GetByte(2) == 168) ||
195     (GetByte(3) == 172 &&
196       (GetByte(2) >= 16 && GetByte(2) <= 31)));
197 }
198
199 bool CAddress::IsRFC3927() const
200 {
201   return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
202 }
203
204 bool CAddress::IsLocal() const
205 {
206   return IsIPv4() && (GetByte(3) == 127 ||
207       GetByte(3) == 0);
208 }
209
210 bool CAddress::IsRoutable() const
211 {
212     return IsValid() &&
213         !(IsRFC1918() || IsRFC3927() || IsLocal());
214 }
215
216 bool CAddress::IsValid() const
217 {
218     // Clean up 3-byte shifted addresses caused by garbage in size field
219     // of addr messages from versions before 0.2.9 checksum.
220     // Two consecutive addr messages look like this:
221     // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
222     // so if the first length field is garbled, it reads the second batch
223     // of addr misaligned by 3 bytes.
224     if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
225         return false;
226
227     return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
228 }
229
230 unsigned char CAddress::GetByte(int n) const
231 {
232     return ((unsigned char*)&ip)[3-n];
233 }
234
235 std::string CAddress::ToStringIPPort() const
236 {
237     return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
238 }
239
240 std::string CAddress::ToStringIP() const
241 {
242     return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
243 }
244
245 std::string CAddress::ToStringPort() const
246 {
247     return strprintf("%u", ntohs(port));
248 }
249
250 std::string CAddress::ToString() const
251 {
252     return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
253 }
254
255 void CAddress::print() const
256 {
257     printf("CAddress(%s)\n", ToString().c_str());
258 }
259
260 CInv::CInv()
261 {
262     type = 0;
263     hash = 0;
264 }
265
266 CInv::CInv(int typeIn, const uint256& hashIn)
267 {
268     type = typeIn;
269     hash = hashIn;
270 }
271
272 CInv::CInv(const std::string& strType, const uint256& hashIn)
273 {
274     unsigned int i;
275     for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
276     {
277         if (strType == ppszTypeName[i])
278         {
279             type = i;
280             break;
281         }
282     }
283     if (i == ARRAYLEN(ppszTypeName))
284         throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
285     hash = hashIn;
286 }
287
288 bool operator<(const CInv& a, const CInv& b)
289 {
290     return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
291 }
292
293 bool CInv::IsKnownType() const
294 {
295     return (type >= 1 && type < ARRAYLEN(ppszTypeName));
296 }
297
298 const char* CInv::GetCommand() const
299 {
300     if (!IsKnownType())
301         throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
302     return ppszTypeName[type];
303 }
304
305 std::string CInv::ToString() const
306 {
307     return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
308 }
309
310 void CInv::print() const
311 {
312     printf("CInv(%s)\n", ToString().c_str());
313 }