]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Add all this crapola
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index 23d89a71f054164adc48a2701775bc1405ecfbfa..18fd346b7259130fcb14ece569413d711fa2764e 100644 (file)
@@ -2,12 +2,9 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                       E-mail:
- *                <brain@chatspike.net>
- *               <Craig@chatspike.net>
- *     
- * Written by Craig Edwards, Craig McLure, and others.
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
+ *
  * This program is free but copyrighted software; see
  *            the file COPYING for details.
  *
@@ -64,14 +61,12 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool
                        this->fd = -1;
                        this->state = I_ERROR;
                        this->OnError(I_ERR_SOCKET);
-                       this->Instance->Log(DEBUG,"OpenTCPSocket() error");
                        return;
                }
                else
                {
                        if (!SI->BindSocket(this->fd,this->client,this->server,aport,(char*)ipaddr.c_str()))
                        {
-                               this->Instance->Log(DEBUG,"BindSocket() error %s",strerror(errno));
                                this->Close();
                                this->fd = -1;
                                this->state = I_ERROR;
@@ -91,7 +86,6 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool
                                                this->OnError(I_ERR_NOMOREFDS);
                                        }
                                }
-                               this->Instance->Log(DEBUG,"New socket now in I_LISTENING state");
                                return;
                        }
                }                       
@@ -103,7 +97,7 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool
 
                if (insp_aton(host,&addy) < 1)
                {
-                       this->Instance->Log(DEBUG,"You cannot pass hostnames to InspSocket, resolve them first with Resolver!");
+                       this->Instance->Log(DEBUG,"BUG: Hostname passed to InspSocket, rather than an IP address!");
                        this->Close();
                        this->fd = -1;
                        this->state = I_ERROR;
@@ -112,7 +106,6 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool
                }
                else
                {
-                       this->Instance->Log(DEBUG,"No need to resolve %s",this->host);
                        strlcpy(this->IP,host,MAXBUF);
                        timeout_val = maxtime;
                        this->DoConnect();
@@ -147,8 +140,6 @@ bool InspSocket::BindAddr()
 {
        insp_inaddr n;
        ConfigReader Conf(this->Instance);
-
-       this->Instance->Log(DEBUG,"In InspSocket::BindAddr()");
        for (int j =0; j < Conf.Enumerate("bind"); j++)
        {
                std::string Type = Conf.ReadValue("bind","type",j);
@@ -161,7 +152,6 @@ bool InspSocket::BindAddr()
 
                                if (insp_aton(IP.c_str(),&n) > 0)
                                {
-                                       this->Instance->Log(DEBUG,"Found an IP to bind to: %s",IP.c_str());
 #ifdef IPV6
                                        s.sin6_addr = n;
                                        s.sin6_family = AF_FAMILY;
@@ -171,32 +161,23 @@ bool InspSocket::BindAddr()
 #endif
                                        if (bind(this->fd,(struct sockaddr*)&s,sizeof(s)) < 0)
                                        {
-                                               this->Instance->Log(DEBUG,"Cant bind()");
                                                this->state = I_ERROR;
                                                this->OnError(I_ERR_BIND);
                                                this->fd = -1;
                                                return false;
                                        }
-                                       this->Instance->Log(DEBUG,"bind() reports outbound fd bound to ip %s",IP.c_str());
                                        return true;
                                }
-                               else
-                               {
-                                       this->Instance->Log(DEBUG,"Address '%s' was not an IP address",IP.c_str());
-                               }
                        }
                }
        }
-       this->Instance->Log(DEBUG,"Found no suitable IPs to bind, binding INADDR_ANY");
        return true;
 }
 
 bool InspSocket::DoConnect()
 {
-       this->Instance->Log(DEBUG,"In DoConnect()");
        if ((this->fd = socket(AF_FAMILY, SOCK_STREAM, 0)) == -1)
        {
-               this->Instance->Log(DEBUG,"Cant socket()");
                this->state = I_ERROR;
                this->OnError(I_ERR_SOCKET);
                return false;
@@ -208,7 +189,6 @@ bool InspSocket::DoConnect()
                        return false;
        }
 
-       this->Instance->Log(DEBUG,"Part 2 DoConnect() %s",this->IP);
        insp_aton(this->IP,&addy);
 #ifdef IPV6
        addr.sin6_family = AF_FAMILY;
@@ -228,7 +208,6 @@ bool InspSocket::DoConnect()
        {
                if (errno != EINPROGRESS)
                {
-                       this->Instance->Log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
                        this->OnError(I_ERR_CONNECT);
                        this->Close();
                        this->state = I_ERROR;
@@ -250,7 +229,6 @@ bool InspSocket::DoConnect()
                }
                this->SetQueues(this->fd);
        }
-       this->Instance->Log(DEBUG,"Returning true from InspSocket::DoConnect");
        return true;
 }
 
@@ -259,15 +237,15 @@ void InspSocket::Close()
 {
        if (this->fd > -1)
        {
-                if (this->IsIOHooked)
+                if (this->IsIOHooked && Instance->Config->GetIOHook(this))
                {
                        try
                        {
                                Instance->Config->GetIOHook(this)->OnRawSocketClose(this->fd);
                        }
-                       catch (ModuleException& modexcept)
+                       catch (CoreException& modexcept)
                        {
-                               Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+                               Instance->Log(DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                        }
                }
                this->OnClose();
@@ -296,9 +274,9 @@ char* InspSocket::Read()
                {
                        MOD_RESULT = Instance->Config->GetIOHook(this)->OnRawSocketRead(this->fd,this->ibuf,sizeof(this->ibuf),result2);
                }
-               catch (ModuleException& modexcept)
+               catch (CoreException& modexcept)
                {
-                       Instance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
+                       Instance->Log(DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                }
                if (MOD_RESULT < 0)
                {
@@ -324,23 +302,14 @@ char* InspSocket::Read()
        {
                int err = errno;
                if (err == EAGAIN)
-               {
                        return "";
-               }
                else
-               {
-                       if (!n)
-                               this->Instance->Log(DEBUG,"EOF or error on socket: EOF");
-                       else
-                               this->Instance->Log(DEBUG,"EOF or error on socket: %s",strerror(err));
                        return NULL;
-               }
        }
 }
 
 void InspSocket::MarkAsClosed()
 {
-       this->Instance->Log(DEBUG,"Marked as closed");
 }
 
 // There are two possible outcomes to this function.
@@ -367,11 +336,32 @@ bool InspSocket::FlushWriteBuffer()
                        {
                                try
                                {
-                                       Instance->Config->GetIOHook(this)->OnRawSocketWrite(this->fd, outbuffer[0].c_str(), outbuffer[0].length());
+                                       int result = Instance->Config->GetIOHook(this)->OnRawSocketWrite(this->fd, outbuffer[0].c_str(), outbuffer[0].length());
+                                       if (result > 0)
+                                       {
+                                               if ((unsigned int)result == outbuffer[0].length())
+                                               {
+                                                       outbuffer.pop_front();
+                                               }
+                                               else
+                                               {
+                                                       std::string temp = outbuffer[0].substr(result);
+                                                       outbuffer[0] = temp;
+                                                       errno = EAGAIN;
+                                               }
+                                       }
+                                       else if (((result == -1) && (errno != EAGAIN)) || (result == 0))
+                                       {
+                                               this->OnError(I_ERR_WRITE);
+                                               this->state = I_ERROR;
+                                               this->Instance->SE->DelFd(this);
+                                               this->Close();
+                                               return true;
+                                       }
                                }
-                               catch (ModuleException& modexcept)
+                               catch (CoreException& modexcept)
                                {
-                                       Instance->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
+                                       Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                                        return true;
                                }
                        }
@@ -410,7 +400,6 @@ bool InspSocket::FlushWriteBuffer()
                                }
                                else if ((result == -1) && (errno != EAGAIN))
                                {
-                                       this->Instance->Log(DEBUG,"Write error on socket: %s",strerror(errno));
                                        this->OnError(I_ERR_WRITE);
                                        this->state = I_ERROR;
                                        this->Instance->SE->DelFd(this);
@@ -432,14 +421,10 @@ bool InspSocket::FlushWriteBuffer()
 void SocketTimeout::Tick(time_t now)
 {
        if (ServerInstance->SE->GetRef(this->sfd) != this->sock)
-       {
-               ServerInstance->Log(DEBUG,"Our socket has been deleted before the timeout was reached.");
                return;
-       }
 
        if (this->sock->state == I_CONNECTING)
        {
-               ServerInstance->Log(DEBUG,"Timed out, current=%lu",now);
                // for non-listening sockets, the timeout can occur
                // which causes termination of the connection after
                // the given number of seconds without a successful
@@ -472,7 +457,6 @@ bool InspSocket::Poll()
        switch (this->state)
        {
                case I_CONNECTING:
-                       this->Instance->Log(DEBUG,"State = I_CONNECTING");
                        /* Our socket was in write-state, so delete it and re-add it
                         * in read-state.
                         */
@@ -483,15 +467,15 @@ bool InspSocket::Poll()
                                if (!this->Instance->SE->AddFd(this))
                                        return false;
                        }
-                       if (this->IsIOHooked)
+                       if (Instance->Config->GetIOHook(this))
                        {
                                try
                                {
                                        Instance->Config->GetIOHook(this)->OnRawSocketConnect(this->fd);
                                }
-                               catch (ModuleException& modexcept)
+                               catch (CoreException& modexcept)
                                {
-                                       Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+                                       Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                                }
                        }
                        return this->OnConnected();
@@ -500,6 +484,12 @@ bool InspSocket::Poll()
                        length = sizeof (client);
                        incoming = accept (this->fd, (sockaddr*)&client,&length);
 
+#ifdef IPV6
+                       this->OnIncomingConnection(incoming, (char*)insp_ntoa(client.sin6_addr));
+#else
+                       this->OnIncomingConnection(incoming, (char*)insp_ntoa(client.sin_addr));
+#endif
+
                        if (this->IsIOHooked)
                        {
                                try
@@ -510,18 +500,13 @@ bool InspSocket::Poll()
                                        Instance->Config->GetIOHook(this)->OnRawSocketAccept(incoming, insp_ntoa(client.sin_addr), this->port);
 #endif
                                }
-                               catch (ModuleException& modexcept)
+                               catch (CoreException& modexcept)
                                {
-                                       Instance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+                                       Instance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                                }
                        }
 
                        this->SetQueues(incoming);
-#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:
@@ -536,7 +521,6 @@ bool InspSocket::Poll()
 
 void InspSocket::SetState(InspSocketState s)
 {
-       this->Instance->Log(DEBUG,"Socket state change");
        this->state = s;
 }
 
@@ -608,7 +592,6 @@ void InspSocket::HandleEvent(EventType et, int errornum)
                        }
                        else
                        {
-                               Instance->Log(DEBUG,"State=%d CONNECTED=%d", this->state, I_CONNECTED);
                                if (this->FlushWriteBuffer())
                                {
                                        this->Instance->SE->DelFd(this);