]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Stuff to make dns work protocol-independent
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index 6987d5ccd5c929c6c1797ae1a7b8f03ad49bf897..08b8f4cd698c2fa907b9d0f41494fd059c9210ff 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.
  *
@@ -59,19 +56,17 @@ InspSocket::InspSocket(InspIRCd* SI, const std::string &ipaddr, int aport, bool
        this->IsIOHooked = false;
        if (listening)
        {
-               if ((this->fd = OpenTCPSocket()) == ERROR)
+               if ((this->fd = OpenTCPSocket(host)) == ERROR)
                {
                        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()))
+                       if (!SI->BindSocket(this->fd,aport,(char*)ipaddr.c_str()))
                        {
-                               this->Instance->Log(DEBUG,"BindSocket() error %s",strerror(errno));
                                this->Close();
                                this->fd = -1;
                                this->state = I_ERROR;
@@ -91,10 +86,9 @@ 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;
                        }
-               }                       
+               }
        }
        else
        {
@@ -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,23 +140,20 @@ 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);
                std::string IP = Conf.ReadValue("bind","address",j);
                if (Type == "servers")
                {
-                       if ((IP != "*") && (IP != "127.0.0.1") && (IP != ""))
+                       if ((IP != "*") && (IP != "127.0.0.1") && (IP != "") && (IP != "::1"))
                        {
                                insp_sockaddr s;
 
                                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;
+                                       memcpy(&s.sin6_addr, &n, sizeof(n));
                                        s.sin6_family = AF_FAMILY;
 #else
                                        s.sin_addr = n;
@@ -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,11 +189,10 @@ 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;
-       memcpy(&addr.sin6_addr, &addy, sizeof(insp_inaddr));
+       memcpy(&addr.sin6_addr, &addy, sizeof(addy));
        addr.sin6_port = htons(this->port);
 #else
        addr.sin_family = AF_FAMILY;
@@ -220,15 +200,13 @@ bool InspSocket::DoConnect()
        addr.sin_port = htons(this->port);
 #endif
 
-       int flags;
-       flags = fcntl(this->fd, F_GETFL, 0);
+       int 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)
                {
-                       this->Instance->Log(DEBUG,"Error connect() %d: %s",this->fd,strerror(errno));
                        this->OnError(I_ERR_CONNECT);
                        this->Close();
                        this->state = I_ERROR;
@@ -250,13 +228,17 @@ bool InspSocket::DoConnect()
                }
                this->SetQueues(this->fd);
        }
-       this->Instance->Log(DEBUG,"Returning true from InspSocket::DoConnect");
        return true;
 }
 
 
 void InspSocket::Close()
 {
+       /* Save this, so we dont lose it,
+        * otherise on failure, error messages
+        * might be inaccurate.
+        */
+       int save = errno;
        if (this->fd > -1)
        {
                 if (this->IsIOHooked && Instance->Config->GetIOHook(this))
@@ -265,15 +247,16 @@ void InspSocket::Close()
                        {
                                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();
                shutdown(this->fd,2);
                close(this->fd);
        }
+       errno = save;
 }
 
 std::string InspSocket::GetIP()
@@ -296,9 +279,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 +307,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.
@@ -381,9 +355,8 @@ bool InspSocket::FlushWriteBuffer()
                                                        errno = EAGAIN;
                                                }
                                        }
-                                       else if ((result == -1) && (errno != EAGAIN))
+                                       else if (((result == -1) && (errno != EAGAIN)) || (result == 0))
                                        {
-                                               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);
@@ -391,9 +364,9 @@ bool InspSocket::FlushWriteBuffer()
                                                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;
                                }
                        }
@@ -432,7 +405,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);
@@ -454,14 +426,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
@@ -494,7 +462,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.
                         */
@@ -511,9 +478,9 @@ bool InspSocket::Poll()
                                {
                                        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();
@@ -538,9 +505,9 @@ 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());
                                }
                        }
 
@@ -559,7 +526,6 @@ bool InspSocket::Poll()
 
 void InspSocket::SetState(InspSocketState s)
 {
-       this->Instance->Log(DEBUG,"Socket state change");
        this->state = s;
 }
 
@@ -631,7 +597,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);