]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Extra checking that the fd's we pass to SocketEngine::AddFd were added (a lot of...
authorbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 8 Aug 2006 09:32:57 +0000 (09:32 +0000)
committerbrain <brain@e03df62e-2008-0410-955e-edbf42e46eb7>
Tue, 8 Aug 2006 09:32:57 +0000 (09:32 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4780 e03df62e-2008-0410-955e-edbf42e46eb7

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

index b644151dc06b7e3205a9f290c60425068100bc6a..d2a8ed522b2701717e435ecc48e3a674623e7804 100644 (file)
@@ -32,7 +32,7 @@ enum InspSocketState { I_DISCONNECTED, I_CONNECTING, I_CONNECTED, I_LISTENING, I
 /**
  * Error types which a socket may exhibit
  */
-enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE };
+enum InspSocketError { I_ERR_TIMEOUT, I_ERR_SOCKET, I_ERR_CONNECT, I_ERR_BIND, I_ERR_RESOLVE, I_ERR_WRITE, I_ERR_NOMOREFDS };
 
 class InspSocket;
 
index 0436095b6bd30afc315837138fd0f7402a4c1891..cbe56337dfaa69935b7b75d454d0ccd9c1449904 100644 (file)
@@ -202,6 +202,10 @@ int DNSRequest::SendRequests(const DNSHeader *header, const int length, QueryTyp
 /* Add a query with a predefined header, and allocate an ID for it. */
 DNSRequest* DNS::AddQuery(DNSHeader *header, int &id)
 {
+       /* Is the DNS connection down? */
+       if (MasterSocket == -1)
+               return NULL;
+
        /* Are there already the max number of requests on the go? */
        if (requests.size() == DNS::MAX_REQUEST_ID + 1)
                return NULL;
@@ -323,7 +327,15 @@ DNS::DNS()
                        log(DEBUG,"Add master socket %d",MasterSocket);
                        /* Hook the descriptor into the socket engine */
                        if (ServerInstance && ServerInstance->SE)
-                               ServerInstance->SE->AddFd(MasterSocket,true,X_ESTAB_DNS);
+                       {
+                               if (!ServerInstance->SE->AddFd(MasterSocket,true,X_ESTAB_DNS))
+                               {
+                                       log(DEFAULT,"Internal error starting DNS - hostnames will NOT resolve.");
+                                       shutdown(MasterSocket,2);
+                                       close(MasterSocket);
+                                       MasterSocket = -1;
+                               }
+                       }
                }
        }
 }
index c31dbae5078612afc01989652be99dea1d6afb73..27029a6e4b18344e90fd73af88ee1703f05d802e 100644 (file)
@@ -908,7 +908,7 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
 int InspIRCd::Run()
 {
        /* Until THIS point, ServerInstance == NULL */
-       
+
        this->Res = new DNS();
 
        LoadAllModules(this);
@@ -916,21 +916,12 @@ int InspIRCd::Run()
        /* Just in case no modules were loaded - fix for bug #101 */
        this->BuildISupport();
 
-       printf("\nInspIRCd is now running!\n");
-
        if (!stats->BoundPortCount)
        {
                printf("\nI couldn't bind any ports! Are you sure you didn't start InspIRCd twice?\n");
                Exit(ERROR);
        }
 
-       if (!Config->nofork)
-       {
-               fclose(stdout);
-               fclose(stderr);
-               fclose(stdin);
-       }
-
        /* Add the listening sockets used for client inbound connections
         * to the socket engine
         */
@@ -938,9 +929,22 @@ int InspIRCd::Run()
        for (unsigned long count = 0; count < stats->BoundPortCount; count++)
        {
                log(DEBUG,"Add listener: %d",Config->openSockfd[count]);
-               SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
+               if (!SE->AddFd(Config->openSockfd[count],true,X_LISTEN))
+               {
+                       printf("\nEH? Could not add listener to socketengine. You screwed up, aborting.\n");
+                       Exit(ERROR);
+               }
        }
 
+       if (!Config->nofork)
+       {
+               fclose(stdout);
+               fclose(stderr);
+               fclose(stdin);
+       }
+
+       printf("\nInspIRCd is now running!\n");
+
        this->WritePID(Config->PID);
 
        /* main loop, this never returns */
index 17268dc7887557a51fd81f47c3bb194fb684b8db..fcd1c3a5ae9bdb7d452cf72fc64208e5f07a2bac 100644 (file)
@@ -52,7 +52,7 @@ InspSocket::InspSocket(int newfd, const char* ip)
        this->ClosePending = false;
        if (this->fd > -1)
        {
-               ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+               this->ClosePending = (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE));
                socket_ref[this->fd] = this;
        }
 }
@@ -88,7 +88,13 @@ InspSocket::InspSocket(const std::string &ipaddr, int aport, bool listening, uns
                                this->state = I_LISTENING;
                                if (this->fd > -1)
                                {
-                                       ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+                                       if (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE))
+                                       {
+                                               this->Close();
+                                               this->state = I_ERROR;
+                                               this->OnError(I_ERR_NOMOREFDS);
+                                               this->ClosePending = true;
+                                       }
                                        socket_ref[this->fd] = this;
                                }
                                log(DEBUG,"New socket now in I_LISTENING state");
@@ -135,7 +141,14 @@ void InspSocket::WantWrite()
         */
        this->WaitingForWriteEvent = true;
        ServerInstance->SE->DelFd(this->fd);
-       ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
+       if (!ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE))
+       {
+               this->Close();
+               this->fd = -1;
+               this->state = I_ERROR;
+               this->OnError(I_ERR_NOMOREFDS);
+               this->ClosePending = true;
+       }
 }
 
 void InspSocket::SetQueues(int nfd)
@@ -253,7 +266,15 @@ bool InspSocket::DoConnect()
        this->state = I_CONNECTING;
        if (this->fd > -1)
        {
-               ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE);
+               if (!ServerInstance->SE->AddFd(this->fd,false,X_ESTAB_MODULE))
+               {
+                       this->OnError(I_ERR_NOMOREFDS);
+                       this->Close();
+                       this->fd = -1;
+                       this->state = I_ERROR;
+                       this->ClosePending = true;
+                       return false;
+               }
                socket_ref[this->fd] = this;
                this->SetQueues(this->fd);
        }
@@ -421,7 +442,8 @@ bool InspSocket::Poll()
                        if (this->fd > -1)
                        {
                                ServerInstance->SE->DelFd(this->fd);
-                               ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+                               if (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE))
+                                       return false;
                        }
                        return this->OnConnected();
                break;
@@ -442,7 +464,9 @@ bool InspSocket::Poll()
                        {
                                /* Switch back to read events */
                                ServerInstance->SE->DelFd(this->fd);
-                               ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE);
+                               if (!ServerInstance->SE->AddFd(this->fd,true,X_ESTAB_MODULE))
+                                       return false;
+
                                /* Trigger the write event */
                                n = this->OnWriteReady();
                        }
index 04a4f5f15336aa6d78aad2cb179edeb3772ca776..1916bb555989bede5609ab1458c7ddccaa54698a 100644 (file)
@@ -359,8 +359,14 @@ int BindPorts(bool bail)
                                                /* Associate the new open port with a slot in the socket engine */
                                                if (Config->openSockfd[count] > -1)
                                                {
-                                                       ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
-                                                       BoundPortCount++;
+                                                       if (!ServerInstance->SE->AddFd(Config->openSockfd[count],true,X_LISTEN))
+                                                       {
+                                                               log(DEFAULT,"ERK! Failed to add listening port to socket engine!");
+                                                               shutdown(Config->openSockfd[count],2);
+                                                               close(Config->openSockfd[count]);
+                                                       }
+                                                       else
+                                                               BoundPortCount++;
                                                }
                                        }
                                }
index c02dbd4ab171e63dae228ef1c0f1ba49530bd69a..34c347cb934deac5aaa0a576a1383801fa20e7cd 100644 (file)
@@ -908,10 +908,13 @@ void AddClient(int socket, int port, bool iscached, insp_inaddr ip)
 
        if (socket > -1)
        {
-               ServerInstance->SE->AddFd(socket,true,X_ESTAB_CLIENT);
+               if (!ServerInstance->SE->AddFd(socket,true,X_ESTAB_CLIENT))
+               {
+                       kill_link(_new, "Internal error handling connection");
+                       return;
+               }
        }
 
-       log(DEBUG,"Writing to client %d",_new->fd);
        WriteServ(_new->fd,"NOTICE Auth :*** Looking up your hostname...");
 }