]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/inspircd_io.cpp
Please diagnose error line 205
[user/henk/code/inspircd.git] / src / inspircd_io.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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include <sys/time.h>
21 #include <sys/resource.h>
22 #include <sys/types.h>
23 #include <string>
24 #include <unistd.h>
25 #include <sstream>
26 #include <iostream>
27 #include <fstream>
28 #include "inspircd.h"
29 #include "inspircd_io.h"
30 #include "inspstring.h"
31 #include "helperfuncs.h"
32 #include "userprocess.h"
33 #include "xline.h"
34
35 extern ServerConfig *Config;
36 extern InspIRCd* ServerInstance;
37 extern int openSockfd[MAXSOCKS];
38 extern time_t TIME;
39
40 extern int MODCOUNT;
41 extern std::vector<Module*> modules;
42 extern std::vector<ircd_module*> factory;
43
44 ServerConfig::ServerConfig()
45 {
46         this->ClearStack();
47         *TempDir = *ServerName = *Network = *ServerDesc = *AdminName = '\0';
48         *HideWhoisServer = *AdminEmail = *AdminNick = *diepass = *restartpass = '\0';
49         *CustomVersion = *motd = *rules = *PrefixQuit = *DieValue = *DNSServer = '\0';
50         *OperOnlyStats = *ModPath = *MyExecutable = *DisabledCommands = *PID = '\0';
51         log_file = NULL;
52         nofork = HideBans = HideSplits = unlimitcore = false;
53         AllowHalfop = true;
54         dns_timeout = DieDelay = 5;
55         MaxTargets = 20;
56         NetBufferSize = 10240;
57         SoftLimit = MAXCLIENTS;
58         MaxConn = SOMAXCONN;
59         MaxWhoResults = 100;
60         debugging = 0;
61         LogLevel = DEFAULT;
62         maxbans.clear();
63 }
64
65 void ServerConfig::ClearStack()
66 {
67         include_stack.clear();
68 }
69
70 Module* ServerConfig::GetIOHook(int port)
71 {
72         std::map<int,Module*>::iterator x = IOHookModule.find(port);
73         return (x != IOHookModule.end() ? x->second : NULL);
74 }
75
76 bool ServerConfig::AddIOHook(int port, Module* iomod)
77 {
78         if (!GetIOHook(port))
79         {
80                 IOHookModule[port] = iomod;
81                 return true;
82         }
83         else
84         {
85                 ModuleException err("Port already hooked by another module");
86                 throw(err);
87                 return false;
88         }
89 }
90
91 bool ServerConfig::DelIOHook(int port)
92 {
93         std::map<int,Module*>::iterator x = IOHookModule.find(port);
94         if (x != IOHookModule.end())
95         {
96                 IOHookModule.erase(x);
97                 return true;
98         }
99         return false;
100 }
101
102 bool ServerConfig::CheckOnce(char* tag, bool bail, userrec* user)
103 {
104         int count = ConfValueEnum(tag,&Config->config_f);
105         if (count > 1)
106         {
107                 if (bail)
108                 {
109                         printf("There were errors in your configuration:\nYou have more than one <%s> tag, this is not permitted.\n",tag);
110                         Exit(0);
111                 }
112                 else
113                 {
114                         if (user)
115                         {
116                                 WriteServ(user->fd,"There were errors in your configuration:");
117                                 WriteServ(user->fd,"You have more than one <%s> tag, this is not permitted.\n",tag);
118                         }
119                         else
120                         {
121                                 WriteOpers("There were errors in the configuration file:");
122                                 WriteOpers("You have more than one <%s> tag, this is not permitted.\n",tag);
123                         }
124                 }
125                 return false;
126         }
127         if (count < 1)
128         {
129                 if (bail)
130                 {
131                         printf("There were errors in your configuration:\nYou have not defined a <%s> tag, this is required.\n",tag);
132                         Exit(0);
133                 }
134                 else
135                 {
136                         if (user)
137                         {
138                                 WriteServ(user->fd,"There were errors in your configuration:");
139                                 WriteServ(user->fd,"You have not defined a <%s> tag, this is required.",tag);
140                         }
141                         else
142                         {
143                                 WriteOpers("There were errors in the configuration file:");
144                                 WriteOpers("You have not defined a <%s> tag, this is required.",tag);
145                         }
146                 }
147                 return false;
148         }
149         return true;
150 }
151
152 typedef bool (*Validator)(const char*, const char*, void*);
153
154 enum ConfigDataType { DT_NOTHING, DT_INTEGER, DT_CHARPTR, DT_BOOLEAN };
155
156 struct InitialConfig {
157         char* tag;
158         char* value;
159         void* val;
160         ConfigDataType datatype;
161         Validator validate_function;
162 };
163
164 bool NoValidation(const char* tag, const char* value, void* data)
165 {
166         log(DEBUG,"No validation for <%s:%s>",tag,value);
167         return true;
168 }
169
170 void ServerConfig::Read(bool bail, userrec* user)
171 {
172         char debug[MAXBUF];
173
174         InitialConfig Values[] = {
175                 {"options",     "softlimit",            &this->SoftLimit,               DT_INTEGER, NoValidation},
176                 {"options",     "somaxconn",            &this->MaxConn,                 DT_INTEGER, NoValidation},
177                 {"server",      "name",                 &this->ServerName,              DT_CHARPTR, NoValidation},
178                 {"server",      "description",          &this->ServerDesc,              DT_CHARPTR, NoValidation},
179                 {"server",      "network",              &this->Network,                 DT_CHARPTR, NoValidation},
180                 {"admin"        "name",                 &this->AdminName,               DT_CHARPTR, NoValidation},
181                 {"admin",       "email",                &this->AdminEmail,              DT_CHARPTR, NoValidation},
182                 {"admin",       "nick",                 &this->AdminNick,               DT_CHARPTR, NoValidation},
183                 {"files",       "motd",                 &this->motd,                    DT_CHARPTR, NoValidation},
184                 {"files",       "rules",                &this->rules,                   DT_CHARPTR, NoValidation},
185                 {"power",       "diepass",              &this->diepass,                 DT_CHARPTR, NoValidation},      
186                 {"power",       "pauseval",             &this->DieDelay,                DT_INTEGER, NoValidation},
187                 {"power",       "restartpass",          &this->restartpass,             DT_CHARPTR, NoValidation},
188                 {"options",     "prefixquit",           &this->PrefixQuit,              DT_CHARPTR, NoValidation},
189                 {"die",         "value",                &this->DieValue,                DT_CHARPTR, NoValidation},
190                 {"options",     "loglevel",             &debug,                         DT_CHARPTR, NoValidation},
191                 {"options",     "netbuffersize",        &this->NetBufferSize,           DT_INTEGER, NoValidation},
192                 {"options",     "maxwho",               &this->MaxWhoResults,           DT_INTEGER, NoValidation},
193                 {"options",     "allowhalfop",          &this->AllowHalfop,             DT_BOOLEAN, NoValidation},
194                 {"dns",         "server",               &this->DNSServer,               DT_CHARPTR, NoValidation},
195                 {"dns",         "timeout",              &this->dns_timeout,             DT_INTEGER, NoValidation},
196                 {"options",     "moduledir",            &this->ModPath,                 DT_CHARPTR, NoValidation},
197                 {"disabled",    "commands",             &this->DisabledCommands,        DT_CHARPTR, NoValidation},
198                 {"options",     "operonlystats",        &this->OperOnlyStats,           DT_CHARPTR, NoValidation},
199                 {"options",     "customversion",        &this->CustomVersion,           DT_CHARPTR, NoValidation},
200                 {"options",     "hidesplits",           &this->HideSplits,              DT_BOOLEAN, NoValidation},
201                 {"options",     "hidebans",             &this->HideBans,                DT_BOOLEAN, NoValidation},
202                 {"options",     "hidewhois",            &this->HideWhoisServer,         DT_CHARPTR, NoValidation},
203                 {"options",     "tempdir",              &this->TempDir,                 DT_CHARPTR, NoValidation},
204                 {NULL}
205         };
206
207         /* XXX - this needs a rework someday, BAD. */
208         char timeout[MAXBUF],flood[MAXBUF],pfreq[MAXBUF],thold[MAXBUF],sqmax[MAXBUF],rqmax[MAXBUF],SLIMT[MAXBUF];
209         char localmax[MAXBUF],globalmax[MAXBUF],ServName[MAXBUF];
210
211         ConnectClass c;
212         std::stringstream errstr;
213         
214         include_stack.clear();
215
216         if (!LoadConf(CONFIG_FILE,&Config->config_f,&errstr))
217         {
218                 errstr.seekg(0);
219                 log(DEFAULT,"There were errors in your configuration:\n%s",errstr.str().c_str());
220
221                 if (bail)
222                 {
223                         printf("There were errors in your configuration:\n%s",errstr.str().c_str());
224                         Exit(0);
225                 }
226                 else
227                 {
228                         char dataline[1024];
229
230                         if (user)
231                         {
232                                 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
233                                 while (!errstr.eof())
234                                 {
235                                         errstr.getline(dataline,1024);
236                                         WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
237                                 }
238                         }
239                         else
240                         {
241                                 WriteOpers("There were errors in the configuration file:");
242                                 while (!errstr.eof())
243                                 {
244                                         errstr.getline(dataline,1024);
245                                         WriteOpers(dataline);
246                                 }
247                         }
248
249                         return;
250                 }
251         }
252
253         /* Check we dont have more than one of singular tags
254          */
255         if (!CheckOnce("server",bail,user) || !CheckOnce("admin",bail,user) || !CheckOnce("files",bail,user)
256                 || !CheckOnce("power",bail,user) || !CheckOnce("options",bail,user) || !CheckOnce("pid",bail,user))
257         {
258                 return;
259         }
260
261         char* convert;
262         for (int Index = 0; Values[Index].datatype != DT_NOTHING; Index++)
263         {
264                 void* val = Values[Index].val;
265
266                 switch (Values[Index].datatype)
267                 {
268                         case DT_CHARPTR:
269                                 ConfValue((char*)Values[Index].tag,(char*)Values[Index].value,(char*) val,this->config_f);
270                         break;
271
272                         case DT_INTEGER:
273                                 convert = new char[MAXBUF];
274                                 ConfValue(Values[Index].tag,Values[Index].value,convert,this->config_f);
275                                 *val = atoi(convert);
276                                 delete[] convert;
277                         break;
278
279                         case DT_BOOLEAN:
280                                 convert = new char[MAXBUF];
281                                 ConfValue(Values[Index].tag,Values[Index].value,convert,this->config_f);
282                                 *val = ((*convert == tolower('y')) || (*convert == tolower('t')) || (*convert == '1'));
283                                 delete[] convert;
284                         break;
285
286                         case DT_NOTHING:
287                         break;
288                 }
289
290                 Values[Index].validation_function(Values[Index].tag, Values[Index].value, Values[Index].val);
291         }
292
293         strlower(this->ServerName);
294
295         if (!*Config->TempDir)
296                 strlcpy(Config->TempDir,"/tmp",1024);
297
298         if ((Config->MaxTargets < 0) || (Config->MaxTargets > 31))
299         {
300                 log(DEFAULT,"WARNING: <options:maxtargets> value is greater than 31 or less than 0, set to 20.");
301                 Config->MaxTargets = 20;
302         }
303
304         if ((Config->SoftLimit < 1) || (Config->SoftLimit > MAXCLIENTS))
305         {
306                 log(DEFAULT,"WARNING: <options:softlimit> value is greater than %d or less than 0, set to %d.",MAXCLIENTS,MAXCLIENTS);
307                 Config->SoftLimit = MAXCLIENTS;
308         }
309
310         Config->MaxConn = atoi(MCON);
311
312         if (Config->MaxConn > SOMAXCONN)
313                 log(DEFAULT,"WARNING: <options:somaxconn> value may be higher than the system-defined SOMAXCONN value!");
314
315         Config->NetBufferSize = atoi(NB);
316         Config->MaxWhoResults = atoi(MW);
317         Config->dns_timeout = atoi(DNT);
318
319         if (!strchr(Config->ServerName,'.'))
320         {
321                 log(DEFAULT,"WARNING: <server:name> '%s' is not a fully-qualified domain name. Changed to '%s%c'",Config->ServerName,Config->ServerName,'.');
322                 charlcat(Config->ServerName,'.',MAXBUF);
323         }
324
325         if (!Config->dns_timeout)
326                 Config->dns_timeout = 5;
327
328         if (!Config->MaxConn)
329                 Config->MaxConn = SOMAXCONN;
330
331         if (!*Config->DNSServer)
332         {
333                 // attempt to look up their nameserver from /etc/resolv.conf
334                 log(DEFAULT,"WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
335                 ifstream resolv("/etc/resolv.conf");
336                 std::string nameserver;
337                 bool found_server = false;
338
339                 if (resolv.is_open())
340                 {
341                         while (resolv >> nameserver)
342                         {
343                                 if ((nameserver == "nameserver") && (!found_server))
344                                 {
345                                         resolv >> nameserver;
346                                         strlcpy(Config->DNSServer,nameserver.c_str(),MAXBUF);
347                                         found_server = true;
348                                         log(DEFAULT,"<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",nameserver.c_str());
349                                 }
350                         }
351
352                         if (!found_server)
353                         {
354                                 log(DEFAULT,"/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
355                                 strlcpy(Config->DNSServer,"127.0.0.1",MAXBUF);
356                         }
357                 }
358                 else
359                 {
360                         log(DEFAULT,"/etc/resolv.conf can't be opened! Defaulting to nameserver '127.0.0.1'!");
361                         strlcpy(Config->DNSServer,"127.0.0.1",MAXBUF);
362                 }
363         }
364
365         if (!*Config->ModPath)
366                 strlcpy(Config->ModPath,MOD_PATH,MAXBUF);
367
368         Config->AllowHalfop = ((!strcasecmp(AH,"true")) || (!strcasecmp(AH,"1")) || (!strcasecmp(AH,"yes")));
369
370         if ((!Config->NetBufferSize) || (Config->NetBufferSize > 65535) || (Config->NetBufferSize < 1024))
371         {
372                 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
373                 Config->NetBufferSize = 10240;
374         }
375
376         if ((!Config->MaxWhoResults) || (Config->MaxWhoResults > 65535) || (Config->MaxWhoResults < 1))
377         {
378                 log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
379                 Config->MaxWhoResults = 128;
380         }
381
382         Config->LogLevel = DEFAULT;
383
384         if (!strcmp(dbg,"debug"))
385         {
386                 Config->LogLevel = DEBUG;
387                 Config->debugging = 1;
388         }
389         else if (!strcmp(dbg,"verbose"))
390                 Config->LogLevel = VERBOSE;
391         else if (!strcmp(dbg,"default"))
392                 Config->LogLevel = DEFAULT;
393         else if (!strcmp(dbg,"sparse"))
394                 Config->LogLevel = SPARSE;
395         else if (!strcmp(dbg,"none"))
396                 Config->LogLevel = NONE;
397
398         readfile(Config->MOTD,Config->motd);
399         log(DEFAULT,"Reading message of the day...");
400         readfile(Config->RULES,Config->rules);
401         log(DEFAULT,"Reading connect classes...");
402         Classes.clear();
403
404         for (int i = 0; i < ConfValueEnum("connect",&Config->config_f); i++)
405         {
406                 *Value = 0;
407                 ConfValue("connect","allow",i,Value,&Config->config_f);
408                 ConfValue("connect","timeout",i,timeout,&Config->config_f);
409                 ConfValue("connect","flood",i,flood,&Config->config_f);
410                 ConfValue("connect","pingfreq",i,pfreq,&Config->config_f);
411                 ConfValue("connect","threshold",i,thold,&Config->config_f);
412                 ConfValue("connect","sendq",i,sqmax,&Config->config_f);
413                 ConfValue("connect","recvq",i,rqmax,&Config->config_f);
414                 ConfValue("connect","localmax",i,localmax,&Config->config_f);
415                 ConfValue("connect","globalmax",i,globalmax,&Config->config_f);
416
417                 if (*Value)
418                 {
419                         c.host = Value;
420                         c.type = CC_ALLOW;
421                         strlcpy(Value,"",MAXBUF);
422                         ConfValue("connect","password",i,Value,&Config->config_f);
423                         c.pass = Value;
424                         c.registration_timeout = 90; // default is 2 minutes
425                         c.pingtime = 120;
426                         c.flood = atoi(flood);
427                         c.threshold = 5;
428                         c.sendqmax = 262144; // 256k
429                         c.recvqmax = 4096;   // 4k
430                         c.maxlocal = 3;
431                         c.maxglobal = 3;
432
433                         if (atoi(localmax)>0)
434                         {
435                                 c.maxlocal = atoi(localmax);
436                         }
437
438                         if (atoi(globalmax)>0)
439                         {
440                                 c.maxglobal = atoi(globalmax);
441                         }
442
443                         if (atoi(thold)>0)
444                         {
445                                 c.threshold = atoi(thold);
446                         }
447                         else
448                         {
449                                 c.threshold = 1;
450                                 c.flood = 999;
451                                 log(DEFAULT,"Warning: Connect allow line '%s' has no flood/threshold settings. Setting this tag to 999 lines in 1 second.",c.host.c_str());
452                         }
453
454                         if (atoi(sqmax)>0)
455                         {
456                                 c.sendqmax = atoi(sqmax);
457                         }
458                         if (atoi(rqmax)>0)
459                         {
460                                 c.recvqmax = atoi(rqmax);
461                         }
462                         if (atoi(timeout)>0)
463                         {
464                                 c.registration_timeout = atoi(timeout);
465                         }
466                         if (atoi(pfreq)>0)
467                         {
468                                 c.pingtime = atoi(pfreq);
469                         }
470
471                         Classes.push_back(c);
472                 }
473                 else
474                 {
475                         ConfValue("connect","deny",i,Value,&Config->config_f);
476                         c.host = Value;
477                         c.type = CC_DENY;
478                         Classes.push_back(c);
479                         log(DEBUG,"Read connect class type DENY, host=%s",c.host.c_str());
480                 }
481         }
482
483         Config->ulines.clear();
484
485         for (int i = 0; i < ConfValueEnum("uline",&Config->config_f); i++)
486         {   
487                 ConfValue("uline","server",i,ServName,&Config->config_f);
488                 {
489                         log(DEBUG,"Read ULINE '%s'",ServName);
490                         Config->ulines.push_back(ServName);
491                 }
492         }
493
494         maxbans.clear();
495         char CM1[MAXBUF],CM2[MAXBUF];
496
497         for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
498         {
499                 Config->ConfValue("banlist","chan",count,CM1,&Config->config_f);
500                 Config->ConfValue("banlist","limit",count,CM2,&Config->config_f);
501                 maxbans[CM1] = atoi(CM2);
502         }
503
504         ReadClassesAndTypes();
505         log(DEFAULT,"Reading K lines,Q lines and Z lines from config...");
506         read_xline_defaults();
507         log(DEFAULT,"Applying K lines, Q lines and Z lines...");
508         apply_lines(APPLY_ALL);
509
510         ConfValue("pid","file",0,Config->PID,&Config->config_f);
511         // write once here, to try it out and make sure its ok
512         WritePID(Config->PID);
513
514         log(DEFAULT,"Done reading configuration file, InspIRCd is now starting.");
515         if (!bail)
516         {
517                 log(DEFAULT,"Adding and removing modules due to rehash...");
518
519                 std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
520
521                 // store the old module names
522                 for (std::vector<std::string>::iterator t = module_names.begin(); t != module_names.end(); t++)
523                 {
524                         old_module_names.push_back(*t);
525                 }
526
527                 // get the new module names
528                 for (int count2 = 0; count2 < ConfValueEnum("module",&Config->config_f); count2++)
529                 {
530                         ConfValue("module","name",count2,Value,&Config->config_f);
531                         new_module_names.push_back(Value);
532                 }
533
534                 // now create a list of new modules that are due to be loaded
535                 // and a seperate list of modules which are due to be unloaded
536                 for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
537                 {
538                         bool added = true;
539
540                         for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
541                         {
542                                 if (*old == *_new)
543                                         added = false;
544                         }
545
546                         if (added)
547                                 added_modules.push_back(*_new);
548                 }
549
550                 for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
551                 {
552                         bool removed = true;
553                         for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
554                         {
555                                 if (*newm == *oldm)
556                                         removed = false;
557                         }
558
559                         if (removed)
560                                 removed_modules.push_back(*oldm);
561                 }
562
563                 /*
564                  * now we have added_modules, a vector of modules to be loaded,
565                  * and removed_modules, a vector of modules
566                  * to be removed.
567                  */
568                 int rem = 0, add = 0;
569                 if (!removed_modules.empty())
570                         for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
571                         {
572                                 if (ServerInstance->UnloadModule(removing->c_str()))
573                                 {
574                                         WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
575
576                                         if (user)
577                                                 WriteServ(user->fd,"973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
578
579                                         rem++;
580                                 }
581                                 else
582                                 {
583                                         if (user)
584                                                 WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
585                                 }
586                         }
587
588                 if (!added_modules.empty())
589                 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
590                 {
591                         if (ServerInstance->LoadModule(adding->c_str()))
592                         {
593                                 WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
594
595                                 if (user)
596                                         WriteServ(user->fd,"975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
597
598                                 add++;
599                         }
600                         else
601                         {
602                                 if (user)
603                                         WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
604                         }
605                 }
606
607                 log(DEFAULT,"Successfully unloaded %lu of %lu modules and loaded %lu of %lu modules.",(unsigned long)rem,(unsigned long)removed_modules.size(),(unsigned long)add,(unsigned long)added_modules.size());
608         }
609 }
610
611
612 void Exit (int status)
613 {
614         if (Config->log_file)
615                 fclose(Config->log_file);
616         send_error("Server shutdown.");
617         exit (status);
618 }
619
620 void Killed(int status)
621 {
622         if (Config->log_file)
623                 fclose(Config->log_file);
624         send_error("Server terminated.");
625         exit(status);
626 }
627
628 char* CleanFilename(char* name)
629 {
630         char* p = name + strlen(name);
631         while ((p != name) && (*p != '/')) p--;
632         return (p != name ? ++p : p);
633 }
634
635
636 void Rehash(int status)
637 {
638         WriteOpers("Rehashing config file %s due to SIGHUP",CleanFilename(CONFIG_FILE));
639         fclose(Config->log_file);
640         OpenLog(NULL,0);
641         Config->Read(false,NULL);
642         FOREACH_MOD(I_OnRehash,OnRehash(""));
643 }
644
645
646
647 void Start (void)
648 {
649         printf("\033[1;32mInspire Internet Relay Chat Server, compiled %s at %s\n",__DATE__,__TIME__);
650         printf("(C) ChatSpike Development team.\033[0m\n\n");
651         printf("Developers:\033[1;32m Brain, FrostyCoolSlug, w00t, Om\033[0m\n");
652         printf("Others:\033[1;32m See /INFO Output\033[0m\n");
653         printf("Name concept:\033[1;32m   Lord_Zathras\033[0m\n\n");
654 }
655
656 void WritePID(std::string filename)
657 {
658         ofstream outfile(filename.c_str());
659         if (outfile.is_open())
660         {
661                 outfile << getpid();
662                 outfile.close();
663         }
664         else
665         {
666                 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
667                 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
668                 Exit(0);
669         }
670 }
671
672 void SetSignals()
673 {
674         signal (SIGALRM, SIG_IGN);
675         signal (SIGHUP, Rehash);
676         signal (SIGPIPE, SIG_IGN);
677         signal (SIGTERM, Exit);
678         signal (SIGSEGV, Error);
679 }
680
681
682 int DaemonSeed (void)
683 {
684         int childpid;
685         if ((childpid = fork ()) < 0)
686                 return (ERROR);
687         else if (childpid > 0)
688         {
689                 /* We wait a few seconds here, so that the shell prompt doesnt come back over the output */
690                 sleep(6);
691                 exit (0);
692         }
693         setsid ();
694         umask (007);
695         printf("InspIRCd Process ID: \033[1;32m%lu\033[0m\n",(unsigned long)getpid());
696
697         if (Config->unlimitcore)
698         {
699                 rlimit rl;
700                 if (getrlimit(RLIMIT_CORE, &rl) == -1)
701                 {
702                         log(DEFAULT,"Failed to getrlimit()!");
703                         return(FALSE);
704                 }
705                 else
706                 {
707                         rl.rlim_cur = rl.rlim_max;
708                         if (setrlimit(RLIMIT_CORE, &rl) == -1)
709                                 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
710                 }
711         }
712   
713         return (TRUE);
714 }
715
716
717 /* Make Sure Modules Are Avaliable!
718  * (BugFix By Craig.. See? I do work! :p)
719  * Modified by brain, requires const char*
720  * to work with other API functions
721  */
722
723 bool FileExists (const char* file)
724 {
725         FILE *input;
726         if ((input = fopen (file, "r")) == NULL)
727         {
728                 return(false);
729         }
730         else
731         {
732                 fclose (input);
733                 return(true);
734         }
735 }
736
737 /* ConfProcess does the following things to a config line in the following order:
738  *
739  * Processes the line for syntax errors as shown below
740  *  (1) Line void of quotes or equals (a malformed, illegal tag format)
741  *  (2) Odd number of quotes on the line indicating a missing quote
742  *  (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
743  *  (4) Spaces between the opening bracket (<) and the keyword
744  *  (5) Spaces between a keyword and an equals sign
745  *  (6) Spaces between an equals sign and a quote
746  * Removes trailing spaces
747  * Removes leading spaces
748  * Converts tabs to spaces
749  * Turns multiple spaces that are outside of quotes into single spaces
750  */
751
752 std::string ServerConfig::ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
753 {
754         long number_of_quotes = 0;
755         long number_of_equals = 0;
756         bool has_open_bracket = false;
757         bool in_quotes = false;
758         error = false;
759         if (!buffer)
760         {
761                 return "";
762         }
763         // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
764         for (char* d = buffer; *d; d++)
765                 if (*d == 9)
766                         *d = ' ';
767         while (*buffer == ' ') buffer++;
768         while ((buffer[strlen(buffer)-1] == ' ') && (*buffer)) buffer[strlen(buffer)-1] = '\0';
769
770         // empty lines are syntactically valid, as are comments
771         if (!(*buffer) || buffer[0] == '#')
772                 return "";
773
774         for (unsigned int c = 0; c < strlen(buffer); c++)
775         {
776                 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
777                 // search and replace later :)
778                 if ((!in_quotes) && (buffer[c] == ' '))
779                         buffer[c] = '\xA0';
780                 if ((buffer[c] == '<') && (!in_quotes))
781                 {
782                         has_open_bracket = true;
783                         if (strlen(buffer) == 1)
784                         {
785                                 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
786                                 error = true;
787                                 return "";
788                         }
789                         else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
790                         {
791                                 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
792                                 error = true;
793                                 return "";
794                         }
795                 }
796                 if (buffer[c] == '"')
797                 {
798                         number_of_quotes++;
799                         in_quotes = (!in_quotes);
800                 }
801                 if ((buffer[c] == '=') && (!in_quotes))
802                 {
803                         number_of_equals++;
804                         if (strlen(buffer) == c)
805                         {
806                                 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
807                                 error = true;
808                                 return "";
809                         }
810                         else if (buffer[c+1] != '"')
811                         {
812                                 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
813                                 error = true;
814                                 return "";
815                         }
816                         else if (!c)
817                         {
818                                 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
819                                 error = true;
820                                 return "";
821                         }
822                         else if (buffer[c-1] == '\xA0')
823                         {
824                                 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
825                                 error = true;
826                                 return "";
827                         }
828                 }
829         }
830         // no quotes, and no equals. something freaky.
831         if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
832         {
833                 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
834                 error = true;
835                 return "";
836         }
837         // odd number of quotes. thats just wrong.
838         if ((number_of_quotes % 2) != 0)
839         {
840                 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
841                 error = true;
842                 return "";
843         }
844         if (number_of_equals < (number_of_quotes/2))
845         {
846                 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
847         }
848         if (number_of_equals > (number_of_quotes/2))
849         {
850                 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
851         }
852
853         std::string parsedata = buffer;
854         // turn multispace into single space
855         while (parsedata.find("\xA0\xA0") != std::string::npos)
856         {
857                 parsedata.erase(parsedata.find("\xA0\xA0"),1);
858         }
859
860         // turn our hardspace back into softspace
861         for (unsigned int d = 0; d < parsedata.length(); d++)
862         {
863                 if (parsedata[d] == '\xA0')
864                         parsedata[d] = ' ';
865         }
866
867         // and we're done, the line is fine!
868         return parsedata;
869 }
870
871 int ServerConfig::fgets_safe(char* buffer, size_t maxsize, FILE* &file)
872 {
873         char c_read = '\0';
874         unsigned int bufptr = 0;
875         while ((!feof(file)) && (c_read != '\n') && (c_read != '\r') && (bufptr < maxsize))
876         {
877                 c_read = fgetc(file);
878                 if ((c_read != '\n') && (c_read != '\r'))
879                         buffer[bufptr++] = c_read;
880         }
881         buffer[bufptr] = '\0';
882         return bufptr;
883 }
884
885 bool ServerConfig::LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
886 {
887         target->str("");
888         errorstream->str("");
889         long linenumber = 1;
890         // first, check that the file exists before we try to do anything with it
891         if (!FileExists(filename))
892         {
893                 *errorstream << "File " << filename << " not found." << endl;
894                 return false;
895         }
896         // Fix the chmod of the file to restrict it to the current user and group
897         chmod(filename,0600);
898         for (unsigned int t = 0; t < include_stack.size(); t++)
899         {
900                 if (std::string(filename) == include_stack[t])
901                 {
902                         *errorstream << "File " << filename << " is included recursively (looped inclusion)." << endl;
903                         return false;
904                 }
905         }
906         include_stack.push_back(filename);
907         // now open it
908         FILE* conf = fopen(filename,"r");
909         char buffer[MAXBUF];
910         if (conf)
911         {
912                 while (!feof(conf))
913                 {
914                         if (fgets_safe(buffer, MAXBUF, conf))
915                         {
916                                 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
917                                 {
918                                         if ((buffer[0] != '#') && (buffer[0] != '\r')  && (buffer[0] != '\n'))
919                                         {
920                                                 if (!strncmp(buffer,"<include file=\"",15))
921                                                 {
922                                                         char* buf = buffer;
923                                                         char confpath[10240],newconf[10240];
924                                                         // include file directive
925                                                         buf += 15;      // advance to filename
926                                                         for (unsigned int j = 0; j < strlen(buf); j++)
927                                                         {
928                                                                 if (buf[j] == '\\')
929                                                                         buf[j] = '/';
930                                                                 if (buf[j] == '"')
931                                                                 {
932                                                                         buf[j] = '\0';
933                                                                         break;
934                                                                 }
935                                                         }
936                                                         log(DEBUG,"Opening included file '%s'",buf);
937                                                         if (*buf != '/')
938                                                         {
939                                                                 strlcpy(confpath,CONFIG_FILE,10240);
940                                                                 if (strstr(confpath,"/inspircd.conf"))
941                                                                 {
942                                                                         // leaves us with just the path
943                                                                         *(strstr(confpath,"/inspircd.conf")) = '\0';
944                                                                 }
945                                                                 snprintf(newconf,10240,"%s/%s",confpath,buf);
946                                                         }
947                                                         else strlcpy(newconf,buf,10240);
948                                                         std::stringstream merge(stringstream::in | stringstream::out);
949                                                         // recursively call LoadConf and get the new data, use the same errorstream
950                                                         if (LoadConf(newconf, &merge, errorstream))
951                                                         {
952                                                                 // append to the end of the file
953                                                                 std::string newstuff = merge.str();
954                                                                 *target << newstuff;
955                                                         }
956                                                         else
957                                                         {
958                                                                 // the error propogates up to its parent recursively
959                                                                 // causing the config reader to bail at the top level.
960                                                                 fclose(conf);
961                                                                 return false;
962                                                         }
963                                                 }
964                                                 else
965                                                 {
966                                                         bool error = false;
967                                                         std::string data = this->ConfProcess(buffer,linenumber++,errorstream,error,filename);
968                                                         if (error)
969                                                         {
970                                                                 return false;
971                                                         }
972                                                         *target << data;
973                                                 }
974                                         }
975                                         else linenumber++;
976                                 }
977                         }
978                 }
979                 fclose(conf);
980         }
981         target->seekg(0);
982         return true;
983 }
984
985 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
986
987 int ServerConfig::EnumConf(std::stringstream *config, const char* tag)
988 {
989         int ptr = 0;
990         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
991         int in_token, in_quotes, tptr, idx = 0;
992
993         std::string x = config->str();
994         const char* buf = x.c_str();
995         long bptr = 0;
996         long len = config->str().length();
997         
998         ptr = 0;
999         in_token = 0;
1000         in_quotes = 0;
1001         lastc = '\0';
1002         while (bptr<len)
1003         {
1004                 lastc = c;
1005                 c = buf[bptr++];
1006                 if ((c == '#') && (lastc == '\n'))
1007                 {
1008                         while ((c != '\n') && (bptr<len))
1009                         {
1010                                 lastc = c;
1011                                 c = buf[bptr++];
1012                         }
1013                 }
1014                 if ((c == '<') && (!in_quotes))
1015                 {
1016                         tptr = 0;
1017                         in_token = 1;
1018                         do {
1019                                 c = buf[bptr++];
1020                                 if (c != ' ')
1021                                 {
1022                                         c_tag[tptr++] = c;
1023                                         c_tag[tptr] = '\0';
1024                                 }
1025                         } while (c != ' ');
1026                 }
1027                 if (c == '"')
1028                 {
1029                         in_quotes = (!in_quotes);
1030                 }
1031                 if ((c == '>') && (!in_quotes))
1032                 {
1033                         in_token = 0;
1034                         if (!strcmp(c_tag,tag))
1035                         {
1036                                 /* correct tag, but wrong index */
1037                                 idx++;
1038                         }
1039                         c_tag[0] = '\0';
1040                         buffer[0] = '\0';
1041                         ptr = 0;
1042                         tptr = 0;
1043                 }
1044                 if (c != '>')
1045                 {
1046                         if ((in_token) && (c != '\n') && (c != '\r'))
1047                         {
1048                                 buffer[ptr++] = c;
1049                                 buffer[ptr] = '\0';
1050                         }
1051                 }
1052         }
1053         return idx;
1054 }
1055
1056 /* Counts the number of values within a certain tag */
1057
1058 int ServerConfig::EnumValues(std::stringstream *config, const char* tag, int index)
1059 {
1060         int ptr = 0;
1061         char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
1062         int in_token, in_quotes, tptr, idx = 0;
1063         
1064         bool correct_tag = false;
1065         int num_items = 0;
1066
1067         const char* buf = config->str().c_str();
1068         long bptr = 0;
1069         long len = strlen(buf);
1070         
1071         ptr = 0;
1072         in_token = 0;
1073         in_quotes = 0;
1074         lastc = '\0';
1075         while (bptr<len)
1076         {
1077                 lastc = c;
1078                 c = buf[bptr++];
1079                 if ((c == '#') && (lastc == '\n'))
1080                 {
1081                         while ((c != '\n') && (bptr<len))
1082                         {
1083                                 lastc = c;
1084                                 c = buf[bptr++];
1085                         }
1086                 }
1087                 if ((c == '<') && (!in_quotes))
1088                 {
1089                         tptr = 0;
1090                         in_token = 1;
1091                         do {
1092                                 c = buf[bptr++];
1093                                 if (c != ' ')
1094                                 {
1095                                         c_tag[tptr++] = c;
1096                                         c_tag[tptr] = '\0';
1097                                         
1098                                         if ((!strcmp(c_tag,tag)) && (idx == index))
1099                                         {
1100                                                 correct_tag = true;
1101                                         }
1102                                 }
1103                         } while (c != ' ');
1104                 }
1105                 if (c == '"')
1106                 {
1107                         in_quotes = (!in_quotes);
1108                 }
1109                 
1110                 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
1111                 {
1112                         num_items++;
1113                 }
1114                 if ((c == '>') && (!in_quotes))
1115                 {
1116                         in_token = 0;
1117                         if (correct_tag)
1118                                 correct_tag = false;
1119                         if (!strcmp(c_tag,tag))
1120                         {
1121                                 /* correct tag, but wrong index */
1122                                 idx++;
1123                         }
1124                         c_tag[0] = '\0';
1125                         buffer[0] = '\0';
1126                         ptr = 0;
1127                         tptr = 0;
1128                 }
1129                 if (c != '>')
1130                 {
1131                         if ((in_token) && (c != '\n') && (c != '\r'))
1132                         {
1133                                 buffer[ptr++] = c;
1134                                 buffer[ptr] = '\0';
1135                         }
1136                 }
1137         }
1138         return num_items+1;
1139 }
1140
1141
1142
1143 int ServerConfig::ConfValueEnum(char* tag, std::stringstream* config)
1144 {
1145         return EnumConf(config,tag);
1146 }
1147
1148
1149
1150 /* Retrieves a value from the config file. If there is more than one value of the specified
1151  * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
1152  *
1153  * ConfValue("oper","name",2,result);
1154  */
1155
1156 int ServerConfig::ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
1157 {
1158         int ptr = 0;
1159         char buffer[65535], c_tag[MAXBUF], c, lastc;
1160         int in_token, in_quotes, tptr, idx = 0;
1161         char* key;
1162
1163         std::string x = config->str();
1164         const char* buf = x.c_str();
1165         long bptr = 0;
1166         long len = config->str().length();
1167         
1168         ptr = 0;
1169         in_token = 0;
1170         in_quotes = 0;
1171         lastc = '\0';
1172         c_tag[0] = '\0';
1173         buffer[0] = '\0';
1174         while (bptr<len)
1175         {
1176                 lastc = c;
1177                 c = buf[bptr++];
1178                 // FIX: Treat tabs as spaces
1179                 if (c == 9)
1180                         c = 32;
1181                 if ((c == '<') && (!in_quotes))
1182                 {
1183                         tptr = 0;
1184                         in_token = 1;
1185                         do {
1186                                 c = buf[bptr++];
1187                                 if (c != ' ')
1188                                 {
1189                                         c_tag[tptr++] = c;
1190                                         c_tag[tptr] = '\0';
1191                                 }
1192                         // FIX: Tab can follow a tagname as well as space.
1193                         } while ((c != ' ') && (c != 9));
1194                 }
1195                 if (c == '"')
1196                 {
1197                         in_quotes = (!in_quotes);
1198                 }
1199                 if ((c == '>') && (!in_quotes))
1200                 {
1201                         in_token = 0;
1202                         if (idx == index)
1203                         {
1204                                 if (!strcmp(c_tag,tag))
1205                                 {
1206                                         if ((buffer) && (c_tag) && (var))
1207                                         {
1208                                                 key = strstr(buffer,var);
1209                                                 if (!key)
1210                                                 {
1211                                                         /* value not found in tag */
1212                                                         *result = 0;
1213                                                         return 0;
1214                                                 }
1215                                                 else
1216                                                 {
1217                                                         key+=strlen(var);
1218                                                         while (*key !='"')
1219                                                         {
1220                                                                 if (!*key)
1221                                                                 {
1222                                                                         /* missing quote */
1223                                                                         *result = 0;
1224                                                                         return 0;
1225                                                                 }
1226                                                                 key++;
1227                                                         }
1228                                                         key++;
1229                                                         for (unsigned j = 0; j < strlen(key); j++)
1230                                                         {
1231                                                                 if (key[j] == '"')
1232                                                                 {
1233                                                                         key[j] = '\0';
1234                                                                 }
1235                                                         }
1236                                                         strlcpy(result,key,MAXBUF);
1237                                                         return 1;
1238                                                 }
1239                                         }
1240                                 }
1241                         }
1242                         if (!strcmp(c_tag,tag))
1243                         {
1244                                 /* correct tag, but wrong index */
1245                                 idx++;
1246                         }
1247                         c_tag[0] = '\0';
1248                         buffer[0] = '\0';
1249                         ptr = 0;
1250                         tptr = 0;
1251                 }
1252                 if (c != '>')
1253                 {
1254                         if ((in_token) && (c != '\n') && (c != '\r'))
1255                         {
1256                                 buffer[ptr++] = c;
1257                                 buffer[ptr] = '\0';
1258                         }
1259                 }
1260         }
1261         *result = 0; // value or its tag not found at all
1262         return 0;
1263 }
1264
1265
1266
1267 int ServerConfig::ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
1268 {
1269         ReadConf(config, tag, var, index, result);
1270         return 0;
1271 }
1272
1273
1274
1275 // This will bind a socket to a port. It works for UDP/TCP
1276 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
1277 {
1278         memset((char *)&server,0,sizeof(server));
1279         struct in_addr addy;
1280         inet_aton(addr,&addy);
1281         server.sin_family = AF_INET;
1282         if (!*addr)
1283         {
1284                 server.sin_addr.s_addr = htonl(INADDR_ANY);
1285         }
1286         else
1287         {
1288                 server.sin_addr = addy;
1289         }
1290         server.sin_port = htons(port);
1291         if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
1292         {
1293                 return(ERROR);
1294         }
1295         else
1296         {
1297                 listen(sockfd, Config->MaxConn);
1298                 return(TRUE);
1299         }
1300 }
1301
1302
1303 // Open a TCP Socket
1304 int OpenTCPSocket (void)
1305 {
1306         int sockfd;
1307         int on = 1;
1308         struct linger linger = { 0 };
1309   
1310         if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
1311                 return (ERROR);
1312         else
1313         {
1314                 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
1315                 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
1316                 linger.l_onoff = 1;
1317                 linger.l_linger = 1;
1318                 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
1319                 return (sockfd);
1320         }
1321 }
1322
1323 int BindPorts()
1324 {
1325         char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
1326         sockaddr_in client,server;
1327         int clientportcount = 0;
1328         int BoundPortCount = 0;
1329
1330         for (int count = 0; count < Config->ConfValueEnum("bind",&Config->config_f); count++)
1331         {
1332                 Config->ConfValue("bind","port",count,configToken,&Config->config_f);
1333                 Config->ConfValue("bind","address",count,Addr,&Config->config_f);
1334                 Config->ConfValue("bind","type",count,Type,&Config->config_f);
1335
1336                 if ((!*Type) || (!strcmp(Type,"clients")))
1337                 {
1338                         // modules handle server bind types now
1339                         Config->ports[clientportcount] = atoi(configToken);
1340
1341                         // If the client put bind "*", this is an unrealism.
1342                         // We don't actually support this as documented, but
1343                         // i got fed up of people trying it, so now it converts
1344                         // it to an empty string meaning the same 'bind to all'.
1345                         if (*Addr == '*')
1346                                 *Addr = 0;
1347
1348                         strlcpy(Config->addrs[clientportcount],Addr,256);
1349                         clientportcount++;
1350                         log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
1351                 }
1352         }
1353
1354         int PortCount = clientportcount;
1355
1356         for (int count = 0; count < PortCount; count++)
1357         {
1358                 if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
1359                 {
1360                         log(DEBUG,"InspIRCd: startup: bad fd %lu binding port [%s:%d]",(unsigned long)openSockfd[BoundPortCount],Config->addrs[count],(unsigned long)Config->ports[count]);
1361                         return(ERROR);
1362                 }
1363
1364                 if (BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]) == ERROR)
1365                 {
1366                         log(DEFAULT,"InspIRCd: startup: failed to bind port [%s:%lu]: %s",Config->addrs[count],(unsigned long)Config->ports[count],strerror(errno));
1367                 }
1368                 else
1369                 {
1370                         /* well we at least bound to one socket so we'll continue */
1371                         BoundPortCount++;
1372                 }
1373         }
1374
1375         /* if we didn't bind to anything then abort */
1376         if (!BoundPortCount)
1377         {
1378                 log(DEFAULT,"InspIRCd: startup: no ports bound, bailing!");
1379                 printf("\nERROR: Was not able to bind any of %lu ports! Please check your configuration.\n\n", (unsigned long)PortCount);
1380                 return (ERROR);
1381         }
1382
1383         return BoundPortCount;
1384 }
1385