From cd25ff271a0474177f5b181f7c6d9af3e5911385 Mon Sep 17 00:00:00 2001 From: pythonix <9782029+Pythonix@users.noreply.github.com> Date: Tue, 2 Aug 2022 14:30:04 +0200 Subject: [PATCH] Split DNS resolving functionality out of net structures --- src/init.cpp | 7 ++++--- src/net.cpp | 16 ++++++++++++---- src/netbase.cpp | 34 ++++++++++++++++++---------------- src/netbase.h | 18 +++++++++--------- 4 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 92d2c7a7e2..a575d3cd6d 100755 --- a/src/init.cpp +++ b/src/init.cpp @@ -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); } } diff --git a/src/net.cpp b/src/net.cpp index ac6393ccbd..de8c34db3a 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -1311,11 +1311,11 @@ void ThreadDNSAddressSeed2(void* parg) if (HaveNameProxy()) { AddOneShot(seed[1]); } else { - vector vaddr; + vector vIPs; vector 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())); @@ -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); + } } } } diff --git a/src/netbase.cpp b/src/netbase.cpp index c334aa6a2c..b290a1e11f 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -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"); @@ -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 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 vIP; - if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup)) + if (LookupHost(strIp.c_str(), vIP, 1, false)) *this = vIP[0]; } @@ -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; } diff --git a/src/netbase.h b/src/netbase.h index e667a52277..0b1bf6539a 100644 --- a/src/netbase.h +++ b/src/netbase.h @@ -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 @@ -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; @@ -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& 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& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); +bool LookupHost(const char *pszName, std::vector& vIP, unsigned int nMaxSolutions, bool fAllowLookup); +bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup); +bool Lookup(const char *pszName, std::vector& 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);