]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
core_dns Make question a member of request, move common FindAnswerOfType to be a...
authorAdam <Adam@anope.org>
Tue, 16 Aug 2016 16:43:40 +0000 (12:43 -0400)
committerAttila Molnar <attilamolnar@hush.com>
Thu, 25 Aug 2016 15:12:48 +0000 (17:12 +0200)
include/modules/dns.h
src/coremods/core_dns.cpp
src/coremods/core_hostname_lookup.cpp
src/modules/m_spanningtree/resolvers.cpp

index 1ba54cc616375fcfed8d4d26b75b25607b617d77..5f28367618c92acf23d08a170aafa60e30980010 100644 (file)
@@ -117,6 +117,18 @@ namespace DNS
 
                Query() : error(ERROR_NONE), cached(false) { }
                Query(const Question& q) : question(q), error(ERROR_NONE), cached(false) { }
+
+               const ResourceRecord* FindAnswerOfType(QueryType qtype) const
+               {
+                       for (std::vector<DNS::ResourceRecord>::const_iterator i = answers.begin(); i != answers.end(); ++i)
+                       {
+                               const DNS::ResourceRecord& rr = *i;
+                               if (rr.type == qtype)
+                                       return &rr;
+                       }
+
+                       return NULL;
+               }
        };
 
        class ReplySocket;
@@ -136,11 +148,12 @@ namespace DNS
 
        /** A DNS query.
         */
-       class Request : public Timer, public Question
+       class Request : public Timer
        {
         protected:
                Manager* const manager;
         public:
+               Question question;
                /* Use result cache if available */
                bool use_cache;
                /* Request id */
@@ -150,8 +163,8 @@ namespace DNS
 
                Request(Manager* mgr, Module* mod, const std::string& addr, QueryType qt, bool usecache = true)
                        : Timer((ServerInstance->Config->dns_timeout ? ServerInstance->Config->dns_timeout : 5))
-                       , Question(addr, qt)
                        , manager(mgr)
+                       , question(addr, qt)
                        , use_cache(usecache)
                        , id(0)
                        , creator(mod)
@@ -178,7 +191,7 @@ namespace DNS
                 */
                bool Tick(time_t now)
                {
-                       Query rr(*this);
+                       Query rr(this->question);
                        rr.error = ERROR_TIMEDOUT;
                        this->OnError(&rr);
                        delete this;
index 25182eb1b9242029516b6f76dfd75fe61938d33e..c95d42ea3b7349009487d4b6ba2063681f3d909b 100644 (file)
@@ -416,7 +416,7 @@ class MyManager : public Manager, public Timer, public EventHandler
                        if (!request)
                                continue;
 
-                       Query rr(*request);
+                       Query rr(request->question);
                        rr.error = ERROR_UNKNOWN;
                        request->OnError(&rr);
 
@@ -426,7 +426,7 @@ class MyManager : public Manager, public Timer, public EventHandler
 
        void Process(DNS::Request* req)
        {
-               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Processing request to lookup " + req->name + " of type " + ConvToStr(req->type) + " to " + this->myserver.addr());
+               ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Processing request to lookup " + req->question.name + " of type " + ConvToStr(req->question.type) + " to " + this->myserver.addr());
 
                /* Create an id */
                unsigned int tries = 0;
@@ -463,7 +463,7 @@ class MyManager : public Manager, public Timer, public EventHandler
                Packet p;
                p.flags = QUERYFLAGS_RD;
                p.id = req->id;
-               p.question = *req;
+               p.question = req->question;
 
                unsigned char buffer[524];
                unsigned short len = p.Pack(buffer, sizeof(buffer));
@@ -479,7 +479,7 @@ class MyManager : public Manager, public Timer, public EventHandler
                }
 
                // Update name in the original request so question checking works for PTR queries
-               req->name = p.question.name;
+               req->question.name = p.question.name;
 
                if (SocketEngine::SendTo(this, buffer, len, 0, &this->myserver.sa, this->myserver.sa_size()) != len)
                        throw Exception("DNS: Unable to send query");
@@ -568,7 +568,7 @@ class MyManager : public Manager, public Timer, public EventHandler
                        return;
                }
 
-               if (static_cast<Question&>(*request) != recv_packet.question)
+               if (request->question != recv_packet.question)
                {
                        // This can happen under high latency, drop it silently, do not fail the request
                        ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Received an answer that isn't for a question we asked");
@@ -631,7 +631,7 @@ class MyManager : public Manager, public Timer, public EventHandler
                }
                else
                {
-                       ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Lookup complete for " + request->name);
+                       ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "Lookup complete for " + request->question.name);
                        ServerInstance->stats.DnsGood++;
                        request->OnLookupComplete(&recv_packet);
                        this->AddCache(recv_packet);
@@ -815,7 +815,7 @@ class ModuleDNS : public Module
 
                        if (req->creator == mod)
                        {
-                               Query rr(*req);
+                               Query rr(req->question);
                                rr.error = ERROR_UNLOADED;
                                req->OnError(&rr);
 
index cbd80931c727ab2fd9c00529b2068104660a6e50..b9adc9c2e7433df71b9629a18880ba5fa3feb6fa 100644 (file)
@@ -37,21 +37,6 @@ class UserResolver : public DNS::Request
         */
        const bool fwd;
 
-       const DNS::ResourceRecord* FindAnswerOfType(const DNS::Query* response, DNS::QueryType qtype)
-       {
-               for (std::vector<DNS::ResourceRecord>::const_iterator it = response->answers.begin(); it != response->answers.end(); ++it)
-               {
-                       const DNS::ResourceRecord& rr = *it;
-
-                       if (rr.type == qtype)
-                       {
-                               return &rr;
-                       }
-               }
-
-               return NULL;
-       }
-
  public:
        /** Create a resolver.
         * @param mgr DNS Manager
@@ -80,10 +65,10 @@ class UserResolver : public DNS::Request
                        return;
                }
 
-               const DNS::ResourceRecord* ans_record = FindAnswerOfType(r, this->type);
+               const DNS::ResourceRecord* ans_record = r->FindAnswerOfType(this->question.type);
                if (ans_record == NULL)
                {
-                       ServerInstance->Logs->Log(MODNAME, LOG_DEBUG, "DNS result for %s: no record of type %d", uuid.c_str(), this->type);
+                       OnError(r);
                        return;
                }
 
index 3d04a5085dd0a9bc3c38b76aa961c8e12ab9657e..6682b8dbed7a296cb1f51c3171d78b6d9d12dfba 100644 (file)
@@ -42,7 +42,12 @@ ServernameResolver::ServernameResolver(DNS::Manager* mgr, const std::string& hos
 
 void ServernameResolver::OnLookupComplete(const DNS::Query *r)
 {
-       const DNS::ResourceRecord &ans_record = r->answers[0];
+       const DNS::ResourceRecord* const ans_record = r->FindAnswerOfType(this->question.type);
+       if (!ans_record)
+       {
+               OnError(r);
+               return;
+       }
 
        /* Initiate the connection, now that we have an IP to use.
         * Passing a hostname directly to BufferedSocket causes it to
@@ -51,7 +56,7 @@ void ServernameResolver::OnLookupComplete(const DNS::Query *r)
        TreeServer* CheckDupe = Utils->FindServer(MyLink->Name.c_str());
        if (!CheckDupe) /* Check that nobody tried to connect it successfully while we were resolving */
        {
-               TreeSocket* newsocket = new TreeSocket(MyLink, myautoconnect, ans_record.rdata);
+               TreeSocket* newsocket = new TreeSocket(MyLink, myautoconnect, ans_record->rdata);
                if (newsocket->GetFd() > -1)
                {
                        /* We're all OK */