]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Stability fix
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index 012fc38de20123782adfddfc66b02634020efb23..cc0368525f6626302ac1fcf053e16fd96995f607 100644 (file)
@@ -26,6 +26,7 @@
 #include "inspstring.h"
 #include "helperfuncs.h"
 #include "socketengine.h"
+#include "message.h"
 
 
 extern InspIRCd* ServerInstance;
@@ -41,7 +42,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;
@@ -72,12 +73,12 @@ InspSocket::InspSocket(const std::string &ahost, int aport, bool listening, unsi
                {
                        if (!BindSocket(this->fd,this->client,this->server,aport,(char*)ahost.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
@@ -119,6 +120,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
@@ -177,9 +195,21 @@ 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;
+                               char resolved_addr[MAXBUF];
+                               
+                               if (!inet_aton(IP.c_str(),&n))
+                               {
+                                       /* If they gave a hostname, bind to the IP it resolves to */
+                                       log(DEBUG,"Resolving host %s",IP.c_str());
+                                       if (CleanAndResolve(resolved_addr, IP.c_str(), true))
+                                       {
+                                               log(DEBUG,"Resolved host %s to %s",IP.c_str(),resolved_addr);
+                                               IP = resolved_addr;
+                                       }
+                               }
 
                                if (inet_aton(IP.c_str(),&n))
                                {
@@ -433,7 +463,20 @@ bool InspSocket::Poll()
                        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 +513,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; }