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.
12 # include <arpa/inet.h>
15 // Prototypes from net.h, but that header (currently) stinks, can't #include it without breaking things
16 bool Lookup(const char *pszName, std::vector<CAddress>& vaddr, int nServices, int nMaxSolutions, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
17 bool Lookup(const char *pszName, CAddress& addr, int nServices, bool fAllowLookup = false, int portDefault = 0, bool fAllowPort = false);
19 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
20 static const char* ppszTypeName[] =
27 CMessageHeader::CMessageHeader()
29 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
30 memset(pchCommand, 0, sizeof(pchCommand));
36 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
38 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
39 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
40 nMessageSize = nMessageSizeIn;
44 std::string CMessageHeader::GetCommand() const
46 if (pchCommand[COMMAND_SIZE-1] == 0)
47 return std::string(pchCommand, pchCommand + strlen(pchCommand));
49 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
52 bool CMessageHeader::IsValid() const
55 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
58 // Check the command string for errors
59 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
63 // Must be all zeros after the first zero
64 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
68 else if (*p1 < ' ' || *p1 > 0x7E)
73 if (nMessageSize > MAX_SIZE)
75 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
87 CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64_t nServicesIn)
91 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
92 nServices = nServicesIn;
95 CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64_t nServicesIn)
98 ip = sockaddr.sin_addr.s_addr;
99 port = sockaddr.sin_port;
100 nServices = nServicesIn;
103 CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64_t nServicesIn)
106 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
109 CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64_t nServicesIn)
112 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
115 CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64_t nServicesIn)
118 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
121 CAddress::CAddress(std::string strIn, bool fNameLookup, uint64_t nServicesIn)
124 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
127 void CAddress::Init()
129 nServices = NODE_NETWORK;
130 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
132 port = htons(GetDefaultPort());
137 bool operator==(const CAddress& a, const CAddress& b)
139 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
144 bool operator!=(const CAddress& a, const CAddress& b)
149 bool operator<(const CAddress& a, const CAddress& b)
151 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
156 if (ntohl(a.ip) < ntohl(b.ip))
158 else if (a.ip == b.ip)
159 return ntohs(a.port) < ntohs(b.port);
164 std::vector<unsigned char> CAddress::GetKey() const
168 ss << FLATDATA(pchReserved) << ip << port;
170 #if defined(_MSC_VER) && _MSC_VER < 1300
171 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
173 return std::vector<unsigned char>(ss.begin(), ss.end());
177 struct sockaddr_in CAddress::GetSockAddr() const
179 struct sockaddr_in sockaddr;
180 memset(&sockaddr, 0, sizeof(sockaddr));
181 sockaddr.sin_family = AF_INET;
182 sockaddr.sin_addr.s_addr = ip;
183 sockaddr.sin_port = port;
187 bool CAddress::IsIPv4() const
189 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
192 bool CAddress::IsRFC1918() const
194 return IsIPv4() && (GetByte(3) == 10 ||
195 (GetByte(3) == 192 && GetByte(2) == 168) ||
196 (GetByte(3) == 172 &&
197 (GetByte(2) >= 16 && GetByte(2) <= 31)));
200 bool CAddress::IsRFC3927() const
202 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
205 bool CAddress::IsLocal() const
207 return IsIPv4() && (GetByte(3) == 127 ||
211 bool CAddress::IsRoutable() const
214 !(IsRFC1918() || IsRFC3927() || IsLocal());
217 bool CAddress::IsValid() const
219 // Clean up 3-byte shifted addresses caused by garbage in size field
220 // of addr messages from versions before 0.2.9 checksum.
221 // Two consecutive addr messages look like this:
222 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
223 // so if the first length field is garbled, it reads the second batch
224 // of addr misaligned by 3 bytes.
225 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
228 return (ip != 0 && ip != INADDR_NONE && port != htons(std::numeric_limits<unsigned short>::max()));
231 unsigned char CAddress::GetByte(int n) const
233 return ((unsigned char*)&ip)[3-n];
236 std::string CAddress::ToStringIPPort() const
238 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
241 std::string CAddress::ToStringIP() const
243 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
246 std::string CAddress::ToStringPort() const
248 return strprintf("%u", ntohs(port));
251 std::string CAddress::ToString() const
253 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
256 void CAddress::print() const
258 printf("CAddress(%s)\n", ToString().c_str());
267 CInv::CInv(int typeIn, const uint256& hashIn)
273 CInv::CInv(const std::string& strType, const uint256& hashIn)
276 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
278 if (strType == ppszTypeName[i])
284 if (i == ARRAYLEN(ppszTypeName))
285 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
289 bool operator<(const CInv& a, const CInv& b)
291 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
294 bool CInv::IsKnownType() const
296 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
299 const char* CInv::GetCommand() const
302 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
303 return ppszTypeName[type];
306 std::string CInv::ToString() const
308 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
311 void CInv::print() const
313 printf("CInv(%s)\n", ToString().c_str());