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;
60 extern time_t OLDTIME;
61 extern std::vector<userrec*> local_users;
62 extern InspIRCd* ServerInstance;
63 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
66 extern user_hash clientlist;
67 extern chan_hash chanlist;
69 void ProcessUser(userrec* cu)
73 if (cu->fd == FD_MAGIC_NUMBER)
76 log(DEBUG,"Processing user with fd %d",cu->fd);
78 if (ServerInstance->Config->GetIOHook(cu->GetPort()))
85 MOD_RESULT = ServerInstance->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
86 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
88 catch (ModuleException& modexcept)
90 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
104 result = cu->ReadData(data, 65535);
107 log(DEBUG,"Read result: %d",result);
109 if ((result) && (result != -EAGAIN))
115 ServerInstance->stats->statsRecv += result;
117 * perform a check on the raw buffer as an array (not a string!) to remove
118 * character 0 which is illegal in the RFC - replace them with spaces.
119 * XXX - no garauntee there's not \0's in the middle of the data,
120 * and no reason for it to be terminated either. -- Om
123 for (int checker = 0; checker < result; checker++)
125 if (data[checker] == 0)
133 currfd = current->fd;
135 // add the data to the users buffer
138 if (!current->AddBuffer(data))
140 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
141 if (current->registered == REG_ALL)
143 // Make sure they arn't flooding long lines.
144 if (TIME > current->reset_due)
146 current->reset_due = TIME + current->threshold;
147 current->lines_in = 0;
152 if (current->lines_in > current->flood)
154 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
155 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
156 userrec::QuitUser(current,"Excess flood");
161 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
167 WriteOpers("*** Excess flood from %s",current->GetIPString());
168 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
169 add_zline(120,ServerInstance->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
170 apply_lines(APPLY_ZLINES);
176 if (current->recvq.length() > (unsigned)ServerInstance->Config->NetBufferSize)
178 if (current->registered == REG_ALL)
180 userrec::QuitUser(current,"RecvQ exceeded");
184 WriteOpers("*** Excess flood from %s",current->GetIPString());
185 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
186 add_zline(120,ServerInstance->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
187 apply_lines(APPLY_ZLINES);
193 // while there are complete lines to process...
194 while (current->BufferIsReady())
196 if (TIME > current->reset_due)
198 current->reset_due = TIME + current->threshold;
199 current->lines_in = 0;
202 if (++current->lines_in > current->flood)
204 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
205 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
206 userrec::QuitUser(current,"Excess flood");
210 if ((++floodlines > current->flood) && (current->flood != 0))
212 if (current->registered == REG_ALL)
214 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
215 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
216 userrec::QuitUser(current,"Excess flood");
220 add_zline(120,ServerInstance->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
221 apply_lines(APPLY_ZLINES);
227 // use GetBuffer to copy single lines into the sanitized string
228 std::string single_line = current->GetBuffer();
229 current->bytes_in += single_line.length();
231 if (single_line.length() > 512)
232 single_line.resize(512);
234 userrec* old_comp = fd_ref_table[currfd];
236 ServerInstance->Parser->ProcessBuffer(single_line,current);
238 * look for the user's record in case it's changed... if theyve quit,
239 * we cant do anything more with their buffer, so bail.
240 * there used to be an ugly, slow loop here. Now we have a reference
241 * table, life is much easier (and FASTER)
243 userrec* new_comp = fd_ref_table[currfd];
244 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
250 /* The user is still here, flush their buffer */
251 current->FlushWriteBuf();
258 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
260 log(DEBUG,"killing: %s",cu->nick);
261 userrec::QuitUser(cu,strerror(errno));
266 // result EAGAIN means nothing read
267 else if ((result == EAGAIN) || (result == -EAGAIN))
271 else if (result == 0)
273 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
274 userrec::QuitUser(cu,"Client exited");
275 log(DEBUG,"Bailing from client exit");
280 void DoSocketTimeouts(time_t TIME, InspIRCd* SI)
282 unsigned int numsockets = SI->module_sockets.size();
283 SocketEngine* SE = SI->SE;
285 for (std::vector<InspSocket*>::iterator a = SI->module_sockets.begin(); a < SI->module_sockets.end(); a++)
287 InspSocket* s = (InspSocket*)*a;
288 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (SI->socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
290 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
291 SI->socket_ref[s->GetFd()] = NULL;
292 SE->DelFd(s->GetFd());
293 SI->module_sockets.erase(a);
299 if (SI->module_sockets.size() != numsockets)
305 * This function is called once a second from the mainloop.
306 * It is intended to do background checking on all the user structs, e.g.
307 * stuff like ping checks, registration timeouts, etc. This function is
308 * also responsible for checking if InspSocket derived classes are timed out.
310 void DoBackgroundUserStuff(time_t TIME)
312 CullList GlobalGoners;
314 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
315 for (unsigned long count2 = 0; count2 != local_users.size(); count2++)
317 if (count2 >= local_users.size())
320 userrec* curr = local_users[count2];
325 * registration timeout -- didnt send USER/NICK/HOST
326 * in the time specified in their connection class.
328 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
330 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
331 //ZapThisDns(curr->fd);
332 GlobalGoners.AddItem(curr,"Registration timeout");
336 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
337 * say this user is ok to proceed, fully connect them.
339 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
341 curr->dns_done = true;
342 //ZapThisDns(curr->fd);
343 ServerInstance->stats->statsDnsBad++;
344 curr->FullConnect(&GlobalGoners);
347 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
349 log(DEBUG,"dns done, registered=3, and modules ready, OK");
350 curr->FullConnect(&GlobalGoners);
351 //ZapThisDns(curr->fd);
354 // It's time to PING this user. Send them a ping.
355 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
357 // This user didn't answer the last ping, remove them
360 GlobalGoners.AddItem(curr,"Ping timeout");
362 curr->nping = TIME+curr->pingmax;
365 curr->Write("PING :%s",ServerInstance->Config->ServerName);
367 curr->nping = TIME+curr->pingmax;
371 * We can flush the write buffer as the last thing we do, because if they
372 * match any of the above conditions its no use flushing their buffer anyway.
375 curr->FlushWriteBuf();
376 if (*curr->GetWriteError())
378 GlobalGoners.AddItem(curr,curr->GetWriteError());
386 /* Remove all the queued users who are due to be quit, free memory used. */
387 GlobalGoners.Apply();