]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Fix global session checks
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index 012fc38de20123782adfddfc66b02634020efb23..17268dc7887557a51fd81f47c3bb194fb684b8db 100644 (file)
 #include "inspstring.h"
 #include "helperfuncs.h"
 #include "socketengine.h"
+#include "message.h"
 
 
 extern InspIRCd* ServerInstance;
 extern ServerConfig* Config;
 extern time_t TIME;
+extern Server* MyServer;
 
 InspSocket* socket_ref[MAX_DESCRIPTORS];
 
+
 InspSocket::InspSocket()
 {
        this->state = I_DISCONNECTED;
@@ -41,7 +44,7 @@ InspSocket::InspSocket()
        this->ClosePending = false;
 }
 
-InspSocket::InspSocket(int newfd, char* ip)
+InspSocket::InspSocket(int newfd, const char* ip)
 {
        this->fd = newfd;
        this->state = I_CONNECTED;
@@ -54,9 +57,9 @@ InspSocket::InspSocket(int newfd, char* ip)
        }
 }
 
-InspSocket::InspSocket(const std::string &ahost, int aport, bool listening, unsigned long maxtime) : fd(-1)
+InspSocket::InspSocket(const std::string &ipaddr, int aport, bool listening, unsigned long maxtime) : fd(-1)
 {
-       strlcpy(host,ahost.c_str(),MAXBUF);
+       strlcpy(host,ipaddr.c_str(),MAXBUF);
        this->ClosePending = false;
        if (listening) {
                if ((this->fd = OpenTCPSocket()) == ERROR)
@@ -70,14 +73,14 @@ InspSocket::InspSocket(const std::string &ahost, int aport, bool listening, unsi
                }
                else
                {
-                       if (!BindSocket(this->fd,this->client,this->server,aport,(char*)ahost.c_str()))
+                       if (!BindSocket(this->fd,this->client,this->server,aport,(char*)ipaddr.c_str()))
                        {
+                               log(DEBUG,"BindSocket() error %s",strerror(errno));
                                this->Close();
                                this->fd = -1;
                                this->state = I_ERROR;
                                this->OnError(I_ERR_BIND);
                                this->ClosePending = true;
-                               log(DEBUG,"BindSocket() error %s",strerror(errno));
                                return;
                        }
                        else
@@ -95,19 +98,18 @@ InspSocket::InspSocket(const std::string &ahost, int aport, bool listening, unsi
        }
        else
        {
-               strlcpy(this->host,ahost.c_str(),MAXBUF);
+               strlcpy(this->host,ipaddr.c_str(),MAXBUF);
                this->port = aport;
 
-               if (!inet_aton(host,&addy))
+               if (insp_aton(host,&addy) < 1)
                {
-                       log(DEBUG,"Attempting to resolve %s",this->host);
-                       /* Its not an ip, spawn the resolver */
-                       this->dns.SetNS(std::string(Config->DNSServer));
-                       this->dns.ForwardLookupWithFD(host,fd);
-                       timeout_end = time(NULL) + maxtime;
-                       timeout = false;
-                       this->state = I_RESOLVING;
-                       socket_ref[this->fd] = this;
+                       log(DEBUG,"You cannot pass hostnames to InspSocket, resolve them first with Resolver!");
+                       this->Close();
+                       this->fd = -1;
+                       this->state = I_ERROR;
+                       this->OnError(I_ERR_RESOLVE);
+                       this->ClosePending = true;
+                       return;
                }
                else
                {
@@ -119,6 +121,23 @@ InspSocket::InspSocket(const std::string &ahost, int aport, bool listening, unsi
        }
 }
 
+void InspSocket::WantWrite()
+{
+       /** XXX:
+        * The socket engine may only have each FD in the list ONCE.
+        * This means we cant watch for write AND read at the same
+        * time. We have to remove the READ fd, to insert the WRITE
+        * fd. Once we receive our WRITE event (which WILL ARRIVE,
+        * pretty much gauranteed) we switch back to watching for
+        * READ events again.
+        *
+        * This behaviour may be fixed in a later version.
+        */
+       this->WaitingForWriteEvent = true;
+       ServerInstance->SE->DelFd(this->fd);
+       ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
+}
+
 void InspSocket::SetQueues(int nfd)
 {
        // attempt to increase socket sendq and recvq as high as its possible
@@ -128,35 +147,6 @@ void InspSocket::SetQueues(int nfd)
        setsockopt(nfd,SOL_SOCKET,SO_RCVBUF,(const void *)&recvbuf,sizeof(sendbuf));
 }
 
-bool InspSocket::DoResolve()
-{
-       log(DEBUG,"In DoResolve(), trying to resolve IP");
-       if (this->dns.HasResult())
-       {
-               log(DEBUG,"Socket has result");
-               std::string res_ip = dns.GetResultIP();
-               if (res_ip != "")
-               {
-                       log(DEBUG,"Socket result set to %s",res_ip.c_str());
-                       strlcpy(this->IP,res_ip.c_str(),MAXBUF);
-                       socket_ref[this->fd] = NULL;
-               }
-               else
-               {
-                       log(DEBUG,"Socket DNS failure");
-                       this->Close();
-                       this->state = I_ERROR;
-                       this->OnError(I_ERR_RESOLVE);
-                       this->fd = -1;
-                       this->ClosePending = true;
-                       return false;
-               }
-               return this->DoConnect();
-       }
-       log(DEBUG,"No result for socket yet!");
-       return true;
-}
-
 /* Most irc servers require you to specify the ip you want to bind to.
  * If you dont specify an IP, they rather dumbly bind to the first IP
  * of the box (e.g. INADDR_ANY). In InspIRCd, we scan thought the IP
@@ -177,15 +167,20 @@ bool InspSocket::BindAddr()
                std::string IP = Conf.ReadValue("bind","address",j);
                if (Type == "servers")
                {
-                       if ((IP != "*") && (IP != "127.0.0.1"))
+                       if ((IP != "*") && (IP != "127.0.0.1") && (IP != ""))
                        {
                                insp_sockaddr s;
 
-                               if (inet_aton(IP.c_str(),&n))
+                               if (insp_aton(IP.c_str(),&n) > 0)
                                {
                                        log(DEBUG,"Found an IP to bind to: %s",IP.c_str());
+#ifdef IPV6
+                                       s.sin6_addr = n;
+                                       s.sin6_family = AF_FAMILY;
+#else
                                        s.sin_addr = n;
-                                       s.sin_family = AF_INET;
+                                       s.sin_family = AF_FAMILY;
+#endif
                                        if (bind(this->fd,(struct sockaddr*)&s,sizeof(s)) < 0)
                                        {
                                                log(DEBUG,"Cant bind()");
@@ -211,7 +206,7 @@ bool InspSocket::BindAddr()
 bool InspSocket::DoConnect()
 {
        log(DEBUG,"In DoConnect()");
-       if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
+       if ((this->fd = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
        {
                log(DEBUG,"Cant socket()");
                this->state = I_ERROR;
@@ -220,14 +215,23 @@ bool InspSocket::DoConnect()
                return false;
        }
 
-       if (!this->BindAddr())
-               return false;
+       if ((strstr(this->IP,"::ffff:") != (char*)&this->IP) && (strstr(this->IP,"::FFFF:") != (char*)&this->IP))
+       {
+               if (!this->BindAddr())
+                       return false;
+       }
 
        log(DEBUG,"Part 2 DoConnect() %s",this->IP);
-       inet_aton(this->IP,&addy);
-       addr.sin_family = AF_INET;
+       insp_aton(this->IP,&addy);
+#ifdef IPV6
+       addr.sin6_family = AF_FAMILY;
+       memcpy(&addr.sin6_addr, &addy, sizeof(insp_inaddr));
+       addr.sin6_port = htons(this->port);
+#else
+       addr.sin_family = AF_FAMILY;
        addr.sin_addr = addy;
        addr.sin_port = htons(this->port);
+#endif
 
        int flags;
        flags = fcntl(this->fd, F_GETFL, 0);
@@ -378,7 +382,7 @@ bool InspSocket::Timeout(time_t current)
                return true;
        }
 
-       if (((this->state == I_RESOLVING) || (this->state == I_CONNECTING)) && (current > timeout_end))
+       if ((this->state == I_CONNECTING) && (current > timeout_end))
        {
                log(DEBUG,"Timed out, current=%lu timeout_end=%lu");
                // for non-listening sockets, the timeout can occur
@@ -408,10 +412,6 @@ bool InspSocket::Poll()
 
        switch (this->state)
        {
-               case I_RESOLVING:
-                       log(DEBUG,"State = I_RESOLVING, calling DoResolve()");
-                       return this->DoResolve();
-               break;
                case I_CONNECTING:
                        log(DEBUG,"State = I_CONNECTING");
                        this->SetState(I_CONNECTED);
@@ -429,11 +429,28 @@ bool InspSocket::Poll()
                        length = sizeof (client);
                        incoming = accept (this->fd, (sockaddr*)&client,&length);
                        this->SetQueues(incoming);
-                       this->OnIncomingConnection(incoming,inet_ntoa(client.sin_addr));
+#ifdef IPV6
+                       this->OnIncomingConnection(incoming,(char*)insp_ntoa(client.sin6_addr));
+#else
+                       this->OnIncomingConnection(incoming,(char*)insp_ntoa(client.sin_addr));
+#endif
                        return true;
                break;
                case I_CONNECTED:
-                       n = this->OnDataReady();
+
+                       if (this->WaitingForWriteEvent)
+                       {
+                               /* Switch back to read events */
+                               ServerInstance->SE->DelFd(this->fd);
+                               ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+                               /* Trigger the write event */
+                               n = this->OnWriteReady();
+                       }
+                       else
+                       {
+                               /* Process the read event */
+                               n = this->OnDataReady();
+                       }
                        /* Flush any pending, but not till after theyre done with the event
                         * so there are less write calls involved.
                         * Both FlushWriteBuffer AND the return result of OnDataReady must
@@ -470,6 +487,7 @@ void InspSocket::OnError(InspSocketError e) { return; }
 int InspSocket::OnDisconnect() { return 0; }
 int InspSocket::OnIncomingConnection(int newfd, char* ip) { return 0; }
 bool InspSocket::OnDataReady() { return true; }
+bool InspSocket::OnWriteReady() { return true; }
 void InspSocket::OnTimeout() { return; }
 void InspSocket::OnClose() { return; }