]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspsocket.cpp
Fix potential for ssl to block, based on patch submitted by djGrrr, adjusted for...
[user/henk/code/inspircd.git] / src / inspsocket.cpp
index c0a77a28ddbf4bde85ecc7b212510bf0569106b7..dc0c5bebafc7e519f22252f9f683837de4066e83 100644 (file)
@@ -12,7 +12,6 @@
  */
 
 #include "socket.h"
-#include "configreader.h"
 #include "inspstring.h"
 #include "socketengine.h"
 #include "inspircd.h"
@@ -179,6 +178,7 @@ bool InspSocket::BindAddr(const std::string &ip)
                {
                        if (!ip.empty() || ((IP != "*") && (IP != "127.0.0.1") && (!IP.empty()) && (IP != "::1")))
                        {
+                               /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */
                                sockaddr* s = new sockaddr[2];
 #ifdef IPV6
                                if (v6)
@@ -186,7 +186,7 @@ bool InspSocket::BindAddr(const std::string &ip)
                                        in6_addr n;
                                        if (inet_pton(AF_INET6, IP.c_str(), &n) > 0)
                                        {
-                                               memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(n));
+                                               memcpy(&((sockaddr_in6*)s)->sin6_addr, &n, sizeof(sockaddr_in6));
                                                ((sockaddr_in6*)s)->sin6_port = 0;
                                                ((sockaddr_in6*)s)->sin6_family = AF_INET6;
                                                size = sizeof(sockaddr_in6);
@@ -216,7 +216,7 @@ bool InspSocket::BindAddr(const std::string &ip)
                                        }
                                }
 
-                               if (bind(this->fd, s, size) < 0)
+                               if (Instance->SE->Bind(this->fd, s, size) < 0)
                                {
                                        this->state = I_ERROR;
                                        this->OnError(I_ERR_BIND);
@@ -236,6 +236,7 @@ bool InspSocket::BindAddr(const std::string &ip)
 
 bool InspSocket::DoConnect()
 {
+       /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */
        sockaddr* addr = new sockaddr[2];
        socklen_t size = sizeof(sockaddr_in);
 #ifdef IPV6
@@ -300,14 +301,15 @@ bool InspSocket::DoConnect()
                        ((sockaddr_in*)addr)->sin_port = htons(this->port);
                }
        }
-#ifndef WIN32
-       int flags = fcntl(this->fd, F_GETFL, 0);
-       fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);
-#else
-       unsigned long flags = 0;
-       ioctlsocket(this->fd, FIONBIO, &flags);
+
+       Instance->SE->NonBlocking(this->fd);
+
+#ifdef WIN32
+       /* UGH for the LOVE OF ZOMBIE JESUS SOMEONE FIX THIS!!!!!!!!!!! */
+       Instance->SE->Blocking(this->fd);
 #endif
-       if (connect(this->fd, (sockaddr*)addr, size) == -1)
+
+       if (Instance->SE->Connect(this, (sockaddr*)addr, size) == -1)
        {
                if (errno != EINPROGRESS)
                {
@@ -321,9 +323,8 @@ bool InspSocket::DoConnect()
                this->Instance->Timers->AddTimer(this->Timeout);
        }
 #ifdef WIN32
-       /* Set nonblocking mode after the connect() call */
-       flags = 0;
-       ioctlsocket(this->fd, FIONBIO, &flags);
+       /* CRAQ SMOKING STUFF TO BE FIXED */
+       Instance->SE->NonBlocking(this->fd);
 #endif
        this->state = I_CONNECTING;
        if (this->fd > -1)
@@ -361,8 +362,8 @@ void InspSocket::Close()
                                Instance->Log(DEFAULT,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                        }
                }
-               shutdown(this->fd,2);
-               if (close(this->fd) != -1)
+               Instance->SE->Shutdown(this, 2);
+               if (Instance->SE->Close(this) != -1)
                        this->OnClose();
 
                if (Instance->SocketCull.find(this) == Instance->SocketCull.end())
@@ -474,11 +475,8 @@ bool InspSocket::FlushWriteBuffer()
                        while (outbuffer.size() && (errno != EAGAIN))
                        {
                                /* Send a line */
-#ifndef WIN32
-                               int result = write(this->fd,outbuffer[0].c_str(),outbuffer[0].length());
-#else
-                               int result = send(this->fd,outbuffer[0].c_str(),outbuffer[0].length(), 0);
-#endif
+                               int result = Instance->SE->Send(this, outbuffer[0].c_str(), outbuffer[0].length(), 0);
+
                                if (result > 0)
                                {
                                        if ((unsigned int)result >= outbuffer[0].length())
@@ -502,6 +500,12 @@ bool InspSocket::FlushWriteBuffer()
                                                errno = EAGAIN;
                                        }
                                }
+                               else if (result == 0)
+                               {
+                                       this->Instance->SE->DelFd(this);
+                                       this->Close();
+                                       return true;
+                               }
                                else if ((result == -1) && (errno != EAGAIN))
                                {
                                        this->OnError(I_ERR_WRITE);
@@ -594,6 +598,7 @@ bool InspSocket::Poll()
                break;
                case I_LISTENING:
                {
+                       /* The [2] is required because we may write a sockaddr_in6 here, and sockaddr_in6 is larger than sockaddr, where sockaddr_in4 is not. */
                        sockaddr* client = new sockaddr[2];
                        length = sizeof (sockaddr_in);
                        std::string recvip;
@@ -601,7 +606,7 @@ bool InspSocket::Poll()
                        if ((!*this->host) || strchr(this->host, ':'))
                                length = sizeof(sockaddr_in6);
 #endif
-                       incoming = _accept (this->fd, client, &length);
+                       incoming = Instance->SE->Accept(this, client, &length);
 #ifdef IPV6
                        if ((!*this->host) || strchr(this->host, ':'))
                        {
@@ -610,6 +615,8 @@ bool InspSocket::Poll()
                        }
                        else
 #endif
+                       Instance->SE->NonBlocking(incoming);
+
                        recvip = inet_ntoa(((sockaddr_in*)client)->sin_addr);
                        this->OnIncomingConnection(incoming, (char*)recvip.c_str());