]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
*UNTESTED DO NOT USE YET* - async dns for InspSocket as test - removing requirement...
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Thu, 2 Feb 2006 16:34:34 +0000 (16:34 +0000)
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Thu, 2 Feb 2006 16:34:34 +0000 (16:34 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@3017 e03df62e-2008-0410-955e-edbf42e46eb7

include/dns.h
include/socket.h
src/dns.cpp
src/inspircd.cpp
src/socket.cpp

index 33b5f8c7ae7f21744294f89b901a27aac8ad573e..91e65577561fe6fc9035ca9d1966afe5ca3ba9ad 100644 (file)
@@ -78,6 +78,9 @@ public:
         * and returns true if the lookup was successfully initiated.
         */
        bool ForwardLookup(std::string host);
+       /** Used by modules to perform a dns lookup but have the socket engine poll a module, instead of the dns object directly.
+        */
+       bool ForwardLookupWithFD(std::string host, int &fd);
        /** This method will return true when the lookup is completed. It uses poll internally
         * to determine the status of the socket.
         */
index 287726a0ce9a07e087ce22f5e77c0c5c809f8a29..e8eec405cf6f2e4f124acbe71735fe87175fde8b 100644 (file)
 /**
  * States which a socket may be in
  */
-enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
+enum InspSocketState { I_DISCONNECTED, I_RESOLVING, I_CONNECTING, I_CONNECTED, I_LISTENING, I_ERROR };
 
 /**
  * Error types which a socket may exhibit
  */
-enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND };
+enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE };
 
 /**
  * InspSocket is an extendable socket class which modules
@@ -53,6 +53,11 @@ private:
         */
         int fd;
 
+       /**
+        * The resolver for this socket
+        */
+       DNS dns;
+
        /**
         * The hostname connected to
         */
@@ -308,6 +313,8 @@ public:
         * used for this socket.
         */
        virtual ~InspSocket();
+
+       virtual DoResolve();
 };
 
 #endif
index f6bba28daf7a35f53aa7c3bd8280a1925809da47..67c309f4108663beca2d50078170e11f2033a5e0 100644 (file)
@@ -735,6 +735,20 @@ bool DNS::ForwardLookup(std::string host)
        return true;
 }
 
+bool DNS::ForwardLookupWithFD(std::string host, int &fd)
+{
+       ServerInstance->stats->statsDns++;
+       this->myfd = dns_getip4(host.c_str());
+       fd = this->myfd;
+       if (this->myfd == -1)
+       {
+               
+       }
+       log(DEBUG,"DNS: ForwardLookupWithFD, fd=%d",this->myfd);
+       ServerInstance->SE->AddFd(this->myfd,true,X_ESTAB_MODULE);
+       return true;
+}
+
 bool DNS::HasResult(int fd)
 {
        return (fd == this->myfd);
index a3297f921b444ce504aea3922436b67123631a20..6f61fbb311d73f27434be1757eb8b13d5781a964 100644 (file)
@@ -21,11 +21,9 @@ using namespace std;
 #include "inspircd_config.h"
 #include "inspircd.h"
 #include "inspircd_io.h"
-#include <unistd.h>
 #include <fcntl.h>
 #include <sys/errno.h>
 #include <sys/ioctl.h>
-#include <sys/utsname.h>
 #include <time.h>
 #include <string>
 #ifdef GCC3
@@ -480,7 +478,6 @@ bool InspIRCd::LoadModule(const char* filename)
                 {
                         log(DEFAULT,"Unable to load %s: %s",modfile,factory[MODCOUNT+1]->LastError());
                        snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[MODCOUNT+1]->LastError());
-                       MODCOUNT--;
                        return false;
                 }
                 if (factory[MODCOUNT+1]->factory)
index 160f0f652dd04a8c6774a3f13d4546c7ecf6c67a..9c2f5a362ba8d473a7ed432e6a7f1c77ad29e354 100644 (file)
@@ -88,50 +88,67 @@ InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long
                        }
                }                       
        } else {
-               char* ip;
                this->host = host;
-               hostent* hoste = gethostbyname(host.c_str());
-               if (!hoste) {
-                       ip = (char*)host.c_str();
-               } else {
-                       struct in_addr* ia = (in_addr*)hoste->h_addr;
-                       ip = inet_ntoa(*ia);
+
+               if (this->dns.ForwardLookupWithFD(host,fd))
+               {
+                       timeout_end = time(NULL)+maxtime;
+                       timeout = false;
+                       this->state = I_RESOLVING;
                }
+               else
+               {
+                       this->state = I_ERROR;
+                       this->OnError(I_ERR_RESOLVE);
+                        return;
+               }
+}
 
-               this->IP = ip;
+bool InspSocket::DoResolve()
+{
+       if (this->dns.HasResult())
+       {
+               std::string res_ip = dns.GetResultIP();
+               
+               if (res_ip != "")
+               {
+                       this->IP = ip;
+               }
+               else
+               {
+                       this->IP = this->host;
+               }
 
-                timeout_end = time(NULL)+maxtime;
-                timeout = false;
-                if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+               if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
                {
                        this->state = I_ERROR;
                        this->OnError(I_ERR_SOCKET);
-                        return;
+                       return false;
                }
                this->port = port;
-                inet_aton(ip,&addy);
-                addr.sin_family = AF_INET;
-                addr.sin_addr = addy;
-                addr.sin_port = htons(this->port);
+               inet_aton(ip,&addy);
+               addr.sin_family = AF_INET;
+               addr.sin_addr = addy;
+               addr.sin_port = htons(this->port);
 
-                int flags;
-                flags = fcntl(this->fd, F_GETFL, 0);
-                fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
+               int flags;
+               flags = fcntl(this->fd, F_GETFL, 0);
+               fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
 
-                if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
-                {
-                        if (errno != EINPROGRESS)
-                        {
+               if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
+               {
+                       if (errno != EINPROGRESS)
+                       {
                                this->Close();
                                this->OnError(I_ERR_CONNECT);
                                this->state = I_ERROR;
-                                return;
-                        }
-                }
-                this->state = I_CONNECTING;
+                               return false;
+                       }
+               }
+               this->state = I_CONNECTING;
                ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
                socket_ref[this->fd] = this;
-                return;
+               return true;
        }
 }
 
@@ -203,7 +220,7 @@ void InspSocket::FlushWriteBuffer()
 
 bool InspSocket::Timeout(time_t current)
 {
-       if ((this->state == I_CONNECTING) && (current > timeout_end))
+       if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
        {
                // for non-listening sockets, the timeout can occur
                // which causes termination of the connection after
@@ -226,6 +243,9 @@ bool InspSocket::Poll()
        
        switch (this->state)
        {
+               case I_RESOLVING:
+                       return this->DoResolve();
+               break;
                case I_CONNECTING:
                        this->SetState(I_CONNECTED);
                        /* Our socket was in write-state, so delete it and re-add it