]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/listensocket.cpp
Change SERVICE_{CMODE,UMODE} to SERVICE_MODE, which makes more sense
[user/henk/code/inspircd.git] / src / listensocket.cpp
index 452925bf3a7af97af02e4f1ccf826c17d0689a39..b0fce7b5b4d475cac50b713f269ab7316fae2476 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
  * See: http://wiki.inspircd.org/Credits
  *
  * This program is free but copyrighted software; see
 #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 irc::sockets::sockaddrs& bind_to)
+       : bind_tag(tag)
 {
-       irc::sockets::sockaddrs bind_to;
+       irc::sockets::satoap(bind_to, bind_addr, bind_port);
+       bind_desc = irc::sockets::satouser(bind_to);
+
+       fd = socket(bind_to.sa.sa_family, SOCK_STREAM, 0);
 
-       bind_addr = addr;
-       bind_port = port;
+       if (this->fd == -1)
+               return;
 
-       // canonicalize address if it is defined
-       if (!addr.empty() && irc::sockets::aptosa(addr.c_str(), port, &bind_to))
-               irc::sockets::satoap(&bind_to, bind_addr, bind_port);
+       ServerInstance->SE->SetReuse(fd);
+       int rv = ServerInstance->SE->Bind(this->fd, bind_to);
+       if (rv >= 0)
+               rv = ServerInstance->SE->Listen(this->fd, ServerInstance->Config->MaxConn);
 
-       this->SetFd(irc::sockets::OpenTCPSocket(bind_addr.c_str()));
-       if (this->GetFd() > -1)
+       if (rv < 0)
+       {
+               int errstore = errno;
+               ServerInstance->SE->Shutdown(this, 2);
+               ServerInstance->SE->Close(this);
+               this->fd = -1;
+               errno = errstore;
+       }
+       else
        {
-               if (!Instance->BindSocket(this->fd,port,bind_addr.c_str()))
-                       this->fd = -1;
-               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)
        {
@@ -54,25 +61,27 @@ 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;
        }
 
        socklen_t sz = sizeof(server);
        if (getsockname(incomingSockfd, &server.sa, &sz))
+       {
                ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
+               irc::sockets::aptosa(bind_addr, bind_port, server);
+       }
 
        /*
         * XXX -
@@ -125,18 +134,33 @@ void ListenSocketBase::AcceptInternal()
                }
        }
 
-       std::string server_addr;
-       std::string client_addr;
-       int dummy_port;
-       irc::sockets::satoap(&server, server_addr, dummy_port);
-       irc::sockets::satoap(&client, client_addr, dummy_port);
-
        ServerInstance->SE->NonBlocking(incomingSockfd);
-       ServerInstance->stats->statsAccept++;
-       this->OnAcceptReady(server_addr, incomingSockfd, client_addr);
+
+       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)
        {
@@ -151,8 +175,3 @@ void ListenSocketBase::HandleEvent(EventType e, int err)
                        break;
        }
 }
-
-void ClientListenSocket::OnAcceptReady(const std::string &ipconnectedto, int nfd, const std::string &incomingip)
-{
-       ServerInstance->Users->AddUser(ServerInstance, nfd, bind_port, false, &client, ipconnectedto);
-}