]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/listensocket.cpp
Fix sqllog compile error
[user/henk/code/inspircd.git] / src / listensocket.cpp
index 823cb9ecacdf45565798c3869ce0ede9a8f779ef..f0daf8ef0cc549ba339dd97eb815a041810b5f6a 100644 (file)
 #include "socket.h"
 #include "socketengine.h"
 
-/* Private static member data must be declared in this manner */
-irc::sockets::sockaddrs ListenSocketBase::client;
-irc::sockets::sockaddrs ListenSocketBase::server;
-
-ListenSocketBase::ListenSocketBase(InspIRCd* Instance, int port, const std::string &addr) : ServerInstance(Instance), desc("plaintext")
+ListenSocket::ListenSocket(ConfigTag* tag, const std::string& addr, int port)
+       : bind_tag(tag)
 {
        irc::sockets::sockaddrs bind_to;
 
        // canonicalize address if it is defined
-       if (!irc::sockets::aptosa(addr.c_str(), port, &bind_to))
+       if (!irc::sockets::aptosa(addr, port, bind_to))
        {
-               // malformed address
-               bind_addr = addr;
-               bind_port = port;
-               bind_desc = addr + ":" + ConvToStr(port);
-               this->fd = -1;
+               fd = -1;
+               return;
        }
-       else
-       {
-               irc::sockets::satoap(&bind_to, bind_addr, bind_port);
-               bind_desc = irc::sockets::satouser(&bind_to);
+       irc::sockets::satoap(bind_to, bind_addr, bind_port);
+       bind_desc = irc::sockets::satouser(bind_to);
 
-               this->fd = irc::sockets::OpenTCPSocket(bind_addr.c_str());
-       }
+       fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);
 
        if (this->fd > -1)
        {
-               int rv = Instance->SE->Bind(this->fd, &bind_to.sa, sizeof(bind_to));
+               ServerInstance->SE->SetReuse(fd);
+               int rv = ServerInstance->SE->Bind(this->fd, bind_to);
                if (rv >= 0)
-                       rv = Instance->SE->Listen(this->fd, Instance->Config->MaxConn);
+                       rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);
 
                if (rv < 0)
                {
-                       Instance->SE->Shutdown(this, 2);
-                       Instance->SE->Close(this);
+                       ServerInstance->SE->Shutdown(this, 2);
+                       ServerInstance->SE->Close(this);
                        this->fd = -1;
                }
                else
                {
-                       Instance->SE->NonBlocking(this->fd);
-                       Instance->SE->AddFd(this);
+                       ServerInstance->SE->NonBlocking(this->fd);
+                       ServerInstance->SE->AddFd(this, FD_WANT_POLL_READ | FD_WANT_NO_WRITE);
                }
        }
 }
 
-ListenSocketBase::~ListenSocketBase()
+ListenSocket::~ListenSocket()
 {
        if (this->GetFd() > -1)
        {
@@ -75,18 +67,17 @@ ListenSocketBase::~ListenSocketBase()
 }
 
 /* Just seperated into another func for tidiness really.. */
-void ListenSocketBase::AcceptInternal()
+void ListenSocket::AcceptInternal()
 {
-       ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
-       int incomingSockfd;
+       irc::sockets::sockaddrs client;
+       irc::sockets::sockaddrs server;
 
        socklen_t length = sizeof(client);
-       incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
+       int incomingSockfd = ServerInstance->SE->Accept(this, &client.sa, &length);
 
+       ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket %s nfd=%d", bind_desc.c_str(), incomingSockfd);
        if (incomingSockfd < 0)
        {
-               ServerInstance->SE->Shutdown(incomingSockfd, 2);
-               ServerInstance->SE->Close(incomingSockfd);
                ServerInstance->stats->statsRefused++;
                return;
        }
@@ -147,11 +138,32 @@ void ListenSocketBase::AcceptInternal()
        }
 
        ServerInstance->SE->NonBlocking(incomingSockfd);
-       ServerInstance->stats->statsAccept++;
-       this->OnAcceptReady(incomingSockfd);
+
+       ModResult res;
+       FIRST_MOD_RESULT(OnAcceptConnection, res, (incomingSockfd, this, &client, &server));
+       if (res == MOD_RES_PASSTHRU)
+       {
+               std::string type = bind_tag->getString("type", "clients");
+               if (type == "clients")
+               {
+                       ServerInstance->Users->AddUser(incomingSockfd, this, &client, &server);
+                       res = MOD_RES_ALLOW;
+               }
+       }
+       if (res == MOD_RES_ALLOW)
+       {
+               ServerInstance->stats->statsAccept++;
+       }
+       else
+       {
+               ServerInstance->stats->statsRefused++;
+               ServerInstance->Logs->Log("SOCKET",DEFAULT,"Refusing connection on %s - %s",
+                       bind_desc.c_str(), res == MOD_RES_DENY ? "Connection refused by module" : "Module for this port not found");
+               ServerInstance->SE->Close(incomingSockfd);
+       }
 }
 
-void ListenSocketBase::HandleEvent(EventType e, int err)
+void ListenSocket::HandleEvent(EventType e, int err)
 {
        switch (e)
        {
@@ -166,8 +178,3 @@ void ListenSocketBase::HandleEvent(EventType e, int err)
                        break;
        }
 }
-
-void ClientListenSocket::OnAcceptReady(int nfd)
-{
-       ServerInstance->Users->AddUser(ServerInstance, nfd, this, &client, &server);
-}