1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 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"
32 #include "userprocess.h"
35 extern ServerConfig *Config;
36 extern InspIRCd* ServerInstance;
37 extern int openSockfd[MAXSOCKS];
41 ServerConfig::ServerConfig()
44 *ServerName = *Network = *ServerDesc = *AdminName = '\0';
45 *AdminEmail = *AdminNick = *diepass = *restartpass = '\0';
46 *motd = *rules = *PrefixQuit = *DieValue = *DNSServer = '\0';
47 *OperOnlyStats = *ModPath = *MyExecutable = *DisabledCommands = *PID = '\0';
53 NetBufferSize = 10240;
54 SoftLimit = MAXCLIENTS;
62 void ServerConfig::ClearStack()
64 include_stack.clear();
67 Module* ServerConfig::GetIOHook(int port)
69 std::map<int,Module*>::iterator x = IOHookModule.find(port);
70 return (x != IOHookModule.end() ? x->second : NULL);
73 bool ServerConfig::AddIOHook(int port, Module* iomod)
77 IOHookModule[port] = iomod;
83 bool ServerConfig::DelIOHook(int port)
85 std::map<int,Module*>::iterator x = IOHookModule.find(port);
86 if (x != IOHookModule.end())
88 IOHookModule.erase(x);
94 void ServerConfig::Read(bool bail, userrec* user)
96 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF],MW[MAXBUF],MCON[MAXBUF];
97 char AH[MAXBUF],AP[MAXBUF],AF[MAXBUF],DNT[MAXBUF],pfreq[MAXBUF],thold[MAXBUF],sqmax[MAXBUF],rqmax[MAXBUF],SLIMT[MAXBUF];
99 std::stringstream errstr;
100 include_stack.clear();
102 if (!LoadConf(CONFIG_FILE,&Config->config_f,&errstr))
105 log(DEFAULT,"There were errors in your configuration:\n%s",errstr.str().c_str());
108 printf("There were errors in your configuration:\n%s",errstr.str().c_str());
116 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
117 while (!errstr.eof())
119 errstr.getline(dataline,1024);
120 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
125 WriteOpers("There were errors in the configuration file:");
126 while (!errstr.eof())
128 errstr.getline(dataline,1024);
129 WriteOpers(dataline);
136 ConfValue("server","name",0,Config->ServerName,&Config->config_f);
137 ConfValue("server","description",0,Config->ServerDesc,&Config->config_f);
138 ConfValue("server","network",0,Config->Network,&Config->config_f);
139 ConfValue("admin","name",0,Config->AdminName,&Config->config_f);
140 ConfValue("admin","email",0,Config->AdminEmail,&Config->config_f);
141 ConfValue("admin","nick",0,Config->AdminNick,&Config->config_f);
142 ConfValue("files","motd",0,Config->motd,&Config->config_f);
143 ConfValue("files","rules",0,Config->rules,&Config->config_f);
144 ConfValue("power","diepass",0,Config->diepass,&Config->config_f);
145 ConfValue("power","pause",0,pauseval,&Config->config_f);
146 ConfValue("power","restartpass",0,Config->restartpass,&Config->config_f);
147 ConfValue("options","prefixquit",0,Config->PrefixQuit,&Config->config_f);
148 ConfValue("die","value",0,Config->DieValue,&Config->config_f);
149 ConfValue("options","loglevel",0,dbg,&Config->config_f);
150 ConfValue("options","netbuffersize",0,NB,&Config->config_f);
151 ConfValue("options","maxwho",0,MW,&Config->config_f);
152 ConfValue("options","allowhalfop",0,AH,&Config->config_f);
153 ConfValue("options","allowprotect",0,AP,&Config->config_f);
154 ConfValue("options","allowfounder",0,AF,&Config->config_f);
155 ConfValue("dns","server",0,Config->DNSServer,&Config->config_f);
156 ConfValue("dns","timeout",0,DNT,&Config->config_f);
157 ConfValue("options","moduledir",0,Config->ModPath,&Config->config_f);
158 ConfValue("disabled","commands",0,Config->DisabledCommands,&Config->config_f);
159 ConfValue("options","somaxconn",0,MCON,&Config->config_f);
160 ConfValue("options","softlimit",0,SLIMT,&Config->config_f);
161 ConfValue("options","operonlystats",0,Config->OperOnlyStats,&Config->config_f);
163 Config->SoftLimit = atoi(SLIMT);
164 if ((Config->SoftLimit < 1) || (Config->SoftLimit > MAXCLIENTS))
166 log(DEFAULT,"WARNING: <options:softlimit> value is greater than %d or less than 0, set to %d.",MAXCLIENTS,MAXCLIENTS);
167 Config->SoftLimit = MAXCLIENTS;
169 Config->MaxConn = atoi(MCON);
170 if (Config->MaxConn > SOMAXCONN)
171 log(DEFAULT,"WARNING: <options:somaxconn> value may be higher than the system-defined SOMAXCONN value!");
172 Config->NetBufferSize = atoi(NB);
173 Config->MaxWhoResults = atoi(MW);
174 Config->dns_timeout = atoi(DNT);
175 if (!Config->dns_timeout)
176 Config->dns_timeout = 5;
177 if (!Config->MaxConn)
178 Config->MaxConn = SOMAXCONN;
179 if (!*Config->DNSServer)
180 strlcpy(Config->DNSServer,"127.0.0.1",MAXBUF);
181 if (!*Config->ModPath)
182 strlcpy(Config->ModPath,MOD_PATH,MAXBUF);
183 Config->AllowHalfop = ((!strcasecmp(AH,"true")) || (!strcasecmp(AH,"1")) || (!strcasecmp(AH,"yes")));
184 if ((!Config->NetBufferSize) || (Config->NetBufferSize > 65535) || (Config->NetBufferSize < 1024))
186 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
187 Config->NetBufferSize = 10240;
189 if ((!Config->MaxWhoResults) || (Config->MaxWhoResults > 65535) || (Config->MaxWhoResults < 1))
191 log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
192 Config->MaxWhoResults = 128;
194 Config->LogLevel = DEFAULT;
195 if (!strcmp(dbg,"debug"))
197 Config->LogLevel = DEBUG;
198 Config->debugging = 1;
200 if (!strcmp(dbg,"verbose"))
201 Config->LogLevel = VERBOSE;
202 if (!strcmp(dbg,"default"))
203 Config->LogLevel = DEFAULT;
204 if (!strcmp(dbg,"sparse"))
205 Config->LogLevel = SPARSE;
206 if (!strcmp(dbg,"none"))
207 Config->LogLevel = NONE;
209 readfile(Config->MOTD,Config->motd);
210 log(DEFAULT,"Reading message of the day...");
211 readfile(Config->RULES,Config->rules);
212 log(DEFAULT,"Reading connect classes...");
214 for (int i = 0; i < ConfValueEnum("connect",&Config->config_f); i++)
217 ConfValue("connect","allow",i,Value,&Config->config_f);
218 ConfValue("connect","timeout",i,timeout,&Config->config_f);
219 ConfValue("connect","flood",i,flood,&Config->config_f);
220 ConfValue("connect","pingfreq",i,pfreq,&Config->config_f);
221 ConfValue("connect","threshold",i,thold,&Config->config_f);
222 ConfValue("connect","sendq",i,sqmax,&Config->config_f);
223 ConfValue("connect","recvq",i,rqmax,&Config->config_f);
228 strlcpy(Value,"",MAXBUF);
229 ConfValue("connect","password",i,Value,&Config->config_f);
231 c.registration_timeout = 90; // default is 2 minutes
233 c.flood = atoi(flood);
235 c.sendqmax = 262144; // 256k
236 c.recvqmax = 4096; // 4k
239 c.threshold = atoi(thold);
243 c.sendqmax = atoi(sqmax);
247 c.recvqmax = atoi(rqmax);
251 c.registration_timeout = atoi(timeout);
255 c.pingtime = atoi(pfreq);
257 Classes.push_back(c);
261 ConfValue("connect","deny",i,Value,&Config->config_f);
264 Classes.push_back(c);
265 log(DEBUG,"Read connect class type DENY, host=%s",c.host.c_str());
269 log(DEFAULT,"Reading K lines,Q lines and Z lines from config...");
270 read_xline_defaults();
271 log(DEFAULT,"Applying K lines, Q lines and Z lines...");
272 apply_lines(APPLY_ALL);
274 ConfValue("pid","file",0,Config->PID,&Config->config_f);
275 // write once here, to try it out and make sure its ok
276 WritePID(Config->PID);
278 log(DEFAULT,"Done reading configuration file, InspIRCd is now starting.");
281 log(DEFAULT,"Adding and removing modules due to rehash...");
283 std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
285 // store the old module names
286 for (std::vector<std::string>::iterator t = module_names.begin(); t != module_names.end(); t++)
288 old_module_names.push_back(*t);
291 // get the new module names
292 for (int count2 = 0; count2 < ConfValueEnum("module",&Config->config_f); count2++)
294 ConfValue("module","name",count2,Value,&Config->config_f);
295 new_module_names.push_back(Value);
298 // now create a list of new modules that are due to be loaded
299 // and a seperate list of modules which are due to be unloaded
300 for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
303 for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
309 added_modules.push_back(*_new);
311 for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
314 for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
320 removed_modules.push_back(*oldm);
322 // now we have added_modules, a vector of modules to be loaded, and removed_modules, a vector of modules
324 int rem = 0, add = 0;
325 if (!removed_modules.empty())
326 for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
328 if (ServerInstance->UnloadModule(removing->c_str()))
330 WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
331 WriteServ(user->fd,"973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
336 WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
339 if (!added_modules.empty())
340 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
342 if (ServerInstance->LoadModule(adding->c_str()))
344 WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
345 WriteServ(user->fd,"975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
350 WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
353 log(DEFAULT,"Successfully unloaded %lu of %lu modules and loaded %lu of %lu modules.",(unsigned long)rem,(unsigned long)removed_modules.size(),
354 (unsigned long)add,(unsigned long)added_modules.size());
359 void Exit (int status)
361 if (Config->log_file)
362 fclose(Config->log_file);
363 send_error("Server shutdown.");
367 void Killed(int status)
369 if (Config->log_file)
370 fclose(Config->log_file);
371 send_error("Server terminated.");
375 void Rehash(int status)
377 WriteOpers("Rehashing config file %s due to SIGHUP",CONFIG_FILE);
378 fclose(Config->log_file);
380 Config->Read(false,NULL);
387 printf("\033[1;32mInspire Internet Relay Chat Server, compiled %s at %s\n",__DATE__,__TIME__);
388 printf("(C) ChatSpike Development team.\033[0m\n\n");
389 printf("Developers:\033[1;32m Brain, FrostyCoolSlug\033[0m\n");
390 printf("Documentation:\033[1;32m FrostyCoolSlug, w00t\033[0m\n");
391 printf("Testers:\033[1;32m typobox43, piggles, Lord_Zathras, CC\033[0m\n");
392 printf("Name concept:\033[1;32m Lord_Zathras\033[0m\n\n");
395 void WritePID(std::string filename)
397 ofstream outfile(filename.c_str());
398 if (outfile.is_open())
405 printf("Failed to write PID-file '%s', exiting.\n",filename.c_str());
406 log(DEFAULT,"Failed to write PID-file '%s', exiting.",filename.c_str());
413 signal (SIGALRM, SIG_IGN);
414 signal (SIGHUP, Rehash);
415 signal (SIGPIPE, SIG_IGN);
416 signal (SIGTERM, Exit);
417 signal (SIGSEGV, Error);
421 int DaemonSeed (void)
424 if ((childpid = fork ()) < 0)
426 else if (childpid > 0)
430 printf("InspIRCd Process ID: \033[1;32m%lu\033[0m\n",(unsigned long)getpid());
432 setpriority(PRIO_PROCESS,(int)getpid(),15);
434 if (Config->unlimitcore)
437 if (getrlimit(RLIMIT_CORE, &rl) == -1)
439 log(DEFAULT,"Failed to getrlimit()!");
444 rl.rlim_cur = rl.rlim_max;
445 if (setrlimit(RLIMIT_CORE, &rl) == -1)
446 log(DEFAULT,"setrlimit() failed, cannot increase coredump size.");
454 /* Make Sure Modules Are Avaliable!
455 * (BugFix By Craig.. See? I do work! :p)
456 * Modified by brain, requires const char*
457 * to work with other API functions
460 bool FileExists (const char* file)
463 if ((input = fopen (file, "r")) == NULL)
474 /* ConfProcess does the following things to a config line in the following order:
476 * Processes the line for syntax errors as shown below
477 * (1) Line void of quotes or equals (a malformed, illegal tag format)
478 * (2) Odd number of quotes on the line indicating a missing quote
479 * (3) number of equals signs not equal to number of quotes / 2 (missing an equals sign)
480 * (4) Spaces between the opening bracket (<) and the keyword
481 * (5) Spaces between a keyword and an equals sign
482 * (6) Spaces between an equals sign and a quote
483 * Removes trailing spaces
484 * Removes leading spaces
485 * Converts tabs to spaces
486 * Turns multiple spaces that are outside of quotes into single spaces
489 std::string ServerConfig::ConfProcess(char* buffer, long linenumber, std::stringstream* errorstream, bool &error, std::string filename)
491 long number_of_quotes = 0;
492 long number_of_equals = 0;
493 bool has_open_bracket = false;
494 bool in_quotes = false;
500 // firstly clean up the line by stripping spaces from the start and end and converting tabs to spaces
501 for (unsigned int d = 0; d < strlen(buffer); d++)
502 if ((buffer[d]) == 9)
504 while ((buffer[0] == ' ') && (strlen(buffer)>0)) buffer++;
505 while ((buffer[strlen(buffer)-1] == ' ') && (strlen(buffer)>0)) buffer[strlen(buffer)-1] = '\0';
507 // empty lines are syntactically valid, as are comments
508 if (!(*buffer) || buffer[0] == '#')
511 for (unsigned int c = 0; c < strlen(buffer); c++)
513 // convert all spaces that are OUTSIDE quotes into hardspace (0xA0) as this will make them easier to
514 // search and replace later :)
515 if ((!in_quotes) && (buffer[c] == ' '))
517 if ((buffer[c] == '<') && (!in_quotes))
519 has_open_bracket = true;
520 if (strlen(buffer) == 1)
522 *errorstream << "Tag without identifier at " << filename << ":" << linenumber << endl;
526 else if ((tolower(buffer[c+1]) < 'a') || (tolower(buffer[c+1]) > 'z'))
528 *errorstream << "Invalid characters in identifier at " << filename << ":" << linenumber << endl;
533 if (buffer[c] == '"')
536 in_quotes = (!in_quotes);
538 if ((buffer[c] == '=') && (!in_quotes))
541 if (strlen(buffer) == c)
543 *errorstream << "Variable without a value at " << filename << ":" << linenumber << endl;
547 else if (buffer[c+1] != '"')
549 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
555 *errorstream << "Value without a variable (line starts with '=') at " << filename << ":" << linenumber << endl;
559 else if (buffer[c-1] == '\xA0')
561 *errorstream << "Variable name not followed immediately by its value at " << filename << ":" << linenumber << endl;
567 // no quotes, and no equals. something freaky.
568 if ((!number_of_quotes) || (!number_of_equals) && (strlen(buffer)>2) && (buffer[0]=='<'))
570 *errorstream << "Malformed tag at " << filename << ":" << linenumber << endl;
574 // odd number of quotes. thats just wrong.
575 if ((number_of_quotes % 2) != 0)
577 *errorstream << "Missing \" at " << filename << ":" << linenumber << endl;
581 if (number_of_equals < (number_of_quotes/2))
583 *errorstream << "Missing '=' at " << filename << ":" << linenumber << endl;
585 if (number_of_equals > (number_of_quotes/2))
587 *errorstream << "Too many '=' at " << filename << ":" << linenumber << endl;
590 std::string parsedata = buffer;
591 // turn multispace into single space
592 while (parsedata.find("\xA0\xA0") != std::string::npos)
594 parsedata.erase(parsedata.find("\xA0\xA0"),1);
597 // turn our hardspace back into softspace
598 for (unsigned int d = 0; d < parsedata.length(); d++)
600 if (parsedata[d] == '\xA0')
604 // and we're done, the line is fine!
608 int ServerConfig::fgets_safe(char* buffer, size_t maxsize, FILE* &file)
611 unsigned int bufptr = 0;
612 while ((!feof(file)) && (c_read != '\n') && (c_read != '\r') && (bufptr < maxsize))
614 c_read = fgetc(file);
615 if ((c_read != '\n') && (c_read != '\r'))
616 buffer[bufptr++] = c_read;
618 buffer[bufptr] = '\0';
622 bool ServerConfig::LoadConf(const char* filename, std::stringstream *target, std::stringstream* errorstream)
625 errorstream->str("");
627 // first, check that the file exists before we try to do anything with it
628 if (!FileExists(filename))
630 *errorstream << "File " << filename << " not found." << endl;
633 // Fix the chmod of the file to restrict it to the current user and group
634 chmod(filename,0600);
635 for (unsigned int t = 0; t < include_stack.size(); t++)
637 if (std::string(filename) == include_stack[t])
639 *errorstream << "File " << filename << " is included recursively (looped inclusion)." << endl;
643 include_stack.push_back(filename);
645 FILE* conf = fopen(filename,"r");
651 if (fgets_safe(buffer, MAXBUF, conf))
653 if ((!feof(conf)) && (buffer) && (strlen(buffer)))
655 if ((buffer[0] != '#') && (buffer[0] != '\r') && (buffer[0] != '\n'))
657 if (!strncmp(buffer,"<include file=\"",15))
660 char confpath[10240],newconf[10240];
661 // include file directive
662 buf += 15; // advance to filename
663 for (unsigned int j = 0; j < strlen(buf); j++)
673 log(DEFAULT,"Opening included file '%s'",buf);
676 strlcpy(confpath,CONFIG_FILE,10240);
677 if (strstr(confpath,"/inspircd.conf"))
679 // leaves us with just the path
680 *(strstr(confpath,"/inspircd.conf")) = '\0';
682 snprintf(newconf,10240,"%s/%s",confpath,buf);
684 else strlcpy(newconf,buf,10240);
685 std::stringstream merge(stringstream::in | stringstream::out);
686 // recursively call LoadConf and get the new data, use the same errorstream
687 if (LoadConf(newconf, &merge, errorstream))
689 // append to the end of the file
690 std::string newstuff = merge.str();
695 // the error propogates up to its parent recursively
696 // causing the config reader to bail at the top level.
704 std::string data = this->ConfProcess(buffer,linenumber++,errorstream,error,filename);
722 /* Counts the number of tags of a certain type within the config file, e.g. to enumerate opers */
724 int ServerConfig::EnumConf(std::stringstream *config, const char* tag)
727 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
728 int in_token, in_quotes, tptr, idx = 0;
730 const char* buf = config->str().c_str();
732 long len = strlen(buf);
742 if ((c == '#') && (lastc == '\n'))
744 while ((c != '\n') && (bptr<len))
750 if ((c == '<') && (!in_quotes))
765 in_quotes = (!in_quotes);
767 if ((c == '>') && (!in_quotes))
770 if (!strcmp(c_tag,tag))
772 /* correct tag, but wrong index */
782 if ((in_token) && (c != '\n') && (c != '\r'))
792 /* Counts the number of values within a certain tag */
794 int ServerConfig::EnumValues(std::stringstream *config, const char* tag, int index)
797 char buffer[MAXBUF], c_tag[MAXBUF], c, lastc;
798 int in_token, in_quotes, tptr, idx = 0;
800 bool correct_tag = false;
803 const char* buf = config->str().c_str();
805 long len = strlen(buf);
815 if ((c == '#') && (lastc == '\n'))
817 while ((c != '\n') && (bptr<len))
823 if ((c == '<') && (!in_quotes))
834 if ((!strcmp(c_tag,tag)) && (idx == index))
843 in_quotes = (!in_quotes);
846 if ( (correct_tag) && (!in_quotes) && ( (c == ' ') || (c == '\n') || (c == '\r') ) )
850 if ((c == '>') && (!in_quotes))
855 if (!strcmp(c_tag,tag))
857 /* correct tag, but wrong index */
867 if ((in_token) && (c != '\n') && (c != '\r'))
879 int ServerConfig::ConfValueEnum(char* tag, std::stringstream* config)
881 return EnumConf(config,tag);
886 /* Retrieves a value from the config file. If there is more than one value of the specified
887 * key and section (e.g. for opers etc) then the index value specifies which to retreive, e.g.
889 * ConfValue("oper","name",2,result);
892 int ServerConfig::ReadConf(std::stringstream *config, const char* tag, const char* var, int index, char *result)
895 char buffer[65535], c_tag[MAXBUF], c, lastc;
896 int in_token, in_quotes, tptr, idx = 0;
899 const char* buf = config->str().c_str();
901 long len = config->str().length();
913 // FIX: Treat tabs as spaces
916 if ((c == '<') && (!in_quotes))
927 // FIX: Tab can follow a tagname as well as space.
928 } while ((c != ' ') && (c != 9));
932 in_quotes = (!in_quotes);
934 if ((c == '>') && (!in_quotes))
939 if (!strcmp(c_tag,tag))
941 if ((buffer) && (c_tag) && (var))
943 key = strstr(buffer,var);
946 /* value not found in tag */
964 for (unsigned j = 0; j < strlen(key); j++)
971 strlcpy(result,key,MAXBUF);
977 if (!strcmp(c_tag,tag))
979 /* correct tag, but wrong index */
989 if ((in_token) && (c != '\n') && (c != '\r'))
996 *result = 0; // value or its tag not found at all
1002 int ServerConfig::ConfValue(char* tag, char* var, int index, char *result,std::stringstream *config)
1004 ReadConf(config, tag, var, index, result);
1010 // This will bind a socket to a port. It works for UDP/TCP
1011 int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
1013 memset((char *)&server,0,sizeof(server));
1014 struct in_addr addy;
1015 inet_aton(addr,&addy);
1016 server.sin_family = AF_INET;
1017 if (!strcmp(addr,""))
1019 server.sin_addr.s_addr = htonl(INADDR_ANY);
1023 server.sin_addr = addy;
1025 server.sin_port = htons(port);
1026 if (bind(sockfd,(struct sockaddr*)&server,sizeof(server))<0)
1032 listen(sockfd, Config->MaxConn);
1038 // Open a TCP Socket
1039 int OpenTCPSocket (void)
1043 struct linger linger = { 0 };
1045 if ((sockfd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
1049 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&on, sizeof(on));
1050 /* This is BSD compatible, setting l_onoff to 0 is *NOT* http://web.irc.org/mla/ircd-dev/msg02259.html */
1052 linger.l_linger = 1;
1053 setsockopt(sockfd, SOL_SOCKET, SO_LINGER, (const char*)&linger,sizeof(linger));
1060 char configToken[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
1061 sockaddr_in client,server;
1062 int clientportcount = 0;
1063 int BoundPortCount = 0;
1064 for (int count = 0; count < Config->ConfValueEnum("bind",&Config->config_f); count++)
1066 Config->ConfValue("bind","port",count,configToken,&Config->config_f);
1067 Config->ConfValue("bind","address",count,Addr,&Config->config_f);
1068 Config->ConfValue("bind","type",count,Type,&Config->config_f);
1069 if (strcmp(Type,"servers"))
1071 // modules handle server bind types now,
1072 // its not a typo in the strcmp.
1073 Config->ports[clientportcount] = atoi(configToken);
1074 strlcpy(Config->addrs[clientportcount],Addr,256);
1076 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
1079 int PortCount = clientportcount;
1081 for (int count = 0; count < PortCount; count++)
1083 if ((openSockfd[BoundPortCount] = OpenTCPSocket()) == ERROR)
1085 log(DEBUG,"InspIRCd: startup: bad fd %lu",(unsigned long)openSockfd[BoundPortCount]);
1088 if (BindSocket(openSockfd[BoundPortCount],client,server,Config->ports[count],Config->addrs[count]) == ERROR)
1090 log(DEFAULT,"InspIRCd: startup: failed to bind port %lu",(unsigned long)Config->ports[count]);
1092 else /* well we at least bound to one socket so we'll continue */
1098 /* if we didn't bind to anything then abort */
1099 if (!BoundPortCount)
1101 log(DEFAULT,"InspIRCd: startup: no ports bound, bailing!");
1102 printf("\nERROR: Was not able to bind any of %lu ports! Please check your configuration.\n\n", (unsigned long)PortCount);
1106 return BoundPortCount;