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