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.
10 # include <arpa/inet.h>
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);
17 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
18 static const char* ppszTypeName[] =
25 CMessageHeader::CMessageHeader()
27 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
28 memset(pchCommand, 0, sizeof(pchCommand));
34 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
36 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
37 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
38 nMessageSize = nMessageSizeIn;
42 std::string CMessageHeader::GetCommand() const
44 if (pchCommand[COMMAND_SIZE-1] == 0)
45 return std::string(pchCommand, pchCommand + strlen(pchCommand));
47 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
50 bool CMessageHeader::IsValid() const
53 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
56 // Check the command string for errors
57 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
61 // Must be all zeros after the first zero
62 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
66 else if (*p1 < ' ' || *p1 > 0x7E)
71 if (nMessageSize > MAX_SIZE)
73 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
85 CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
89 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
90 nServices = nServicesIn;
93 CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
96 ip = sockaddr.sin_addr.s_addr;
97 port = sockaddr.sin_port;
98 nServices = nServicesIn;
101 CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn)
104 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
107 CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn)
110 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
113 CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn)
116 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
119 CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn)
122 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
125 void CAddress::Init()
127 nServices = NODE_NETWORK;
128 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
130 port = htons(GetDefaultPort());
135 bool operator==(const CAddress& a, const CAddress& b)
137 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
142 bool operator!=(const CAddress& a, const CAddress& b)
147 bool operator<(const CAddress& a, const CAddress& b)
149 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
154 if (ntohl(a.ip) < ntohl(b.ip))
156 else if (a.ip == b.ip)
157 return ntohs(a.port) < ntohs(b.port);
162 std::vector<unsigned char> CAddress::GetKey() const
166 ss << FLATDATA(pchReserved) << ip << port;
168 #if defined(_MSC_VER) && _MSC_VER < 1300
169 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
171 return std::vector<unsigned char>(ss.begin(), ss.end());
175 struct sockaddr_in CAddress::GetSockAddr() const
177 struct sockaddr_in sockaddr;
178 memset(&sockaddr, 0, sizeof(sockaddr));
179 sockaddr.sin_family = AF_INET;
180 sockaddr.sin_addr.s_addr = ip;
181 sockaddr.sin_port = port;
185 bool CAddress::IsIPv4() const
187 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
190 bool CAddress::IsRFC1918() const
192 return IsIPv4() && (GetByte(3) == 10 ||
193 (GetByte(3) == 192 && GetByte(2) == 168) ||
194 (GetByte(3) == 172 &&
195 (GetByte(2) >= 16 && GetByte(2) <= 31)));
198 bool CAddress::IsRFC3927() const
200 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
203 bool CAddress::IsLocal() const
205 return IsIPv4() && (GetByte(3) == 127 ||
209 bool CAddress::IsRoutable() const
212 !(IsRFC1918() || IsRFC3927() || IsLocal());
215 bool CAddress::IsValid() const
217 // Clean up 3-byte shifted addresses caused by garbage in size field
218 // of addr messages from versions before 0.2.9 checksum.
219 // Two consecutive addr messages look like this:
220 // header20 vectorlen3 addr26 addr26 addr26 header20 vectorlen3 addr26 addr26 addr26...
221 // so if the first length field is garbled, it reads the second batch
222 // of addr misaligned by 3 bytes.
223 if (memcmp(pchReserved, pchIPv4+3, sizeof(pchIPv4)-3) == 0)
226 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
229 unsigned char CAddress::GetByte(int n) const
231 return ((unsigned char*)&ip)[3-n];
234 std::string CAddress::ToStringIPPort() const
236 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
239 std::string CAddress::ToStringIP() const
241 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
244 std::string CAddress::ToStringPort() const
246 return strprintf("%u", ntohs(port));
249 std::string CAddress::ToString() const
251 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
254 void CAddress::print() const
256 printf("CAddress(%s)\n", ToString().c_str());
265 CInv::CInv(int typeIn, const uint256& hashIn)
271 CInv::CInv(const std::string& strType, const uint256& hashIn)
274 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
276 if (strType == ppszTypeName[i])
282 if (i == ARRAYLEN(ppszTypeName))
283 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
287 bool operator<(const CInv& a, const CInv& b)
289 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
292 bool CInv::IsKnownType() const
294 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
297 const char* CInv::GetCommand() const
300 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
301 return ppszTypeName[type];
304 std::string CInv::ToString() const
306 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
309 void CInv::print() const
311 printf("CInv(%s)\n", ToString().c_str());