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"
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
32 #include <ext/hash_map>
54 #include "inspstring.h"
56 #include "helperfuncs.h"
58 #include "socketengine.h"
60 #include "command_parse.h"
61 #include "cull_list.h"
64 extern struct sockaddr_in client,server;
65 extern socklen_t length;
66 extern std::vector<Module*> modules;
67 extern std::vector<ircd_module*> factory;
68 extern std::vector<InspSocket*> module_sockets;
70 extern time_t OLDTIME;
71 extern std::vector<userrec*> local_users;
73 extern InspIRCd* ServerInstance;
74 extern ServerConfig *Config;
75 extern userrec* fd_ref_table[65536];
78 extern user_hash clientlist;
79 extern chan_hash chanlist;
81 void ProcessUser(userrec* cu)
84 log(DEBUG,"Processing user with fd %d",cu->fd);
85 if (Config->GetIOHook(cu->port))
88 int MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
89 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
101 result = cu->ReadData(data, 65535);
103 log(DEBUG,"Read result: %d",result);
106 ServerInstance->stats->statsRecv += result;
107 // perform a check on the raw buffer as an array (not a string!) to remove
108 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
109 // hopefully this should stop even more people whining about "Unknown command: *"
110 for (int checker = 0; checker < result; checker++)
112 if ((data[checker] == 0) || (data[checker] == 7))
117 userrec* current = cu;
118 int currfd = current->fd;
120 // add the data to the users buffer
123 if (!current->AddBuffer(data))
125 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
126 if (current->registered == 7)
128 kill_link(current,"RecvQ exceeded");
132 WriteOpers("*** Excess flood from %s",current->ip);
133 log(DEFAULT,"Excess flood from: %s",current->ip);
134 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
135 apply_lines(APPLY_ZLINES);
139 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
141 if (current->registered == 7)
143 kill_link(current,"RecvQ exceeded");
147 WriteOpers("*** Excess flood from %s",current->ip);
148 log(DEFAULT,"Excess flood from: %s",current->ip);
149 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
150 apply_lines(APPLY_ZLINES);
154 // while there are complete lines to process...
155 while (current->BufferIsReady())
158 if (TIME > current->reset_due)
160 current->reset_due = TIME + current->threshold;
161 current->lines_in = 0;
164 if (current->lines_in > current->flood)
166 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
167 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
168 kill_link(current,"Excess flood");
171 if ((floodlines > current->flood) && (current->flood != 0))
173 if (current->registered == 7)
175 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
176 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
177 kill_link(current,"Excess flood");
181 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
182 apply_lines(APPLY_ZLINES);
186 char sanitized[MAXBUF];
187 // use GetBuffer to copy single lines into the sanitized string
188 std::string single_line = current->GetBuffer();
189 current->bytes_in += single_line.length();
191 if (single_line.length()>512)
193 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
194 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
195 kill_link(current,"Excess flood");
198 strlcpy(sanitized,single_line.c_str(),MAXBUF);
201 userrec* old_comp = fd_ref_table[currfd];
202 // we're gonna re-scan to check if the nick is gone, after every
203 // command - if it has, we're gonna bail
204 ServerInstance->Parser->ProcessBuffer(sanitized,current);
205 // look for the user's record in case it's changed... if theyve quit,
206 // we cant do anything more with their buffer, so bail.
207 // there used to be an ugly, slow loop here. Now we have a reference
208 // table, life is much easier (and FASTER)
209 userrec* new_comp = fd_ref_table[currfd];
210 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
216 /* The user is still here, flush their buffer */
217 current->FlushWriteBuf();
223 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
225 log(DEBUG,"killing: %s",cu->nick);
226 kill_link(cu,strerror(errno));
230 // result EAGAIN means nothing read
231 else if (result == EAGAIN)
234 else if (result == 0)
236 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
237 kill_link(cu,"Client exited");
238 log(DEBUG,"Bailing from client exit");
244 * This function is called once a second from the mainloop.
245 * It is intended to do background checking on all the user structs, e.g.
246 * stuff like ping checks, registration timeouts, etc.
247 * The function returns false when it is finished, and true if
248 * it needs to be run again (e.g. it has processed one of a batch of
249 * QUIT messages, but couldnt continue iterating because the iterator
250 * became invalid). This function is also responsible for checking
251 * if InspSocket derived classes are timed out.
253 bool DoBackgroundUserStuff(time_t TIME)
255 unsigned int numsockets = module_sockets.size();
256 SocketEngine* SE = ServerInstance->SE;
257 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
259 InspSocket* s = (InspSocket*)*a;
260 if (s->Timeout(TIME))
262 log(DEBUG,"Socket poll returned false, close and bail");
263 SE->DelFd(s->GetFd());
265 module_sockets.erase(a);
269 if (module_sockets.size() != numsockets) break;
271 CullList* GlobalGoners = new CullList();
272 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
274 /* Sanity checks for corrupted iterators (yes, really) */
275 userrec* curr = NULL;
277 curr = (userrec*)(*count2);
278 if ((long)curr == -1)
283 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
284 // their connection class.
285 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
287 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
288 GlobalGoners->AddItem(curr,"Registration timeout");
291 // user has signed on with USER/NICK/PASS, and dns has completed, all the modules
292 // say this user is ok to proceed, fully connect them.
293 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
295 curr->dns_done = true;
296 ServerInstance->stats->statsDnsBad++;
297 FullConnectUser(curr,GlobalGoners);
300 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
302 log(DEBUG,"dns done, registered=3, and modules ready, OK");
303 FullConnectUser(curr,GlobalGoners);
306 // It's time to PING this user. Send them a ping.
307 // XXX: We were checking isnick() here -- why when we check curr->registered? - Brain
308 if ((TIME > curr->nping) && (curr->registered == 7))
310 // This user didn't answer the last ping, remove them
313 GlobalGoners->AddItem(curr,"Ping timeout");
316 Write(curr->fd,"PING :%s",Config->ServerName);
318 curr->nping = TIME+curr->pingmax;
320 // XXX: We can flush the write buffer as the last thing we do, because if they
321 // match any of the above conditions its no use flushing their buffer anyway.
322 curr->FlushWriteBuf();
323 if (curr->GetWriteError() != "")
325 GlobalGoners->AddItem(curr,curr->GetWriteError());
330 /** Remove all the queued users who are due to be quit
332 GlobalGoners->Apply();
333 /** Free to memory used
336 /** XXX: The old system prior to 1.0RC2 would call this function
337 * repeatedly until everything was ship-shape, however now we are
338 * using CullList to avoid bailing from the loop, so this is no
339 * longer required. We always return false here so this only executes
340 * once. At some future date the while loop may be removed from
341 * the mainloop which calls this function.
346 void OpenLog(char** argv, int argc)
348 std::string logpath = GetFullProgDir(argv,argc) + "/ircd.log";
349 Config->log_file = fopen(logpath.c_str(),"a+");
350 if (!Config->log_file)
352 printf("ERROR: Could not write to logfile %s, bailing!\n\n",logpath.c_str());
356 printf("Logging to ircd.log...\n");
358 printf("Logging to %s...\n",logpath.c_str());
367 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
368 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
376 if (*Config->DieValue)
378 printf("WARNING: %s\n\n",Config->DieValue);
379 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
384 void LoadAllModules(InspIRCd* ServerInstance)
386 /* We must load the modules AFTER initializing the socket engine, now */
388 char configToken[MAXBUF];
389 for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
391 Config->ConfValue("module","name",count,configToken,&Config->config_f);
392 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
393 if (!ServerInstance->LoadModule(configToken))
395 log(DEFAULT,"Exiting due to a module loader error.");
396 printf("\nThere was an error loading a module: %s\n\nYou might want to do './inspircd start' instead of 'bin/inspircd'\n\n",ServerInstance->ModuleError());
400 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);