]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/socket.cpp
Spellig mistak.
[user/henk/code/inspircd.git] / src / socket.cpp
index 560541f4d0d724e64b6471c72452b2e5b4028151..472d58c2e91cd731a6114b2dc7e9e6a5673aac12 100644 (file)
@@ -2,14 +2,11 @@
  *       | 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.
+ *            the file COPYING for details.
  *
  * ---------------------------------------------------
  */
@@ -21,7 +18,6 @@
 #include "socketengine.h"
 #include "wildcard.h"
 
-using namespace std;
 using namespace irc::sockets;
 
 /* Used when comparing CIDR masks for the modulus bits left over.
@@ -40,13 +36,19 @@ const char inverted_bits[8] = {     0x00, /* 00000000 - 0 bits - never actually used
 };
 
 
-ListenSocket::ListenSocket(InspIRCd* Instance, int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr) : ServerInstance(Instance)
+ListenSocket::ListenSocket(InspIRCd* Instance, int sockfd, insp_sockaddr client, insp_sockaddr server, int port, char* addr) : ServerInstance(Instance), desc("plaintext")
 {
        this->SetFd(sockfd);
-       Instance->Log(DEBUG,"Binding to port %s:%d",addr,port);
        if (!Instance->BindSocket(this->fd,client,server,port,addr))
+               this->fd = -1;
+}
+
+ListenSocket::~ListenSocket()
+{
+       if (this->GetFd() > -1)
        {
-               Instance->Log(DEBUG,"Binding failed!");
+               shutdown(this->fd, 2);
+               close(this->fd);
                this->fd = -1;
        }
 }
@@ -58,9 +60,6 @@ void ListenSocket::HandleEvent(EventType et, int errornum)
        insp_sockaddr client;
        socklen_t length;
        int incomingSockfd, in_port;
-
-       ServerInstance->Log(DEBUG,"Handle ListenSocket event");
-
        uslen = sizeof(sock_us);
        length = sizeof(client);
        incomingSockfd = accept (this->GetFd(),(struct sockaddr*)&client, &length);
@@ -72,7 +71,6 @@ void ListenSocket::HandleEvent(EventType et, int errornum)
 #else
                in_port = ntohs(sock_us.sin_port);
 #endif
-               ServerInstance->Log(DEBUG,"Accepted socket %d",incomingSockfd);
                NonBlocking(incomingSockfd);
                if (ServerInstance->Config->GetIOHook(in_port))
                {
@@ -84,24 +82,20 @@ void ListenSocket::HandleEvent(EventType et, int errornum)
                                ServerInstance->Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, insp_ntoa(client.sin_addr), in_port);
 #endif
                        }
-                       catch (ModuleException& modexcept)
+                       catch (CoreException& modexcept)
                        {
-                               ServerInstance->Log(DEBUG,"Module exception cought: %s",modexcept.GetReason());
+                               ServerInstance->Log(DEBUG,"%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
                        }
                }
                ServerInstance->stats->statsAccept++;
 #ifdef IPV6
-               ServerInstance->Log(DEBUG,"Add ipv6 client");
                userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, client.sin6_addr);
 #else
-               ServerInstance->Log(DEBUG,"Add ipv4 client");
                userrec::AddClient(ServerInstance, incomingSockfd, in_port, false, client.sin_addr);
 #endif
-               ServerInstance->Log(DEBUG,"Adding client on port %d fd=%d",in_port,incomingSockfd);
        }
        else
        {
-               ServerInstance->Log(DEBUG,"Accept failed on fd %d: %s",incomingSockfd,strerror(errno));
                shutdown(incomingSockfd,2);
                close(incomingSockfd);
                ServerInstance->stats->statsRefused++;
@@ -275,19 +269,18 @@ bool irc::sockets::MatchCIDR(const char* address, const char* cidr_mask, bool ma
        return MatchCIDRBits(addr_raw, mask_raw, bits);
 }
 
-inline void irc::sockets::Blocking(int s)
+void irc::sockets::Blocking(int s)
 {
        int flags = fcntl(s, F_GETFL, 0);
        fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
 }
 
-inline void irc::sockets::NonBlocking(int s)
+void irc::sockets::NonBlocking(int s)
 {
        int flags = fcntl(s, F_GETFL, 0);
        fcntl(s, F_SETFL, flags | O_NONBLOCK);
 }
 
-
 /** 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'.
@@ -301,10 +294,7 @@ bool InspIRCd::BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server
                *addr = 0;
 
        if ((*addr) && (insp_aton(addr,&addy) < 1))
-       {
-               this->Log(DEBUG,"Invalid IP '%s' given to BindSocket()", addr);
                return false;;
-       }
 
 #ifdef IPV6
        server.sin6_family = AF_FAMILY;
@@ -338,7 +328,6 @@ bool InspIRCd::BindSocket(int sockfd, insp_sockaddr client, insp_sockaddr server
        }
        else
        {
-               this->Log(DEBUG,"Bound port %s:%d",*addr ? addr : "*",port);
                if (listen(sockfd, Config->MaxConn) == -1)
                {
                        this->Log(DEFAULT,"ERROR in listen(): %s",strerror(errno));
@@ -389,7 +378,7 @@ bool InspIRCd::HasPort(int port, char* addr)
 }
 
 /* XXX: Probably belongs in class InspIRCd */
-int InspIRCd::BindPorts(bool bail, int &ports_found)
+int InspIRCd::BindPorts(bool bail, int &ports_found, FailedPortList &failed_ports)
 {
        char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
        insp_sockaddr client, server;
@@ -399,7 +388,6 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
        ports_found = 0;
 
        int InitialPortCount = stats->BoundPortCount;
-       this->Log(DEBUG,"Initial port count: %d",InitialPortCount);
 
        for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "bind"); count++)
        {
@@ -409,55 +397,19 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
 
                if ((!*Type) || (!strcmp(Type,"clients")))
                {
-                       irc::commasepstream portrange(configToken);
-                       std::string portno = "*";
-                       while ((portno = portrange.GetToken()) != "")
+                       irc::portparser portrange(configToken, false);
+                       long portno = -1;
+                       while ((portno = portrange.GetToken()))
                        {
-                               std::string::size_type dash = portno.rfind('-');
-                               if (dash != std::string::npos)
-                               {
-                                       this->Log(DEBUG,"Port range, %s", portno.c_str());
-                                       /* Range of ports */
-                                       std::string sbegin = portno.substr(0, dash);
-                                       std::string send = portno.substr(dash+1, portno.length());
-                                       long begin = atoi(sbegin.c_str());
-                                       long end = atoi(send.c_str());
-                                       if ((begin < 0) || (end < 0) || (begin > 65535) || (end > 65535) || (begin >= end))
-                                       {
-                                               this->Log(DEFAULT,"WARNING: Port range \"%d-%d\" discarded. begin >= end, or begin/end out of range.", begin, end);
-                                       }
-                                       else
-                                       {
-                                               for (int portval = begin; portval <= end; ++portval)
-                                               {
-                                                       if (!HasPort(portval, Addr))
-                                                       {
-                                                               ports_found++;
-                                                               Config->ports[clientportcount+InitialPortCount] = portval;
-                                                               if (*Addr == '*')
-                                                                       *Addr = 0;
-
-                                                               strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
-                                                               clientportcount++;
-                                                               this->Log(DEBUG,"NEW binding %s:%d [%s] from config (part of port range %s)",Addr, portval, Type, portno.c_str());
-                                                       }
-                                               }
-                                       }
-                               }
-                               else
+                               if (!HasPort(portno, Addr))
                                {
-                                       this->Log(DEBUG,"Single port, %s", portno.c_str());
-                                       /* Single port */
-                                       if (!HasPort(atoi(portno.c_str()), Addr))
-                                       {
-                                               ports_found++;
-                                               Config->ports[clientportcount+InitialPortCount] = atoi(portno.c_str());
-                                               if (*Addr == '*')
-                                                       *Addr = 0;
-                                               strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
-                                               clientportcount++;
-                                               this->Log(DEBUG,"NEW binding %s:%s [%s] from config (single port)",Addr, portno.c_str(), Type);
-                                       }
+                                       ports_found++;
+                                       Config->ports[clientportcount+InitialPortCount] = portno;
+                                       if (*Addr == '*')
+                                               *Addr = 0;
+
+                                       strlcpy(Config->addrs[clientportcount+InitialPortCount],Addr,256);
+                                       clientportcount++;
                                }
                        }
                }
@@ -473,7 +425,7 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
                                        int fd = OpenTCPSocket();
                                        if (fd == ERROR)
                                        {
-                                               this->Log(DEBUG,"Bad fd %d binding port [%s:%d]",fd,Config->addrs[count],Config->ports[count]);
+                                               failed_ports.push_back(std::make_pair(Config->addrs[count],Config->ports[count]));
                                        }
                                        else
                                        {
@@ -486,6 +438,7 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
                                                                shutdown(Config->openSockfd[BoundPortCount]->GetFd(),2);
                                                                close(Config->openSockfd[BoundPortCount]->GetFd());
                                                                delete Config->openSockfd[BoundPortCount];
+                                                               failed_ports.push_back(std::make_pair(Config->addrs[count],Config->ports[count]));
                                                        }
                                                        else
                                                                BoundPortCount++;
@@ -494,10 +447,6 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
                                }
                                return InitialPortCount + BoundPortCount;
                        }
-                       else
-                       {
-                               this->Log(DEBUG,"There is nothing new to bind!");
-                       }
                        return InitialPortCount;
                }
        }
@@ -509,7 +458,7 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
                int fd = OpenTCPSocket();
                if (fd == ERROR)
                {
-                       this->Log(DEBUG,"Bad fd %d binding port [%s:%d]",fd,Config->addrs[count],Config->ports[count]);
+                       failed_ports.push_back(std::make_pair(Config->addrs[count],Config->ports[count]));
                }
                else
                {
@@ -518,6 +467,8 @@ int InspIRCd::BindPorts(bool bail, int &ports_found)
                        {
                                BoundPortCount++;
                        }
+                       else
+                               failed_ports.push_back(std::make_pair(Config->addrs[count],Config->ports[count]));
                }
        }
        return BoundPortCount;