]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd_io.cpp
Signal handlers were not being set when -nofork was enabled
[user/henk/code/inspircd.git] / src / inspircd_io.cpp
index c435de38398e29bf66126e32a8147b973c631e6e..dd5b146922c11e32278e4787c038aabdd66828d4 100644 (file)
@@ -27,13 +27,12 @@ using namespace std;
 #include <fstream>
 #include "inspircd.h"
 #include "inspircd_io.h"
-#include "inspircd_util.h"
 #include "inspstring.h"
 #include "helperfuncs.h"
 #include "xline.h"
 
 extern ServerConfig *Config;
-extern int boundPortCount;
+extern InspIRCd* ServerInstance;
 extern int openSockfd[MAXSOCKS];
 extern time_t TIME;
 
@@ -64,6 +63,32 @@ void ServerConfig::ClearStack()
        include_stack.clear();
 }
 
+Module* ServerConfig::GetIOHook(int port)
+{
+       std::map<int,Module*>::iterator x = IOHookModule.find(port);
+       return (x != IOHookModule.end() ? x->second : NULL);
+}
+
+bool ServerConfig::AddIOHook(int port, Module* iomod)
+{
+       if (!GetIOHook(port))
+       {
+               IOHookModule[port] = iomod;
+               return true;
+       }
+       return false;
+}
+
+bool ServerConfig::DelIOHook(int port)
+{
+       std::map<int,Module*>::iterator x = IOHookModule.find(port);
+       if (x != IOHookModule.end())
+       {
+               IOHookModule.erase(x);
+               return true;
+       }
+       return false;
+}
 
 void ServerConfig::Read(bool bail, userrec* user)
 {
@@ -186,7 +211,7 @@ void ServerConfig::Read(bool bail, userrec* user)
         Classes.clear();
         for (int i = 0; i < ConfValueEnum("connect",&Config->config_f); i++)
         {
-                strcpy(Value,"");
+                *Value = 0;
                 ConfValue("connect","allow",i,Value,&Config->config_f);
                 ConfValue("connect","timeout",i,timeout,&Config->config_f);
                 ConfValue("connect","flood",i,flood,&Config->config_f);
@@ -196,11 +221,11 @@ void ServerConfig::Read(bool bail, userrec* user)
                 ConfValue("connect","recvq",i,rqmax,&Config->config_f);
                 if (*Value)
                 {
-                        strlcpy(c.host,Value,MAXBUF);
+                        c.host = Value;
                         c.type = CC_ALLOW;
                         strlcpy(Value,"",MAXBUF);
                         ConfValue("connect","password",i,Value,&Config->config_f);
-                        strlcpy(c.pass,Value,MAXBUF);
+                        c.pass = Value;
                         c.registration_timeout = 90; // default is 2 minutes
                         c.pingtime = 120;
                         c.flood = atoi(flood);
@@ -232,10 +257,10 @@ void ServerConfig::Read(bool bail, userrec* user)
                 else
                 {
                         ConfValue("connect","deny",i,Value,&Config->config_f);
-                        strlcpy(c.host,Value,MAXBUF);
+                        c.host = Value;
                         c.type = CC_DENY;
                         Classes.push_back(c);
-                        log(DEBUG,"Read connect class type DENY, host=%s",c.host);
+                        log(DEBUG,"Read connect class type DENY, host=%s",c.host.c_str());
                 }
 
         }
@@ -298,7 +323,7 @@ void ServerConfig::Read(bool bail, userrec* user)
                 if (!removed_modules.empty())
                 for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
                 {
-                        if (UnloadModule(removing->c_str()))
+                        if (ServerInstance->UnloadModule(removing->c_str()))
                         {
                                 WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
                                 WriteServ(user->fd,"973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
@@ -306,13 +331,13 @@ void ServerConfig::Read(bool bail, userrec* user)
                         }
                         else
                         {
-                                WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ModuleError());
+                                WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
                         }
                 }
                 if (!added_modules.empty())
                 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
                 {
-                        if (LoadModule(adding->c_str()))
+                        if (ServerInstance->LoadModule(adding->c_str()))
                         {
                                 WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
                                 WriteServ(user->fd,"975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
@@ -320,7 +345,7 @@ void ServerConfig::Read(bool bail, userrec* user)
                         }
                         else
                         {
-                                WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ModuleError());
+                                WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
                         }
                 }
                 log(DEFAULT,"Successfully unloaded %lu of %lu modules and loaded %lu of %lu modules.",(unsigned long)rem,(unsigned long)removed_modules.size(),
@@ -329,8 +354,6 @@ void ServerConfig::Read(bool bail, userrec* user)
 }
 
 
-void WriteOpers(char* text, ...);
-
 void Exit (int status)
 {
        if (Config->log_file)
@@ -381,15 +404,19 @@ void WritePID(std::string filename)
        }
 }
 
-
-int DaemonSeed (void)
+void SetSignals()
 {
-       int childpid;
        signal (SIGALRM, SIG_IGN);
        signal (SIGHUP, Rehash);
        signal (SIGPIPE, SIG_IGN);
        signal (SIGTERM, Exit);
        signal (SIGSEGV, Error);
+}
+
+
+int DaemonSeed (void)
+{
+       int childpid;
        if ((childpid = fork ()) < 0)
                return (ERROR);
        else if (childpid > 0)
@@ -913,18 +940,18 @@ int ServerConfig::ReadConf(std::stringstream *config, const char* tag, const cha
                                                if (!key)
                                                {
                                                        /* value not found in tag */
-                                                       strcpy(result,"");
+                                                       *result = 0;
                                                        return 0;
                                                }
                                                else
                                                {
                                                        key+=strlen(var);
-                                                       while (key[0] !='"')
+                                                       while (*key !='"')
                                                        {
-                                                               if (!strlen(key))
+                                                               if (!*key)
                                                                {
                                                                        /* missing quote */
-                                                                       strcpy(result,"");
+                                                                       *result = 0;
                                                                        return 0;
                                                                }
                                                                key++;
@@ -962,7 +989,7 @@ int ServerConfig::ReadConf(std::stringstream *config, const char* tag, const cha
                        }
                }
        }
-       strcpy(result,""); // value or its tag not found at all
+       *result = 0; // value or its tag not found at all
        return 0;
 }
 
@@ -1023,3 +1050,55 @@ int OpenTCPSocket (void)
                return (sockfd);
        }
 }
+
+int BindPorts()
+{
+        char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
+       sockaddr_in client,server;
+        int clientportcount = 0;
+       int BoundPortCount = 0;
+        for (int count = 0; count < Config->ConfValueEnum("bind",&Config->config_f); count++)
+        {
+                Config->ConfValue("bind","port",count,configToken,&Config->config_f);
+                Config->ConfValue("bind","address",count,Addr,&Config->config_f);
+                Config->ConfValue("bind","type",count,Type,&Config->config_f);
+                if (strcmp(Type,"servers"))
+                {
+                        // modules handle server bind types now,
+                        // its not a typo in the strcmp.
+                        Config->ports[clientportcount] = atoi(configToken);
+                        strlcpy(Config->addrs[clientportcount],Addr,256);
+                        clientportcount++;
+                        log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
+                }
+        }
+        int PortCount = clientportcount;
+
+        for (int count = 0; count < PortCount; count++)
+        {
+                if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
+                {
+                        log(DEBUG,"InspIRCd: startup: bad fd %lu",(unsigned long)openSockfd[BoundPortCount]);
+                        return(ERROR);
+                }
+                if (BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]) == ERROR)
+                {
+                        log(DEFAULT,"InspIRCd: startup: failed to bind port %lu",(unsigned long)Config->ports[count]);
+                }
+                else    /* well we at least bound to one socket so we'll continue */
+                {
+                        BoundPortCount++;
+                }
+        }
+
+        /* if we didn't bind to anything then abort */
+        if (!BoundPortCount)
+        {
+                log(DEFAULT,"InspIRCd: startup: no ports bound, bailing!");
+                printf("\nERROR: Was not able to bind any of %lu ports! Please check your configuration.\n\n", (unsigned long)PortCount);
+                return (ERROR);
+        }
+
+        return BoundPortCount;
+}
+