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 * ---------------------------------------------------
17 /* Now with added unF! ;) */
21 #include "inspircd_config.h"
23 #include "configreader.h"
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
31 #include <ext/hash_map>
50 #include "inspstring.h"
52 #include "helperfuncs.h"
54 #include "socketengine.h"
55 #include "userprocess.h"
57 #include "command_parse.h"
58 #include "cull_list.h"
60 extern struct sockaddr_in client,server;
61 extern socklen_t length;
62 extern std::vector<Module*> modules;
63 extern std::vector<ircd_module*> factory;
64 extern std::vector<InspSocket*> module_sockets;
66 extern time_t OLDTIME;
67 extern std::vector<userrec*> local_users;
68 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
70 extern InspIRCd* ServerInstance;
71 extern ServerConfig *Config;
72 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
78 void ProcessUser(userrec* cu)
82 if (cu->fd == FD_MAGIC_NUMBER)
85 log(DEBUG,"Processing user with fd %d",cu->fd);
87 if (Config->GetIOHook(cu->port))
94 MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
95 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
97 catch (ModuleException& modexcept)
99 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
113 result = cu->ReadData(data, 65535);
116 log(DEBUG,"Read result: %d",result);
118 if ((result) && (result != -EAGAIN))
124 ServerInstance->stats->statsRecv += result;
126 * perform a check on the raw buffer as an array (not a string!) to remove
127 * characters 0 and 7 which are illegal in the RFC - replace them with spaces.
128 * hopefully this should stop even more people whining about "Unknown command: *"
132 * XXX - potential replacement for the below using my beloved pointers. --w00t
133 * XXX - no garauntee there's not \0's in the middle of the data,
134 * and no reason for it to be terminated either. -- Om
136 * for (char *c = data; data && *data; data++)
138 * if (*data == 0 || *data == 7)
143 for (int checker = 0; checker < result; checker++)
145 if ((data[checker] == 0) || (data[checker] == 7))
153 currfd = current->fd;
156 // add the data to the users buffer
159 if (!current->AddBuffer(data))
161 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
162 if (current->registered == 7)
164 // Make sure they arn't flooding long lines.
165 if (TIME > current->reset_due)
167 current->reset_due = TIME + current->threshold;
168 current->lines_in = 0;
173 if (current->lines_in > current->flood)
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");
182 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
188 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
189 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
190 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
191 apply_lines(APPLY_ZLINES);
197 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
199 if (current->registered == 7)
201 kill_link(current,"RecvQ exceeded");
205 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
206 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
207 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
208 apply_lines(APPLY_ZLINES);
214 // while there are complete lines to process...
215 while (current->BufferIsReady())
217 char sanitized[MAXBUF];
220 if (TIME > current->reset_due)
222 current->reset_due = TIME + current->threshold;
223 current->lines_in = 0;
228 if (current->lines_in > current->flood)
230 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
231 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
232 kill_link(current,"Excess flood");
236 if ((floodlines > current->flood) && (current->flood != 0))
238 if (current->registered == 7)
240 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
241 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
242 kill_link(current,"Excess flood");
246 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
247 apply_lines(APPLY_ZLINES);
253 // use GetBuffer to copy single lines into the sanitized string
254 std::string single_line = current->GetBuffer();
255 current->bytes_in += single_line.length();
257 strlcpy(sanitized,single_line.c_str(),511);
261 userrec* old_comp = fd_ref_table[currfd];
263 ServerInstance->Parser->ProcessBuffer(sanitized,current);
265 * look for the user's record in case it's changed... if theyve quit,
266 * we cant do anything more with their buffer, so bail.
267 * there used to be an ugly, slow loop here. Now we have a reference
268 * table, life is much easier (and FASTER)
270 userrec* new_comp = fd_ref_table[currfd];
271 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
277 /* The user is still here, flush their buffer */
278 current->FlushWriteBuf();
286 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
288 log(DEBUG,"killing: %s",cu->nick);
289 kill_link(cu,strerror(errno));
294 // result EAGAIN means nothing read
295 else if ((result == EAGAIN) || (result == -EAGAIN))
299 else if (result == 0)
301 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
302 kill_link(cu,"Client exited");
303 log(DEBUG,"Bailing from client exit");
308 void DoSocketTimeouts(time_t TIME)
310 unsigned int numsockets = module_sockets.size();
311 SocketEngine* SE = ServerInstance->SE;
313 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
315 InspSocket* s = (InspSocket*)*a;
316 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
318 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
319 socket_ref[s->GetFd()] = NULL;
320 SE->DelFd(s->GetFd());
321 module_sockets.erase(a);
327 if (module_sockets.size() != numsockets)
333 * This function is called once a second from the mainloop.
334 * It is intended to do background checking on all the user structs, e.g.
335 * stuff like ping checks, registration timeouts, etc. This function is
336 * also responsible for checking if InspSocket derived classes are timed out.
338 void DoBackgroundUserStuff(time_t TIME)
340 CullList GlobalGoners;
342 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
344 /* Sanity checks for corrupted iterators (yes, really) */
345 userrec* curr = NULL;
348 curr = (userrec*)(*count2);
349 if ((long)curr == -1)
355 * registration timeout -- didnt send USER/NICK/HOST
356 * in the time specified in their connection class.
358 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
360 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
361 GlobalGoners.AddItem(curr,"Registration timeout");
366 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
367 * say this user is ok to proceed, fully connect them.
369 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
371 curr->dns_done = true;
372 ServerInstance->stats->statsDnsBad++;
373 FullConnectUser(curr,&GlobalGoners);
377 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
379 log(DEBUG,"dns done, registered=3, and modules ready, OK");
380 FullConnectUser(curr,&GlobalGoners);
384 // It's time to PING this user. Send them a ping.
385 if ((TIME > curr->nping) && (curr->registered == 7))
387 // This user didn't answer the last ping, remove them
390 GlobalGoners.AddItem(curr,"Ping timeout");
394 Write(curr->fd,"PING :%s",Config->ServerName);
396 curr->nping = TIME+curr->pingmax;
400 * We can flush the write buffer as the last thing we do, because if they
401 * match any of the above conditions its no use flushing their buffer anyway.
403 curr->FlushWriteBuf();
405 if (*curr->GetWriteError())
407 GlobalGoners.AddItem(curr,curr->GetWriteError());
413 /* Remove all the queued users who are due to be quit, free memory used. */
414 GlobalGoners.Apply();