]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socket.cpp
Update comment that says we dont send simplemodes in FJOIN, we do now.
[user/henk/code/inspircd.git] / src / socket.cpp
index a599631a40e02f6c506cdd4a49ac1ec1f1505688..d2090c3a2542a3c752a816d833589d09c9fa6f8b 100644 (file)
  * ---------------------------------------------------
  */
 
-/* $Core: libIRCDsocket */
+/* $Core */
 
 #include "inspircd.h"
 #include "socket.h"
 #include "socketengine.h"
 
-/* Private static member data must be initialized in this manner */
-unsigned int ListenSocket::socketcount = 0;
-sockaddr* ListenSocket::sock_us = NULL;
-sockaddr* ListenSocket::client = NULL;
-sockaddr* ListenSocket::raddr = NULL;
-
-ListenSocket::ListenSocket(InspIRCd* Instance, int port, char* addr) : ServerInstance(Instance), desc("plaintext"), bind_addr(addr), bind_port(port)
-{
-       this->SetFd(irc::sockets::OpenTCPSocket(addr));
-       if (this->GetFd() > -1)
-       {
-               if (!Instance->BindSocket(this->fd,port,addr))
-                       this->fd = -1;
-#ifdef IPV6
-               if ((!*addr) || (strchr(addr,':')))
-                       this->family = AF_INET6;
-               else
-#endif
-               this->family = AF_INET;
-               Instance->SE->AddFd(this);
-       }
-       /* Saves needless allocations */
-       if (socketcount == 0)
-       {
-               /* All instances of ListenSocket share these, so reference count it */
-               ServerInstance->Logs->Log("SOCKET", DEBUG,"Allocate sockaddr structures");
-               sock_us = new sockaddr[2];
-               client = new sockaddr[2];
-               raddr = new sockaddr[2];
-       }
-       socketcount++;
-}
-
-ListenSocket::~ListenSocket()
-{
-       if (this->GetFd() > -1)
-       {
-               ServerInstance->SE->DelFd(this);
-               ServerInstance->Logs->Log("SOCKET", DEBUG,"Shut down listener on fd %d", this->fd);
-               if (ServerInstance->SE->Shutdown(this, 2) || ServerInstance->SE->Close(this))
-                       ServerInstance->Logs->Log("SOCKET", DEBUG,"Failed to cancel listener: %s", strerror(errno));
-               this->fd = -1;
-       }
-       socketcount--;
-       if (socketcount == 0)
-       {
-               delete[] sock_us;
-               delete[] client;
-               delete[] raddr;
-       }
-}
-
-void ListenSocket::HandleEvent(EventType e, int err)
-{
-       switch (e)
-       {
-               case EVENT_ERROR:
-                       ServerInstance->Logs->Log("SOCKET",DEFAULT,"ListenSocket::HandleEvent() received a socket engine error event! well shit! '%s'", strerror(err));
-               break;
-               case EVENT_WRITE:
-                       ServerInstance->Logs->Log("SOCKET",DEBUG,"*** BUG *** ListenSocket::HandleEvent() got a WRITE event!!!");
-               break;
-               case EVENT_READ:
-               {
-                       ServerInstance->Logs->Log("SOCKET",DEBUG,"HandleEvent for Listensoket");
-                       socklen_t uslen, length;                // length of our port number
-                       int incomingSockfd, in_port;
-
-#ifdef IPV6
-                       if (this->family == AF_INET6)
-                       {
-                               uslen = sizeof(sockaddr_in6);
-                               length = sizeof(sockaddr_in6);
-                       }
-                       else
-#endif
-                       {
-                               uslen = sizeof(sockaddr_in);
-                               length = sizeof(sockaddr_in);
-                       }
-
-                       incomingSockfd = ServerInstance->SE->Accept(this, (sockaddr*)client, &length);
-
-                       if ((incomingSockfd > -1) && (!ServerInstance->SE->GetSockName(this, sock_us, &uslen)))
-                       {
-                               char buf[MAXBUF];
-                               char target[MAXBUF];    
-
-                               *target = *buf = '\0';
-
-#ifdef IPV6
-                               if (this->family == AF_INET6)
-                               {
-                                       in_port = ntohs(((sockaddr_in6*)sock_us)->sin6_port);
-                                       inet_ntop(AF_INET6, &((const sockaddr_in6*)client)->sin6_addr, buf, sizeof(buf));
-                                       socklen_t raddrsz = sizeof(sockaddr_in6);
-                                       if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
-                                               inet_ntop(AF_INET6, &((const sockaddr_in6*)raddr)->sin6_addr, target, sizeof(target));
-                                       else
-                                               ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
-                               }
-                               else
-#endif
-                               {
-                                       inet_ntop(AF_INET, &((const sockaddr_in*)client)->sin_addr, buf, sizeof(buf));
-                                       in_port = ntohs(((sockaddr_in*)sock_us)->sin_port);
-                                       socklen_t raddrsz = sizeof(sockaddr_in);
-                                       if (getpeername(incomingSockfd, (sockaddr*) raddr, &raddrsz) == 0)
-                                               inet_ntop(AF_INET, &((const sockaddr_in*)raddr)->sin_addr, target, sizeof(target));
-                                       else
-                                               ServerInstance->Logs->Log("SOCKET", DEBUG, "Can't get peername: %s", strerror(errno));
-                               }
-
-                               ServerInstance->SE->NonBlocking(incomingSockfd);
-                               ServerInstance->stats->statsAccept++;
-                               ServerInstance->Users->AddUser(ServerInstance, incomingSockfd, in_port, false, this->family, client, target);   
-                       }
-                       else
-                       {
-                               ServerInstance->SE->Shutdown(incomingSockfd, 2);
-                               ServerInstance->SE->Close(incomingSockfd);
-                               ServerInstance->stats->statsRefused++;
-                       }
-               }
-               break;
-       }
-}
-
 /** This will bind a socket to a port. It works for UDP/TCP.
  * It can only bind to IP addresses, if you wish to bind to hostnames
  * you should first resolve them using class 'Resolver'.
@@ -271,7 +143,7 @@ bool InspIRCd::BindSocket(int sockfd, int port, const char* addr, bool dolisten)
 }
 
 // Open a TCP Socket
-int irc::sockets::OpenTCPSocket(char* addr, int socktype)
+int irc::sockets::OpenTCPSocket(const char* addr, int socktype)
 {
        int sockfd;
        int on = 1;
@@ -300,6 +172,7 @@ int irc::sockets::OpenTCPSocket(char* addr, int socktype)
        }
 }
 
+// XXX: it would be VERY nice to genericize this so all listen stuff (server/client) could use the one function. -- w00t
 int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
 {
        char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
@@ -308,7 +181,7 @@ int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
        std::vector<std::pair<std::string, int> > old_ports;
 
        /* XXX: Make a copy of the old ip/port pairs here */
-       for (std::vector<ListenSocket*>::iterator o = Config->ports.begin(); o != Config->ports.end(); ++o)
+       for (std::vector<ListenSocketBase *>::iterator o = Config->ports.begin(); o != Config->ports.end(); ++o)
                old_ports.push_back(make_pair((*o)->GetIP(), (*o)->GetPort()));
 
        for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
@@ -330,7 +203,7 @@ int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
                                        *Addr = 0;
 
                                bool skip = false;
-                               for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
+                               for (std::vector<ListenSocketBase *>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
                                {
                                        if (((*n)->GetIP() == Addr) && ((*n)->GetPort() == portno))
                                        {
@@ -348,7 +221,7 @@ int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
                                }
                                if (!skip)
                                {
-                                       ListenSocket* ll = new ListenSocket(this, portno, Addr);
+                                       ClientListenSocket *ll = new ClientListenSocket(this, portno, Addr);
                                        if (ll->GetFd() > -1)
                                        {
                                                bound++;
@@ -356,7 +229,7 @@ int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
                                        }
                                        else
                                        {
-                                               failed_ports.push_back(std::make_pair(Addr, portno));
+                                               failed_ports.push_back(std::make_pair((*Addr ? Addr : "*") + std::string(":") + ConvToStr(portno), strerror(errno)));
                                        }
                                        ports_found++;
                                }
@@ -369,7 +242,7 @@ int InspIRCd::BindPorts(bool, int &ports_found, FailedPortList &failed_ports)
        {
                for (size_t k = 0; k < old_ports.size(); ++k)
                {
-                       for (std::vector<ListenSocket*>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
+                       for (std::vector<ListenSocketBase *>::iterator n = Config->ports.begin(); n != Config->ports.end(); ++n)
                        {
                                if (((*n)->GetIP() == old_ports[k].first) && ((*n)->GetPort() == old_ports[k].second))
                                {