]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd.cpp
Added '%' to STATUSMSG
[user/henk/code/inspircd.git] / src / inspircd.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 /* Now with added unF! ;) */
18
19 using namespace std;
20
21 #include "inspircd_config.h"
22 #include "inspircd.h"
23 #include "inspircd_io.h"
24 #include <fcntl.h>
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <time.h>
28 #include <string>
29 #ifdef GCC3
30 #include <ext/hash_map>
31 #else
32 #include <hash_map>
33 #endif
34 #include <map>
35 #include <sstream>
36 #include <vector>
37 #include <deque>
38 #ifdef THREADED_DNS
39 #include <pthread.h>
40 #endif
41 #include "users.h"
42 #include "ctables.h"
43 #include "globals.h"
44 #include "modules.h"
45 #include "dynamic.h"
46 #include "wildcard.h"
47 #include "message.h"
48 #include "mode.h"
49 #include "commands.h"
50 #include "xline.h"
51 #include "inspstring.h"
52 #include "dnsqueue.h"
53 #include "helperfuncs.h"
54 #include "hashcomp.h"
55 #include "socketengine.h"
56 #include "userprocess.h"
57 #include "socket.h"
58 #include "typedefs.h"
59 #include "command_parse.h"
60
61 InspIRCd* ServerInstance;
62
63 int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
64 int WHOWAS_MAX = 100;  // default 100 people maximum in the WHOWAS list
65
66 extern std::vector<Module*> modules;
67 extern std::vector<ircd_module*> factory;
68
69 std::vector<InspSocket*> module_sockets;
70 std::vector<userrec*> local_users;
71
72 extern int MODCOUNT;
73 int openSockfd[MAXSOCKS];
74 sockaddr_in client,server;
75 socklen_t length;
76
77 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
78
79 time_t TIME = time(NULL), OLDTIME = time(NULL);
80
81 // This table references users by file descriptor.
82 // its an array to make it VERY fast, as all lookups are referenced
83 // by an integer, meaning there is no need for a scan/search operation.
84 userrec* fd_ref_table[MAX_DESCRIPTORS];
85
86 Server* MyServer = new Server;
87 ServerConfig *Config = new ServerConfig;
88
89 user_hash clientlist;
90 chan_hash chanlist;
91 whowas_hash whowas;
92 servernamelist servernames;
93 char lowermap[255];
94
95 void AddServerName(std::string servername)
96 {
97         log(DEBUG,"Adding server name: %s",servername.c_str());
98         for (servernamelist::iterator a = servernames.begin(); a < servernames.end(); a++)
99         {
100                 if (*a == servername)
101                         return;
102         }
103         servernames.push_back(servername);
104 }
105
106 const char* FindServerNamePtr(std::string servername)
107 {
108         for (servernamelist::iterator a = servernames.begin(); a < servernames.end(); a++)
109         {
110                 if (*a == servername)
111                         return a->c_str();
112         }
113         AddServerName(servername);
114         return FindServerNamePtr(servername);
115 }
116
117 bool FindServerName(std::string servername)
118 {
119         for (servernamelist::iterator a = servernames.begin(); a < servernames.end(); a++)
120         {
121                 if (*a == servername)
122                         return true;
123         }
124         return false;
125 }
126
127 std::string InspIRCd::GetRevision()
128 {
129         /* w00t got me to replace a bunch of strtok_r
130          * with something nicer, so i did this. Its the
131          * same thing really, only in C++. It places the
132          * text into a std::stringstream which is a readable
133          * and writeable buffer stream, and then pops two
134          * words off it, space delimited. Because it reads
135          * into the same variable twice, the first word
136          * is discarded, and the second one returned.
137          */
138         std::stringstream Revision("$Revision$");
139         std::string single;
140         Revision >> single >> single;
141         return single;
142 }
143
144 void InspIRCd::MakeLowerMap()
145 {
146         // initialize the lowercase mapping table
147         for (unsigned int cn = 0; cn < 256; cn++)
148                 lowermap[cn] = cn;
149         // lowercase the uppercase chars
150         for (unsigned int cn = 65; cn < 91; cn++)
151                 lowermap[cn] = tolower(cn);
152         // now replace the specific chars for scandanavian comparison
153         lowermap[(unsigned)'['] = '{';
154         lowermap[(unsigned)']'] = '}';
155         lowermap[(unsigned)'\\'] = '|';
156 }
157
158 InspIRCd::InspIRCd(int argc, char** argv)
159 {
160         Start();
161         module_sockets.clear();
162         this->startup_time = time(NULL);
163         srand(time(NULL));
164         log(DEBUG,"*** InspIRCd starting up!");
165         if (!FileExists(CONFIG_FILE))
166         {
167                 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
168                 log(DEFAULT,"main: no config");
169                 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
170                 Exit(ERROR);
171         }
172         if (argc > 1) {
173                 for (int i = 1; i < argc; i++)
174                 {
175                         if (!strcmp(argv[i],"-nofork")) {
176                                 Config->nofork = true;
177                         }
178                         if (!strcmp(argv[i],"-wait")) {
179                                 sleep(6);
180                         }
181                         if (!strcmp(argv[i],"-nolimit")) {
182                                 Config->unlimitcore = true;
183                         }
184                 }
185         }
186
187         strlcpy(Config->MyExecutable,argv[0],MAXBUF);
188
189         this->MakeLowerMap();
190
191         OpenLog(argv, argc);
192         Config->ClearStack();
193         Config->Read(true,NULL);
194         CheckRoot();
195         this->ModeGrok = new ModeParser();
196         this->Parser = new CommandParser();
197         this->stats = new serverstats();
198         AddServerName(Config->ServerName);
199         CheckDie();
200         stats->BoundPortCount = BindPorts();
201
202         for(int t = 0; t < 255; t++)
203                 Config->global_implementation[t] = 0;
204
205         memset(&Config->implement_lists,0,sizeof(Config->implement_lists));
206
207         printf("\n");
208         SetSignals();
209         if (!Config->nofork)
210         {
211                 if (DaemonSeed() == ERROR)
212                 {
213                         printf("ERROR: could not go into daemon mode. Shutting down.\n");
214                         Exit(ERROR);
215                 }
216         }
217
218         /* Because of limitations in kqueue on freebsd, we must fork BEFORE we
219          * initialize the socket engine.
220          */
221         SE = new SocketEngine();
222
223         /* We must load the modules AFTER initializing the socket engine, now */
224
225         return;
226 }
227
228 std::string InspIRCd::GetVersionString()
229 {
230         char versiondata[MAXBUF];
231 #ifdef THREADED_DNS
232         char dnsengine[] = "multithread";
233 #else
234         char dnsengine[] = "singlethread";
235 #endif
236         if (*Config->CustomVersion)
237         {
238                 snprintf(versiondata,MAXBUF,"%s Rev. %s %s :%s",VERSION,GetRevision().c_str(),Config->ServerName,Config->CustomVersion);
239         }
240         else
241         {
242                 snprintf(versiondata,MAXBUF,"%s Rev. %s %s :%s [FLAGS=%lu,%s,%s]",VERSION,GetRevision().c_str(),Config->ServerName,SYSTEM,(unsigned long)OPTIMISATION,SE->GetName().c_str(),dnsengine);
243         }
244         return versiondata;
245 }
246
247 char* InspIRCd::ModuleError()
248 {
249         return MODERR;
250 }
251
252 void InspIRCd::erase_factory(int j)
253 {
254         int v = 0;
255         for (std::vector<ircd_module*>::iterator t = factory.begin(); t != factory.end(); t++)
256         {
257                 if (v == j)
258                 {
259                         factory.erase(t);
260                         factory.push_back(NULL);
261                         return;
262                 }
263                 v++;
264         }
265 }
266
267 void InspIRCd::erase_module(int j)
268 {
269         int v1 = 0;
270         for (std::vector<Module*>::iterator m = modules.begin(); m!= modules.end(); m++)
271         {
272                 if (v1 == j)
273                 {
274                         delete *m;
275                         modules.erase(m);
276                         modules.push_back(NULL);
277                         break;
278                 }
279                 v1++;
280         }
281         int v2 = 0;
282         for (std::vector<std::string>::iterator v = Config->module_names.begin(); v != Config->module_names.end(); v++)
283         {
284                 if (v2 == j)
285                 {
286                        Config->module_names.erase(v);
287                        break;
288                 }
289                 v2++;
290         }
291
292 }
293
294 void InspIRCd::MoveTo(std::string modulename,int slot)
295 {
296         unsigned int v2 = 256;
297         log(DEBUG,"Moving %s to slot %d",modulename.c_str(),slot);
298         for (unsigned int v = 0; v < Config->module_names.size(); v++)
299         {
300                 if (Config->module_names[v] == modulename)
301                 {
302                         // found an instance, swap it with the item at MODCOUNT
303                         v2 = v;
304                         break;
305                 }
306         }
307         if (v2 == (unsigned int)slot)
308         {
309                 log(DEBUG,"Item %s already in slot %d!",modulename.c_str(),slot);
310         }
311         else if (v2 < 256)
312         {
313                 // Swap the module names over
314                 Config->module_names[v2] = Config->module_names[slot];
315                 Config->module_names[slot] = modulename;
316                 // now swap the module factories
317                 ircd_module* temp = factory[v2];
318                 factory[v2] = factory[slot];
319                 factory[slot] = temp;
320                 // now swap the module objects
321                 Module* temp_module = modules[v2];
322                 modules[v2] = modules[slot];
323                 modules[slot] = temp_module;
324                 // now swap the implement lists (we dont
325                 // need to swap the global or recount it)
326                 for (int n = 0; n < 255; n++)
327                 {
328                         char x = Config->implement_lists[v2][n];
329                         Config->implement_lists[v2][n] = Config->implement_lists[slot][n];
330                         Config->implement_lists[slot][n] = x;
331                 }
332                 log(DEBUG,"Moved %s to slot successfully",modulename.c_str());
333         }
334         else
335         {
336                 log(DEBUG,"Move of %s to slot failed!",modulename.c_str());
337         }
338 }
339
340 void InspIRCd::MoveAfter(std::string modulename, std::string after)
341 {
342         log(DEBUG,"Move %s after %s...",modulename.c_str(),after.c_str());
343         for (unsigned int v = 0; v < Config->module_names.size(); v++)
344         {
345                 if (Config->module_names[v] == after)
346                 {
347                         MoveTo(modulename, v);
348                         return;
349                 }
350         }
351 }
352
353 void InspIRCd::MoveBefore(std::string modulename, std::string before)
354 {
355         log(DEBUG,"Move %s before %s...",modulename.c_str(),before.c_str());
356         for (unsigned int v = 0; v < Config->module_names.size(); v++)
357         {
358                 if (Config->module_names[v] == before)
359                 {
360                         if (v > 0)
361                         {
362                                 MoveTo(modulename, v-1);
363                         }
364                         else
365                         {
366                                 MoveTo(modulename, v);
367                         }
368                         return;
369                 }
370         }
371 }
372
373 void InspIRCd::MoveToFirst(std::string modulename)
374 {
375         MoveTo(modulename,0);
376 }
377
378 void InspIRCd::MoveToLast(std::string modulename)
379 {
380         MoveTo(modulename,MODCOUNT);
381 }
382
383 void InspIRCd::BuildISupport()
384 {
385         // the neatest way to construct the initial 005 numeric, considering the number of configure constants to go in it...
386         std::stringstream v;
387         v << "WALLCHOPS MODES=" << MAXMODES << " CHANTYPES=# PREFIX=(ohv)@%+ MAP SAFELIST MAXCHANNELS=" << MAXCHANS << " MAXBANS=60 NICKLEN=" << NICKMAX-1;
388         v << " CASEMAPPING=rfc1459 STATUSMSG=@%+ CHARSET=ascii TOPICLEN=" << MAXTOPIC << " KICKLEN=" << MAXKICK << " MAXTARGETS=" << Config->MaxTargets << " AWAYLEN=";
389         v << MAXAWAY << " CHANMODES=b,k,l,psmnti NETWORK=" << Config->Network;
390         Config->data005 = v.str();
391         FOREACH_MOD(I_On005Numeric,On005Numeric(Config->data005));
392 }
393
394 bool InspIRCd::UnloadModule(const char* filename)
395 {
396         std::string filename_str = filename;
397         for (unsigned int j = 0; j != Config->module_names.size(); j++)
398         {
399                 if (Config->module_names[j] == filename_str)
400                 {
401                         if (modules[j]->GetVersion().Flags & VF_STATIC)
402                         {
403                                 log(DEFAULT,"Failed to unload STATIC module %s",filename);
404                                 snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)");
405                                 return false;
406                         }
407                         /* Give the module a chance to tidy out all its metadata */
408                         for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
409                         {
410                                 modules[j]->OnCleanup(TYPE_CHANNEL,c->second);
411                         }
412                         for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
413                         {
414                                 modules[j]->OnCleanup(TYPE_USER,u->second);
415                         }
416
417                         FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(modules[j],Config->module_names[j]));
418
419                         for(int t = 0; t < 255; t++)
420                         {
421                                 Config->global_implementation[t] -= Config->implement_lists[j][t];
422                         }
423
424                         /* We have to renumber implement_lists after unload because the module numbers change!
425                          */
426                         for(int j2 = j; j2 < 254; j2++)
427                         {
428                                 for(int t = 0; t < 255; t++)
429                                 {
430                                         Config->implement_lists[j2][t] = Config->implement_lists[j2+1][t];
431                                 }
432                         }
433
434                         // found the module
435                         log(DEBUG,"Deleting module...");
436                         erase_module(j);
437                         log(DEBUG,"Erasing module entry...");
438                         erase_factory(j);
439                         log(DEBUG,"Removing dependent commands...");
440                         Parser->RemoveCommands(filename);
441                         log(DEFAULT,"Module %s unloaded",filename);
442                         MODCOUNT--;
443                         BuildISupport();
444                         return true;
445                 }
446         }
447         log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename);
448         snprintf(MODERR,MAXBUF,"Module not loaded");
449         return false;
450 }
451
452 bool InspIRCd::LoadModule(const char* filename)
453 {
454         char modfile[MAXBUF];
455 #ifdef STATIC_LINK
456         strlcpy(modfile,filename,MAXBUF);
457 #else
458         snprintf(modfile,MAXBUF,"%s/%s",Config->ModPath,filename);
459 #endif
460         std::string filename_str = filename;
461 #ifndef STATIC_LINK
462 #ifndef IS_CYGWIN
463         if (!DirValid(modfile))
464         {
465                 log(DEFAULT,"Module %s is not within the modules directory.",modfile);
466                 snprintf(MODERR,MAXBUF,"Module %s is not within the modules directory.",modfile);
467                 return false;
468         }
469 #endif
470 #endif
471         log(DEBUG,"Loading module: %s",modfile);
472 #ifndef STATIC_LINK
473         if (FileExists(modfile))
474         {
475 #endif
476                 for (unsigned int j = 0; j < Config->module_names.size(); j++)
477                 {
478                         if (Config->module_names[j] == filename_str)
479                         {
480                                 log(DEFAULT,"Module %s is already loaded, cannot load a module twice!",modfile);
481                                 snprintf(MODERR,MAXBUF,"Module already loaded");
482                                 return false;
483                         }
484                 }
485                 ircd_module* a = new ircd_module(modfile);
486                 factory[MODCOUNT+1] = a;
487                 if (factory[MODCOUNT+1]->LastError())
488                 {
489                         log(DEFAULT,"Unable to load %s: %s",modfile,factory[MODCOUNT+1]->LastError());
490                         snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[MODCOUNT+1]->LastError());
491                         return false;
492                 }
493                 if (factory[MODCOUNT+1]->factory)
494                 {
495                         Module* m = factory[MODCOUNT+1]->factory->CreateModule(MyServer);
496                         modules[MODCOUNT+1] = m;
497                         /* save the module and the module's classfactory, if
498                          * this isnt done, random crashes can occur :/ */
499                         Config->module_names.push_back(filename);
500
501                         char* x = &Config->implement_lists[MODCOUNT+1][0];
502                         for(int t = 0; t < 255; t++)
503                                 x[t] = 0;
504
505                         modules[MODCOUNT+1]->Implements(x);
506
507                         for(int t = 0; t < 255; t++)
508                         {
509                                 Config->global_implementation[t] += Config->implement_lists[MODCOUNT+1][t];
510                                 if (Config->implement_lists[MODCOUNT+1][t])
511                                 {
512                                         log(DEBUG,"Add global implementation: %d %d => %d",MODCOUNT+1,t,Config->global_implementation[t]);
513                                 }
514                         }
515                 }
516                 else
517                 {
518                         log(DEFAULT,"Unable to load %s",modfile);
519                         snprintf(MODERR,MAXBUF,"Factory function failed!");
520                         return false;
521                 }
522 #ifndef STATIC_LINK
523         }
524         else
525         {
526                 log(DEFAULT,"InspIRCd: startup: Module Not Found %s",modfile);
527                 snprintf(MODERR,MAXBUF,"Module file could not be found");
528                 return false;
529         }
530 #endif
531         MODCOUNT++;
532         FOREACH_MOD(I_OnLoadModule,OnLoadModule(modules[MODCOUNT],filename_str));
533         // now work out which modules, if any, want to move to the back of the queue,
534         // and if they do, move them there.
535         std::vector<std::string> put_to_back;
536         std::vector<std::string> put_to_front;
537         std::map<std::string,std::string> put_before;
538         std::map<std::string,std::string> put_after;
539         for (unsigned int j = 0; j < Config->module_names.size(); j++)
540         {
541                 if (modules[j]->Prioritize() == PRIORITY_LAST)
542                 {
543                         put_to_back.push_back(Config->module_names[j]);
544                 }
545                 else if (modules[j]->Prioritize() == PRIORITY_FIRST)
546                 {
547                         put_to_front.push_back(Config->module_names[j]);
548                 }
549                 else if ((modules[j]->Prioritize() & 0xFF) == PRIORITY_BEFORE)
550                 {
551                         log(DEBUG,"Module %d wants PRIORITY_BEFORE",j);
552                         put_before[Config->module_names[j]] = Config->module_names[modules[j]->Prioritize() >> 8];
553                         log(DEBUG,"Before: %s",Config->module_names[modules[j]->Prioritize() >> 8].c_str());
554                 }
555                 else if ((modules[j]->Prioritize() & 0xFF) == PRIORITY_AFTER)
556                 {
557                         log(DEBUG,"Module %d wants PRIORITY_AFTER",j);
558                         put_after[Config->module_names[j]] = Config->module_names[modules[j]->Prioritize() >> 8];
559                         log(DEBUG,"After: %s",Config->module_names[modules[j]->Prioritize() >> 8].c_str());
560                 }
561         }
562         for (unsigned int j = 0; j < put_to_back.size(); j++)
563         {
564                 MoveToLast(put_to_back[j]);
565         }
566         for (unsigned int j = 0; j < put_to_front.size(); j++)
567         {
568                 MoveToFirst(put_to_front[j]);
569         }
570         for (std::map<std::string,std::string>::iterator j = put_before.begin(); j != put_before.end(); j++)
571         {
572                 MoveBefore(j->first,j->second);
573         }
574         for (std::map<std::string,std::string>::iterator j = put_after.begin(); j != put_after.end(); j++)
575         {
576                 MoveAfter(j->first,j->second);
577         }
578         BuildISupport();
579         return true;
580 }
581
582 int InspIRCd::Run()
583 {
584         bool expire_run = false;
585         int activefds[MAX_DESCRIPTORS];
586         int incomingSockfd;
587         int in_port;
588         userrec* cu = NULL;
589         InspSocket* s = NULL;
590         InspSocket* s_del = NULL;
591         unsigned int numberactive;
592         sockaddr_in sock_us;     // our port number
593         socklen_t uslen;         // length of our port number
594
595         /* Until THIS point, ServerInstance == NULL */
596         
597         LoadAllModules(this);
598
599         printf("\nInspIRCd is now running!\n");
600         
601         if (!Config->nofork)
602         {
603                 freopen("/dev/null","w",stdout);
604                 freopen("/dev/null","w",stderr);
605         }
606
607         /* Add the listening sockets used for client inbound connections
608          * to the socket engine
609          */
610         for (int count = 0; count < stats->BoundPortCount; count++)
611                 SE->AddFd(openSockfd[count],true,X_LISTEN);
612
613         WritePID(Config->PID);
614
615         /* main loop, this never returns */
616         for (;;)
617         {
618                 /* time() seems to be a pretty expensive syscall, so avoid calling it too much.
619                  * Once per loop iteration is pleanty.
620                  */
621                 OLDTIME = TIME;
622                 TIME = time(NULL);
623
624                 /* Run background module timers every few seconds
625                  * (the docs say modules shouldnt rely on accurate
626                  * timing using this event, so we dont have to
627                  * time this exactly).
628                  */
629                 if (((TIME % 8) == 0) && (!expire_run))
630                 {
631                         expire_lines();
632                         FOREACH_MOD(I_OnBackgroundTimer,OnBackgroundTimer(TIME));
633                         expire_run = true;
634                         continue;
635                 }
636                 else if ((TIME % 8) == 1)
637                 {
638                         expire_run = false;
639                 }
640                 
641                 /* Once a second, do the background processing */
642                 if (TIME != OLDTIME)
643                         DoBackgroundUserStuff(TIME);
644
645                 /* Call the socket engine to wait on the active
646                  * file descriptors. The socket engine has everything's
647                  * descriptors in its list... dns, modules, users,
648                  * servers... so its nice and easy, just one call.
649                  */
650                 if (!(numberactive = SE->Wait(activefds)))
651                         continue;
652
653                 /**
654                  * Now process each of the fd's. For users, we have a fast
655                  * lookup table which can find a user by file descriptor, so
656                  * processing them by fd isnt expensive. If we have a lot of
657                  * listening ports or module sockets though, things could get
658                  * ugly.
659                  */
660                 for (unsigned int activefd = 0; activefd < numberactive; activefd++)
661                 {
662                         int socket_type = SE->GetType(activefds[activefd]);
663                         switch (socket_type)
664                         {
665                                 case X_ESTAB_CLIENT:
666
667                                         cu = fd_ref_table[activefds[activefd]];
668                                         if (cu)
669                                                 ProcessUser(cu);
670
671                                 break;
672
673                                 case X_ESTAB_MODULE:
674
675                                         /* Process module-owned sockets.
676                                          * Modules are encouraged to inherit their sockets from
677                                          * InspSocket so we can process them neatly like this.
678                                          */
679                                         s = socket_ref[activefds[activefd]];
680
681                                         if ((s) && (!s->Poll()))
682                                         {
683                                                 log(DEBUG,"Socket poll returned false, close and bail");
684                                                 SE->DelFd(s->GetFd());
685                                                 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
686                                                 {
687                                                         s_del = (InspSocket*)*a;
688                                                         if ((s_del) && (s_del->GetFd() == activefds[activefd]))
689                                                         {
690                                                                 module_sockets.erase(a);
691                                                                 break;
692                                                         }
693                                                 }
694                                                 s->Close();
695                                                 delete s;
696                                         }
697
698                                 break;
699
700                                 case X_ESTAB_DNS:
701
702                                         /* When we are using single-threaded dns,
703                                          * the sockets for dns end up in our mainloop.
704                                          * When we are using multi-threaded dns,
705                                          * each thread has its own basic poll() loop
706                                          * within it, making them 'fire and forget'
707                                          * and independent of the mainloop.
708                                          */
709 #ifndef THREADED_DNS
710                                         dns_poll(activefds[activefd]);
711 #endif
712                                 break;
713                                 
714                                 case X_LISTEN:
715
716                                         /* It's a listener */
717                                         uslen = sizeof(sock_us);
718                                         length = sizeof(client);
719                                         incomingSockfd = accept (activefds[activefd],(struct sockaddr*)&client,&length);
720                                         
721                                         if ((incomingSockfd > -1) && (!getsockname(incomingSockfd,(sockaddr*)&sock_us,&uslen)))
722                                         {
723                                                 in_port = ntohs(sock_us.sin_port);
724                                                 log(DEBUG,"Accepted socket %d",incomingSockfd);
725                                                 /* Years and years ago, we used to resolve here
726                                                  * using gethostbyaddr(). That is sucky and we
727                                                  * don't do that any more...
728                                                  */
729                                                 NonBlocking(incomingSockfd);
730                                                 if (Config->GetIOHook(in_port))
731                                                 {
732                                                         Config->GetIOHook(in_port)->OnRawSocketAccept(incomingSockfd, (char*)inet_ntoa(client.sin_addr), in_port);
733                                                 }
734                                                 stats->statsAccept++;
735                                                 AddClient(incomingSockfd, in_port, false, client.sin_addr);
736                                                 log(DEBUG,"Adding client on port %lu fd=%lu",(unsigned long)in_port,(unsigned long)incomingSockfd);
737                                         }
738                                         else
739                                         {
740                                                 log(DEBUG,"Accept failed on fd %lu: %s",(unsigned long)incomingSockfd,strerror(errno));
741                                                 shutdown(incomingSockfd,2);
742                                                 close(incomingSockfd);
743                                                 stats->statsRefused++;
744                                         }
745                                 break;
746
747                                 default:
748                                         /* Something went wrong if we're in here.
749                                          * In fact, so wrong, im not quite sure
750                                          * what we would do, so for now, its going
751                                          * to safely do bugger all.
752                                          */
753                                 break;
754                         }
755                 }
756
757         }
758         /* This is never reached -- we hope! */
759         return 0;
760 }
761
762 /**********************************************************************************/
763
764 /**
765  * An ircd in four lines! bwahahaha. ahahahahaha. ahahah *cough*.
766  */
767
768 int main(int argc, char** argv)
769 {
770         ServerInstance = new InspIRCd(argc, argv);
771         ServerInstance->Run();
772         delete ServerInstance;
773         return 0;
774 }
775