X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=src%2Fdns.cpp;h=8526d5be22bc4735db1a51c1220b49f77700403b;hb=4498f1abd163b140efcbbd9e75173665c9b1c29f;hp=2e016f58e632eb204b77c1f7cbdbe4c6401d010a;hpb=de25d946733f774e3a5b53a58438a9c92af0acbe;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/src/dns.cpp b/src/dns.cpp index 2e016f58e..8526d5be2 100644 --- a/src/dns.cpp +++ b/src/dns.cpp @@ -2,7 +2,7 @@ * | Inspire Internet Relay Chat Daemon | * +------------------------------------+ * - * InspIRCd: (C) 2002-2009 InspIRCd Development Team + * InspIRCd: (C) 2002-2010 InspIRCd Development Team * See: http://wiki.inspircd.org/Credits * * This program is free but copyrighted software; see @@ -147,6 +147,17 @@ class RequestTimeout : public Timer } }; +CachedQuery::CachedQuery(const std::string &res, unsigned int ttl) : data(res) +{ + expires = ServerInstance->Time() + ttl; +} + +int CachedQuery::CalcTTLRemaining() +{ + int n = expires - ServerInstance->Time(); + return (n < 0 ? 0 : n); +} + /* Allocate the processing buffer */ DNSRequest::DNSRequest(DNS* dns, int rid, const std::string &original) : dnsobj(dns) { @@ -216,14 +227,6 @@ int DNSRequest::SendRequests(const DNSHeader *header, const int length, QueryTyp DNS::EmptyHeader(payload,header,length); - if (this->dnsobj->myserver.sa.sa_family == AF_INET6) - { - dnsobj->myserver.in6.sin6_port = htons(DNS::QUERY_PORT); - } - else - { - dnsobj->myserver.in4.sin_port = htons(DNS::QUERY_PORT); - } if (ServerInstance->SE->SendTo(dnsobj, payload, length + 12, 0, &(dnsobj->myserver.sa), sa_size(dnsobj->myserver)) != length+12) return -1; @@ -239,11 +242,9 @@ DNSRequest* DNS::AddQuery(DNSHeader *header, int &id, const char* original) return NULL; /* Create an id */ - id = this->PRNG() & DNS::MAX_REQUEST_ID; - - /* If this id is already 'in flight', pick another. */ - while (requests[id]) - id = this->PRNG() & DNS::MAX_REQUEST_ID; + do { + id = ServerInstance->GenRandomInt(DNS::MAX_REQUEST_ID); + } while (requests[id]); DNSRequest* req = new DNSRequest(this, id, original); @@ -311,16 +312,17 @@ void DNS::Rehash() this->cache = new dnscache(); } - irc::sockets::aptosa(ServerInstance->Config->DNSServer, 0, &myserver); + irc::sockets::aptosa(ServerInstance->Config->DNSServer, DNS::QUERY_PORT, myserver); /* Initialize mastersocket */ - int s = irc::sockets::OpenTCPSocket(ServerInstance->Config->DNSServer, SOCK_DGRAM); + int s = socket(myserver.sa.sa_family, SOCK_DGRAM, 0); this->SetFd(s); - ServerInstance->SE->NonBlocking(this->GetFd()); /* Have we got a socket and is it nonblocking? */ if (this->GetFd() != -1) { + ServerInstance->SE->SetReuse(s); + ServerInstance->SE->NonBlocking(s); /* Bind the port - port 0 INADDR_ANY */ if (!ServerInstance->BindSocket(this->GetFd(), portpass, "", false)) { @@ -565,10 +567,7 @@ DNSResult DNS::GetResult() * * -- Thanks jilles for pointing this one out. */ - irc::sockets::sockaddrs expect_src; - irc::sockets::aptosa(ServerInstance->Config->DNSServer, DNS::QUERY_PORT, &expect_src); - - if (memcmp(&from, &expect_src, sizeof(irc::sockets::sockaddrs))) + if (memcmp(&from, &myserver, sizeof(irc::sockets::sockaddrs))) { return DNSResult(-1,"",0,""); } @@ -1040,16 +1039,3 @@ void DNS::CleanResolvers(Module* module) } } } - -/** Generate pseudo-random number */ -unsigned long DNS::PRNG() -{ - unsigned long val = 0; - timeval n; - serverstats* s = ServerInstance->stats; - gettimeofday(&n,NULL); - val = (n.tv_usec ^ (getpid() ^ geteuid()) ^ ((this->currid++)) ^ s->statsAccept) + n.tv_sec; - val = val + (s->statsCollisions ^ s->statsDnsGood) - s->statsDnsBad; - val += (s->statsConnects ^ (unsigned long)s->statsSent ^ (unsigned long)s->statsRecv) - ServerInstance->ports.size(); - return val; -}