net: Split resolving out of CService

pull/308/head
Cory Fields 9 years ago
parent 31d6b1d5f0
commit f96c7c4d91

@ -614,7 +614,7 @@ CService HTTPRequest::GetPeer()
const char* address = ""; const char* address = "";
uint16_t port = 0; uint16_t port = 0;
evhttp_connection_get_peer(con, (char**)&address, &port); evhttp_connection_get_peer(con, (char**)&address, &port);
peer = CService(address, port); LookupNumeric(address, peer, port);
} }
return peer; return peer;
} }

@ -1096,7 +1096,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
std::string proxyArg = GetArg("-proxy", ""); std::string proxyArg = GetArg("-proxy", "");
SetLimited(NET_TOR); SetLimited(NET_TOR);
if (proxyArg != "" && proxyArg != "0") { if (proxyArg != "" && proxyArg != "0") {
proxyType addrProxy = proxyType(CService(proxyArg, 9050), proxyRandomize); CService resolved;
LookupNumeric(proxyArg.c_str(), resolved, 9050);
proxyType addrProxy = proxyType(resolved, proxyRandomize);
if (!addrProxy.IsValid()) if (!addrProxy.IsValid())
return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg)); return InitError(strprintf(_("Invalid -proxy address: '%s'"), proxyArg));
@ -1115,7 +1117,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
if (onionArg == "0") { // Handle -noonion/-onion=0 if (onionArg == "0") { // Handle -noonion/-onion=0
SetLimited(NET_TOR); // set onions as unreachable SetLimited(NET_TOR); // set onions as unreachable
} else { } else {
proxyType addrOnion = proxyType(CService(onionArg, 9050), proxyRandomize); CService resolved;
LookupNumeric(onionArg.c_str(), resolved, 9050);
proxyType addrOnion = proxyType(resolved, proxyRandomize);
if (!addrOnion.IsValid()) if (!addrOnion.IsValid())
return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg)); return InitError(strprintf(_("Invalid -onion address: '%s'"), onionArg));
SetProxy(NET_TOR, addrOnion); SetProxy(NET_TOR, addrOnion);

@ -175,7 +175,7 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
// one by discovery. // one by discovery.
CAddress GetLocalAddress(const CNetAddr *paddrPeer) CAddress GetLocalAddress(const CNetAddr *paddrPeer)
{ {
CAddress ret(CService("0.0.0.0",GetListenPort()), NODE_NONE); CAddress ret(CService(CNetAddr(),GetListenPort()), NODE_NONE);
CService addr; CService addr;
if (GetLocal(addr, paddrPeer)) if (GetLocal(addr, paddrPeer))
{ {
@ -494,7 +494,7 @@ void CNode::PushVersion()
int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0); int nBestHeight = GetNodeSignals().GetHeight().get_value_or(0);
int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime()); int64_t nTime = (fInbound ? GetAdjustedTime() : GetTime());
CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService("0.0.0.0", 0), addr.nServices)); CAddress addrYou = (addr.IsRoutable() && !IsProxy(addr) ? addr : CAddress(CService(), addr.nServices));
CAddress addrMe = GetLocalAddress(&addr); CAddress addrMe = GetLocalAddress(&addr);
GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce)); GetRandBytes((unsigned char*)&nLocalHostNonce, sizeof(nLocalHostNonce));
if (fLogIPs) if (fLogIPs)
@ -1727,7 +1727,8 @@ std::vector<AddedNodeInfo> GetAddedNodeInfo()
} }
BOOST_FOREACH(const std::string& strAddNode, lAddresses) { BOOST_FOREACH(const std::string& strAddNode, lAddresses) {
CService service(strAddNode, Params().GetDefaultPort()); CService service;
LookupNumeric(strAddNode.c_str(), service, Params().GetDefaultPort());
if (service.IsValid()) { if (service.IsValid()) {
// strAddNode is an IP:port // strAddNode is an IP:port
auto it = mapConnected.find(service); auto it = mapConnected.find(service);
@ -1765,7 +1766,8 @@ void ThreadOpenAddedConnections()
CSemaphoreGrant grant(*semOutbound); CSemaphoreGrant grant(*semOutbound);
// If strAddedNode is an IP/port, decode it immediately, so // If strAddedNode is an IP/port, decode it immediately, so
// OpenNetworkConnection can detect existing connections to that IP/port. // OpenNetworkConnection can detect existing connections to that IP/port.
CService service(info.strAddedNode, Params().GetDefaultPort()); CService service;
LookupNumeric(info.strAddedNode.c_str(), service, Params().GetDefaultPort());
OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false); OpenNetworkConnection(CAddress(service, NODE_NONE), false, &grant, info.strAddedNode.c_str(), false);
MilliSleep(500); MilliSleep(500);
} }
@ -2063,8 +2065,11 @@ void StartNode(boost::thread_group& threadGroup, CScheduler& scheduler)
semOutbound = new CSemaphore(nMaxOutbound); semOutbound = new CSemaphore(nMaxOutbound);
} }
if (pnodeLocalHost == NULL) if (pnodeLocalHost == NULL) {
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService("127.0.0.1", 0), nLocalServices)); CNetAddr local;
LookupHost("127.0.0.1", local, false);
pnodeLocalHost = new CNode(INVALID_SOCKET, CAddress(CService(local, 0), nLocalServices));
}
Discover(threadGroup); Discover(threadGroup);

@ -639,7 +639,7 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
} }
} }
addr = CService("0.0.0.0:0"); addr = CService();
if (!HaveNameProxy()) if (!HaveNameProxy())
return false; return false;
@ -1124,38 +1124,6 @@ bool CService::SetSockAddr(const struct sockaddr *paddr)
} }
} }
CService::CService(const char *pszIpPort)
{
Init();
CService ip;
if (Lookup(pszIpPort, ip, 0, false))
*this = ip;
}
CService::CService(const char *pszIpPort, int portDefault)
{
Init();
CService ip;
if (Lookup(pszIpPort, ip, portDefault, false))
*this = ip;
}
CService::CService(const std::string &strIpPort)
{
Init();
CService ip;
if (Lookup(strIpPort.c_str(), ip, 0, false))
*this = ip;
}
CService::CService(const std::string &strIpPort, int portDefault)
{
Init();
CService ip;
if (Lookup(strIpPort.c_str(), ip, portDefault, false))
*this = ip;
}
unsigned short CService::GetPort() const unsigned short CService::GetPort() const
{ {
return port; return port;

@ -152,10 +152,6 @@ class CService : public CNetAddr
CService(const CNetAddr& ip, unsigned short port); CService(const CNetAddr& ip, unsigned short port);
CService(const struct in_addr& ipv4Addr, unsigned short port); CService(const struct in_addr& ipv4Addr, unsigned short port);
CService(const struct sockaddr_in& addr); CService(const struct sockaddr_in& addr);
explicit CService(const char *pszIpPort, int portDefault);
explicit CService(const char *pszIpPort);
explicit CService(const std::string& strIpPort, int portDefault);
explicit CService(const std::string& strIpPort);
void Init(); void Init();
void SetPort(unsigned short portIn); void SetPort(unsigned short portIn);
unsigned short GetPort() const; unsigned short GetPort() const;

@ -327,7 +327,9 @@ QValidator::State ProxyAddressValidator::validate(QString &input, int &pos) cons
{ {
Q_UNUSED(pos); Q_UNUSED(pos);
// Validate the proxy // Validate the proxy
proxyType addrProxy = proxyType(CService(input.toStdString(), 9050), true); CService serv;
LookupNumeric(input.toStdString().c_str(), serv, 9050);
proxyType addrProxy = proxyType(serv, true);
if (addrProxy.IsValid()) if (addrProxy.IsValid())
return QValidator::Acceptable; return QValidator::Acceptable;

@ -62,6 +62,18 @@ static CNetAddr ResolveIP(std::string ip)
return ResolveIP(ip.c_str()); return ResolveIP(ip.c_str());
} }
static CService ResolveService(const char* ip, int port = 0)
{
CService serv;
BOOST_CHECK_MESSAGE(Lookup(ip, serv, port, false), strprintf("failed to resolve: %s:%i", ip, port));
return serv;
}
static CService ResolveService(std::string ip, int port = 0)
{
return ResolveService(ip.c_str(), port);
}
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)
@ -79,7 +91,7 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
BOOST_CHECK(addr_null.ToString() == "[::]:0"); BOOST_CHECK(addr_null.ToString() == "[::]:0");
// Test 2: Does Addrman::Add work as expected. // Test 2: Does Addrman::Add work as expected.
CService addr1 = CService("250.1.1.1", 8333); CService addr1 = ResolveService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1, NODE_NONE), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
CAddrInfo addr_ret1 = addrman.Select(); CAddrInfo addr_ret1 = addrman.Select();
@ -87,14 +99,14 @@ BOOST_AUTO_TEST_CASE(addrman_simple)
// Test 3: Does IP address deduplication work correctly. // Test 3: Does IP address deduplication work correctly.
// Expected dup IP should not be added. // Expected dup IP should not be added.
CService addr1_dup = CService("250.1.1.1", 8333); CService addr1_dup = ResolveService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1_dup, NODE_NONE), source); addrman.Add(CAddress(addr1_dup, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
// Test 5: New table has one addr and we add a diff addr we should // Test 5: New table has one addr and we add a diff addr we should
// have two addrs. // have two addrs.
CService addr2 = CService("250.1.1.2", 8333); CService addr2 = ResolveService("250.1.1.2", 8333);
addrman.Add(CAddress(addr2, NODE_NONE), source); addrman.Add(CAddress(addr2, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 2); BOOST_CHECK(addrman.size() == 2);
@ -117,11 +129,11 @@ BOOST_AUTO_TEST_CASE(addrman_ports)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
// Test 7; Addr with same IP but diff port does not replace existing addr. // Test 7; Addr with same IP but diff port does not replace existing addr.
CService addr1 = CService("250.1.1.1", 8333); CService addr1 = ResolveService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1, NODE_NONE), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
CService addr1_port = CService("250.1.1.1", 8334); CService addr1_port = ResolveService("250.1.1.1", 8334);
addrman.Add(CAddress(addr1_port, NODE_NONE), source); addrman.Add(CAddress(addr1_port, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
CAddrInfo addr_ret2 = addrman.Select(); CAddrInfo addr_ret2 = addrman.Select();
@ -147,7 +159,7 @@ BOOST_AUTO_TEST_CASE(addrman_select)
CNetAddr source = ResolveIP("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 = ResolveService("250.1.1.1", 8333);
addrman.Add(CAddress(addr1, NODE_NONE), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 1); BOOST_CHECK(addrman.size() == 1);
@ -168,24 +180,24 @@ BOOST_AUTO_TEST_CASE(addrman_select)
// Add three addresses to new table. // Add three addresses to new table.
CService addr2 = CService("250.3.1.1", 8333); CService addr2 = ResolveService("250.3.1.1", 8333);
CService addr3 = CService("250.3.2.2", 9999); CService addr3 = ResolveService("250.3.2.2", 9999);
CService addr4 = CService("250.3.3.3", 9999); CService addr4 = ResolveService("250.3.3.3", 9999);
addrman.Add(CAddress(addr2, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE); addrman.Add(CAddress(addr2, NODE_NONE), ResolveService("250.3.1.1", 8333));
addrman.Add(CAddress(addr3, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE); addrman.Add(CAddress(addr3, NODE_NONE), ResolveService("250.3.1.1", 8333));
addrman.Add(CAddress(addr4, NODE_NONE), CService("250.4.1.1", 8333), NODE_NONE); addrman.Add(CAddress(addr4, NODE_NONE), ResolveService("250.4.1.1", 8333));
// Add three addresses to tried table. // Add three addresses to tried table.
CService addr5 = CService("250.4.4.4", 8333); CService addr5 = ResolveService("250.4.4.4", 8333);
CService addr6 = CService("250.4.5.5", 7777); CService addr6 = ResolveService("250.4.5.5", 7777);
CService addr7 = CService("250.4.6.6", 8333); CService addr7 = ResolveService("250.4.6.6", 8333);
addrman.Add(CAddress(addr5, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE); addrman.Add(CAddress(addr5, NODE_NONE), ResolveService("250.3.1.1", 8333));
addrman.Good(CAddress(addr5, NODE_NONE)); addrman.Good(CAddress(addr5, NODE_NONE));
addrman.Add(CAddress(addr6, NODE_NONE), CService("250.3.1.1", 8333), NODE_NONE); addrman.Add(CAddress(addr6, NODE_NONE), ResolveService("250.3.1.1", 8333));
addrman.Good(CAddress(addr6, NODE_NONE)); addrman.Good(CAddress(addr6, NODE_NONE));
addrman.Add(CAddress(addr7, NODE_NONE), CService("250.1.1.3", 8333), NODE_NONE); addrman.Add(CAddress(addr7, NODE_NONE), ResolveService("250.1.1.3", 8333));
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.
@ -210,7 +222,7 @@ BOOST_AUTO_TEST_CASE(addrman_new_collisions)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
for (unsigned int i = 1; i < 18; i++) { for (unsigned int i = 1; i < 18; i++) {
CService addr = CService("250.1.1." + boost::to_string(i)); CService addr = ResolveService("250.1.1." + boost::to_string(i));
addrman.Add(CAddress(addr, NODE_NONE), source); addrman.Add(CAddress(addr, NODE_NONE), source);
//Test 13: No collision in new table yet. //Test 13: No collision in new table yet.
@ -218,11 +230,11 @@ BOOST_AUTO_TEST_CASE(addrman_new_collisions)
} }
//Test 14: new table collision! //Test 14: new table collision!
CService addr1 = CService("250.1.1.18"); CService addr1 = ResolveService("250.1.1.18");
addrman.Add(CAddress(addr1, NODE_NONE), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 17); BOOST_CHECK(addrman.size() == 17);
CService addr2 = CService("250.1.1.19"); CService addr2 = ResolveService("250.1.1.19");
addrman.Add(CAddress(addr2, NODE_NONE), source); addrman.Add(CAddress(addr2, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 18); BOOST_CHECK(addrman.size() == 18);
} }
@ -239,7 +251,7 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
for (unsigned int i = 1; i < 80; i++) { for (unsigned int i = 1; i < 80; i++) {
CService addr = CService("250.1.1." + boost::to_string(i)); CService addr = ResolveService("250.1.1." + boost::to_string(i));
addrman.Add(CAddress(addr, NODE_NONE), source); addrman.Add(CAddress(addr, NODE_NONE), source);
addrman.Good(CAddress(addr, NODE_NONE)); addrman.Good(CAddress(addr, NODE_NONE));
@ -249,11 +261,11 @@ BOOST_AUTO_TEST_CASE(addrman_tried_collisions)
} }
//Test 16: tried table collision! //Test 16: tried table collision!
CService addr1 = CService("250.1.1.80"); CService addr1 = ResolveService("250.1.1.80");
addrman.Add(CAddress(addr1, NODE_NONE), source); addrman.Add(CAddress(addr1, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 79); BOOST_CHECK(addrman.size() == 79);
CService addr2 = CService("250.1.1.81"); CService addr2 = ResolveService("250.1.1.81");
addrman.Add(CAddress(addr2, NODE_NONE), source); addrman.Add(CAddress(addr2, NODE_NONE), source);
BOOST_CHECK(addrman.size() == 80); BOOST_CHECK(addrman.size() == 80);
} }
@ -267,9 +279,9 @@ BOOST_AUTO_TEST_CASE(addrman_find)
BOOST_CHECK(addrman.size() == 0); BOOST_CHECK(addrman.size() == 0);
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
CAddress addr3 = CAddress(CService("251.255.2.1", 8333), NODE_NONE); CAddress addr3 = CAddress(ResolveService("251.255.2.1", 8333), NODE_NONE);
CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
CNetAddr source2 = ResolveIP("250.1.2.2"); CNetAddr source2 = ResolveIP("250.1.2.2");
@ -306,7 +318,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(ResolveService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
int nId; int nId;
@ -329,7 +341,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(ResolveService("250.1.2.1", 8333), NODE_NONE);
CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
int nId; int nId;
@ -356,15 +368,15 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
vector<CAddress> vAddr1 = addrman.GetAddr(); vector<CAddress> vAddr1 = addrman.GetAddr();
BOOST_CHECK(vAddr1.size() == 0); BOOST_CHECK(vAddr1.size() == 0);
CAddress addr1 = CAddress(CService("250.250.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(ResolveService("250.250.2.1", 8333), NODE_NONE);
addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false addr1.nTime = GetAdjustedTime(); // Set time so isTerrible = false
CAddress addr2 = CAddress(CService("250.251.2.2", 9999), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.251.2.2", 9999), NODE_NONE);
addr2.nTime = GetAdjustedTime(); addr2.nTime = GetAdjustedTime();
CAddress addr3 = CAddress(CService("251.252.2.3", 8333), NODE_NONE); CAddress addr3 = CAddress(ResolveService("251.252.2.3", 8333), NODE_NONE);
addr3.nTime = GetAdjustedTime(); addr3.nTime = GetAdjustedTime();
CAddress addr4 = CAddress(CService("252.253.3.4", 8333), NODE_NONE); CAddress addr4 = CAddress(ResolveService("252.253.3.4", 8333), NODE_NONE);
addr4.nTime = GetAdjustedTime(); addr4.nTime = GetAdjustedTime();
CAddress addr5 = CAddress(CService("252.254.4.5", 8333), NODE_NONE); CAddress addr5 = CAddress(ResolveService("252.254.4.5", 8333), NODE_NONE);
addr5.nTime = GetAdjustedTime(); addr5.nTime = GetAdjustedTime();
CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
CNetAddr source2 = ResolveIP("250.2.3.3"); CNetAddr source2 = ResolveIP("250.2.3.3");
@ -390,7 +402,7 @@ BOOST_AUTO_TEST_CASE(addrman_getaddr)
int octet2 = (i / 256) % 256; int octet2 = (i / 256) % 256;
int octet3 = (i / (256 * 2)) % 256; int octet3 = (i / (256 * 2)) % 256;
string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23"; string strAddr = boost::to_string(octet1) + "." + boost::to_string(octet2) + "." + boost::to_string(octet3) + ".23";
CAddress addr = CAddress(CService(strAddr), NODE_NONE); CAddress addr = CAddress(ResolveService(strAddr), NODE_NONE);
// Ensure that for all addrs in addrman, isTerrible == false. // Ensure that for all addrs in addrman, isTerrible == false.
addr.nTime = GetAdjustedTime(); addr.nTime = GetAdjustedTime();
@ -415,8 +427,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CAddress addr1 = CAddress(CService("250.1.1.1", 8333), NODE_NONE); CAddress addr1 = CAddress(ResolveService("250.1.1.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.1.1", 9999), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.1.1", 9999), NODE_NONE);
CNetAddr source1 = ResolveIP("250.1.1.1"); CNetAddr source1 = ResolveIP("250.1.1.1");
@ -443,7 +455,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
set<int> buckets; set<int> buckets;
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(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE),
ResolveIP("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);
@ -455,7 +467,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_tried_bucket)
buckets.clear(); buckets.clear();
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(ResolveService("250." + boost::to_string(j) + ".1.1"), NODE_NONE),
ResolveIP("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);
@ -472,8 +484,8 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
// Set addrman addr placement to be deterministic. // Set addrman addr placement to be deterministic.
addrman.MakeDeterministic(); addrman.MakeDeterministic();
CAddress addr1 = CAddress(CService("250.1.2.1", 8333), NODE_NONE); CAddress addr1 = CAddress(ResolveService("250.1.2.1", 8333), NODE_NONE);
CAddress addr2 = CAddress(CService("250.1.2.1", 9999), NODE_NONE); CAddress addr2 = CAddress(ResolveService("250.1.2.1", 9999), NODE_NONE);
CNetAddr source1 = ResolveIP("250.1.2.1"); CNetAddr source1 = ResolveIP("250.1.2.1");
@ -496,7 +508,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
set<int> buckets; set<int> buckets;
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(ResolveService("250.1.1." + boost::to_string(i)), NODE_NONE),
ResolveIP("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);
@ -508,7 +520,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
buckets.clear(); buckets.clear();
for (int j = 0; j < 4 * 255; j++) { for (int j = 0; j < 4 * 255; j++) {
CAddrInfo infoj = CAddrInfo(CAddress( CAddrInfo infoj = CAddrInfo(CAddress(
CService( ResolveService(
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),
ResolveIP("251.4.1.1")); ResolveIP("251.4.1.1"));
int bucket = infoj.GetNewBucket(nKey1); int bucket = infoj.GetNewBucket(nKey1);
@ -521,7 +533,7 @@ BOOST_AUTO_TEST_CASE(caddrinfo_get_new_bucket)
buckets.clear(); buckets.clear();
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(ResolveService("250.1.1.1"), NODE_NONE),
ResolveIP("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);

@ -51,7 +51,9 @@ public:
int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30); int nUBuckets = ADDRMAN_NEW_BUCKET_COUNT ^ (1 << 30);
s << nUBuckets; s << nUBuckets;
CAddress addr = CAddress(CService("252.1.1.1", 7777), NODE_NONE); CService serv;
Lookup("252.1.1.1", serv, 7777, false);
CAddress addr = CAddress(serv, NODE_NONE);
CNetAddr resolved; CNetAddr resolved;
LookupHost("252.2.2.2", resolved, false); LookupHost("252.2.2.2", resolved, false);
CAddrInfo info = CAddrInfo(addr, resolved); CAddrInfo info = CAddrInfo(addr, resolved);
@ -76,14 +78,17 @@ BOOST_AUTO_TEST_CASE(caddrdb_read)
CAddrManUncorrupted addrmanUncorrupted; CAddrManUncorrupted addrmanUncorrupted;
addrmanUncorrupted.MakeDeterministic(); addrmanUncorrupted.MakeDeterministic();
CService addr1 = CService("250.7.1.1", 8333); CService addr1, addr2, addr3;
CService addr2 = CService("250.7.2.2", 9999); Lookup("250.7.1.1", addr1, 8333, false);
CService addr3 = CService("250.7.3.3", 9999); Lookup("250.7.2.2", addr2, 9999, false);
Lookup("250.7.3.3", addr3, 9999, false);
// Add three addresses to new table. // Add three addresses to new table.
addrmanUncorrupted.Add(CAddress(addr1, NODE_NONE), CService("252.5.1.1", 8333)); CService source;
addrmanUncorrupted.Add(CAddress(addr2, NODE_NONE), CService("252.5.1.1", 8333)); Lookup("252.5.1.1", source, 8333, false);
addrmanUncorrupted.Add(CAddress(addr3, NODE_NONE), CService("252.5.1.1", 8333)); addrmanUncorrupted.Add(CAddress(addr1, NODE_NONE), source);
addrmanUncorrupted.Add(CAddress(addr2, NODE_NONE), source);
addrmanUncorrupted.Add(CAddress(addr3, NODE_NONE), source);
// Test that the de-serialization does not throw an exception. // Test that the de-serialization does not throw an exception.
CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted); CDataStream ssPeers1 = AddrmanToStream(addrmanUncorrupted);

@ -437,8 +437,7 @@ void TorController::add_onion_cb(TorControlConnection& conn, const TorControlRep
if ((i = m.find("PrivateKey")) != m.end()) if ((i = m.find("PrivateKey")) != m.end())
private_key = i->second; private_key = i->second;
} }
LookupNumeric(std::string(service_id+".onion").c_str(), service, GetListenPort());
service = CService(service_id+".onion", GetListenPort());
LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString()); LogPrintf("tor: Got service ID %s, advertising service %s\n", service_id, service.ToString());
if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) { if (WriteBinaryFile(GetPrivateKeyFile(), private_key)) {
LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile()); LogPrint("tor", "tor: Cached service private key to %s\n", GetPrivateKeyFile());
@ -462,7 +461,9 @@ void TorController::auth_cb(TorControlConnection& conn, const TorControlReply& r
// Now that we know Tor is running setup the proxy for onion addresses // Now that we know Tor is running setup the proxy for onion addresses
// if -onion isn't set to something else. // if -onion isn't set to something else.
if (GetArg("-onion", "") == "") { if (GetArg("-onion", "") == "") {
proxyType addrOnion = proxyType(CService("127.0.0.1", 9050), true); CService resolved;
assert(LookupNumeric("127.0.0.1", resolved, 9050));
proxyType addrOnion = proxyType(resolved, true);
SetProxy(NET_TOR, addrOnion); SetProxy(NET_TOR, addrOnion);
SetLimited(NET_TOR, false); SetLimited(NET_TOR, false);
} }

Loading…
Cancel
Save