1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include <sys/resource.h>
22 #include <sys/types.h>
29 #include "inspircd_io.h"
30 #include "inspstring.h"
31 #include "helperfuncs.h"
34 extern ServerConfig *Config;
35 extern InspIRCd* ServerInstance;
36 extern int openSockfd[MAXSOCKS];
40 ServerConfig::ServerConfig()
43 *ServerName = *Network = *ServerDesc = *AdminName = '\0';
44 *AdminEmail = *AdminNick = *diepass = *restartpass = '\0';
45 *motd = *rules = *PrefixQuit = *DieValue = *DNSServer = '\0';
46 *ModPath = *MyExecutable = *DisabledCommands = *PID = '\0';
52 NetBufferSize = 10240;
53 SoftLimit = MAXCLIENTS;
61 void ServerConfig::ClearStack()
63 include_stack.clear();
66 Module* ServerConfig::GetIOHook(int port)
68 std::map<int,Module*>::iterator x = IOHookModule.find(port);
69 return (x != IOHookModule.end() ? x->second : NULL);
72 bool ServerConfig::AddIOHook(int port, Module* iomod)
76 IOHookModule[port] = iomod;
82 bool ServerConfig::DelIOHook(int port)
84 std::map<int,Module*>::iterator x = IOHookModule.find(port);
85 if (x != IOHookModule.end())
87 IOHookModule.erase(x);
93 void ServerConfig::Read(bool bail, userrec* user)
95 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF],MW[MAXBUF],MCON[MAXBUF];
96 char AH[MAXBUF],AP[MAXBUF],AF[MAXBUF],DNT[MAXBUF],pfreq[MAXBUF],thold[MAXBUF],sqmax[MAXBUF],rqmax[MAXBUF],SLIMT[MAXBUF];
98 std::stringstream errstr;
99 include_stack.clear();
101 if (!LoadConf(CONFIG_FILE,&Config->config_f,&errstr))
104 log(DEFAULT,"There were errors in your configuration:\n%s",errstr.str().c_str());
107 printf("There were errors in your configuration:\n%s",errstr.str().c_str());
115 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
116 while (!errstr.eof())
118 errstr.getline(dataline,1024);
119 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
124 WriteOpers("There were errors in the configuration file:");
125 while (!errstr.eof())
127 errstr.getline(dataline,1024);
128 WriteOpers(dataline);
135 ConfValue("server","name",0,Config->ServerName,&Config->config_f);
136 ConfValue("server","description",0,Config->ServerDesc,&Config->config_f);
137 ConfValue("server","network",0,Config->Network,&Config->config_f);
138 ConfValue("admin","name",0,Config->AdminName,&Config->config_f);
139 ConfValue("admin","email",0,Config->AdminEmail,&Config->config_f);
140 ConfValue("admin","nick",0,Config->AdminNick,&Config->config_f);
141 ConfValue("files","motd",0,Config->motd,&Config->config_f);
142 ConfValue("files","rules",0,Config->rules,&Config->config_f);
143 ConfValue("power","diepass",0,Config->diepass,&Config->config_f);
144 ConfValue("power","pause",0,pauseval,&Config->config_f);
145 ConfValue("power","restartpass",0,Config->restartpass,&Config->config_f);
146 ConfValue("options","prefixquit",0,Config->PrefixQuit,&Config->config_f);
147 ConfValue("die","value",0,Config->DieValue,&Config->config_f);
148 ConfValue("options","loglevel",0,dbg,&Config->config_f);
149 ConfValue("options","netbuffersize",0,NB,&Config->config_f);
150 ConfValue("options","maxwho",0,MW,&Config->config_f);
151 ConfValue("options","allowhalfop",0,AH,&Config->config_f);
152 ConfValue("options","allowprotect",0,AP,&Config->config_f);
153 ConfValue("options","allowfounder",0,AF,&Config->config_f);
154 ConfValue("dns","server",0,Config->DNSServer,&Config->config_f);
155 ConfValue("dns","timeout",0,DNT,&Config->config_f);
156 ConfValue("options","moduledir",0,Config->ModPath,&Config->config_f);
157 ConfValue("disabled","commands",0,Config->DisabledCommands,&Config->config_f);
158 ConfValue("options","somaxconn",0,MCON,&Config->config_f);
159 ConfValue("options","softlimit",0,SLIMT,&Config->config_f);
161 Config->SoftLimit = atoi(SLIMT);
162 if ((Config->SoftLimit < 1) || (Config->SoftLimit > MAXCLIENTS))
164 log(DEFAULT,"WARNING: <options:softlimit> value is greater than %d or less than 0, set to %d.",MAXCLIENTS,MAXCLIENTS);
165 Config->SoftLimit = MAXCLIENTS;
167 Config->MaxConn = atoi(MCON);
168 if (Config->MaxConn > SOMAXCONN)
169 log(DEFAULT,"WARNING: <options:somaxconn> value may be higher than the system-defined SOMAXCONN value!");
170 Config->NetBufferSize = atoi(NB);
171 Config->MaxWhoResults = atoi(MW);
172 Config->dns_timeout = atoi(DNT);
173 if (!Config->dns_timeout)
174 Config->dns_timeout = 5;
175 if (!Config->MaxConn)
176 Config->MaxConn = SOMAXCONN;
177 if (!*Config->DNSServer)
178 strlcpy(Config->DNSServer,"127.0.0.1",MAXBUF);
179 if (!*Config->ModPath)
180 strlcpy(Config->ModPath,MOD_PATH,MAXBUF);
181 Config->AllowHalfop = ((!strcasecmp(AH,"true")) || (!strcasecmp(AH,"1")) || (!strcasecmp(AH,"yes")));
182 if ((!Config->NetBufferSize) || (Config->NetBufferSize > 65535) || (Config->NetBufferSize < 1024))
184 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
185 Config->NetBufferSize = 10240;
187 if ((!Config->MaxWhoResults) || (Config->MaxWhoResults > 65535) || (Config->MaxWhoResults < 1))
189 log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
190 Config->MaxWhoResults = 128;
192 Config->LogLevel = DEFAULT;
193 if (!strcmp(dbg,"debug"))
195 Config->LogLevel = DEBUG;
196 Config->debugging = 1;
198 if (!strcmp(dbg,"verbose"))
199 Config->LogLevel = VERBOSE;
200 if (!strcmp(dbg,"default"))
201 Config->LogLevel = DEFAULT;
202 if (!strcmp(dbg,"sparse"))
203 Config->LogLevel = SPARSE;
204 if (!strcmp(dbg,"none"))
205 Config->LogLevel = NONE;
207 readfile(Config->MOTD,Config->motd);
208 log(DEFAULT,"Reading message of the day...");
209 readfile(Config->RULES,Config->rules);
210 log(DEFAULT,"Reading connect classes...");
212 for (int i = 0; i < ConfValueEnum("connect",&Config->config_f); i++)
215 ConfValue("connect","allow",i,Value,&Config->config_f);
216 ConfValue("connect","timeout",i,timeout,&Config->config_f);
217 ConfValue("connect","flood",i,flood,&Config->config_f);
218 ConfValue("connect","pingfreq",i,pfreq,&Config->config_f);
219 ConfValue("connect","threshold",i,thold,&Config->config_f);
220 ConfValue("connect","sendq",i,sqmax,&Config->config_f);
221 ConfValue("connect","recvq",i,rqmax,&Config->config_f);
226 strlcpy(Value,"",MAXBUF);
227 ConfValue("connect","password",i,Value,&Config->config_f);
229 c.registration_timeout = 90; // default is 2 minutes
231 c.flood = atoi(flood);
233 c.sendqmax = 262144; // 256k
234 c.recvqmax = 4096; // 4k
237 c.threshold = atoi(thold);
241 c.sendqmax = atoi(sqmax);
245 c.recvqmax = atoi(rqmax);
249 c.registration_timeout = atoi(timeout);
253 c.pingtime = atoi(pfreq);
255 Classes.push_back(c);
259 ConfValue("connect","deny",i,Value,&Config->config_f);
262 Classes.push_back(c);
263 log(DEBUG,"Read connect class type DENY, host=%s",c.host.c_str());
267 log(DEFAULT,"Reading K lines,Q lines and Z lines from config...");
268 read_xline_defaults();
269 log(DEFAULT,"Applying K lines, Q lines and Z lines...");
270 apply_lines(APPLY_ALL);
272 ConfValue("pid","file",0,Config->PID,&Config->config_f);
273 // write once here, to try it out and make sure its ok
274 WritePID(Config->PID);
276 log(DEFAULT,"Done reading configuration file, InspIRCd is now starting.");
279 log(DEFAULT,"Adding and removing modules due to rehash...");
281 std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
283 // store the old module names
284 for (std::vector<std::string>::iterator t = module_names.begin(); t != module_names.end(); t++)
286 old_module_names.push_back(*t);
289 // get the new module names
290 for (int count2 = 0; count2 < ConfValueEnum("module",&Config->config_f); count2++)
292 ConfValue("module","name",count2,Value,&Config->config_f);
293 new_module_names.push_back(Value);
296 // now create a list of new modules that are due to be loaded
297 // and a seperate list of modules which are due to be unloaded
298 for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
301 for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
307 added_modules.push_back(*_new);
309 for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
312 for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
318 removed_modules.push_back(*oldm);
320 // now we have added_modules, a vector of modules to be loaded, and removed_modules, a vector of modules
322 int rem = 0, add = 0;
323 if (!removed_modules.empty())
324 for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
326 if (ServerInstance->UnloadModule(removing->c_str()))
328 WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
329 WriteServ(user->fd,"973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
334 WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
337 if (!added_modules.empty())
338 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
340 if (ServerInstance->LoadModule(adding->c_str()))
342 WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
343 WriteServ(user->fd,"975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
348 WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
351 log(DEFAULT,"Successfully unloaded %lu of %lu modules and loaded %lu of %lu modules.",(unsigned long)rem,(unsigned long)removed_modules.size(),
352 (unsigned long)add,(unsigned long)added_modules.size());
357 void Exit (int status)
359 if (Config->log_file)
360 fclose(Config->log_file);
361 send_error("Server shutdown.");
365 void Killed(int status)
367 if (Config->log_file)
368 fclose(Config->log_file);
369 send_error("Server terminated.");
373 void Rehash(int status)
375 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
376 Config->Read(false,NULL);
383 printf("\033[1;32mInspire Internet Relay Chat Server, compiled %s at %s\n",__DATE__,__TIME__);
384 printf("(C) ChatSpike Development team.\033[0m\n\n");
385 printf("Developers:\033[1;32m Brain, FrostyCoolSlug\033[0m\n");
386 printf("Documentation:\033[1;32m FrostyCoolSlug, w00t\033[0m\n");
387 printf("Testers:\033[1;32m typobox43, piggles, Lord_Zathras, CC\033[0m\n");
388 printf("Name concept:\033[1;32m Lord_Zathras\033[0m\n\n");
391 void WritePID(std::string filename)
393 ofstream outfile(filename.c_str());
394 if (outfile.is_open())
401 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
402 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
409 signal (SIGALRM, SIG_IGN);
410 signal (SIGHUP, Rehash);
411 signal (SIGPIPE, SIG_IGN);
412 signal (SIGTERM, Exit);
413 signal (SIGSEGV, Error);
417 int DaemonSeed (void)
420 if ((childpid = fork ()) < 0)
422 else if (childpid > 0)
426 printf("InspIRCd Process ID: \033[1;32m%lu\033[0m\n",(unsigned long)getpid());
428 setpriority(PRIO_PROCESS,(int)getpid(),15);
430 if (Config->unlimitcore)
433 if (getrlimit(RLIMIT_CORE, &rl) == -1)
435 log(DEFAULT,"Failed to getrlimit()!");
440 rl.rlim_cur = rl.rlim_max;
441 if (setrlimit(RLIMIT_CORE, &rl) == -1)
442 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
450 /* Make Sure Modules Are Avaliable!
451 * (BugFix By Craig.. See? I do work! :p)
452 * Modified by brain, requires const char*
453 * to work with other API functions
456 bool FileExists (const char* file)
459 if ((input = fopen (file, "r")) == NULL)
470 /* ConfProcess does the following things to a config line in the following order:
472 * Processes the line for syntax errors as shown below
473 * (1) Line void of quotes or equals (a malformed, illegal tag format)
474 * (2) Odd number of quotes on the line indicating a missing quote
475 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
476 * (4) Spaces between the opening bracket (<) and the keyword
477 * (5) Spaces between a keyword and an equals sign
478 * (6) Spaces between an equals sign and a quote
479 * Removes trailing spaces
480 * Removes leading spaces
481 * Converts tabs to spaces
482 * Turns multiple spaces that are outside of quotes into single spaces
485 std::string ServerConfig::ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
487 long number_of_quotes = 0;
488 long number_of_equals = 0;
489 bool has_open_bracket = false;
490 bool in_quotes = false;
496 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
497 for (unsigned int d = 0; d < strlen(buffer); d++)
498 if ((buffer[d]) == 9)
500 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
501 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
503 // empty lines are syntactically valid, as are comments
504 if (!(*buffer) || buffer[0] == '#')
507 for (unsigned int c = 0; c < strlen(buffer); c++)
509 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
510 // search and replace later :)
511 if ((!in_quotes) && (buffer[c] == ' '))
513 if ((buffer[c] == '<') && (!in_quotes))
515 has_open_bracket = true;
516 if (strlen(buffer) == 1)
518 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
522 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
524 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
529 if (buffer[c] == '"')
532 in_quotes = (!in_quotes);
534 if ((buffer[c] == '=') && (!in_quotes))
537 if (strlen(buffer) == c)
539 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
543 else if (buffer[c+1] != '"')
545 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
551 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
555 else if (buffer[c-1] == '\xA0')
557 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
563 // no quotes, and no equals. something freaky.
564 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
566 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
570 // odd number of quotes. thats just wrong.
571 if ((number_of_quotes % 2) != 0)
573 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
577 if (number_of_equals < (number_of_quotes/2))
579 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
581 if (number_of_equals > (number_of_quotes/2))
583 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
586 std::string parsedata = buffer;
587 // turn multispace into single space
588 while (parsedata.find("\xA0\xA0") != std::string::npos)
590 parsedata.erase(parsedata.find("\xA0\xA0"),1);
593 // turn our hardspace back into softspace
594 for (unsigned int d = 0; d < parsedata.length(); d++)
596 if (parsedata[d] == '\xA0')
600 // and we're done, the line is fine!
604 int ServerConfig::fgets_safe(char* buffer, size_t maxsize, FILE* &file)
607 unsigned int bufptr = 0;
608 while ((!feof(file)) && (c_read != '\n') && (c_read != '\r') && (bufptr < maxsize))
610 c_read = fgetc(file);
611 if ((c_read != '\n') && (c_read != '\r'))
612 buffer[bufptr++] = c_read;
614 buffer[bufptr] = '\0';
618 bool ServerConfig::LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
621 errorstream->str("");
623 // first, check that the file exists before we try to do anything with it
624 if (!FileExists(filename))
626 *errorstream << "File " << filename << " not found." << endl;
629 // Fix the chmod of the file to restrict it to the current user and group
630 chmod(filename,0600);
631 for (unsigned int t = 0; t < include_stack.size(); t++)
633 if (std::string(filename) == include_stack[t])
635 *errorstream << "File " << filename << " is included recursively (looped inclusion)." << endl;
639 include_stack.push_back(filename);
641 FILE* conf = fopen(filename,"r");
647 if (fgets_safe(buffer, MAXBUF, conf))
649 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
651 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
653 if (!strncmp(buffer,"<include file=\"",15))
656 char confpath[10240],newconf[10240];
657 // include file directive
658 buf += 15; // advance to filename
659 for (unsigned int j = 0; j < strlen(buf); j++)
669 log(DEFAULT,"Opening included file '%s'",buf);
672 strlcpy(confpath,CONFIG_FILE,10240);
673 if (strstr(confpath,"/inspircd.conf"))
675 // leaves us with just the path
676 *(strstr(confpath,"/inspircd.conf")) = '\0';
678 snprintf(newconf,10240,"%s/%s",confpath,buf);
680 else snprintf(newconf,10240,"%s",buf);
681 std::stringstream merge(stringstream::in | stringstream::out);
682 // recursively call LoadConf and get the new data, use the same errorstream
683 if (LoadConf(newconf, &merge, errorstream))
685 // append to the end of the file
686 std::string newstuff = merge.str();
691 // the error propogates up to its parent recursively
692 // causing the config reader to bail at the top level.
700 std::string data = this->ConfProcess(buffer,linenumber++,errorstream,error,filename);
718 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
720 int ServerConfig::EnumConf(std::stringstream *config, const char* tag)
723 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
724 int in_token, in_quotes, tptr, idx = 0;
726 const char* buf = config->str().c_str();
728 long len = strlen(buf);
738 if ((c == '#') && (lastc == '\n'))
740 while ((c != '\n') && (bptr<len))
746 if ((c == '<') && (!in_quotes))
761 in_quotes = (!in_quotes);
763 if ((c == '>') && (!in_quotes))
766 if (!strcmp(c_tag,tag))
768 /* correct tag, but wrong index */
778 if ((in_token) && (c != '\n') && (c != '\r'))
788 /* Counts the number of values within a certain tag */
790 int ServerConfig::EnumValues(std::stringstream *config, const char* tag, int index)
793 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
794 int in_token, in_quotes, tptr, idx = 0;
796 bool correct_tag = false;
799 const char* buf = config->str().c_str();
801 long len = strlen(buf);
811 if ((c == '#') && (lastc == '\n'))
813 while ((c != '\n') && (bptr<len))
819 if ((c == '<') && (!in_quotes))
830 if ((!strcmp(c_tag,tag)) && (idx == index))
839 in_quotes = (!in_quotes);
842 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
846 if ((c == '>') && (!in_quotes))
851 if (!strcmp(c_tag,tag))
853 /* correct tag, but wrong index */
863 if ((in_token) && (c != '\n') && (c != '\r'))
875 int ServerConfig::ConfValueEnum(char* tag, std::stringstream* config)
877 return EnumConf(config,tag);
882 /* Retrieves a value from the config file. If there is more than one value of the specified
883 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
885 * ConfValue("oper","name",2,result);
888 int ServerConfig::ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
891 char buffer[65535], c_tag[MAXBUF], c, lastc;
892 int in_token, in_quotes, tptr, idx = 0;
895 const char* buf = config->str().c_str();
897 long len = config->str().length();
909 // FIX: Treat tabs as spaces
912 if ((c == '<') && (!in_quotes))
923 // FIX: Tab can follow a tagname as well as space.
924 } while ((c != ' ') && (c != 9));
928 in_quotes = (!in_quotes);
930 if ((c == '>') && (!in_quotes))
935 if (!strcmp(c_tag,tag))
937 if ((buffer) && (c_tag) && (var))
939 key = strstr(buffer,var);
942 /* value not found in tag */
960 for (unsigned j = 0; j < strlen(key); j++)
967 strlcpy(result,key,MAXBUF);
973 if (!strcmp(c_tag,tag))
975 /* correct tag, but wrong index */
985 if ((in_token) && (c != '\n') && (c != '\r'))
992 *result = 0; // value or its tag not found at all
998 int ServerConfig::ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
1000 ReadConf(config, tag, var, index, result);
1006 // This will bind a socket to a port. It works for UDP/TCP
1007 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
1009 memset((char *)&server,0,sizeof(server));
1010 struct in_addr addy;
1011 inet_aton(addr,&addy);
1012 server.sin_family = AF_INET;
1013 if (!strcmp(addr,""))
1015 server.sin_addr.s_addr = htonl(INADDR_ANY);
1019 server.sin_addr = addy;
1021 server.sin_port = htons(port);
1022 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
1028 listen(sockfd, Config->MaxConn);
1034 // Open a TCP Socket
1035 int OpenTCPSocket (void)
1039 struct linger linger = { 0 };
1041 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
1045 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
1046 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
1048 linger.l_linger = 1;
1049 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
1056 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
1057 sockaddr_in client,server;
1058 int clientportcount = 0;
1059 int BoundPortCount = 0;
1060 for (int count = 0; count < Config->ConfValueEnum("bind",&Config->config_f); count++)
1062 Config->ConfValue("bind","port",count,configToken,&Config->config_f);
1063 Config->ConfValue("bind","address",count,Addr,&Config->config_f);
1064 Config->ConfValue("bind","type",count,Type,&Config->config_f);
1065 if (strcmp(Type,"servers"))
1067 // modules handle server bind types now,
1068 // its not a typo in the strcmp.
1069 Config->ports[clientportcount] = atoi(configToken);
1070 strlcpy(Config->addrs[clientportcount],Addr,256);
1072 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
1075 int PortCount = clientportcount;
1077 for (int count = 0; count < PortCount; count++)
1079 if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
1081 log(DEBUG,"InspIRCd: startup: bad fd %lu",(unsigned long)openSockfd[BoundPortCount]);
1084 if (BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]) == ERROR)
1086 log(DEFAULT,"InspIRCd: startup: failed to bind port %lu",(unsigned long)Config->ports[count]);
1088 else /* well we at least bound to one socket so we'll continue */
1094 /* if we didn't bind to anything then abort */
1095 if (!BoundPortCount)
1097 log(DEFAULT,"InspIRCd: startup: no ports bound, bailing!");
1098 printf("\nERROR: Was not able to bind any of %lu ports! Please check your configuration.\n\n", (unsigned long)PortCount);
1102 return BoundPortCount;