]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/coremods/core_dns.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / coremods / core_dns.cpp
index 32613546597af81c9570dd13439b8ceab35f9a2e..b16658242212df098ef1639a91096fddfa61aa1c 100644 (file)
@@ -2,7 +2,7 @@
  * InspIRCd -- Internet Relay Chat Daemon
  *
  *   Copyright (C) 2019 Robby <robby@chatbelgie.be>
- *   Copyright (C) 2015, 2017-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2015, 2017-2021 Sadie Powell <sadie@witchery.services>
  *   Copyright (C) 2013-2016 Attila Molnar <attilamolnar@hush.com>
  *   Copyright (C) 2013, 2015-2016 Adam <Adam@anope.org>
  *
@@ -423,6 +423,7 @@ class MyManager : public Manager, public Timer, public EventHandler
        ~MyManager()
        {
                // Ensure Process() will fail for new requests
+               Close();
                unloading = true;
 
                for (unsigned int i = 0; i <= MAX_REQUEST_ID; ++i)
@@ -439,11 +440,32 @@ class MyManager : public Manager, public Timer, public EventHandler
                }
        }
 
+       void Close()
+       {
+               // Shutdown the socket if it exists.
+               if (HasFd())
+               {
+                       SocketEngine::Shutdown(this, 2);
+                       SocketEngine::Close(this);
+               }
+
+               // Remove all entries from the cache.
+               cache.clear();
+       }
+
        void Process(DNS::Request* req) CXX11_OVERRIDE
        {
                if ((unloading) || (req->creator->dying))
                        throw Exception("Module is being unloaded");
 
+               if (!HasFd())
+               {
+                       Query rr(req->question);
+                       rr.error = ERROR_DISABLED;
+                       req->OnError(&rr);
+                       return;
+               }
+
                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 */
@@ -533,6 +555,8 @@ class MyManager : public Manager, public Timer, public EventHandler
                        case ERROR_DOMAIN_NOT_FOUND:
                        case ERROR_NO_RECORDS:
                                return "Domain not found";
+                       case ERROR_DISABLED:
+                               return "DNS lookups are disabled";
                        case ERROR_NONE:
                        case ERROR_UNKNOWN:
                        default:
@@ -703,23 +727,15 @@ class MyManager : public Manager, public Timer, public EventHandler
 
        void Rehash(const std::string& dnsserver, std::string sourceaddr, unsigned int sourceport)
        {
-               if (this->HasFd())
-               {
-                       SocketEngine::Shutdown(this, 2);
-                       SocketEngine::Close(this);
-
-                       /* Remove expired entries from the cache */
-                       this->Tick(ServerInstance->Time());
-               }
-
                irc::sockets::aptosa(dnsserver, DNS::PORT, myserver);
 
                /* Initialize mastersocket */
+               Close();
                int s = socket(myserver.family(), SOCK_DGRAM, 0);
                this->SetFd(s);
 
                /* Have we got a socket? */
-               if (this->GetFd() != -1)
+               if (this->HasFd())
                {
                        SocketEngine::SetReuse(s);
                        SocketEngine::NonBlocking(s);
@@ -832,13 +848,25 @@ class ModuleDNS : public Module
 
        void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
        {
-               std::string oldserver = DNSServer;
-               const std::string oldip = SourceIP;
-               const unsigned int oldport = SourcePort;
-
                ConfigTag* tag = ServerInstance->Config->ConfValue("dns");
+               if (!tag->getBool("enabled", true))
+               {
+                       // Clear these so they get reset if DNS is enabled again.
+                       DNSServer.clear();
+                       SourceIP.clear();
+                       SourcePort = 0;
+
+                       this->manager.Close();
+                       return;
+               }
+
+               const std::string oldserver = DNSServer;
                DNSServer = tag->getString("server");
+
+               const std::string oldip = SourceIP;
                SourceIP = tag->getString("sourceip");
+
+               const unsigned int oldport = SourcePort;
                SourcePort = tag->getUInt("sourceport", 0, 0, UINT16_MAX);
 
                if (DNSServer.empty())