#include "configreader.h"
#include "socket.h"
-using namespace std;
using irc::sockets::insp_sockaddr;
using irc::sockets::insp_inaddr;
using irc::sockets::insp_ntoa;
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);
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);
};
/* 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 */
}
/* 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;
return req;
}
-/** Initialise the DNS UDP socket so that we can send requests */
-DNS::DNS(InspIRCd* Instance) : ServerInstance(Instance)
+void DNS::Rehash()
{
- ServerInstance->Log(DEBUG,"DNS::DNS: Instance = %08x",Instance);
-
insp_inaddr addr;
-
- /* Clear the Resolver class table */
- memset(Classes,0,sizeof(Classes));
-
- /* Set the id of the next request to 0
- */
- currid = 0;
-
- /* By default we're not munging ip's
- */
ip6munge = false;
- /* Clear the namesever address */
- memset(&myserver,0,sizeof(insp_inaddr));
+ if (this->GetFd() > -1)
+ {
+ if (ServerInstance && ServerInstance->SE)
+ ServerInstance->SE->DelFd(this);
+ shutdown(this->GetFd(), 2);
+ close(this->GetFd());
+ this->SetFd(-1);
+ }
- /* Convert the nameserver address into an insp_inaddr */
if (insp_aton(ServerInstance->Config->DNSServer,&addr) > 0)
{
memcpy(&myserver,&addr,sizeof(insp_inaddr));
if ((strstr(ServerInstance->Config->DNSServer,"::ffff:") == (char*)&ServerInstance->Config->DNSServer) || (strstr(ServerInstance->Config->DNSServer,"::FFFF:") == (char*)&ServerInstance->Config->DNSServer))
{
- /* These dont come back looking like they did when they went in.
- * We're forced to turn some checks off.
- * If anyone knows how to fix this, let me know. --Brain
- */
ServerInstance->Log(DEFAULT,"WARNING: Using IPv4 addresses over IPv6 forces some DNS checks to be disabled.");
ServerInstance->Log(DEFAULT," This should not cause a problem, however it is recommended you migrate");
ServerInstance->Log(DEFAULT," to a true IPv6 environment.");
{
ServerInstance->Log(DEBUG,"I cant socket() this socket! (%s)",strerror(errno));
}
+
/* Have we got a socket and is it nonblocking? */
if (this->GetFd() != -1)
{
}
}
+/** Initialise the DNS UDP socket so that we can send requests */
+DNS::DNS(InspIRCd* Instance) : ServerInstance(Instance)
+{
+ ServerInstance->Log(DEBUG,"DNS::DNS: Instance = %08x",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;
+
+ this->SetFd(-1);
+
+ this->Rehash();
+}
+
/** Build a payload to be placed after the header, based upon input data, a resource type, a class and a pointer to a buffer */
int DNS::MakePayload(const char * const name, const QueryType rr, const unsigned short rr_class, unsigned char * const payload)
{
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);
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.
/** 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? */