diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-18 18:35:25 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2006-12-18 18:35:25 +0000 |
commit | 1c52b90e826ca3fdef6d55fccd7abbc4fdd90574 (patch) | |
tree | 371a98680608cc7442a331f632f312ba0bd4f84c | |
parent | 52d979a28200288ed38d8292f0727a804df76a9b (diff) |
Refactor dns.cpp to use an array of request types rather than map, for O(1) lookup time
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@6042 e03df62e-2008-0410-955e-edbf42e46eb7
-rw-r--r-- | include/dns.h | 19 | ||||
-rw-r--r-- | src/dns.cpp | 47 |
2 files changed, 24 insertions, 42 deletions
diff --git a/include/dns.h b/include/dns.h index 4087e2476..038323a43 100644 --- a/include/dns.h +++ b/include/dns.h @@ -87,16 +87,6 @@ class DNSHeader; class ResourceRecord; /** - * A set of requests keyed by request id - */ -typedef std::map<int,DNSRequest*> requestlist; - -/** - * An iterator into a set of requests - */ -typedef requestlist::iterator requestlist_iter; - -/** * Query and resource record types */ enum QueryType @@ -255,11 +245,6 @@ class DNS : public EventHandler static const int MAX_REQUEST_ID = 0xFFFF; /** - * Requests that are currently 'in flight' - */ - requestlist requests; - - /** * Server address being used currently */ insp_inaddr myserver; @@ -288,6 +273,10 @@ class DNS : public EventHandler */ Resolver* Classes[MAX_REQUEST_ID]; /** + * Requests that are currently 'in flight' + */ + DNSRequest* requests[MAX_REQUEST_ID]; + /** * The port number DNS requests are made on, * and replies have as a source-port number. */ diff --git a/src/dns.cpp b/src/dns.cpp index 54032bf2e..756c2d5dc 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -95,7 +95,7 @@ class DNSRequest insp_inaddr myserver; /* DNS server address*/ DNS* dnsobj; /* DNS caller (where we get our FD from) */ - DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id, requestlist &requests); + DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id); ~DNSRequest(); DNSInfo ResultIsReady(DNSHeader &h, int length); int SendRequests(const DNSHeader *header, const int length, QueryType qt); @@ -106,30 +106,26 @@ class RequestTimeout : public InspTimer InspIRCd* ServerInstance; DNSRequest* watch; int watchid; - requestlist &rl; public: - RequestTimeout(unsigned long n, InspIRCd* SI, DNSRequest* watching, int id, requestlist &requests) : InspTimer(n, time(NULL)), ServerInstance(SI), watch(watching), watchid(id), rl(requests) + RequestTimeout(unsigned long n, InspIRCd* SI, DNSRequest* watching, int id) : InspTimer(n, time(NULL)), ServerInstance(SI), watch(watching), watchid(id) { ServerInstance->Log(DEBUG, "New DNS timeout set on %08x", watching); } void Tick(time_t TIME) { - if (rl.find(watchid) != rl.end()) + if (ServerInstance->Res->requests[watchid] == watch) { /* Still exists, whack it */ - if (rl.find(watchid)->second == watch) + if (ServerInstance->Res->Classes[watchid]) { - if (ServerInstance->Res->Classes[watchid]) - { - ServerInstance->Res->Classes[watchid]->OnError(RESOLVER_TIMEOUT, "Request timed out"); - delete ServerInstance->Res->Classes[watchid]; - ServerInstance->Res->Classes[watchid] = NULL; - } - rl.erase(rl.find(watchid)); - delete watch; - ServerInstance->Log(DEBUG, "DNS timeout on %08x squished pointer", watch); + ServerInstance->Res->Classes[watchid]->OnError(RESOLVER_TIMEOUT, "Request timed out"); + delete ServerInstance->Res->Classes[watchid]; + ServerInstance->Res->Classes[watchid] = NULL; } + ServerInstance->Res->requests[watchid] = NULL; + delete watch; + ServerInstance->Log(DEBUG, "DNS timeout on %08x squished pointer", watch); return; } ServerInstance->Log(DEBUG, "DNS timeout on %08x: result already received!", watch); @@ -137,12 +133,12 @@ class RequestTimeout : public InspTimer }; /* Allocate the processing buffer */ -DNSRequest::DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id, requestlist &requests) : dnsobj(dns) +DNSRequest::DNSRequest(InspIRCd* Instance, DNS* dns, insp_inaddr server, int id) : dnsobj(dns) { res = new unsigned char[512]; *res = 0; memcpy(&myserver, &server, sizeof(insp_inaddr)); - RequestTimeout* RT = new RequestTimeout(Instance->Config->dns_timeout ? Instance->Config->dns_timeout : 5, Instance, this, id, requests); + RequestTimeout* RT = new RequestTimeout(Instance->Config->dns_timeout ? Instance->Config->dns_timeout : 5, Instance, this, id); Instance->Timers->AddTimer(RT); /* The timer manager frees this */ } @@ -226,19 +222,15 @@ DNSRequest* DNS::AddQuery(DNSHeader *header, int &id) /* Is the DNS connection down? */ if (this->GetFd() == -1) return NULL; - - /* Are there already the max number of requests on the go? */ - if (requests.size() == DNS::MAX_REQUEST_ID + 1) - return NULL; /* Create an id */ id = this->PRNG() & DNS::MAX_REQUEST_ID; /* If this id is already 'in flight', pick another. */ - while (requests.find(id) != requests.end()) + while (requests[id]) id = this->PRNG() & DNS::MAX_REQUEST_ID; - DNSRequest* req = new DNSRequest(ServerInstance, this, this->myserver, id, requests); + DNSRequest* req = new DNSRequest(ServerInstance, this, this->myserver, id); header->id[0] = req->id[0] = id >> 8; header->id[1] = req->id[1] = id & 0xFF; @@ -268,6 +260,9 @@ DNS::DNS(InspIRCd* Instance) : ServerInstance(Instance) /* Clear the Resolver class table */ memset(Classes,0,sizeof(Classes)); + /* Clear the requests class table */ + memset(requests,0,sizeof(requests)); + /* Set the id of the next request to 0 */ currid = 0; @@ -617,8 +612,7 @@ DNSResult DNS::GetResult() unsigned long this_id = header.id[1] + (header.id[0] << 8); /* Do we have a pending request matching this id? */ - requestlist_iter n_iter = requests.find(this_id); - if (n_iter == requests.end()) + if (!requests[this_id]) { /* Somehow we got a DNS response for a request we never made... */ ServerInstance->Log(DEBUG,"DNS: got a response for a query we didnt send with fd=%d queryid=%d",this->GetFd(),this_id); @@ -627,8 +621,8 @@ DNSResult DNS::GetResult() else { /* Remove the query from the list of pending queries */ - req = (DNSRequest*)n_iter->second; - requests.erase(n_iter); + req = requests[this_id]; + requests[this_id] = NULL; } /* Inform the DNSRequest class that it has a result to be read. @@ -954,7 +948,6 @@ Module* Resolver::GetCreator() /** Process a socket read event */ void DNS::HandleEvent(EventType et, int errornum) { - ServerInstance->Log(DEBUG,"Marshall reads: %d",this->GetFd()); /* Fetch the id and result of the next available packet */ DNSResult res = this->GetResult(); /* Is there a usable request id? */ |