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"
63 extern struct sockaddr_in client,server;
64 extern socklen_t length;
65 extern std::vector<Module*> modules;
66 extern std::vector<ircd_module*> factory;
67 extern std::vector<InspSocket*> module_sockets;
69 extern time_t OLDTIME;
70 extern std::vector<userrec*> local_users;
72 extern InspIRCd* ServerInstance;
73 extern ServerConfig *Config;
74 extern userrec* fd_ref_table[65536];
77 extern user_hash clientlist;
78 extern chan_hash chanlist;
80 extern Module* IOHookModule;
82 void ProcessUser(userrec* cu)
85 log(DEBUG,"Processing user with fd %d",cu->fd);
90 IOHookModule->OnRawSocketRead(cu->fd,data,65535,result2);
93 result = cu->ReadData(data, 65535);
97 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
103 result = cu->ReadData(data, 65535);
105 log(DEBUG,"Read result: %d",result);
108 ServerInstance->stats->statsRecv += result;
109 // perform a check on the raw buffer as an array (not a string!) to remove
110 // characters 0 and 7 which are illegal in the RFC - replace them with spaces.
111 // hopefully this should stop even more people whining about "Unknown command: *"
112 for (int checker = 0; checker < result; checker++)
114 if ((data[checker] == 0) || (data[checker] == 7))
119 userrec* current = cu;
120 int currfd = current->fd;
122 // add the data to the users buffer
125 if (!current->AddBuffer(data))
127 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
128 if (current->registered == 7)
130 kill_link(current,"RecvQ exceeded");
134 WriteOpers("*** Excess flood from %s",current->ip);
135 log(DEFAULT,"Excess flood from: %s",current->ip);
136 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
137 apply_lines(APPLY_ZLINES);
141 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
143 if (current->registered == 7)
145 kill_link(current,"RecvQ exceeded");
149 WriteOpers("*** Excess flood from %s",current->ip);
150 log(DEFAULT,"Excess flood from: %s",current->ip);
151 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
152 apply_lines(APPLY_ZLINES);
156 // while there are complete lines to process...
157 while (current->BufferIsReady())
160 if (TIME > current->reset_due)
162 current->reset_due = TIME + current->threshold;
163 current->lines_in = 0;
166 if (current->lines_in > current->flood)
168 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
169 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
170 kill_link(current,"Excess flood");
173 if ((floodlines > current->flood) && (current->flood != 0))
175 if (current->registered == 7)
177 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
178 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
179 kill_link(current,"Excess flood");
183 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->ip);
184 apply_lines(APPLY_ZLINES);
188 char sanitized[MAXBUF];
189 // use GetBuffer to copy single lines into the sanitized string
190 std::string single_line = current->GetBuffer();
191 current->bytes_in += single_line.length();
193 if (single_line.length()>512)
195 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
196 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
197 kill_link(current,"Excess flood");
200 strlcpy(sanitized,single_line.c_str(),MAXBUF);
203 userrec* old_comp = fd_ref_table[currfd];
204 // we're gonna re-scan to check if the nick is gone, after every
205 // command - if it has, we're gonna bail
206 ServerInstance->Parser->ProcessBuffer(sanitized,current);
207 // look for the user's record in case it's changed... if theyve quit,
208 // we cant do anything more with their buffer, so bail.
209 // there used to be an ugly, slow loop here. Now we have a reference
210 // table, life is much easier (and FASTER)
211 userrec* new_comp = fd_ref_table[currfd];
212 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
218 /* The user is still here, flush their buffer */
219 current->FlushWriteBuf();
225 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
227 log(DEBUG,"killing: %s",cu->nick);
228 kill_link(cu,strerror(errno));
232 // result EAGAIN means nothing read
233 else if (result == EAGAIN)
236 else if (result == 0)
238 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
239 kill_link(cu,"Client exited");
240 log(DEBUG,"Bailing from client exit");
247 * This function is called once a second from the mainloop.
248 * It is intended to do background checking on all the user structs, e.g.
249 * stuff like ping checks, registration timeouts, etc.
250 * The function returns false when it is finished, and true if
251 * it needs to be run again (e.g. it has processed one of a batch of
252 * QUIT messages, but couldnt continue iterating because the iterator
253 * became invalid). This function is also responsible for checking
254 * if InspSocket derived classes are timed out.
256 bool DoBackgroundUserStuff(time_t TIME)
258 unsigned int numsockets = module_sockets.size();
259 SocketEngine* SE = ServerInstance->SE;
260 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
262 InspSocket* s = (InspSocket*)*a;
263 if (s->Timeout(TIME))
265 log(DEBUG,"Socket poll returned false, close and bail");
266 SE->DelFd(s->GetFd());
268 module_sockets.erase(a);
272 if (module_sockets.size() != numsockets) break;
274 /* TODO: We need a seperate hash containing only local users for this
276 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
278 /* Sanity checks for corrupted iterators (yes, really) */
279 userrec* curr = NULL;
281 curr = (userrec*)(*count2);
282 if ((long)curr == -1)
285 if ((curr) && (curr->fd != 0)) /* XXX - why are we checking fd != 0? --w00t */
287 int currfd = curr->fd;
288 // we don't check the state of remote users.
291 curr->FlushWriteBuf();
292 if (curr->GetWriteError() != "")
294 log(DEBUG,"InspIRCd: write error: %s",curr->GetWriteError().c_str());
295 kill_link(curr,curr->GetWriteError().c_str());
298 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
299 // their connection class.
300 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
302 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
303 kill_link(curr,"Registration timeout");
306 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
308 log(DEBUG,"signon exceed, registered=3, and modules ready, OK: %d %d",TIME,curr->signon);
309 curr->dns_done = true;
310 ServerInstance->stats->statsDnsBad++;
311 FullConnectUser(curr);
312 if (fd_ref_table[currfd] != curr) // something changed, bail pronto
315 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
317 log(DEBUG,"dns done, registered=3, and modules ready, OK");
318 FullConnectUser(curr);
319 if (fd_ref_table[currfd] != curr) // something changed, bail pronto
322 if ((TIME > curr->nping) && (isnick(curr->nick)) && (curr->registered == 7))
324 if ((!curr->lastping) && (curr->registered == 7))
326 log(DEBUG,"InspIRCd: ping timeout: %s",curr->nick);
327 kill_link(curr,"Ping timeout");
330 Write(curr->fd,"PING :%s",Config->ServerName);
331 log(DEBUG,"InspIRCd: pinging: %s",curr->nick);
333 curr->nping = TIME+curr->pingmax; // was hard coded to 120
341 void OpenLog(char** argv, int argc)
343 std::string logpath = GetFullProgDir(argv,argc) + "/ircd.log";
344 Config->log_file = fopen(logpath.c_str(),"a+");
345 if (!Config->log_file)
347 printf("ERROR: Could not write to logfile %s, bailing!\n\n",logpath.c_str());
351 printf("Logging to ircd.log...\n");
353 printf("Logging to %s...\n",logpath.c_str());
362 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
363 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
371 if (*Config->DieValue)
373 printf("WARNING: %s\n\n",Config->DieValue);
374 log(DEFAULT,"Ut-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
379 void LoadAllModules(InspIRCd* ServerInstance)
381 /* We must load the modules AFTER initializing the socket engine, now */
383 char configToken[MAXBUF];
384 for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
386 Config->ConfValue("module","name",count,configToken,&Config->config_f);
387 printf("Loading module... \033[1;32m%s\033[0m\n",configToken);
388 if (!ServerInstance->LoadModule(configToken))
390 log(DEFAULT,"Exiting due to a module loader error.");
391 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());
395 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);