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>
11 # include <netinet/in.h>
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);
18 static const unsigned char pchIPv4[12] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
19 static const char* ppszTypeName[] =
26 CMessageHeader::CMessageHeader()
28 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
29 memset(pchCommand, 0, sizeof(pchCommand));
35 CMessageHeader::CMessageHeader(const char* pszCommand, unsigned int nMessageSizeIn)
37 memcpy(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart));
38 strncpy(pchCommand, pszCommand, COMMAND_SIZE);
39 nMessageSize = nMessageSizeIn;
43 std::string CMessageHeader::GetCommand() const
45 if (pchCommand[COMMAND_SIZE-1] == 0)
46 return std::string(pchCommand, pchCommand + strlen(pchCommand));
48 return std::string(pchCommand, pchCommand + COMMAND_SIZE);
51 bool CMessageHeader::IsValid() const
54 if (memcmp(pchMessageStart, ::pchMessageStart, sizeof(pchMessageStart)) != 0)
57 // Check the command string for errors
58 for (const char* p1 = pchCommand; p1 < pchCommand + COMMAND_SIZE; p1++)
62 // Must be all zeros after the first zero
63 for (; p1 < pchCommand + COMMAND_SIZE; p1++)
67 else if (*p1 < ' ' || *p1 > 0x7E)
72 if (nMessageSize > MAX_SIZE)
74 printf("CMessageHeader::IsValid() : (%s, %u bytes) nMessageSize > MAX_SIZE\n", GetCommand().c_str(), nMessageSize);
86 CAddress::CAddress(unsigned int ipIn, unsigned short portIn, uint64 nServicesIn)
90 port = htons(portIn == 0 ? GetDefaultPort() : portIn);
91 nServices = nServicesIn;
94 CAddress::CAddress(const struct sockaddr_in& sockaddr, uint64 nServicesIn)
97 ip = sockaddr.sin_addr.s_addr;
98 port = sockaddr.sin_port;
99 nServices = nServicesIn;
102 CAddress::CAddress(const char* pszIn, int portIn, bool fNameLookup, uint64 nServicesIn)
105 Lookup(pszIn, *this, nServicesIn, fNameLookup, portIn);
108 CAddress::CAddress(const char* pszIn, bool fNameLookup, uint64 nServicesIn)
111 Lookup(pszIn, *this, nServicesIn, fNameLookup, 0, true);
114 CAddress::CAddress(std::string strIn, int portIn, bool fNameLookup, uint64 nServicesIn)
117 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, portIn);
120 CAddress::CAddress(std::string strIn, bool fNameLookup, uint64 nServicesIn)
123 Lookup(strIn.c_str(), *this, nServicesIn, fNameLookup, 0, true);
126 void CAddress::Init()
128 nServices = NODE_NETWORK;
129 memcpy(pchReserved, pchIPv4, sizeof(pchReserved));
131 port = htons(GetDefaultPort());
136 bool operator==(const CAddress& a, const CAddress& b)
138 return (memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved)) == 0 &&
143 bool operator!=(const CAddress& a, const CAddress& b)
148 bool operator<(const CAddress& a, const CAddress& b)
150 int ret = memcmp(a.pchReserved, b.pchReserved, sizeof(a.pchReserved));
155 if (ntohl(a.ip) < ntohl(b.ip))
157 else if (a.ip == b.ip)
158 return ntohs(a.port) < ntohs(b.port);
163 std::vector<unsigned char> CAddress::GetKey() const
167 ss << FLATDATA(pchReserved) << ip << port;
169 #if defined(_MSC_VER) && _MSC_VER < 1300
170 return std::vector<unsigned char>((unsigned char*)&ss.begin()[0], (unsigned char*)&ss.end()[0]);
172 return std::vector<unsigned char>(ss.begin(), ss.end());
176 struct sockaddr_in CAddress::GetSockAddr() const
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;
186 bool CAddress::IsIPv4() const
188 return (memcmp(pchReserved, pchIPv4, sizeof(pchIPv4)) == 0);
191 bool CAddress::IsRFC1918() const
193 return IsIPv4() && (GetByte(3) == 10 ||
194 (GetByte(3) == 192 && GetByte(2) == 168) ||
195 (GetByte(3) == 172 &&
196 (GetByte(2) >= 16 && GetByte(2) <= 31)));
199 bool CAddress::IsRFC3927() const
201 return IsIPv4() && (GetByte(3) == 169 && GetByte(2) == 254);
204 bool CAddress::IsLocal() const
206 return IsIPv4() && (GetByte(3) == 127 ||
210 bool CAddress::IsRoutable() const
213 !(IsRFC1918() || IsRFC3927() || IsLocal());
216 bool CAddress::IsValid() const
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)
227 return (ip != 0 && ip != INADDR_NONE && port != htons(USHRT_MAX));
230 unsigned char CAddress::GetByte(int n) const
232 return ((unsigned char*)&ip)[3-n];
235 std::string CAddress::ToStringIPPort() const
237 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
240 std::string CAddress::ToStringIP() const
242 return strprintf("%u.%u.%u.%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0));
245 std::string CAddress::ToStringPort() const
247 return strprintf("%u", ntohs(port));
250 std::string CAddress::ToString() const
252 return strprintf("%u.%u.%u.%u:%u", GetByte(3), GetByte(2), GetByte(1), GetByte(0), ntohs(port));
255 void CAddress::print() const
257 printf("CAddress(%s)\n", ToString().c_str());
266 CInv::CInv(int typeIn, const uint256& hashIn)
272 CInv::CInv(const std::string& strType, const uint256& hashIn)
275 for (i = 1; i < ARRAYLEN(ppszTypeName); i++)
277 if (strType == ppszTypeName[i])
283 if (i == ARRAYLEN(ppszTypeName))
284 throw std::out_of_range(strprintf("CInv::CInv(string, uint256) : unknown type '%s'", strType.c_str()));
288 bool operator<(const CInv& a, const CInv& b)
290 return (a.type < b.type || (a.type == b.type && a.hash < b.hash));
293 bool CInv::IsKnownType() const
295 return (type >= 1 && type < ARRAYLEN(ppszTypeName));
298 const char* CInv::GetCommand() const
301 throw std::out_of_range(strprintf("CInv::GetCommand() : type=%d unknown type", type));
302 return ppszTypeName[type];
305 std::string CInv::ToString() const
307 return strprintf("%s %s", GetCommand(), hash.ToString().substr(0,20).c_str());
310 void CInv::print() const
312 printf("CInv(%s)\n", ToString().c_str());