Browse Source

Merge #7868: net: Split DNS resolving functionality out of net structures

d39f5b4 net: disable resolving from storage structures (Cory Fields)
3675699 net: resolve outside of storage structures (Cory Fields)
a98cd1f net: manually resolve dns seed sources (Cory Fields)
e9fc71e net: require lookup functions to specify all arguments (Cory Fields)
0.13
Pieter Wuille 9 years ago
parent
commit
7daa3adb24
No known key found for this signature in database
GPG Key ID: DBA1A67379A1A931
  1. 7
      src/init.cpp
  2. 14
      src/net.cpp
  3. 32
      src/netbase.cpp
  4. 20
      src/netbase.h
  5. 2
      src/torcontrol.cpp

7
src/init.cpp

@ -1138,10 +1138,11 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
if (mapArgs.count("-externalip")) { if (mapArgs.count("-externalip")) {
BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-externalip"]) { BOOST_FOREACH(const std::string& strAddr, mapMultiArgs["-externalip"]) {
CService addrLocal(strAddr, GetListenPort(), fNameLookup); CService addrLocal;
if (!addrLocal.IsValid()) if (Lookup(strAddr.c_str(), addrLocal, GetListenPort(), fNameLookup) && addrLocal.IsValid())
AddLocal(addrLocal, LOCAL_MANUAL);
else
return InitError(ResolveErrMsg("externalip", strAddr)); return InitError(ResolveErrMsg("externalip", strAddr));
AddLocal(CService(strAddr, GetListenPort(), fNameLookup), LOCAL_MANUAL);
} }
} }

14
src/net.cpp

@ -1443,7 +1443,7 @@ void ThreadDNSAddressSeed()
} else { } else {
vector<CNetAddr> vIPs; vector<CNetAddr> vIPs;
vector<CAddress> vAdd; vector<CAddress> vAdd;
if (LookupHost(seed.host.c_str(), vIPs)) if (LookupHost(seed.host.c_str(), vIPs, 0, true))
{ {
BOOST_FOREACH(const CNetAddr& ip, vIPs) BOOST_FOREACH(const CNetAddr& ip, vIPs)
{ {
@ -1454,7 +1454,15 @@ void ThreadDNSAddressSeed()
found++; found++;
} }
} }
addrman.Add(vAdd, CNetAddr(seed.name, 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.name.c_str(), seedSource, 0, true);
addrman.Add(vAdd, seedSource);
}
} }
} }
@ -1884,7 +1892,7 @@ void static Discover(boost::thread_group& threadGroup)
if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR) if (gethostname(pszHostName, sizeof(pszHostName)) != SOCKET_ERROR)
{ {
vector<CNetAddr> vaddr; vector<CNetAddr> vaddr;
if (LookupHost(pszHostName, vaddr)) if (LookupHost(pszHostName, vaddr, 0, true))
{ {
BOOST_FOREACH (const CNetAddr &addr, vaddr) BOOST_FOREACH (const CNetAddr &addr, vaddr)
{ {

32
src/netbase.cpp

@ -614,11 +614,13 @@ bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest
proxyType nameProxy; proxyType nameProxy;
GetNameProxy(nameProxy); GetNameProxy(nameProxy);
CService addrResolved(CNetAddr(strDest, fNameLookup && !HaveNameProxy()), port); CService addrResolved;
if (Lookup(strDest.c_str(), addrResolved, port, fNameLookup && !HaveNameProxy())) {
if (addrResolved.IsValid()) { if (addrResolved.IsValid()) {
addr = addrResolved; addr = addrResolved;
return ConnectSocket(addr, hSocketRet, nTimeout); return ConnectSocket(addr, hSocketRet, nTimeout);
} }
}
addr = CService("0.0.0.0:0"); addr = CService("0.0.0.0:0");
@ -686,19 +688,19 @@ CNetAddr::CNetAddr(const struct in6_addr& ipv6Addr, const uint32_t scope)
scopeId = scope; scopeId = scope;
} }
CNetAddr::CNetAddr(const char *pszIp, bool fAllowLookup) CNetAddr::CNetAddr(const char *pszIp)
{ {
Init(); Init();
std::vector<CNetAddr> vIP; std::vector<CNetAddr> vIP;
if (LookupHost(pszIp, vIP, 1, fAllowLookup)) if (LookupHost(pszIp, vIP, 1, false))
*this = vIP[0]; *this = vIP[0];
} }
CNetAddr::CNetAddr(const std::string &strIp, bool fAllowLookup) CNetAddr::CNetAddr(const std::string &strIp)
{ {
Init(); Init();
std::vector<CNetAddr> vIP; std::vector<CNetAddr> vIP;
if (LookupHost(strIp.c_str(), vIP, 1, fAllowLookup)) if (LookupHost(strIp.c_str(), vIP, 1, false))
*this = vIP[0]; *this = vIP[0];
} }
@ -1121,35 +1123,35 @@ bool CService::SetSockAddr(const struct sockaddr *paddr)
} }
} }
CService::CService(const char *pszIpPort, bool fAllowLookup) CService::CService(const char *pszIpPort)
{ {
Init(); Init();
CService ip; CService ip;
if (Lookup(pszIpPort, ip, 0, fAllowLookup)) if (Lookup(pszIpPort, ip, 0, false))
*this = ip; *this = ip;
} }
CService::CService(const char *pszIpPort, int portDefault, bool fAllowLookup) CService::CService(const char *pszIpPort, int portDefault)
{ {
Init(); Init();
CService ip; CService ip;
if (Lookup(pszIpPort, ip, portDefault, fAllowLookup)) if (Lookup(pszIpPort, ip, portDefault, false))
*this = ip; *this = ip;
} }
CService::CService(const std::string &strIpPort, bool fAllowLookup) CService::CService(const std::string &strIpPort)
{ {
Init(); Init();
CService ip; CService ip;
if (Lookup(strIpPort.c_str(), ip, 0, fAllowLookup)) if (Lookup(strIpPort.c_str(), ip, 0, false))
*this = ip; *this = ip;
} }
CService::CService(const std::string &strIpPort, int portDefault, bool fAllowLookup) CService::CService(const std::string &strIpPort, int portDefault)
{ {
Init(); Init();
CService ip; CService ip;
if (Lookup(strIpPort.c_str(), ip, portDefault, fAllowLookup)) if (Lookup(strIpPort.c_str(), ip, portDefault, false))
*this = ip; *this = ip;
} }
@ -1243,7 +1245,7 @@ CSubNet::CSubNet():
memset(netmask, 0, sizeof(netmask)); memset(netmask, 0, sizeof(netmask));
} }
CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup) CSubNet::CSubNet(const std::string &strSubnet)
{ {
size_t slash = strSubnet.find_last_of('/'); size_t slash = strSubnet.find_last_of('/');
std::vector<CNetAddr> vIP; std::vector<CNetAddr> vIP;
@ -1253,7 +1255,7 @@ CSubNet::CSubNet(const std::string &strSubnet, bool fAllowLookup)
memset(netmask, 255, sizeof(netmask)); memset(netmask, 255, sizeof(netmask));
std::string strAddress = strSubnet.substr(0, slash); std::string strAddress = strSubnet.substr(0, slash);
if (LookupHost(strAddress.c_str(), vIP, 1, fAllowLookup)) if (LookupHost(strAddress.c_str(), vIP, 1, false))
{ {
network = vIP[0]; network = vIP[0];
if (slash != strSubnet.npos) if (slash != strSubnet.npos)

20
src/netbase.h

@ -49,8 +49,8 @@ class CNetAddr
public: public:
CNetAddr(); CNetAddr();
CNetAddr(const struct in_addr& ipv4Addr); CNetAddr(const struct in_addr& ipv4Addr);
explicit CNetAddr(const char *pszIp, bool fAllowLookup = false); explicit CNetAddr(const char *pszIp);
explicit CNetAddr(const std::string &strIp, bool fAllowLookup = false); explicit CNetAddr(const std::string &strIp);
void Init(); void Init();
void SetIP(const CNetAddr& ip); void SetIP(const CNetAddr& ip);
@ -119,7 +119,7 @@ class CSubNet
public: public:
CSubNet(); CSubNet();
explicit CSubNet(const std::string &strSubnet, bool fAllowLookup = false); explicit CSubNet(const std::string &strSubnet);
//constructor for single ip subnet (<ipv4>/32 or <ipv6>/128) //constructor for single ip subnet (<ipv4>/32 or <ipv6>/128)
explicit CSubNet(const CNetAddr &addr); explicit CSubNet(const CNetAddr &addr);
@ -154,10 +154,10 @@ 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, bool fAllowLookup = false); explicit CService(const char *pszIpPort, int portDefault);
explicit CService(const char *pszIpPort, bool fAllowLookup = false); explicit CService(const char *pszIpPort);
explicit CService(const std::string& strIpPort, int portDefault, bool fAllowLookup = false); explicit CService(const std::string& strIpPort, int portDefault);
explicit CService(const std::string& strIpPort, bool fAllowLookup = false); 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;
@ -206,9 +206,9 @@ bool GetProxy(enum Network net, proxyType &proxyInfoOut);
bool IsProxy(const CNetAddr &addr); 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 = 0, bool fAllowLookup = true); bool LookupHost(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup);
bool Lookup(const char *pszName, CService& addr, int portDefault = 0, bool fAllowLookup = true); bool Lookup(const char *pszName, CService& addr, int portDefault, bool fAllowLookup);
bool Lookup(const char *pszName, std::vector<CService>& vAddr, int portDefault = 0, bool fAllowLookup = true, unsigned int nMaxSolutions = 0); 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);
bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0); bool ConnectSocket(const CService &addr, SOCKET& hSocketRet, int nTimeout, bool *outProxyConnectionFailed = 0);
bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0); bool ConnectSocketByName(CService &addr, SOCKET& hSocketRet, const char *pszDest, int portDefault, int nTimeout, bool *outProxyConnectionFailed = 0);

2
src/torcontrol.cpp

@ -438,7 +438,7 @@ void TorController::add_onion_cb(TorControlConnection& conn, const TorControlRep
private_key = i->second; private_key = i->second;
} }
service = CService(service_id+".onion", GetListenPort(), false); 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());

Loading…
Cancel
Save