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>
46 #include "inspstring.h"
47 #include "helperfuncs.h"
49 #include "socketengine.h"
50 #include "userprocess.h"
52 #include "command_parse.h"
53 #include "cull_list.h"
55 extern struct sockaddr_in client,server;
56 extern socklen_t length;
57 extern std::vector<Module*> modules;
58 extern std::vector<ircd_module*> factory;
59 extern std::vector<InspSocket*> module_sockets;
61 extern time_t OLDTIME;
62 extern std::vector<userrec*> local_users;
63 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
65 extern InspIRCd* ServerInstance;
66 extern ServerConfig *Config;
67 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
70 extern user_hash clientlist;
71 extern chan_hash chanlist;
73 void ProcessUser(userrec* cu)
77 if (cu->fd == FD_MAGIC_NUMBER)
80 log(DEBUG,"Processing user with fd %d",cu->fd);
82 if (Config->GetIOHook(cu->GetPort()))
89 MOD_RESULT = Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
90 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
92 catch (ModuleException& modexcept)
94 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
108 result = cu->ReadData(data, 65535);
111 log(DEBUG,"Read result: %d",result);
113 if ((result) && (result != -EAGAIN))
119 ServerInstance->stats->statsRecv += result;
121 * perform a check on the raw buffer as an array (not a string!) to remove
122 * character 0 which is illegal in the RFC - replace them with spaces.
123 * XXX - no garauntee there's not \0's in the middle of the data,
124 * and no reason for it to be terminated either. -- Om
127 for (int checker = 0; checker < result; checker++)
129 if (data[checker] == 0)
137 currfd = current->fd;
139 // add the data to the users buffer
142 if (!current->AddBuffer(data))
144 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
145 if (current->registered == REG_ALL)
147 // Make sure they arn't flooding long lines.
148 if (TIME > current->reset_due)
150 current->reset_due = TIME + current->threshold;
151 current->lines_in = 0;
156 if (current->lines_in > current->flood)
158 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
159 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
160 kill_link(current,"Excess flood");
165 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
171 WriteOpers("*** Excess flood from %s",current->GetIPString());
172 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
173 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->GetIPString());
174 apply_lines(APPLY_ZLINES);
180 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
182 if (current->registered == REG_ALL)
184 kill_link(current,"RecvQ exceeded");
188 WriteOpers("*** Excess flood from %s",current->GetIPString());
189 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
190 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->GetIPString());
191 apply_lines(APPLY_ZLINES);
197 // while there are complete lines to process...
198 while (current->BufferIsReady())
200 if (TIME > current->reset_due)
202 current->reset_due = TIME + current->threshold;
203 current->lines_in = 0;
206 if (++current->lines_in > current->flood)
208 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
209 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
210 kill_link(current,"Excess flood");
214 if ((++floodlines > current->flood) && (current->flood != 0))
216 if (current->registered == REG_ALL)
218 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
219 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
220 kill_link(current,"Excess flood");
224 add_zline(120,Config->ServerName,"Flood from unregistered connection",current->GetIPString());
225 apply_lines(APPLY_ZLINES);
231 // use GetBuffer to copy single lines into the sanitized string
232 std::string single_line = current->GetBuffer();
233 current->bytes_in += single_line.length();
235 if (single_line.length() > 512)
236 single_line.resize(512);
238 userrec* old_comp = fd_ref_table[currfd];
240 ServerInstance->Parser->ProcessBuffer(single_line,current);
242 * look for the user's record in case it's changed... if theyve quit,
243 * we cant do anything more with their buffer, so bail.
244 * there used to be an ugly, slow loop here. Now we have a reference
245 * table, life is much easier (and FASTER)
247 userrec* new_comp = fd_ref_table[currfd];
248 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
254 /* The user is still here, flush their buffer */
255 current->FlushWriteBuf();
262 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
264 log(DEBUG,"killing: %s",cu->nick);
265 kill_link(cu,strerror(errno));
270 // result EAGAIN means nothing read
271 else if ((result == EAGAIN) || (result == -EAGAIN))
275 else if (result == 0)
277 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
278 kill_link(cu,"Client exited");
279 log(DEBUG,"Bailing from client exit");
284 void DoSocketTimeouts(time_t TIME)
286 unsigned int numsockets = module_sockets.size();
287 SocketEngine* SE = ServerInstance->SE;
289 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
291 InspSocket* s = (InspSocket*)*a;
292 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
294 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
295 socket_ref[s->GetFd()] = NULL;
296 SE->DelFd(s->GetFd());
297 module_sockets.erase(a);
303 if (module_sockets.size() != numsockets)
309 * This function is called once a second from the mainloop.
310 * It is intended to do background checking on all the user structs, e.g.
311 * stuff like ping checks, registration timeouts, etc. This function is
312 * also responsible for checking if InspSocket derived classes are timed out.
314 void DoBackgroundUserStuff(time_t TIME)
316 CullList GlobalGoners;
318 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
319 for (unsigned long count2 = 0; count2 != local_users.size(); count2++)
321 if (count2 >= local_users.size())
324 userrec* curr = local_users[count2];
329 * registration timeout -- didnt send USER/NICK/HOST
330 * in the time specified in their connection class.
332 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
334 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
335 //ZapThisDns(curr->fd);
336 GlobalGoners.AddItem(curr,"Registration timeout");
340 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
341 * say this user is ok to proceed, fully connect them.
343 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
345 curr->dns_done = true;
346 //ZapThisDns(curr->fd);
347 ServerInstance->stats->statsDnsBad++;
348 FullConnectUser(curr,&GlobalGoners);
351 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
353 log(DEBUG,"dns done, registered=3, and modules ready, OK");
354 FullConnectUser(curr,&GlobalGoners);
355 //ZapThisDns(curr->fd);
358 // It's time to PING this user. Send them a ping.
359 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
361 // This user didn't answer the last ping, remove them
364 GlobalGoners.AddItem(curr,"Ping timeout");
366 curr->nping = TIME+curr->pingmax;
369 Write(curr->fd,"PING :%s",Config->ServerName);
371 curr->nping = TIME+curr->pingmax;
375 * We can flush the write buffer as the last thing we do, because if they
376 * match any of the above conditions its no use flushing their buffer anyway.
379 curr->FlushWriteBuf();
380 if (*curr->GetWriteError())
382 GlobalGoners.AddItem(curr,curr->GetWriteError());
390 /* Remove all the queued users who are due to be quit, free memory used. */
391 GlobalGoners.Apply();