DNS_Mgr changes to match the new IOSource API and loop architecture

This commit is contained in:
Tim Wojtulewicz 2019-11-26 12:58:41 -07:00
parent 8b9160fb7e
commit c4d9566294
3 changed files with 27 additions and 51 deletions

View file

@ -392,7 +392,6 @@ DNS_Mgr::DNS_Mgr(DNS_MgrMode arg_mode)
successful = 0; successful = 0;
failed = 0; failed = 0;
nb_dns = nullptr; nb_dns = nullptr;
next_timestamp = -1.0;
} }
DNS_Mgr::~DNS_Mgr() DNS_Mgr::~DNS_Mgr()
@ -404,7 +403,7 @@ DNS_Mgr::~DNS_Mgr()
delete [] dir; delete [] dir;
} }
void DNS_Mgr::Init() void DNS_Mgr::InitSource()
{ {
if ( did_init ) if ( did_init )
return; return;
@ -440,7 +439,9 @@ void DNS_Mgr::Init()
nb_dns = nb_dns_init2(err, (struct sockaddr*)&ss); nb_dns = nb_dns_init2(err, (struct sockaddr*)&ss);
} }
if ( ! nb_dns ) if ( nb_dns )
iosource_mgr->RegisterFd(nb_dns_fd(nb_dns), this);
else
reporter->Warning("problem initializing NB-DNS: %s", err); reporter->Warning("problem initializing NB-DNS: %s", err);
did_init = true; did_init = true;
@ -460,11 +461,6 @@ void DNS_Mgr::InitPostScript()
// Registering will call Init() // Registering will call Init()
iosource_mgr->Register(this, true); iosource_mgr->Register(this, true);
// We never set idle to false, having the main loop only calling us from
// time to time. If we're issuing more DNS requests than we can handle
// in this way, we are having problems anyway ...
SetIdle(true);
const char* cache_dir = dir ? dir : "."; const char* cache_dir = dir ? dir : ".";
cache_name = new char[strlen(cache_dir) + 64]; cache_name = new char[strlen(cache_dir) + 64];
sprintf(cache_name, "%s/%s", cache_dir, ".zeek-dns-cache"); sprintf(cache_name, "%s/%s", cache_dir, ".zeek-dns-cache");
@ -503,7 +499,7 @@ TableVal* DNS_Mgr::LookupHost(const char* name)
if ( mode == DNS_FAKE ) if ( mode == DNS_FAKE )
return fake_name_lookup_result(name); return fake_name_lookup_result(name);
Init(); InitSource();
if ( ! nb_dns ) if ( ! nb_dns )
return empty_addr_set(); return empty_addr_set();
@ -558,7 +554,7 @@ TableVal* DNS_Mgr::LookupHost(const char* name)
Val* DNS_Mgr::LookupAddr(const IPAddr& addr) Val* DNS_Mgr::LookupAddr(const IPAddr& addr)
{ {
Init(); InitSource();
if ( mode != DNS_PRIME ) if ( mode != DNS_PRIME )
{ {
@ -1078,7 +1074,7 @@ static void resolve_lookup_cb(DNS_Mgr::LookupCallback* callback,
void DNS_Mgr::AsyncLookupAddr(const IPAddr& host, LookupCallback* callback) void DNS_Mgr::AsyncLookupAddr(const IPAddr& host, LookupCallback* callback)
{ {
Init(); InitSource();
if ( mode == DNS_FAKE ) if ( mode == DNS_FAKE )
{ {
@ -1116,7 +1112,7 @@ void DNS_Mgr::AsyncLookupAddr(const IPAddr& host, LookupCallback* callback)
void DNS_Mgr::AsyncLookupName(const string& name, LookupCallback* callback) void DNS_Mgr::AsyncLookupName(const string& name, LookupCallback* callback)
{ {
Init(); InitSource();
if ( mode == DNS_FAKE ) if ( mode == DNS_FAKE )
{ {
@ -1154,7 +1150,7 @@ void DNS_Mgr::AsyncLookupName(const string& name, LookupCallback* callback)
void DNS_Mgr::AsyncLookupNameText(const string& name, LookupCallback* callback) void DNS_Mgr::AsyncLookupNameText(const string& name, LookupCallback* callback)
{ {
Init(); InitSource();
if ( mode == DNS_FAKE ) if ( mode == DNS_FAKE )
{ {
@ -1242,30 +1238,6 @@ void DNS_Mgr::IssueAsyncRequests()
} }
} }
void DNS_Mgr::GetFds(iosource::FD_Set* read, iosource::FD_Set* write,
iosource::FD_Set* except)
{
if ( ! nb_dns )
return;
read->Insert(nb_dns_fd(nb_dns));
}
double DNS_Mgr::NextTimestamp(double* network_time)
{
if ( asyncs_timeouts.empty() )
// No pending requests.
return -1.0;
if ( next_timestamp < 0 )
// Store the timestamp to help prevent starvation by some other
// IOSource always trying to use the same timestamp
// (assuming network_time does actually increase).
next_timestamp = timer_mgr->Time();
return next_timestamp;
}
void DNS_Mgr::CheckAsyncAddrRequest(const IPAddr& addr, bool timeout) void DNS_Mgr::CheckAsyncAddrRequest(const IPAddr& addr, bool timeout)
{ {
// Note that this code is a mirror of that for CheckAsyncHostRequest. // Note that this code is a mirror of that for CheckAsyncHostRequest.
@ -1369,7 +1341,7 @@ void DNS_Mgr::CheckAsyncHostRequest(const char* host, bool timeout)
void DNS_Mgr::Flush() void DNS_Mgr::Flush()
{ {
DoProcess(); Process();
HostMap::iterator it; HostMap::iterator it;
for ( it = host_mappings.begin(); it != host_mappings.end(); ++it ) for ( it = host_mappings.begin(); it != host_mappings.end(); ++it )
@ -1389,13 +1361,15 @@ void DNS_Mgr::Flush()
text_mappings.clear(); text_mappings.clear();
} }
void DNS_Mgr::Process() double DNS_Mgr::GetNextTimeout()
{ {
DoProcess(); if ( asyncs_timeouts.empty() )
next_timestamp = -1.0; return -1;
return network_time + DNS_TIMEOUT;
} }
void DNS_Mgr::DoProcess() void DNS_Mgr::Process()
{ {
if ( ! nb_dns ) if ( ! nb_dns )
return; return;
@ -1513,3 +1487,8 @@ void DNS_Mgr::GetStats(Stats* stats)
stats->cached_texts = text_mappings.size(); stats->cached_texts = text_mappings.size();
} }
void DNS_Mgr::Terminate()
{
if ( nb_dns )
iosource_mgr->UnregisterFd(nb_dns_fd(nb_dns));
}

View file

@ -91,6 +91,8 @@ public:
void GetStats(Stats* stats); void GetStats(Stats* stats);
void Terminate();
protected: protected:
friend class LookupCallback; friend class LookupCallback;
friend class DNS_Mgr_Request; friend class DNS_Mgr_Request;
@ -127,16 +129,11 @@ protected:
void CheckAsyncHostRequest(const char* host, bool timeout); void CheckAsyncHostRequest(const char* host, bool timeout);
void CheckAsyncTextRequest(const char* host, bool timeout); void CheckAsyncTextRequest(const char* host, bool timeout);
// Process outstanding requests.
void DoProcess();
// IOSource interface. // IOSource interface.
void GetFds(iosource::FD_Set* read, iosource::FD_Set* write,
iosource::FD_Set* except) override;
double NextTimestamp(double* network_time) override;
void Process() override; void Process() override;
void Init() override; void InitSource() override;
const char* Tag() override { return "DNS_Mgr"; } const char* Tag() override { return "DNS_Mgr"; }
double GetNextTimeout() override;
DNS_MgrMode mode; DNS_MgrMode mode;
@ -241,7 +238,6 @@ protected:
unsigned long num_requests; unsigned long num_requests;
unsigned long successful; unsigned long successful;
unsigned long failed; unsigned long failed;
double next_timestamp;
}; };
extern DNS_Mgr* dns_mgr; extern DNS_Mgr* dns_mgr;

View file

@ -290,6 +290,7 @@ void terminate_bro()
input_mgr->Terminate(); input_mgr->Terminate();
thread_mgr->Terminate(); thread_mgr->Terminate();
broker_mgr->Terminate(); broker_mgr->Terminate();
dns_mgr->Terminate();
mgr.Drain(); mgr.Drain();