1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2005 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 * ---------------------------------------------------
17 /* Now with added unF! ;) */
21 #include "inspircd_config.h"
23 #include "inspircd_io.h"
24 #include "inspircd_util.h"
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/utsname.h>
33 #include <ext/hash_map>
55 #include "inspstring.h"
57 #include "helperfuncs.h"
59 #include "socketengine.h"
60 #include "userprocess.h"
65 InspIRCd* ServerInstance;
67 int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
68 int WHOWAS_MAX = 100; // default 100 people maximum in the WHOWAS list
70 extern std::vector<Module*> modules;
71 extern std::vector<ircd_module*> factory;
72 std::vector<InspSocket*> module_sockets;
75 int openSockfd[MAXSOCKS];
76 sockaddr_in client,server;
79 extern InspSocket* socket_ref[65535];
81 time_t TIME = time(NULL), OLDTIME = time(NULL);
83 SocketEngine* SE = NULL;
85 // This table references users by file descriptor.
86 // its an array to make it VERY fast, as all lookups are referenced
87 // by an integer, meaning there is no need for a scan/search operation.
88 userrec* fd_ref_table[65536];
90 serverstats* stats = new serverstats;
91 Server* MyServer = new Server;
92 ServerConfig *Config = new ServerConfig;
97 command_table cmdlist;
98 servernamelist servernames;
99 int BoundPortCount = 0;
102 void AddServerName(std::string servername)
104 log(DEBUG,"Adding server name: %s",servername.c_str());
105 for (servernamelist::iterator a = servernames.begin(); a < servernames.end(); a++)
107 if (*a == servername)
110 servernames.push_back(servername);
113 const char* FindServerNamePtr(std::string servername)
115 for (servernamelist::iterator a = servernames.begin(); a < servernames.end(); a++)
117 if (*a == servername)
120 AddServerName(servername);
121 return FindServerNamePtr(servername);
124 std::string InspIRCd::GetRevision()
126 /* w00t got me to replace a bunch of strtok_r
127 * with something nicer, so i did this. Its the
128 * same thing really, only in C++. It places the
129 * text into a std::stringstream which is a readable
130 * and writeable buffer stream, and then pops two
131 * words off it, space delimited. Because it reads
132 * into the same variable twice, the first word
133 * is discarded, and the second one returned.
135 std::stringstream Revision("$Revision$");
137 Revision >> single >> single;
142 /* This function pokes and hacks at a parameter list like the following:
144 * PART #winbot,#darkgalaxy :m00!
146 * to turn it into a series of individual calls like this:
149 * PART #darkgalaxy :m00!
151 * The seperate calls are sent to a callback function provided by the caller
152 * (the caller will usually call itself recursively). The callback function
153 * must be a command handler. Calling this function on a line with no list causes
154 * no action to be taken. You must provide a starting and ending parameter number
155 * where the range of the list can be found, useful if you have a terminating
156 * parameter as above which is actually not part of the list, or parameters
157 * before the actual list as well. This code is used by many functions which
158 * can function as "one to list" (see the RFC) */
160 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
165 char blog[32][MAXBUF];
166 char blog2[32][MAXBUF];
167 int j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
171 for (int i = 0; i <32; i++)
174 for (int i = 0; i <32; i++)
178 for (int i = 0; i <10; i++)
187 if (pcnt > 1) /* we have a key to copy */
189 strlcpy(keystr,parameters[1],MAXBUF);
193 if (!parameters[start])
197 if (!strchr(parameters[start],','))
202 for (int i = start; i <= end; i++)
206 strlcat(plist,parameters[i],MAXBUF);
214 for (int i = 0; i < t; i++)
219 strlcpy(blog[j++],param,MAXBUF);
223 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
228 strlcpy(blog[j++],param,MAXBUF);
231 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
236 if ((joins) && (keystr))
238 if (strchr(keystr,','))
243 for (int i = 0; i < t2; i++)
245 if (keystr[i] == ',')
248 strlcpy(blog2[j++],param,MAXBUF);
252 strlcpy(blog2[j++],param,MAXBUF);
257 for (j = 0; j < total; j++)
263 for (q = end; q < pcnt-1; q++)
267 pars[q-end+1] = parameters[q+1];
270 if ((joins) && (parameters[1]))
281 /* repeatedly call the function with the hacked parameter list */
282 if ((joins) && (pcnt > 1))
286 // pars[1] already set up and containing key from blog2[j]
291 pars[1] = parameters[1];
297 fn(pars,pcnt-(end-start),u);
306 InspIRCd::InspIRCd(int argc, char** argv)
309 module_sockets.clear();
310 this->startup_time = time(NULL);
312 log(DEBUG,"*** InspIRCd starting up!");
313 if (!FileExists(CONFIG_FILE))
315 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
316 log(DEFAULT,"main: no config");
317 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
321 for (int i = 1; i < argc; i++)
323 if (!strcmp(argv[i],"-nofork")) {
324 Config->nofork = true;
326 if (!strcmp(argv[i],"-wait")) {
329 if (!strcmp(argv[i],"-nolimit")) {
330 Config->unlimitcore = true;
335 strlcpy(Config->MyExecutable,argv[0],MAXBUF);
337 // initialize the lowercase mapping table
338 for (unsigned int cn = 0; cn < 256; cn++)
340 // lowercase the uppercase chars
341 for (unsigned int cn = 65; cn < 91; cn++)
342 lowermap[cn] = tolower(cn);
343 // now replace the specific chars for scandanavian comparison
344 lowermap[(unsigned)'['] = '{';
345 lowermap[(unsigned)']'] = '}';
346 lowermap[(unsigned)'\\'] = '|';
350 Config->ClearStack();
351 Config->Read(true,NULL);
354 AddServerName(Config->ServerName);
356 BoundPortCount = BindPorts();
361 if (DaemonSeed() == ERROR)
363 printf("ERROR: could not go into daemon mode. Shutting down.\n");
368 /* Because of limitations in kqueue on freebsd, we must fork BEFORE we
369 * initialize the socket engine.
371 SE = new SocketEngine();
373 /* We must load the modules AFTER initializing the socket engine, now */
376 printf("\nInspIRCd is now running!\n");
381 /* re-allocates a nick in the user_hash after they change nicknames,
382 * returns a pointer to the new user as it may have moved */
384 userrec* ReHashNick(char* Old, char* New)
386 //user_hash::iterator newnick;
387 user_hash::iterator oldnick = clientlist.find(Old);
389 log(DEBUG,"ReHashNick: %s %s",Old,New);
391 if (!strcasecmp(Old,New))
393 log(DEBUG,"old nick is new nick, skipping");
394 return oldnick->second;
397 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
399 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
401 userrec* olduser = oldnick->second;
402 clientlist[New] = olduser;
403 clientlist.erase(oldnick);
405 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
407 return clientlist[New];
411 void* dns_task(void* arg)
413 userrec* u = (userrec*)arg;
414 log(DEBUG,"DNS thread for user %s",u->nick);
419 if (dns1.ReverseLookup(u->ip))
421 log(DEBUG,"DNS Step 1");
422 while (!dns1.HasResult())
426 host = dns1.GetResult();
429 log(DEBUG,"DNS Step 2: '%s'",host.c_str());
430 if (dns2.ForwardLookup(host))
432 while (!dns2.HasResult())
436 ip = dns2.GetResultIP();
437 log(DEBUG,"DNS Step 3 '%s'(%d) '%s'(%d)",ip.c_str(),ip.length(),u->ip,strlen(u->ip));
438 if (ip == std::string(u->ip))
440 log(DEBUG,"DNS Step 4");
441 if (host.length() < 160)
443 log(DEBUG,"DNS Step 5");
444 strcpy(u->host,host.c_str());
445 strcpy(u->dhost,host.c_str());
456 std::string InspIRCd::GetVersionString()
458 char versiondata[MAXBUF];
460 char dnsengine[] = "multithread";
462 char dnsengine[] = "singlethread";
464 snprintf(versiondata,MAXBUF,"%s Rev. %s %s :%s [FLAGS=%lu,%s,%s]",VERSION,GetRevision().c_str(),Config->ServerName,SYSTEM,(unsigned long)OPTIMISATION,SE->GetName().c_str(),dnsengine);
469 bool is_valid_cmd(std::string &commandname, int pcnt, userrec * user)
471 for (unsigned int i = 0; i < cmdlist.size(); i++)
473 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
475 if (cmdlist[i].handler_function)
477 if ((pcnt>=cmdlist[i].min_params) && (strcasecmp(cmdlist[i].source,"<core>")))
479 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
481 if (cmdlist[i].flags_needed)
483 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
501 // calls a handler function for a command
503 void call_handler(std::string &commandname,char **parameters, int pcnt, userrec *user)
505 for (unsigned int i = 0; i < cmdlist.size(); i++)
507 if (!strcasecmp(cmdlist[i].command,commandname.c_str()))
509 if (cmdlist[i].handler_function)
511 if (pcnt>=cmdlist[i].min_params)
513 if ((strchr(user->modes,cmdlist[i].flags_needed)) || (!cmdlist[i].flags_needed))
515 if (cmdlist[i].flags_needed)
517 if ((user->HasPermission(commandname)) || (is_uline(user->server)))
519 cmdlist[i].handler_function(parameters,pcnt,user);
524 cmdlist[i].handler_function(parameters,pcnt,user);
534 void force_nickchange(userrec* user,const char* newnick)
541 FOREACH_RESULT(OnUserPreNick(user,newnick));
543 stats->statsCollisions++;
544 kill_link(user,"Nickname collision");
547 if (matches_qline(newnick))
549 stats->statsCollisions++;
550 kill_link(user,"Nickname collision");
558 strncpy(nick,newnick,MAXBUF);
560 if (user->registered == 7)
564 handle_nick(pars,1,user);
570 int process_parameters(char **command_p,char *parameters)
573 int q = strlen(parameters);
576 /* no parameters, command_p invalid! */
579 if (parameters[0] == ':')
581 command_p[0] = parameters+1;
586 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
588 /* only one parameter */
589 command_p[0] = parameters;
590 if (parameters[0] == ':')
592 if (strchr(parameters,' ') != NULL)
600 command_p[j++] = parameters;
601 for (int i = 0; i <= q; i++)
603 if (parameters[i] == ' ')
605 command_p[j++] = parameters+i+1;
606 parameters[i] = '\0';
607 if (command_p[j-1][0] == ':')
609 *command_p[j-1]++; /* remove dodgy ":" */
611 /* parameter like this marks end of the sequence */
615 return j; /* returns total number of items in the list */
618 void process_command(userrec *user, char* cmd)
622 char *command_p[127];
623 char p[MAXBUF], temp[MAXBUF];
624 int j, items, cmd_found;
626 for (int i = 0; i < 127; i++)
642 int total_params = 0;
645 for (unsigned int q = 0; q < strlen(cmd)-1; q++)
647 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
650 // found a 'trailing', we dont count them after this.
658 // another phidjit bug...
659 if (total_params > 126)
661 *(strchr(cmd,' ')) = '\0';
662 WriteServ(user->fd,"421 %s %s :Too many parameters given",user->nick,cmd);
666 strlcpy(temp,cmd,MAXBUF);
668 std::string tmp = cmd;
669 for (int i = 0; i <= MODCOUNT; i++)
671 std::string oldtmp = tmp;
672 modules[i]->OnServerRaw(tmp,true,user);
675 log(DEBUG,"A Module changed the input string!");
676 log(DEBUG,"New string: %s",tmp.c_str());
677 log(DEBUG,"Old string: %s",oldtmp.c_str());
681 strlcpy(cmd,tmp.c_str(),MAXBUF);
682 strlcpy(temp,cmd,MAXBUF);
684 if (!strchr(cmd,' '))
686 /* no parameters, lets skip the formalities and not chop up
688 log(DEBUG,"About to preprocess command with no params");
692 for (unsigned int i = 0; i <= strlen(cmd); i++)
694 cmd[i] = toupper(cmd[i]);
702 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
703 for (unsigned int i = 0; i < strlen(temp); i++)
705 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
711 /* split the full string into a command plus parameters */
717 for (unsigned int i = 0; i <= strlen(cmd); i++)
719 /* capitalise the command ONLY, leave params intact */
720 cmd[i] = toupper(cmd[i]);
721 /* are we nearly there yet?! :P */
725 parameters = cmd+i+1;
733 for (unsigned int i = 0; i <= strlen(cmd); i++)
735 cmd[i] = toupper(cmd[i]);
742 if (strlen(command)>MAXCOMMAND)
744 WriteServ(user->fd,"421 %s %s :Command too long",user->nick,command);
748 for (unsigned int x = 0; x < strlen(command); x++)
750 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
752 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
754 if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
756 stats->statsUnknown++;
757 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
764 std::string xcommand = command;
765 for (unsigned int i = 0; i != cmdlist.size(); i++)
767 if (cmdlist[i].command[0])
769 if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
775 items = process_parameters(command_p,parameters);
791 /* activity resets the ping pending timer */
792 user->nping = TIME + user->pingmax;
793 if ((items) < cmdlist[i].min_params)
795 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
796 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
799 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
801 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
802 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
806 if ((cmdlist[i].flags_needed) && (!user->HasPermission(xcommand)))
808 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
809 WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
813 /* if the command isnt USER, PASS, or NICK, and nick is empty,
815 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
817 if ((!isnick(user->nick)) || (user->registered != 7))
819 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
820 WriteServ(user->fd,"451 %s :You have not registered",command);
824 if ((user->registered == 7) && (!strchr(user->modes,'o')))
826 std::stringstream dcmds(Config->DisabledCommands);
831 if (!strcasecmp(thiscmd.c_str(),command))
833 // command is disabled!
834 WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
839 if ((user->registered == 7) || (!strncmp(command,"USER",4)) || (!strncmp(command,"NICK",4)) || (!strncmp(command,"PASS",4)))
841 if (cmdlist[i].handler_function)
844 /* ikky /stats counters */
847 cmdlist[i].use_count++;
848 cmdlist[i].total_bytes+=strlen(temp);
852 FOREACH_RESULT(OnPreCommand(command,command_p,items,user));
853 if (MOD_RESULT == 1) {
857 /* WARNING: nothing may come after the
858 * command handler call, as the handler
859 * may free the user structure! */
861 cmdlist[i].handler_function(command_p,items,user);
867 WriteServ(user->fd,"451 %s :You have not registered",command);
875 if ((!cmd_found) && (user))
877 stats->statsUnknown++;
878 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
882 bool removecommands(const char* source)
884 bool go_again = true;
888 for (std::deque<command_t>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
890 if (!strcmp(i->source,source))
892 log(DEBUG,"removecommands(%s) Removing dependent command: %s",i->source,i->command);
903 void process_buffer(const char* cmdbuf,userrec *user)
907 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
913 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
920 while (*cmdbuf == ' ') cmdbuf++; // strip leading spaces
922 strlcpy(cmd,cmdbuf,MAXBUF);
927 int sl = strlen(cmd)-1;
928 if ((cmd[sl] == 13) || (cmd[sl] == 10))
933 if ((cmd[sl] == 13) || (cmd[sl] == 10))
938 while (cmd[sl] == ' ') // strip trailing spaces
948 log(DEBUG,"CMDIN: %s %s",user->nick,cmd);
952 process_command(user,cmd);
956 char* InspIRCd::ModuleError()
961 void InspIRCd::erase_factory(int j)
964 for (std::vector<ircd_module*>::iterator t = factory.begin(); t != factory.end(); t++)
969 factory.push_back(NULL);
976 void InspIRCd::erase_module(int j)
979 for (std::vector<Module*>::iterator m = modules.begin(); m!= modules.end(); m++)
985 modules.push_back(NULL);
991 for (std::vector<std::string>::iterator v = Config->module_names.begin(); v != Config->module_names.end(); v++)
995 Config->module_names.erase(v);
1003 bool InspIRCd::UnloadModule(const char* filename)
1005 std::string filename_str = filename;
1006 for (unsigned int j = 0; j != Config->module_names.size(); j++)
1008 if (Config->module_names[j] == filename_str)
1010 if (modules[j]->GetVersion().Flags & VF_STATIC)
1012 log(DEFAULT,"Failed to unload STATIC module %s",filename);
1013 snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)");
1016 /* Give the module a chance to tidy out all its metadata */
1017 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
1019 modules[j]->OnCleanup(TYPE_CHANNEL,c->second);
1021 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
1023 modules[j]->OnCleanup(TYPE_USER,u->second);
1025 FOREACH_MOD OnUnloadModule(modules[j],Config->module_names[j]);
1027 log(DEBUG,"Deleting module...");
1029 log(DEBUG,"Erasing module entry...");
1031 log(DEBUG,"Removing dependent commands...");
1032 removecommands(filename);
1033 log(DEFAULT,"Module %s unloaded",filename);
1038 log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename);
1039 snprintf(MODERR,MAXBUF,"Module not loaded");
1043 bool InspIRCd::LoadModule(const char* filename)
1045 char modfile[MAXBUF];
1047 snprintf(modfile,MAXBUF,"%s",filename);
1049 snprintf(modfile,MAXBUF,"%s/%s",Config->ModPath,filename);
1051 std::string filename_str = filename;
1053 if (!DirValid(modfile))
1055 log(DEFAULT,"Module %s is not within the modules directory.",modfile);
1056 snprintf(MODERR,MAXBUF,"Module %s is not within the modules directory.",modfile);
1060 log(DEBUG,"Loading module: %s",modfile);
1062 if (FileExists(modfile))
1065 for (unsigned int j = 0; j < Config->module_names.size(); j++)
1067 if (Config->module_names[j] == filename_str)
1069 log(DEFAULT,"Module %s is already loaded, cannot load a module twice!",modfile);
1070 snprintf(MODERR,MAXBUF,"Module already loaded");
1074 ircd_module* a = new ircd_module(modfile);
1075 factory[MODCOUNT+1] = a;
1076 if (factory[MODCOUNT+1]->LastError())
1078 log(DEFAULT,"Unable to load %s: %s",modfile,factory[MODCOUNT+1]->LastError());
1079 snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[MODCOUNT+1]->LastError());
1083 if (factory[MODCOUNT+1]->factory)
1085 Module* m = factory[MODCOUNT+1]->factory->CreateModule(MyServer);
1086 modules[MODCOUNT+1] = m;
1087 /* save the module and the module's classfactory, if
1088 * this isnt done, random crashes can occur :/ */
1089 Config->module_names.push_back(filename);
1093 log(DEFAULT,"Unable to load %s",modfile);
1094 snprintf(MODERR,MAXBUF,"Factory function failed!");
1101 log(DEFAULT,"InspIRCd: startup: Module Not Found %s",modfile);
1102 snprintf(MODERR,MAXBUF,"Module file could not be found");
1107 FOREACH_MOD OnLoadModule(modules[MODCOUNT],filename_str);
1113 bool expire_run = false;
1114 std::vector<int> activefds;
1118 InspSocket* s = NULL;
1119 InspSocket* s_del = NULL;
1121 unsigned int numberactive;
1122 sockaddr_in sock_us; // our port number
1123 socklen_t uslen; // length of our port number
1125 if (!Config->nofork)
1127 freopen("/dev/null","w",stdout);
1128 freopen("/dev/null","w",stderr);
1131 /* Add the listening sockets used for client inbound connections
1132 * to the socket engine
1134 for (int count = 0; count < BoundPortCount; count++)
1135 SE->AddFd(openSockfd[count],true,X_LISTEN);
1137 WritePID(Config->PID);
1139 /* main loop, this never returns */
1142 /* time() seems to be a pretty expensive syscall, so avoid calling it too much.
1143 * Once per loop iteration is pleanty.
1148 /* Run background module timers every few seconds
1149 * (the docs say modules shouldnt rely on accurate
1150 * timing using this event, so we dont have to
1151 * time this exactly).
1153 if (((TIME % 8) == 0) && (!expire_run))
1156 FOREACH_MOD OnBackgroundTimer(TIME);
1160 if ((TIME % 8) == 1)
1163 /* Once a second, do the background processing */
1164 if (TIME != OLDTIME)
1165 while (DoBackgroundUserStuff(TIME));
1167 /* Call the socket engine to wait on the active
1168 * file descriptors. The socket engine has everything's
1169 * descriptors in its list... dns, modules, users,
1170 * servers... so its nice and easy, just one call.
1172 SE->Wait(activefds);
1175 * Now process each of the fd's. For users, we have a fast
1176 * lookup table which can find a user by file descriptor, so
1177 * processing them by fd isnt expensive. If we have a lot of
1178 * listening ports or module sockets though, things could get
1181 numberactive = activefds.size();
1182 for (unsigned int activefd = 0; activefd < numberactive; activefd++)
1184 int socket_type = SE->GetType(activefds[activefd]);
1185 switch (socket_type)
1187 case X_ESTAB_CLIENT:
1189 cu = fd_ref_table[activefds[activefd]];
1195 case X_ESTAB_MODULE:
1197 /* Process module-owned sockets.
1198 * Modules are encouraged to inherit their sockets from
1199 * InspSocket so we can process them neatly like this.
1201 s = socket_ref[activefds[activefd]];
1203 if ((s) && (!s->Poll()))
1205 log(DEBUG,"Socket poll returned false, close and bail");
1206 SE->DelFd(s->GetFd());
1207 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
1209 s_del = (InspSocket*)*a;
1210 if ((s_del) && (s_del->GetFd() == activefds[activefd]))
1212 module_sockets.erase(a);
1224 /* When we are using single-threaded dns,
1225 * the sockets for dns end up in our mainloop.
1226 * When we are using multi-threaded dns,
1227 * each thread has its own basic poll() loop
1228 * within it, making them 'fire and forget'
1229 * and independent of the mainloop.
1231 #ifndef THREADED_DNS
1232 dns_poll(activefds[activefd]);
1238 /* It's a listener */
1239 uslen = sizeof(sock_us);
1240 length = sizeof(client);
1241 incomingSockfd = accept (activefds[activefd],(struct sockaddr*)&client,&length);
1242 if (!getsockname(incomingSockfd,(sockaddr*)&sock_us,&uslen))
1244 in_port = ntohs(sock_us.sin_port);
1245 log(DEBUG,"Accepted socket %d",incomingSockfd);
1246 target = (char*)inet_ntoa(client.sin_addr);
1247 /* Years and years ago, we used to resolve here
1248 * using gethostbyaddr(). That is sucky and we
1249 * don't do that any more...
1251 if (incomingSockfd >= 0)
1253 FOREACH_MOD OnRawSocketAccept(incomingSockfd, target, in_port);
1254 stats->statsAccept++;
1255 AddClient(incomingSockfd, target, in_port, false, target);
1256 log(DEBUG,"Adding client on port %lu fd=%lu",(unsigned long)in_port,(unsigned long)incomingSockfd);
1260 WriteOpers("*** WARNING: accept() failed on port %lu (%s)",(unsigned long)in_port,target);
1261 log(DEBUG,"accept failed: %lu",(unsigned long)in_port);
1262 stats->statsRefused++;
1267 log(DEBUG,"Couldnt look up the port number for fd %lu (OS BROKEN?!)",incomingSockfd);
1268 shutdown(incomingSockfd,2);
1269 close(incomingSockfd);
1274 /* Something went wrong if we're in here.
1275 * In fact, so wrong, im not quite sure
1276 * what we would do, so for now, its going
1277 * to safely do bugger all.
1284 /* This is never reached -- we hope! */
1288 /**********************************************************************************/
1291 * An ircd in four lines! bwahahaha. ahahahahaha. ahahah *cough*.
1294 int main(int argc, char** argv)
1296 ServerInstance = new InspIRCd(argc, argv);
1297 ServerInstance->Run();
1298 delete ServerInstance;