]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
kick_channel -> chanrec::KickUser(), server_kick_channel -> chanrec::ServerKickUser()
[user/henk/code/inspircd.git] / src / inspircd.cpp
index 2f08750bd9b0ec4af399fbaf044ae9624be36f43..27029a6e4b18344e90fd73af88ee1703f05d802e 100644 (file)
@@ -45,9 +45,6 @@
 #include <fstream>
 #include <vector>
 #include <deque>
-#ifdef THREADED_DNS
-#include <pthread.h>
-#endif
 #include "users.h"
 #include "ctables.h"
 #include "globals.h"
@@ -59,7 +56,6 @@
 #include "commands.h"
 #include "xline.h"
 #include "inspstring.h"
-#include "dnsqueue.h"
 #include "helperfuncs.h"
 #include "hashcomp.h"
 #include "socketengine.h"
@@ -159,13 +155,14 @@ void Rehash(int status)
        FOREACH_MOD(I_OnRehash,OnRehash(""));
 }
 
-void InspIRCd::SetSignals()
+void InspIRCd::SetSignals(bool SEGVHandler)
 {
        signal (SIGALRM, SIG_IGN);
        signal (SIGHUP, Rehash);
        signal (SIGPIPE, SIG_IGN);
        signal (SIGTERM, Exit);
-       signal (SIGSEGV, Error);
+       if (SEGVHandler)
+               signal (SIGSEGV, Error);
 }
 
 bool InspIRCd::DaemonSeed()
@@ -236,9 +233,10 @@ void InspIRCd::MakeLowerMap()
 
 InspIRCd::InspIRCd(int argc, char** argv)
 {
+       bool SEGVHandler = false;
+
        this->Start();
        module_sockets.clear();
-       init_dns();
        this->startup_time = time(NULL);
        srand(time(NULL));
        log(DEBUG,"*** InspIRCd starting up!");
@@ -273,6 +271,10 @@ InspIRCd::InspIRCd(int argc, char** argv)
                        {
                                printf("WARNING: The `-nolimit' option is deprecated, and now on by default. This behaviour may change in the future.\n");
                        }
+                       else if (!strcmp(argv[i],"-notraceback"))
+                       {
+                               SEGVHandler = false;
+                       }
                        else if (!strcmp(argv[i],"-logfile"))
                        {
                                if (argc > i+1)
@@ -285,6 +287,12 @@ InspIRCd::InspIRCd(int argc, char** argv)
                                        printf("ERROR: The -logfile parameter must be followed by a log file name and path.\n");
                                        Exit(ERROR);
                                }
+                               i++;
+                       }
+                       else
+                       {
+                               printf("Usage: %s [-nofork] [-nolog] [-debug] [-wait] [-nolimit] [-notraceback] [-logfile <filename>]\n",argv[0]);
+                               Exit(ERROR);
                        }
                }
        }
@@ -297,7 +305,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
        this->stats = new serverstats();
        this->Parser = new CommandParser();
        Config->ClearStack();
-       Config->Read(true,NULL);
+       Config->Read(true, NULL);
        CheckRoot();
        this->ModeGrok = new ModeParser();
        AddServerName(Config->ServerName);
@@ -311,7 +319,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
        memset(&Config->implement_lists,0,sizeof(Config->implement_lists));
 
        printf("\n");
-       this->SetSignals();
+       this->SetSignals(SEGVHandler);
        if (!Config->nofork)
        {
                if (!this->DaemonSeed())
@@ -336,11 +344,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
 std::string InspIRCd::GetVersionString()
 {
        char versiondata[MAXBUF];
-#ifdef THREADED_DNS
-       char dnsengine[] = "multithread";
-#else
-       char dnsengine[] = "singlethread";
-#endif
+       char dnsengine[] = "singlethread-object";
        if (*Config->CustomVersion)
        {
                snprintf(versiondata,MAXBUF,"%s %s :%s",VERSION,Config->ServerName,Config->CustomVersion);
@@ -532,12 +536,12 @@ bool InspIRCd::UnloadModule(const char* filename)
                        }
 
                        // found the module
+                       log(DEBUG,"Removing dependent commands...");
+                       Parser->RemoveCommands(filename);
                        log(DEBUG,"Deleting module...");
                        this->EraseModule(j);
                        log(DEBUG,"Erasing module entry...");
                        this->EraseFactory(j);
-                       log(DEBUG,"Removing dependent commands...");
-                       Parser->RemoveCommands(filename);
                        log(DEFAULT,"Module %s unloaded",filename);
                        MODCOUNT--;
                        BuildISupport();
@@ -582,17 +586,17 @@ bool InspIRCd::LoadModule(const char* filename)
                                return false;
                        }
                }
-               ircd_module* a = new ircd_module(modfile);
-               factory[MODCOUNT+1] = a;
-               if (factory[MODCOUNT+1]->LastError())
-               {
-                       log(DEFAULT,"Unable to load %s: %s",modfile,factory[MODCOUNT+1]->LastError());
-                       snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[MODCOUNT+1]->LastError());
-                       return false;
-               }
                try
                {
-                       if (factory[MODCOUNT+1]->factory)
+                       ircd_module* a = new ircd_module(modfile);
+                       factory[MODCOUNT+1] = a;
+                       if (factory[MODCOUNT+1]->LastError())
+                       {
+                               log(DEFAULT,"Unable to load %s: %s",modfile,factory[MODCOUNT+1]->LastError());
+                               snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[MODCOUNT+1]->LastError());
+                               return false;
+                       }
+                       if ((int)factory[MODCOUNT+1]->factory != -1)
                        {
                                Module* m = factory[MODCOUNT+1]->factory->CreateModule(MyServer);
                                modules[MODCOUNT+1] = m;
@@ -612,7 +616,7 @@ bool InspIRCd::LoadModule(const char* filename)
                        else
                        {
                                        log(DEFAULT,"Unable to load %s",modfile);
-                               snprintf(MODERR,MAXBUF,"Factory function failed!");
+                               snprintf(MODERR,MAXBUF,"Factory function failed: Probably missing init_module() entrypoint.");
                                return false;
                        }
                }
@@ -825,31 +829,16 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                        break;
 
                        case X_ESTAB_DNS:
-                               /* When we are using single-threaded dns,
-                                * the sockets for dns end up in our mainloop.
-                                * When we are using multi-threaded dns,
-                                * each thread has its own basic poll() loop
-                                * within it, making them 'fire and forget'
-                                * and independent of the mainloop.
-                                */
-#ifndef THREADED_DNS
-                               log(DEBUG,"Type: X_ESTAB_DNS: fd=%d",activefds[activefd]);
-                               dns_poll(activefds[activefd]);
-#endif
-                       break;
-
-                       case X_ESTAB_CLASSDNS:
                                /* Handles instances of the Resolver class,
-                                * a simple class extended by modules for
+                                * a simple class extended by modules and the core for
                                 * nonblocking resolving of addresses.
                                 */
-
-                               dns_deal_with_classes(activefds[activefd]);
+                               this->Res->MarshallReads(activefds[activefd]);
                        break;
 
                        case X_LISTEN:
 
-                               log(DEBUG,"Type: X_LISTEN_MODULE: fd=%d",activefds[activefd]);
+                               log(DEBUG,"Type: X_LISTEN: fd=%d",activefds[activefd]);
 
                                /* It's a listener */
                                uslen = sizeof(sock_us);
@@ -858,7 +847,11 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
        
                                if ((incomingSockfd > -1) && (!getsockname(incomingSockfd,(sockaddr*)&sock_us,&uslen)))
                                {
+#ifdef IPV6
+                                       in_port = ntohs(sock_us.sin6_port);
+#else
                                        in_port = ntohs(sock_us.sin_port);
+#endif
                                        log(DEBUG,"Accepted socket %d",incomingSockfd);
                                        /* Years and years ago, we used to resolve here
                                         * using gethostbyaddr(). That is sucky and we
@@ -869,7 +862,11 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                                        {
                                                try
                                                {
-                                                       Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, inet_ntoa(client.sin_addr), in_port);
+#ifdef IPV6
+                                                       Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, insp_ntoa(client.sin6_addr), in_port);
+#else
+                                                       Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, insp_ntoa(client.sin_addr), in_port);
+#endif
                                                }
                                                catch (ModuleException& modexcept)
                                                {
@@ -877,12 +874,18 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
                                                }
                                        }
                                        stats->statsAccept++;
+#ifdef IPV6
+                                       log(DEBUG,"Add ipv6 client");
+                                       AddClient(incomingSockfd, in_port, false, client.sin6_addr);
+#else
+                                       log(DEBUG,"Add ipv4 client");
                                        AddClient(incomingSockfd, in_port, false, client.sin_addr);
-                                       log(DEBUG,"Adding client on port %lu fd=%lu",(unsigned long)in_port,(unsigned long)incomingSockfd);
+#endif
+                                       log(DEBUG,"Adding client on port %d fd=%d",in_port,incomingSockfd);
                                }
                                else
                                {
-                                       log(DEBUG,"Accept failed on fd %lu: %s",(unsigned long)incomingSockfd,strerror(errno));
+                                       log(DEBUG,"Accept failed on fd %d: %s",incomingSockfd,strerror(errno));
                                        shutdown(incomingSockfd,2);
                                        close(incomingSockfd);
                                        stats->statsRefused++;
@@ -905,14 +908,34 @@ void InspIRCd::DoOneIteration(bool process_module_sockets)
 int InspIRCd::Run()
 {
        /* Until THIS point, ServerInstance == NULL */
-       
+
+       this->Res = new DNS();
+
        LoadAllModules(this);
 
        /* Just in case no modules were loaded - fix for bug #101 */
        this->BuildISupport();
 
-       printf("\nInspIRCd is now running!\n");
-       
+       if (!stats->BoundPortCount)
+       {
+               printf("\nI couldn't bind any ports! Are you sure you didn't start InspIRCd twice?\n");
+               Exit(ERROR);
+       }
+
+       /* Add the listening sockets used for client inbound connections
+        * to the socket engine
+        */
+       log(DEBUG,"%d listeners",stats->BoundPortCount);
+       for (unsigned long count = 0; count < stats->BoundPortCount; count++)
+       {
+               log(DEBUG,"Add listener: %d",Config->openSockfd[count]);
+               if (!SE->AddFd(Config->openSockfd[count],true,X_LISTEN))
+               {
+                       printf("\nEH? Could not add listener to socketengine. You screwed up, aborting.\n");
+                       Exit(ERROR);
+               }
+       }
+
        if (!Config->nofork)
        {
                fclose(stdout);
@@ -920,11 +943,7 @@ int InspIRCd::Run()
                fclose(stdin);
        }
 
-       /* Add the listening sockets used for client inbound connections
-        * to the socket engine
-        */
-       for (int count = 0; count < stats->BoundPortCount; count++)
-               SE->AddFd(Config->openSockfd[count],true,X_LISTEN);
+       printf("\nInspIRCd is now running!\n");
 
        this->WritePID(Config->PID);
 
@@ -948,61 +967,14 @@ int InspIRCd::Run()
 
 int main(int argc, char** argv)
 {
-       /* TEST SUITE FOR TOKENSTREAM
-        *
-        * Expected output:
-        * 
-        * String: 'PRIVMSG #test FOO BAR'
-        * Token 0 = 'PRIVMSG'
-        * Token 1 = '#test'
-        * Token 2 = 'FOO'
-        * Token 3 = 'BAR'
-        * String: 'PRIVMSG #test :FOO BAR BAZ'
-        * Token 0 = 'PRIVMSG'
-        * Token 1 = '#test'
-        * Token 2 = 'FOO BAR BAZ'
-        * String: ':PRIVMSG #test :FOO BAR BAZ'
-        * Token 0 = ':PRIVMSG'
-        * String: 'AAAAAAA'
-        * Token 0 = 'AAAAAAA'
-        * String: ''
-        * NumItems = 0
-        *
-       std::string a = "PRIVMSG #test FOO BAR";
-       printf("String: '%s'\n",a.c_str());
-       irc::tokenstream test(a);
-       printf("Token 0 = '%s'\n",test.GetToken().c_str());
-       printf("Token 1 = '%s'\n",test.GetToken().c_str());
-       printf("Token 2 = '%s'\n",test.GetToken().c_str());
-       printf("Token 3 = '%s'\n",test.GetToken().c_str());
-       printf("Token 4 = '%s'\n",test.GetToken().c_str());
-
-       std::string b = "PRIVMSG #test :FOO BAR BAZ";
-       printf("String: '%s'\n",b.c_str());
-       irc::tokenstream test2(b);
-       printf("Token 0 = '%s'\n",test2.GetToken().c_str());
-       printf("Token 1 = '%s'\n",test2.GetToken().c_str());
-       printf("Token 2 = '%s'\n",test2.GetToken().c_str());
-       printf("Token 3 = '%s'\n",test2.GetToken().c_str());
-
-       std::string c = ":PRIVMSG #test :FOO BAR BAZ";
-       printf("String: '%s'\n",c.c_str());
-       irc::tokenstream test3(c);
-       printf("Token 0 = '%s'\n",test3.GetToken().c_str());
-
-       c = "AAAAAAA";
-       printf("String: '%s'\n",c.c_str());
-       irc::tokenstream test4(c);
-       printf("Token 0 = '%s'\n",test4.GetToken().c_str());
-       printf("Token 1 = '%s'\n",test4.GetToken().c_str());
-
-       c = "";
-       printf("String: '%s'\n",c.c_str());
-       irc::tokenstream test5(c);
-       printf("Token 0 = '%s'\n",test5.GetToken().c_str());
-
-       exit(0);
-       */
+       /* This is a MatchCIDR() test suite -
+       printf("Should be 0: %d\n",MatchCIDR("127.0.0.1","1.2.3.4/8"));
+       printf("Should be 1: %d\n",MatchCIDR("127.0.0.1","127.0.0.0/8"));
+       printf("Should be 1: %d\n",MatchCIDR("127.0.0.1","127.0.0.0/18"));
+       printf("Should be 0: %d\n",MatchCIDR("3ffe::0","2fc9::0/16"));
+       printf("Should be 1: %d\n",MatchCIDR("3ffe:1:3::0", "3ffe:1::0/32"));
+       exit(0); */
+
        try
        {
                ServerInstance = new InspIRCd(argc, argv);