Skip to content

Commit

Permalink
Split DNS resolving functionality out of net structures
Browse files Browse the repository at this point in the history
  • Loading branch information
Pythonix committed Aug 2, 2022
1 parent 1ca905e commit cd25ff2
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 32 deletions.
7 changes: 4 additions & 3 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1135,10 +1135,11 @@ bool AppInit2(ThreadHandlerPtr threads)
{
for (auto const& strAddr : gArgs.GetArgs("-externalip"))
{
CService addrLocal(strAddr, GetListenPort(), fNameLookup);
if (!addrLocal.IsValid())
CService addrLocal;
if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
AddLocal(addrLocal, LOCAL_MANUAL);
else
return InitError(strprintf(_("Cannot resolve -externalip address: '%s'"), strAddr));
AddLocal(CService(strAddr, GetListenPort(), fNameLookup), LOCAL_MANUAL);
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/net.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1311,11 +1311,11 @@ void ThreadDNSAddressSeed2(void* parg)
if (HaveNameProxy()) {
AddOneShot(seed[1]);
} else {
vector<CNetAddr> vaddr;
vector<CNetAddr> vIPs;
vector<CAddress> vAdd;
if (LookupHost(seed[1], vaddr))
if (LookupHost(seed[1], vIPs, 0, true))
{
for (auto const& ip : vaddr)
for (auto const& ip : vIPs)
{
int nOneDay = 24*3600;
CAddress addr = CAddress(CService(ip, GetDefaultPort()));
Expand All @@ -1324,7 +1324,15 @@ void ThreadDNSAddressSeed2(void* parg)
found++;
}
}
addrman.Add(vAdd, CNetAddr(seed[0], true));
// TODO: The seed name resolve may fail, yielding an IP of [::], which results in
// addrman assigning the same source to results from different seeds.
// This should switch to a hard-coded stable dummy IP for each seed name, so that the
// resolve is not required at all.
if (!vIPs.empty()) {
CService seedSource;
Lookup(seed[0], seedSource, 0, true);
addrman.Add(vAdd, seedSource);
}
}
}
}
Expand Down
34 changes: 18 additions & 16 deletions src/netbase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -460,10 +460,12 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
CService nameProxy;
GetNameProxy(nameProxy);

CService addrResolved(CNetAddr(strDest, fNameLookup && !HaveNameProxy()), port);
if (addrResolved.IsValid()) {
addr = addrResolved;
return ConnectSocket(addr, hSocketRet, nTimeout);
CService addrResolved;
if (Lookup(strDest.c_str(), addrResolved, port, fNameLookup && !HaveNameProxy())) {
if (addrResolved.IsValid()) {
addr = addrResolved;
return ConnectSocket(addr, hSocketRet, nTimeout);
}
}
addr = CService("0.0.0.0:0");

Expand Down Expand Up @@ -522,19 +524,19 @@ CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr)
memcpy(ip, &ipv6Addr, 16);
}

CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup)
CNetAddr::CNetAddr(const char *pszIp)
{
Init();
std::vector<CNetAddr> vIP;
if (LookupHost(pszIp, vIP, 1, fAllowLookup))
if (LookupHost(pszIp, vIP, 1, false))
*this = vIP[0];
}

CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup)
CNetAddr::CNetAddr(const std::string &strIp)
{
Init();
std::vector<CNetAddr> vIP;
if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup))
if (LookupHost(strIp.c_str(), vIP, 1, false))
*this = vIP[0];
}

Expand Down Expand Up @@ -948,35 +950,35 @@ bool CService::SetSockAddr(const struct sockaddr *paddr)
}
}

CService::CService(const char *pszIpPort, bool fAllowLookup)
CService::CService(const char *pszIpPort)
{
Init();
CService ip;
if (Lookup(pszIpPort, ip, 0, fAllowLookup))
if (Lookup(pszIpPort, ip, 0, false))
*this = ip;
}

CService::CService(const char *pszIpPort, int portDefault, bool fAllowLookup)
CService::CService(const char *pszIpPort, int portDefault)
{
Init();
CService ip;
if (Lookup(pszIpPort, ip, portDefault, fAllowLookup))
if (Lookup(pszIpPort, ip, portDefault, false))
*this = ip;
}

CService::CService(const std::string &strIpPort, bool fAllowLookup)
CService::CService(const std::string &strIpPort)
{
Init();
CService ip;
if (Lookup(strIpPort.c_str(), ip, 0, fAllowLookup))
if (Lookup(strIpPort.c_str(), ip, 0, false))
*this = ip;
}

CService::CService(const std::string &strIpPort, int portDefault, bool fAllowLookup)
CService::CService(const std::string &strIpPort, int portDefault)
{
Init();
CService ip;
if (Lookup(strIpPort.c_str(), ip, portDefault, fAllowLookup))
if (Lookup(strIpPort.c_str(), ip, portDefault, false))
*this = ip;
}

Expand Down
18 changes: 9 additions & 9 deletions src/netbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ class CNetAddr
public:
CNetAddr();
CNetAddr(const struct in_addr& ipv4Addr);
explicit CNetAddr(const char *pszIp, bool fAllowLookup = false);
explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false);
explicit CNetAddr(const char *pszIp);
explicit CNetAddr(const std::string &strIp);
void Init();
void SetIP(const CNetAddr& ip);
bool SetSpecial(const std::string &strName); // for Tor addresses
Expand Down Expand Up @@ -141,10 +141,10 @@ class CService : public CNetAddr
CService(const CNetAddr& ip, unsigned short port);
CService(const struct in_addr& ipv4Addr, unsigned short port);
CService(const struct sockaddr_in& addr);
explicit CService(const char *pszIpPort, int portDefault, bool fAllowLookup = false);
explicit CService(const char *pszIpPort, bool fAllowLookup = false);
explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false);
explicit CService(const std::string& strIpPort, bool fAllowLookup = false);
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 SetPort(unsigned short portIn);
unsigned short GetPort() const;
Expand Down Expand Up @@ -180,9 +180,9 @@ bool GetProxy(enum Network net, proxyType& proxyInfoOut);
bool IsProxy(const CNetAddr& addr);
bool SetNameProxy(const CService& addrProxy);
bool HaveNameProxy();
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions = 0, bool fAllowLookup = true);
bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true);
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0);
bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, 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 LookupNumeric(const char *pszName, CService& addr, int portDefault = 0);
bool LookupSubNet(const char *pszName, CSubNet& subnet);
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout = nConnectTimeout);
Expand Down

0 comments on commit cd25ff2

Please sign in to comment.