Don't split ipv4/ipv6 mappings into separate pointers

This commit is contained in:
Tim Wojtulewicz 2022-02-07 10:14:09 -07:00
parent c7284686a4
commit fb59239f41
2 changed files with 17 additions and 76 deletions

View file

@ -923,57 +923,27 @@ void DNS_Mgr::AddResult(DNS_Request* dr, struct hostent* h, uint32_t ttl, bool m
HostMap::iterator it = host_mappings.find(dr->Host()); HostMap::iterator it = host_mappings.find(dr->Host());
if ( it == host_mappings.end() ) if ( it == host_mappings.end() )
{ {
std::pair<HostMap::iterator, bool> result; auto result = host_mappings.emplace(dr->Host(), new_mapping);
if ( new_mapping->Type() == AF_INET )
result = host_mappings.emplace(dr->Host(), std::pair{new_mapping, nullptr});
else
result = host_mappings.emplace(dr->Host(), std::pair{nullptr, new_mapping});
it = result.first; it = result.first;
} }
else else
{ prev_mapping = it->second;
if ( new_mapping->Type() == AF_INET )
prev_mapping = it->second.first;
else
prev_mapping = it->second.second;
}
if ( prev_mapping && new_mapping->Type() != prev_mapping->Type() ) if ( prev_mapping && prev_mapping->Valid() )
{
if ( new_mapping->Type() == AF_INET )
{
prev_mapping = it->second.second;
it->second.first = new_mapping;
}
else
{
prev_mapping = it->second.first;
it->second.second = new_mapping;
}
}
else if ( prev_mapping && prev_mapping->Valid() )
{ {
if ( new_mapping->Valid() ) if ( new_mapping->Valid() )
{ {
if ( merge ) if ( merge )
new_mapping->Merge(prev_mapping); new_mapping->Merge(prev_mapping);
it->second = new_mapping;
keep_prev = false; keep_prev = false;
if ( new_mapping->Type() == AF_INET )
it->second.first = new_mapping;
else
it->second.second = new_mapping;
} }
} }
else else
{ {
it->second = new_mapping;
keep_prev = false; keep_prev = false;
if ( new_mapping->Type() == AF_INET )
it->second.first = new_mapping;
else
it->second.second = new_mapping;
} }
} }
} }
@ -1100,21 +1070,9 @@ void DNS_Mgr::LoadCache(const std::string& path)
for ( ; ! m->NoMapping() && ! m->InitFailed(); m = new DNS_Mapping(f) ) for ( ; ! m->NoMapping() && ! m->InitFailed(); m = new DNS_Mapping(f) )
{ {
if ( m->ReqHost() ) if ( m->ReqHost() )
{ host_mappings.insert_or_assign(m->ReqHost(), m);
if ( host_mappings.find(m->ReqHost()) == host_mappings.end() )
{
host_mappings[m->ReqHost()].first = 0;
host_mappings[m->ReqHost()].second = 0;
}
if ( m->Type() == AF_INET )
host_mappings[m->ReqHost()].first = m;
else else
host_mappings[m->ReqHost()].second = m; addr_mappings.insert_or_assign(m->ReqAddr(), m);
}
else
{
addr_mappings[m->ReqAddr()] = m;
}
} }
if ( ! m->NoMapping() ) if ( ! m->NoMapping() )
@ -1156,11 +1114,8 @@ void DNS_Mgr::Save(FILE* f, const HostMap& m)
{ {
for ( HostMap::const_iterator it = m.begin(); it != m.end(); ++it ) for ( HostMap::const_iterator it = m.begin(); it != m.end(); ++it )
{ {
if ( it->second.first ) if ( it->second )
it->second.first->Save(f); it->second->Save(f);
if ( it->second.second )
it->second.second->Save(f);
} }
} }
@ -1171,36 +1126,25 @@ TableValPtr DNS_Mgr::LookupNameInCache(const std::string& name, bool cleanup_exp
if ( it == host_mappings.end() ) if ( it == host_mappings.end() )
return nullptr; return nullptr;
DNS_Mapping* d4 = it->second.first; DNS_Mapping* d = it->second;
DNS_Mapping* d6 = it->second.second;
if ( (! d4 || d4->names.empty()) && (! d6 || d6->names.empty()) ) if ( ! d || d->names.empty() )
return nullptr; return nullptr;
if ( cleanup_expired && ((d4 && d4->Expired()) || (d6 && d6->Expired())) ) if ( cleanup_expired && (d && d->Expired()) )
{ {
host_mappings.erase(it); host_mappings.erase(it);
delete d4; delete d;
delete d6;
return nullptr; return nullptr;
} }
if ( check_failed && ((d4 && d4->Failed()) || (d6 && d6->Failed())) ) if ( check_failed && (d && d->Failed()) )
{ {
reporter->Warning("Can't resolve host: %s", name.c_str()); reporter->Warning("Can't resolve host: %s", name.c_str());
return empty_addr_set(); return empty_addr_set();
} }
auto tv4 = d4->AddrsSet(); return d->AddrsSet();
if ( d6 )
{
auto tv6 = d6->AddrsSet();
tv4->AddTo(tv6.get(), false);
return tv6;
}
return tv4;
} }
StringValPtr DNS_Mgr::LookupAddrInCache(const IPAddr& addr, bool cleanup_expired, bool check_failed) StringValPtr DNS_Mgr::LookupAddrInCache(const IPAddr& addr, bool cleanup_expired, bool check_failed)
@ -1366,10 +1310,7 @@ void DNS_Mgr::Flush()
Resolve(); Resolve();
for ( HostMap::iterator it = host_mappings.begin(); it != host_mappings.end(); ++it ) for ( HostMap::iterator it = host_mappings.begin(); it != host_mappings.end(); ++it )
{ delete it->second;
delete it->second.first;
delete it->second.second;
}
for ( AddrMap::iterator it2 = addr_mappings.begin(); it2 != addr_mappings.end(); ++it2 ) for ( AddrMap::iterator it2 = addr_mappings.begin(); it2 != addr_mappings.end(); ++it2 )
delete it2->second; delete it2->second;

View file

@ -265,7 +265,7 @@ protected:
void CompareMappings(DNS_Mapping* prev_dm, DNS_Mapping* new_dm); void CompareMappings(DNS_Mapping* prev_dm, DNS_Mapping* new_dm);
ListValPtr AddrListDelta(ListVal* al1, ListVal* al2); ListValPtr AddrListDelta(ListVal* al1, ListVal* al2);
using HostMap = std::map<std::string, std::pair<DNS_Mapping*, DNS_Mapping*>>; using HostMap = std::map<std::string, DNS_Mapping*>;
using AddrMap = std::map<IPAddr, DNS_Mapping*>; using AddrMap = std::map<IPAddr, DNS_Mapping*>;
using TextMap = std::map<std::string, DNS_Mapping*>; using TextMap = std::map<std::string, DNS_Mapping*>;
void LoadCache(const std::string& path); void LoadCache(const std::string& path);