net: Split resolving out of CNetAddr

This commit is contained in:
Cory Fields 2016-05-31 13:05:52 -04:00
parent 6caf3ee061
commit 31d6b1d5f0
8 changed files with 153 additions and 119 deletions

View file

@ -1396,8 +1396,11 @@ void ThreadMapPort()
{ {
if(externalIPAddress[0]) if(externalIPAddress[0])
{ {
LogPrintf("UPnP: ExternalIPAddress = %s\n", externalIPAddress); CNetAddr resolved;
AddLocal(CNetAddr(externalIPAddress), LOCAL_UPNP); if(LookupHost(externalIPAddress, resolved, false)) {
LogPrintf("UPnP: ExternalIPAddress = %s\n", resolved.ToString().c_str());
AddLocal(resolved, LOCAL_UPNP);
}
} }
else else
LogPrintf("UPnP: GetExternalIPAddress failed.\n"); LogPrintf("UPnP: GetExternalIPAddress failed.\n");
@ -1623,7 +1626,9 @@ void ThreadOpenConnections()
static bool done = false; static bool done = false;
if (!done) { if (!done) {
LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n"); LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
addrman.Add(convertSeed6(Params().FixedSeeds()), CNetAddr("127.0.0.1")); CNetAddr local;
LookupHost("127.0.0.1", local, false);
addrman.Add(convertSeed6(Params().FixedSeeds()), local);
done = true; done = true;
} }
} }

View file

@ -195,6 +195,16 @@ bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nM
return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup); return LookupIntern(strHost.c_str(), vIP, nMaxSolutions, fAllowLookup);
} }
bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup)
{
std::vector<CNetAddr> vIP;
LookupHost(pszName, vIP, 1, fAllowLookup);
if(vIP.empty())
return false;
addr = vIP.front();
return true;
}
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions) bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions)
{ {
if (pszName[0] == 0) if (pszName[0] == 0)
@ -695,22 +705,6 @@ CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
scopeId = scope; scopeId = scope;
} }
CNetAddr::CNetAddr(const char *pszIp)
{
Init();
std::vector<CNetAddr> vIP;
if (LookupHost(pszIp, vIP, 1, false))
*this = vIP[0];
}
CNetAddr::CNetAddr(const std::string &strIp)
{
Init();
std::vector<CNetAddr> vIP;
if (LookupHost(strIp.c_str(), vIP, 1, false))
*this = vIP[0];
}
unsigned int CNetAddr::GetByte(int n) const unsigned int CNetAddr::GetByte(int n) const
{ {
return ip[15-n]; return ip[15-n];

View file

@ -49,8 +49,6 @@ class CNetAddr
public: public:
CNetAddr(); CNetAddr();
CNetAddr(const struct in_addr& ipv4Addr); CNetAddr(const struct in_addr& ipv4Addr);
explicit CNetAddr(const char *pszIp);
explicit CNetAddr(const std::string &strIp);
void Init(); void Init();
void SetIP(const CNetAddr& ip); void SetIP(const CNetAddr& ip);
@ -207,6 +205,7 @@ bool IsProxy(const CNetAddr &addr);
bool SetNameProxy(const proxyType &addrProxy); bool SetNameProxy(const proxyType &addrProxy);
bool HaveNameProxy(); bool HaveNameProxy();
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup); bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
bool LookupHost(const char *pszName, CNetAddr& addr, bool fAllowLookup);
bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup); bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions); bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault, bool fAllowLookup, unsigned int nMaxSolutions);
bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0); bool LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);

View file

@ -898,7 +898,10 @@ void RPCConsole::banSelectedNode(int bantime)
int port = 0; int port = 0;
SplitHostPort(nStr, port, addr); SplitHostPort(nStr, port, addr);
CNode::Ban(CNetAddr(addr), BanReasonManuallyAdded, bantime); CNetAddr resolved;
if(!LookupHost(addr.c_str(), resolved, false))
return;
CNode::Ban(resolved, BanReasonManuallyAdded, bantime);
clearSelectedNode(); clearSelectedNode();
clientModel->getBanTableModel()->refresh(); clientModel->getBanTableModel()->refresh();

View file

@ -494,8 +494,11 @@ UniValue setban(const UniValue& params, bool fHelp)
if (params[0].get_str().find("/") != string::npos) if (params[0].get_str().find("/") != string::npos)
isSubnet = true; isSubnet = true;
if (!isSubnet) if (!isSubnet) {
netAddr = CNetAddr(params[0].get_str()); CNetAddr resolved;
LookupHost(params[0].get_str().c_str(), resolved, false);
netAddr = resolved;
}
else else
subNet = CSubNet(params[0].get_str()); subNet = CSubNet(params[0].get_str());

View file

@ -50,6 +50,18 @@ public:
} }
}; };
static CNetAddr ResolveIP(const char* ip)
{
CNetAddr addr;
BOOST_CHECK_MESSAGE(LookupHost(ip, addr, false), strprintf("failed to resolve: %s", ip));
return addr;
}
static CNetAddr ResolveIP(std::string ip)
{
return ResolveIP(ip.c_str());
}
BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup) BOOST_FIXTURE_TEST_SUITE(addrman_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(addrman_simple) BOOST_AUTO_TEST_CASE(addrman_simple)
@ -59,7 +71,7 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CNetAddr source = CNetAddr("252.2.2.2"); CNetAddr source = ResolveIP("252.2.2.2");
// Test 1: Does Addrman respond correctly when empty. // Test 1: Does Addrman respond correctly when empty.
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
@ -100,7 +112,7 @@ BOOST_AUTO_TEST_CASE(addrman_ports)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CNetAddr source = CNetAddr("252.2.2.2"); CNetAddr source = ResolveIP("252.2.2.2");
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
@ -132,7 +144,7 @@ BOOST_AUTO_TEST_CASE(addrman_select)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CNetAddr source = CNetAddr("252.2.2.2"); CNetAddr source = ResolveIP("252.2.2.2");
// Test 9: Select from new with 1 addr in new. // Test 9: Select from new with 1 addr in new.
CService addr1 = CService("250.1.1.1", 8333); CService addr1 = CService("250.1.1.1", 8333);
@ -160,20 +172,20 @@ BOOST_AUTO_TEST_CASE(addrman_select)
CService addr3 = CService("250.3.2.2", 9999); CService addr3 = CService("250.3.2.2", 9999);
CService addr4 = CService("250.3.3.3", 9999); CService addr4 = CService("250.3.3.3", 9999);
addrman.Add(CAddress(addr2, NODE_NONE), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr2, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE);
addrman.Add(CAddress(addr3, NODE_NONE), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr3, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE);
addrman.Add(CAddress(addr4, NODE_NONE), CService("250.4.1.1", 8333)); addrman.Add(CAddress(addr4, NODE_NONE), CService("250.4.1.1", 8333), NODE_NONE);
// Add three addresses to tried table. // Add three addresses to tried table.
CService addr5 = CService("250.4.4.4", 8333); CService addr5 = CService("250.4.4.4", 8333);
CService addr6 = CService("250.4.5.5", 7777); CService addr6 = CService("250.4.5.5", 7777);
CService addr7 = CService("250.4.6.6", 8333); CService addr7 = CService("250.4.6.6", 8333);
addrman.Add(CAddress(addr5, NODE_NONE), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr5, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE);
addrman.Good(CAddress(addr5, NODE_NONE)); addrman.Good(CAddress(addr5, NODE_NONE));
addrman.Add(CAddress(addr6, NODE_NONE), CService("250.3.1.1", 8333)); addrman.Add(CAddress(addr6, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE);
addrman.Good(CAddress(addr6, NODE_NONE)); addrman.Good(CAddress(addr6, NODE_NONE));
addrman.Add(CAddress(addr7, NODE_NONE), CService("250.1.1.3", 8333)); addrman.Add(CAddress(addr7, NODE_NONE), CService("250.1.1.3", 8333), NODE_NONE);
addrman.Good(CAddress(addr7, NODE_NONE)); addrman.Good(CAddress(addr7, NODE_NONE));
// Test 11: 6 addrs + 1 addr from last test = 7. // Test 11: 6 addrs + 1 addr from last test = 7.
@ -193,7 +205,7 @@ BOOST_AUTO_TEST_CASE(addrman_new_collisions)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CNetAddr source = CNetAddr("252.2.2.2"); CNetAddr source = ResolveIP("252.2.2.2");
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
@ -222,7 +234,7 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CNetAddr source = CNetAddr("252.2.2.2"); CNetAddr source = ResolveIP("252.2.2.2");
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
@ -259,8 +271,8 @@ BOOST_AUTO_TEST_CASE(addrman_find)
CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE); CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE);
CAddress addr3 = CAddress(CService("251.255.2.1", 8333), NODE_NONE); CAddress addr3 = CAddress(CService("251.255.2.1", 8333), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
CNetAddr source2 = CNetAddr("250.1.2.2"); CNetAddr source2 = ResolveIP("250.1.2.2");
addrman.Add(addr1, source1); addrman.Add(addr1, source1);
addrman.Add(addr2, source2); addrman.Add(addr2, source2);
@ -295,7 +307,7 @@ BOOST_AUTO_TEST_CASE(addrman_create)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
int nId; int nId;
CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId); CAddrInfo* pinfo = addrman.Create(addr1, source1, &nId);
@ -318,7 +330,7 @@ BOOST_AUTO_TEST_CASE(addrman_delete)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
int nId; int nId;
addrman.Create(addr1, source1, &nId); addrman.Create(addr1, source1, &nId);
@ -354,8 +366,8 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
addr4.nTime = GetAdjustedTime(); addr4.nTime = GetAdjustedTime();
CAddress addr5 = CAddress(CService("252.254.4.5", 8333), NODE_NONE); CAddress addr5 = CAddress(CService("252.254.4.5", 8333), NODE_NONE);
addr5.nTime = GetAdjustedTime(); addr5.nTime = GetAdjustedTime();
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
CNetAddr source2 = CNetAddr("250.2.3.3"); CNetAddr source2 = ResolveIP("250.2.3.3");
// Test 23: Ensure GetAddr works with new addresses. // Test 23: Ensure GetAddr works with new addresses.
addrman.Add(addr1, source1); addrman.Add(addr1, source1);
@ -382,7 +394,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
// Ensure that for all addrs in addrman, isTerrible == false. // Ensure that for all addrs in addrman, isTerrible == false.
addr.nTime = GetAdjustedTime(); addr.nTime = GetAdjustedTime();
addrman.Add(addr, CNetAddr(strAddr)); addrman.Add(addr, ResolveIP(strAddr));
if (i % 8 == 0) if (i % 8 == 0)
addrman.Good(addr); addrman.Good(addr);
} }
@ -406,7 +418,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
CAddress addr1 = CAddress(CService("250.1.1.1", 8333), NODE_NONE); CAddress addr1 = CAddress(CService("250.1.1.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.1.1", 9999), NODE_NONE); CAddress addr2 = CAddress(CService("250.1.1.1", 9999), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.1.1"); CNetAddr source1 = ResolveIP("250.1.1.1");
CAddrInfo info1 = CAddrInfo(addr1, source1); CAddrInfo info1 = CAddrInfo(addr1, source1);
@ -432,7 +444,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++) {
CAddrInfo infoi = CAddrInfo( CAddrInfo infoi = CAddrInfo(
CAddress(CService("250.1.1." + boost::to_string(i)), NODE_NONE), CAddress(CService("250.1.1." + boost::to_string(i)), NODE_NONE),
CNetAddr("250.1.1." + boost::to_string(i))); ResolveIP("250.1.1." + boost::to_string(i)));
int bucket = infoi.GetTriedBucket(nKey1); int bucket = infoi.GetTriedBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
} }
@ -444,7 +456,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
for (int j = 0; j < 255; j++) { for (int j = 0; j < 255; j++) {
CAddrInfo infoj = CAddrInfo( CAddrInfo infoj = CAddrInfo(
CAddress(CService("250." + boost::to_string(j) + ".1.1"), NODE_NONE), CAddress(CService("250." + boost::to_string(j) + ".1.1"), NODE_NONE),
CNetAddr("250." + boost::to_string(j) + ".1.1")); ResolveIP("250." + boost::to_string(j) + ".1.1"));
int bucket = infoj.GetTriedBucket(nKey1); int bucket = infoj.GetTriedBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
} }
@ -463,7 +475,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE); CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE);
CNetAddr source1 = CNetAddr("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
CAddrInfo info1 = CAddrInfo(addr1, source1); CAddrInfo info1 = CAddrInfo(addr1, source1);
@ -485,7 +497,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
for (int i = 0; i < 255; i++) { for (int i = 0; i < 255; i++) {
CAddrInfo infoi = CAddrInfo( CAddrInfo infoi = CAddrInfo(
CAddress(CService("250.1.1." + boost::to_string(i)), NODE_NONE), CAddress(CService("250.1.1." + boost::to_string(i)), NODE_NONE),
CNetAddr("250.1.1." + boost::to_string(i))); ResolveIP("250.1.1." + boost::to_string(i)));
int bucket = infoi.GetNewBucket(nKey1); int bucket = infoi.GetNewBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
} }
@ -498,7 +510,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
CAddrInfo infoj = CAddrInfo(CAddress( CAddrInfo infoj = CAddrInfo(CAddress(
CService( CService(
boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE), boost::to_string(250 + (j / 255)) + "." + boost::to_string(j % 256) + ".1.1"), NODE_NONE),
CNetAddr("251.4.1.1")); ResolveIP("251.4.1.1"));
int bucket = infoj.GetNewBucket(nKey1); int bucket = infoj.GetNewBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
} }
@ -510,7 +522,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
for (int p = 0; p < 255; p++) { for (int p = 0; p < 255; p++) {
CAddrInfo infoj = CAddrInfo( CAddrInfo infoj = CAddrInfo(
CAddress(CService("250.1.1.1"), NODE_NONE), CAddress(CService("250.1.1.1"), NODE_NONE),
CNetAddr("250." + boost::to_string(p) + ".1.1")); ResolveIP("250." + boost::to_string(p) + ".1.1"));
int bucket = infoj.GetNewBucket(nKey1); int bucket = infoj.GetNewBucket(nKey1);
buckets.insert(bucket); buckets.insert(bucket);
} }

View file

@ -52,7 +52,9 @@ public:
s << nUBuckets; s << nUBuckets;
CAddress addr = CAddress(CService("252.1.1.1", 7777), NODE_NONE); CAddress addr = CAddress(CService("252.1.1.1", 7777), NODE_NONE);
CAddrInfo info = CAddrInfo(addr, CNetAddr("252.2.2.2")); CNetAddr resolved;
LookupHost("252.2.2.2", resolved, false);
CAddrInfo info = CAddrInfo(addr, resolved);
s << info; s << info;
} }
}; };

View file

@ -14,37 +14,47 @@ using namespace std;
BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup) BOOST_FIXTURE_TEST_SUITE(netbase_tests, BasicTestingSetup)
static CNetAddr ResolveIP(const char* ip)
{
CNetAddr addr;
LookupHost(ip, addr, false);
return addr;
}
BOOST_AUTO_TEST_CASE(netbase_networks) BOOST_AUTO_TEST_CASE(netbase_networks)
{ {
BOOST_CHECK(CNetAddr("127.0.0.1").GetNetwork() == NET_UNROUTABLE); BOOST_CHECK(ResolveIP("127.0.0.1").GetNetwork() == NET_UNROUTABLE);
BOOST_CHECK(CNetAddr("::1").GetNetwork() == NET_UNROUTABLE); BOOST_CHECK(ResolveIP("::1").GetNetwork() == NET_UNROUTABLE);
BOOST_CHECK(CNetAddr("8.8.8.8").GetNetwork() == NET_IPV4); BOOST_CHECK(ResolveIP("8.8.8.8").GetNetwork() == NET_IPV4);
BOOST_CHECK(CNetAddr("2001::8888").GetNetwork() == NET_IPV6); BOOST_CHECK(ResolveIP("2001::8888").GetNetwork() == NET_IPV6);
BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_TOR); BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetNetwork() == NET_TOR);
} }
BOOST_AUTO_TEST_CASE(netbase_properties) BOOST_AUTO_TEST_CASE(netbase_properties)
{ {
BOOST_CHECK(CNetAddr("127.0.0.1").IsIPv4());
BOOST_CHECK(CNetAddr("::FFFF:192.168.1.1").IsIPv4()); BOOST_CHECK(ResolveIP("127.0.0.1").IsIPv4());
BOOST_CHECK(CNetAddr("::1").IsIPv6()); BOOST_CHECK(ResolveIP("::FFFF:192.168.1.1").IsIPv4());
BOOST_CHECK(CNetAddr("10.0.0.1").IsRFC1918()); BOOST_CHECK(ResolveIP("::1").IsIPv6());
BOOST_CHECK(CNetAddr("192.168.1.1").IsRFC1918()); BOOST_CHECK(ResolveIP("10.0.0.1").IsRFC1918());
BOOST_CHECK(CNetAddr("172.31.255.255").IsRFC1918()); BOOST_CHECK(ResolveIP("192.168.1.1").IsRFC1918());
BOOST_CHECK(CNetAddr("2001:0DB8::").IsRFC3849()); BOOST_CHECK(ResolveIP("172.31.255.255").IsRFC1918());
BOOST_CHECK(CNetAddr("169.254.1.1").IsRFC3927()); BOOST_CHECK(ResolveIP("2001:0DB8::").IsRFC3849());
BOOST_CHECK(CNetAddr("2002::1").IsRFC3964()); BOOST_CHECK(ResolveIP("169.254.1.1").IsRFC3927());
BOOST_CHECK(CNetAddr("FC00::").IsRFC4193()); BOOST_CHECK(ResolveIP("2002::1").IsRFC3964());
BOOST_CHECK(CNetAddr("2001::2").IsRFC4380()); BOOST_CHECK(ResolveIP("FC00::").IsRFC4193());
BOOST_CHECK(CNetAddr("2001:10::").IsRFC4843()); BOOST_CHECK(ResolveIP("2001::2").IsRFC4380());
BOOST_CHECK(CNetAddr("FE80::").IsRFC4862()); BOOST_CHECK(ResolveIP("2001:10::").IsRFC4843());
BOOST_CHECK(CNetAddr("64:FF9B::").IsRFC6052()); BOOST_CHECK(ResolveIP("FE80::").IsRFC4862());
BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor()); BOOST_CHECK(ResolveIP("64:FF9B::").IsRFC6052());
BOOST_CHECK(CNetAddr("127.0.0.1").IsLocal()); BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").IsTor());
BOOST_CHECK(CNetAddr("::1").IsLocal()); BOOST_CHECK(ResolveIP("127.0.0.1").IsLocal());
BOOST_CHECK(CNetAddr("8.8.8.8").IsRoutable()); BOOST_CHECK(ResolveIP("::1").IsLocal());
BOOST_CHECK(CNetAddr("2001::1").IsRoutable()); BOOST_CHECK(ResolveIP("8.8.8.8").IsRoutable());
BOOST_CHECK(CNetAddr("127.0.0.1").IsValid()); BOOST_CHECK(ResolveIP("2001::1").IsRoutable());
BOOST_CHECK(ResolveIP("127.0.0.1").IsValid());
} }
bool static TestSplitHost(string test, string host, int port) bool static TestSplitHost(string test, string host, int port)
@ -95,44 +105,47 @@ BOOST_AUTO_TEST_CASE(netbase_lookupnumeric)
BOOST_AUTO_TEST_CASE(onioncat_test) BOOST_AUTO_TEST_CASE(onioncat_test)
{ {
// values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat // values from https://web.archive.org/web/20121122003543/http://www.cypherpunk.at/onioncat/wiki/OnionCat
CNetAddr addr1("5wyqrzbvrdsumnok.onion"); CNetAddr addr1(ResolveIP("5wyqrzbvrdsumnok.onion"));
CNetAddr addr2("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"); CNetAddr addr2(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca"));
BOOST_CHECK(addr1 == addr2); BOOST_CHECK(addr1 == addr2);
BOOST_CHECK(addr1.IsTor()); BOOST_CHECK(addr1.IsTor());
BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion"); BOOST_CHECK(addr1.ToStringIP() == "5wyqrzbvrdsumnok.onion");
BOOST_CHECK(addr1.IsRoutable()); BOOST_CHECK(addr1.IsRoutable());
} }
BOOST_AUTO_TEST_CASE(subnet_test) BOOST_AUTO_TEST_CASE(subnet_test)
{ {
BOOST_CHECK(CSubNet("1.2.3.0/24") == CSubNet("1.2.3.0/255.255.255.0")); BOOST_CHECK(CSubNet("1.2.3.0/24") == CSubNet("1.2.3.0/255.255.255.0"));
BOOST_CHECK(CSubNet("1.2.3.0/24") != CSubNet("1.2.4.0/255.255.255.0")); BOOST_CHECK(CSubNet("1.2.3.0/24") != CSubNet("1.2.4.0/255.255.255.0"));
BOOST_CHECK(CSubNet("1.2.3.0/24").Match(CNetAddr("1.2.3.4"))); BOOST_CHECK(CSubNet("1.2.3.0/24").Match(ResolveIP("1.2.3.4")));
BOOST_CHECK(!CSubNet("1.2.2.0/24").Match(CNetAddr("1.2.3.4"))); BOOST_CHECK(!CSubNet("1.2.2.0/24").Match(ResolveIP("1.2.3.4")));
BOOST_CHECK(CSubNet("1.2.3.4").Match(CNetAddr("1.2.3.4"))); BOOST_CHECK(CSubNet("1.2.3.4").Match(ResolveIP("1.2.3.4")));
BOOST_CHECK(CSubNet("1.2.3.4/32").Match(CNetAddr("1.2.3.4"))); BOOST_CHECK(CSubNet("1.2.3.4/32").Match(ResolveIP("1.2.3.4")));
BOOST_CHECK(!CSubNet("1.2.3.4").Match(CNetAddr("5.6.7.8"))); BOOST_CHECK(!CSubNet("1.2.3.4").Match(ResolveIP("5.6.7.8")));
BOOST_CHECK(!CSubNet("1.2.3.4/32").Match(CNetAddr("5.6.7.8"))); BOOST_CHECK(!CSubNet("1.2.3.4/32").Match(ResolveIP("5.6.7.8")));
BOOST_CHECK(CSubNet("::ffff:127.0.0.1").Match(CNetAddr("127.0.0.1"))); BOOST_CHECK(CSubNet("::ffff:127.0.0.1").Match(ResolveIP("127.0.0.1")));
BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:8"))); BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:8")));
BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8").Match(CNetAddr("1:2:3:4:5:6:7:9"))); BOOST_CHECK(!CSubNet("1:2:3:4:5:6:7:8").Match(ResolveIP("1:2:3:4:5:6:7:9")));
BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:0/112").Match(CNetAddr("1:2:3:4:5:6:7:1234"))); BOOST_CHECK(CSubNet("1:2:3:4:5:6:7:0/112").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
BOOST_CHECK(CSubNet("192.168.0.1/24").Match(CNetAddr("192.168.0.2"))); BOOST_CHECK(CSubNet("192.168.0.1/24").Match(ResolveIP("192.168.0.2")));
BOOST_CHECK(CSubNet("192.168.0.20/29").Match(CNetAddr("192.168.0.18"))); BOOST_CHECK(CSubNet("192.168.0.20/29").Match(ResolveIP("192.168.0.18")));
BOOST_CHECK(CSubNet("1.2.2.1/24").Match(CNetAddr("1.2.2.4"))); BOOST_CHECK(CSubNet("1.2.2.1/24").Match(ResolveIP("1.2.2.4")));
BOOST_CHECK(CSubNet("1.2.2.110/31").Match(CNetAddr("1.2.2.111"))); BOOST_CHECK(CSubNet("1.2.2.110/31").Match(ResolveIP("1.2.2.111")));
BOOST_CHECK(CSubNet("1.2.2.20/26").Match(CNetAddr("1.2.2.63"))); BOOST_CHECK(CSubNet("1.2.2.20/26").Match(ResolveIP("1.2.2.63")));
// All-Matching IPv6 Matches arbitrary IPv4 and IPv6 // All-Matching IPv6 Matches arbitrary IPv4 and IPv6
BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1:2:3:4:5:6:7:1234"))); BOOST_CHECK(CSubNet("::/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
BOOST_CHECK(CSubNet("::/0").Match(CNetAddr("1.2.3.4"))); BOOST_CHECK(CSubNet("::/0").Match(ResolveIP("1.2.3.4")));
// All-Matching IPv4 does not Match IPv6 // All-Matching IPv4 does not Match IPv6
BOOST_CHECK(!CSubNet("0.0.0.0/0").Match(CNetAddr("1:2:3:4:5:6:7:1234"))); BOOST_CHECK(!CSubNet("0.0.0.0/0").Match(ResolveIP("1:2:3:4:5:6:7:1234")));
// Invalid subnets Match nothing (not even invalid addresses) // Invalid subnets Match nothing (not even invalid addresses)
BOOST_CHECK(!CSubNet().Match(CNetAddr("1.2.3.4"))); BOOST_CHECK(!CSubNet().Match(ResolveIP("1.2.3.4")));
BOOST_CHECK(!CSubNet("").Match(CNetAddr("4.5.6.7"))); BOOST_CHECK(!CSubNet("").Match(ResolveIP("4.5.6.7")));
BOOST_CHECK(!CSubNet("bloop").Match(CNetAddr("0.0.0.0"))); BOOST_CHECK(!CSubNet("bloop").Match(ResolveIP("0.0.0.0")));
BOOST_CHECK(!CSubNet("bloop").Match(CNetAddr("hab"))); BOOST_CHECK(!CSubNet("bloop").Match(ResolveIP("hab")));
// Check valid/invalid // Check valid/invalid
BOOST_CHECK(CSubNet("1.2.3.0/0").IsValid()); BOOST_CHECK(CSubNet("1.2.3.0/0").IsValid());
BOOST_CHECK(!CSubNet("1.2.3.0/-1").IsValid()); BOOST_CHECK(!CSubNet("1.2.3.0/-1").IsValid());
@ -146,15 +159,15 @@ BOOST_AUTO_TEST_CASE(subnet_test)
BOOST_CHECK(!CSubNet("fuzzy").IsValid()); BOOST_CHECK(!CSubNet("fuzzy").IsValid());
//CNetAddr constructor test //CNetAddr constructor test
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).IsValid()); BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).IsValid());
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).Match(CNetAddr("127.0.0.1"))); BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.1")));
BOOST_CHECK(!CSubNet(CNetAddr("127.0.0.1")).Match(CNetAddr("127.0.0.2"))); BOOST_CHECK(!CSubNet(ResolveIP("127.0.0.1")).Match(ResolveIP("127.0.0.2")));
BOOST_CHECK(CSubNet(CNetAddr("127.0.0.1")).ToString() == "127.0.0.1/32"); BOOST_CHECK(CSubNet(ResolveIP("127.0.0.1")).ToString() == "127.0.0.1/32");
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).IsValid()); BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).IsValid());
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).Match(CNetAddr("1:2:3:4:5:6:7:8"))); BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:8")));
BOOST_CHECK(!CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).Match(CNetAddr("1:2:3:4:5:6:7:9"))); BOOST_CHECK(!CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).Match(ResolveIP("1:2:3:4:5:6:7:9")));
BOOST_CHECK(CSubNet(CNetAddr("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128"); BOOST_CHECK(CSubNet(ResolveIP("1:2:3:4:5:6:7:8")).ToString() == "1:2:3:4:5:6:7:8/128");
CSubNet subnet = CSubNet("1.2.3.4/255.255.255.255"); CSubNet subnet = CSubNet("1.2.3.4/255.255.255.255");
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.3.4/32");
@ -233,22 +246,25 @@ BOOST_AUTO_TEST_CASE(subnet_test)
BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/255.255.232.0"); BOOST_CHECK_EQUAL(subnet.ToString(), "1.2.0.0/255.255.232.0");
subnet = CSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f"); subnet = CSubNet("1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f"); BOOST_CHECK_EQUAL(subnet.ToString(), "1:2:3:4:5:6:7:8/ffff:ffff:ffff:fffe:ffff:ffff:ffff:ff0f");
} }
BOOST_AUTO_TEST_CASE(netbase_getgroup) BOOST_AUTO_TEST_CASE(netbase_getgroup)
{ {
BOOST_CHECK(CNetAddr("127.0.0.1").GetGroup() == boost::assign::list_of(0)); // Local -> !Routable()
BOOST_CHECK(CNetAddr("257.0.0.1").GetGroup() == boost::assign::list_of(0)); // !Valid -> !Routable() BOOST_CHECK(ResolveIP("127.0.0.1").GetGroup() == boost::assign::list_of(0)); // Local -> !Routable()
BOOST_CHECK(CNetAddr("10.0.0.1").GetGroup() == boost::assign::list_of(0)); // RFC1918 -> !Routable() BOOST_CHECK(ResolveIP("257.0.0.1").GetGroup() == boost::assign::list_of(0)); // !Valid -> !Routable()
BOOST_CHECK(CNetAddr("169.254.1.1").GetGroup() == boost::assign::list_of(0)); // RFC3927 -> !Routable() BOOST_CHECK(ResolveIP("10.0.0.1").GetGroup() == boost::assign::list_of(0)); // RFC1918 -> !Routable()
BOOST_CHECK(CNetAddr("1.2.3.4").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // IPv4 BOOST_CHECK(ResolveIP("169.254.1.1").GetGroup() == boost::assign::list_of(0)); // RFC3927 -> !Routable()
BOOST_CHECK(CNetAddr("::FFFF:0:102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6145 BOOST_CHECK(ResolveIP("1.2.3.4").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // IPv4
BOOST_CHECK(CNetAddr("64:FF9B::102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6052 BOOST_CHECK(ResolveIP("::FFFF:0:102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6145
BOOST_CHECK(CNetAddr("2002:102:304:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC3964 BOOST_CHECK(ResolveIP("64:FF9B::102:304").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC6052
BOOST_CHECK(CNetAddr("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC4380 BOOST_CHECK(ResolveIP("2002:102:304:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC3964
BOOST_CHECK(CNetAddr("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup() == boost::assign::list_of((unsigned char)NET_TOR)(239)); // Tor BOOST_CHECK(ResolveIP("2001:0:9999:9999:9999:9999:FEFD:FCFB").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV4)(1)(2)); // RFC4380
BOOST_CHECK(CNetAddr("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(4)(112)(175)); //he.net BOOST_CHECK(ResolveIP("FD87:D87E:EB43:edb1:8e4:3588:e546:35ca").GetGroup() == boost::assign::list_of((unsigned char)NET_TOR)(239)); // Tor
BOOST_CHECK(CNetAddr("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(32)(1)); //IPv6 BOOST_CHECK(ResolveIP("2001:470:abcd:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(4)(112)(175)); //he.net
BOOST_CHECK(ResolveIP("2001:2001:9999:9999:9999:9999:9999:9999").GetGroup() == boost::assign::list_of((unsigned char)NET_IPV6)(32)(1)(32)(1)); //IPv6
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()