]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/dns.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / dns.cpp
index fbe512b9f113a5e9fc663da8279a56435c75d3a7..efb690dc248cd90f6eb0860c298d25b8ee6e7e4c 100644 (file)
@@ -36,9 +36,12 @@ using namespace std;
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include "dns.h"
+#include "inspircd.h"
 #include "helperfuncs.h"
+#include "socketengine.h"
 
-extern int statsAccept,statsRefused,statsUnknown,statsCollisions,statsDns,statsDnsGood,statsDnsBad,statsConnects,statsSent,statsRecv;
+extern InspIRCd* ServerInstance;
+extern ServerConfig* Config;
 
 #define max(a,b) (a > b ? a : b)
 #define DNS_MAX              8                    /* max number of nameservers used */
@@ -648,12 +651,11 @@ void DNS::SetNS(std::string dnsserver)
 
 DNS::~DNS()
 {
-       log(DEBUG,"Delete DNS fd=%d",this->myfd);
 }
 
 bool DNS::ReverseLookup(std::string ip)
 {
-       statsDns++;
+       ServerInstance->stats->statsDns++;
         binip = dns_aton4(ip.c_str());
         if (binip == NULL) {
                 return false;
@@ -665,21 +667,36 @@ bool DNS::ReverseLookup(std::string ip)
                return false;
        }
        log(DEBUG,"DNS: ReverseLookup, fd=%d",this->myfd);
+#ifndef THREADED_DNS
+       ServerInstance->SE->AddFd(this->myfd,true,X_ESTAB_DNS);
+#endif
        return true;
 }
 
 bool DNS::ForwardLookup(std::string host)
 {
-       statsDns++;
+       ServerInstance->stats->statsDns++;
        this->myfd = dns_getip4(host.c_str());
        if (this->myfd == -1)
        {
                return false;
        }
        log(DEBUG,"DNS: ForwardLookup, fd=%d",this->myfd);
+#ifndef THREADED_DNS
+       ServerInstance->SE->AddFd(this->myfd,true,X_ESTAB_DNS);
+#endif
        return true;
 }
 
+bool DNS::HasResult(int fd)
+{
+       return (fd == this->myfd);
+}
+
+/* Only the multithreaded dns uses this poll() based
+ * check now. As its in another thread we dont have
+ * to worry about its performance that much.
+ */
 bool DNS::HasResult()
 {
        log(DEBUG,"DNS: HasResult, fd=%d",this->myfd);
@@ -700,12 +717,15 @@ std::string DNS::GetResult()
 {
        log(DEBUG,"DNS: GetResult()");
         result = dns_getresult(this->myfd);
+#ifndef THREADED_DNS
+       ServerInstance->SE->DelFd(this->myfd);
+#endif
         if (result) {
-               statsDnsGood++;
+               ServerInstance->stats->statsDnsGood++;
                dns_close(this->myfd);
                return result;
         } else {
-               statsDnsBad++;
+               ServerInstance->stats->statsDnsBad++;
                if (this->myfd != -1)
                {
                        dns_close(this->myfd);
@@ -721,11 +741,14 @@ std::string DNS::GetResultIP()
        result = dns_getresult(this->myfd);
        if (this->myfd != -1)
        {
+#ifndef THREADED_DNS
+               ServerInstance->SE->DelFd(this->myfd);
+#endif
                dns_close(this->myfd);
        }
        if (result)
        {
-               statsDnsGood++;
+               ServerInstance->stats->statsDnsGood++;
                unsigned char a = (unsigned)result[0];
                unsigned char b = (unsigned)result[1];
                unsigned char c = (unsigned)result[2];
@@ -735,9 +758,51 @@ std::string DNS::GetResultIP()
        }
        else
        {
-               statsDnsBad++;
+               ServerInstance->stats->statsDnsBad++;
                log(DEBUG,"DANGER WILL ROBINSON! NXDOMAIN for forward lookup, but we got a reverse lookup!");
                return "";
        }
 }
 
+
+
+#ifdef THREADED_DNS
+void* dns_task(void* arg)
+{
+        userrec* u = (userrec*)arg;
+        log(DEBUG,"DNS thread for user %s",u->nick);
+        DNS dns1;
+        DNS dns2;
+        std::string host;
+        std::string ip;
+        if (dns1.ReverseLookup(u->ip))
+        {
+                while (!dns1.HasResult())
+                {
+                        usleep(100);
+                }
+                host = dns1.GetResult();
+                if (host != "")
+                {
+                        if (dns2.ForwardLookup(host))
+                        {
+                                while (!dns2.HasResult())
+                                {
+                                        usleep(100);
+                                }
+                                ip = dns2.GetResultIP();
+                                if (ip == std::string(u->ip))
+                                {
+                                        if (host.length() < 160)
+                                        {
+                                                strcpy(u->host,host.c_str());
+                                                strcpy(u->dhost,host.c_str());
+                                        }
+                                }
+                        }
+                }
+        }
+        u->dns_done = true;
+        return NULL;
+}
+#endif