]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/dns.cpp
Moved some other stuff into ServerConfig
[user/henk/code/inspircd.git] / src / dns.cpp
index 7adbbfe15902aa2276441155cfeb4dfc7fbbf68c..5a70bb41de88926a0c0c5f2ac8c82ee2612ddd82 100644 (file)
@@ -37,8 +37,11 @@ using namespace std;
 #include <arpa/inet.h>
 #include "dns.h"
 #include "helperfuncs.h"
+#include "socketengine.h"
 
-extern int statsAccept,statsRefused,statsUnknown,statsCollisions,statsDns,statsDnsGood,statsDnsBad,statsConnects,statsSent,statsRecv;
+extern SocketEngine* SE;
+extern ServerConfig* Config;
+serverstats* stats;
 
 #define max(a,b) (a > b ? a : b)
 #define DNS_MAX              8                    /* max number of nameservers used */
@@ -152,6 +155,7 @@ void dns_empty_header(unsigned char *output, const s_header *header, const int l
 }
 
 void dns_close(int fd) { /* close query */
+       log(DEBUG,"DNS: dns_close on fd %d",fd);
        if (fd == lastcreate) {
                wantclose = 1;
                return;
@@ -412,12 +416,6 @@ char* DNS::dns_ntoa4(const in_addr * const ip) { /* numeric to ascii: convert 4p
        return dns_ntoa4_s(ip,r);
 }
 
-char* DNS::dns_ntoa4_r(const in_addr *ip) { /* numeric to ascii (reentrant): convert 4part IP addr struct to new string */
-       char *r;
-       r = new char[256];
-       return dns_ntoa4_s(ip,r);
-}
-
 char* DNS::dns_ntoa4_s(const in_addr *ip, char *r) { /* numeric to ascii (buffered): convert 4part IP addr struct to given string */
        unsigned char *m;
        m = (unsigned char *)&ip->s_addr;
@@ -426,18 +424,8 @@ char* DNS::dns_ntoa4_s(const in_addr *ip, char *r) { /* numeric to ascii (buffer
 }
 
 char* DNS::dns_getresult(const int cfd) { /* retrieve result of DNS query */
-       static char r[RESULTSIZE];
-       return dns_getresult_s(cfd,r);
-}
-
-char* DNS::dns_getresult_r(const int cfd) { /* retrieve result of DNS query (reentrant) */
-       char *r;
-       r = new char[RESULTSIZE];
-       if(dns_getresult_s(cfd,r) == NULL) {
-               delete r;
-               return NULL;
-       }
-       return r;
+       log(DEBUG,"DNS: dns_getresult with cfd=%d",cfd);
+       return dns_getresult_s(cfd,this->localbuf);
 }
 
 char* DNS::dns_getresult_s(const int cfd, char *res) { /* retrieve result of DNS query (buffered) */
@@ -462,7 +450,7 @@ char* DNS::dns_getresult_s(const int cfd, char *res) { /* retrieve result of DNS
                c = c->next;
        }
        if (c == NULL) {
-               log(DEBUG,"DNS: got a response for a query we didnt send");
+               log(DEBUG,"DNS: got a response for a query we didnt send with fd=%d",cfd);
                return NULL; /* query not found */
        }
        /* query found-- pull from list: */
@@ -494,7 +482,7 @@ char* DNS::dns_getresult_s(const int cfd, char *res) { /* retrieve result of DNS
                return NULL;
        }
        if ((h.flags2 & FLAGS2_MASK_RCODE) != 0) {
-               log(DEBUG,"DNS: got an RCODE and didnt want one");
+               log(DEBUG,"DNS lookup failed due to SERVFAIL");
                delete c;
                return NULL;
        }
@@ -646,16 +634,19 @@ char* DNS::dns_getresult_s(const int cfd, char *res) { /* retrieve result of DNS
 DNS::DNS()
 {
        dns_init();
+       log(DEBUG,"Create blank DNS");
 }
 
 DNS::DNS(std::string dnsserver)
 {
        dns_init_2(dnsserver.c_str());
+       log(DEBUG,"Create DNS");
 }
 
 void DNS::SetNS(std::string dnsserver)
 {
        dns_init_2(dnsserver.c_str());
+       log(DEBUG,"Set NS");
 }
 
 DNS::~DNS()
@@ -664,57 +655,80 @@ DNS::~DNS()
 
 bool DNS::ReverseLookup(std::string ip)
 {
-       statsDns++;
+       stats->statsDns++;
         binip = dns_aton4(ip.c_str());
         if (binip == NULL) {
                 return false;
         }
 
-        this->fd = dns_getname4(binip);
-       if (this->fd == -1)
+        this->myfd = dns_getname4(binip);
+       if (this->myfd == -1)
        {
                return false;
        }
+       log(DEBUG,"DNS: ReverseLookup, fd=%d",this->myfd);
+#ifndef THREADED_DNS
+       SE->AddFd(this->myfd,true,X_ESTAB_DNS);
+#endif
        return true;
 }
 
 bool DNS::ForwardLookup(std::string host)
 {
-       statsDns++;
-       this->fd = dns_getip4(host.c_str());
-       if (this->fd == -1)
+       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
+       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);
        pollfd polls;
-       polls.fd = this->fd;
+       polls.fd = this->myfd;
        polls.events = POLLIN;
        int ret = poll(&polls,1,1);
+       log(DEBUG,"DNS: Hasresult returning %d",ret);
        return (ret > 0);
 }
 
 int DNS::GetFD()
 {
-       return this->fd;
+       return this->myfd;
 }
 
 std::string DNS::GetResult()
 {
-        result = dns_getresult(this->fd);
+       log(DEBUG,"DNS: GetResult()");
+        result = dns_getresult(this->myfd);
+#ifndef THREADED_DNS
+       SE->DelFd(this->myfd);
+#endif
         if (result) {
-               statsDnsGood++;
-               dns_close(this->fd);
+               stats->statsDnsGood++;
+               dns_close(this->myfd);
                return result;
         } else {
-               statsDnsBad++;
-               if (this->fd != -1)
+               stats->statsDnsBad++;
+               if (this->myfd != -1)
                {
-                       dns_close(this->fd);
+                       dns_close(this->myfd);
                }
                return "";
        }
@@ -723,14 +737,18 @@ std::string DNS::GetResult()
 std::string DNS::GetResultIP()
 {
        char r[1024];
-       result = dns_getresult(this->fd);
-       if (this->fd != -1)
+       log(DEBUG,"DNS: GetResultIP()");
+       result = dns_getresult(this->myfd);
+       if (this->myfd != -1)
        {
-               dns_close(this->fd);
+#ifndef THREADED_DNS
+               SE->DelFd(this->myfd);
+#endif
+               dns_close(this->myfd);
        }
        if (result)
        {
-               statsDnsGood++;
+               stats->statsDnsGood++;
                unsigned char a = (unsigned)result[0];
                unsigned char b = (unsigned)result[1];
                unsigned char c = (unsigned)result[2];
@@ -740,8 +758,9 @@ std::string DNS::GetResultIP()
        }
        else
        {
-               statsDnsBad++;
+               stats->statsDnsBad++;
                log(DEBUG,"DANGER WILL ROBINSON! NXDOMAIN for forward lookup, but we got a reverse lookup!");
                return "";
        }
 }
+