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"
22 #include "configreader.h"
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
30 #include <ext/hash_map>
44 #include "inspstring.h"
47 #include "socketengine.h"
48 #include "userprocess.h"
51 #include "command_parse.h"
52 #include "cull_list.h"
54 void InspIRCd::ProcessUser(userrec* cu)
58 if (cu->GetFd() == FD_MAGIC_NUMBER)
61 if (this->Config->GetIOHook(cu->GetPort()))
68 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
69 this->Log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
71 catch (ModuleException& modexcept)
73 this->Log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
87 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
90 this->Log(DEBUG,"Read result: %d",result);
92 if ((result) && (result != -EAGAIN))
98 this->stats->statsRecv += result;
100 * perform a check on the raw buffer as an array (not a string!) to remove
101 * character 0 which is illegal in the RFC - replace them with spaces.
102 * XXX - no garauntee there's not \0's in the middle of the data,
103 * and no reason for it to be terminated either. -- Om
106 for (int checker = 0; checker < result; checker++)
108 if (ReadBuffer[checker] == 0)
109 ReadBuffer[checker] = ' ';
113 ReadBuffer[result] = '\0';
116 currfd = current->GetFd();
118 // add the data to the users buffer
121 if (!current->AddBuffer(ReadBuffer))
123 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
124 if (current->registered == REG_ALL)
126 // Make sure they arn't flooding long lines.
127 if (TIME > current->reset_due)
129 current->reset_due = TIME + current->threshold;
130 current->lines_in = 0;
135 if (current->lines_in > current->flood)
137 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
138 this->WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
139 userrec::QuitUser(this, current,"Excess flood");
144 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
150 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
151 this->Log(DEFAULT,"Excess flood from: %s",current->GetIPString());
152 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
153 XLines->apply_lines(APPLY_ZLINES);
159 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
161 if (current->registered == REG_ALL)
163 userrec::QuitUser(this, current,"RecvQ exceeded");
167 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
168 this->Log(DEFAULT,"Excess flood from: %s",current->GetIPString());
169 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
170 XLines->apply_lines(APPLY_ZLINES);
176 // while there are complete lines to process...
177 while (current->BufferIsReady())
179 if (TIME > current->reset_due)
181 current->reset_due = TIME + current->threshold;
182 current->lines_in = 0;
185 if (++current->lines_in > current->flood)
187 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
188 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
189 userrec::QuitUser(this, current,"Excess flood");
193 if ((++floodlines > current->flood) && (current->flood != 0))
195 if (current->registered == REG_ALL)
197 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
198 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
199 userrec::QuitUser(this, current,"Excess flood");
203 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
204 XLines->apply_lines(APPLY_ZLINES);
210 // use GetBuffer to copy single lines into the sanitized string
211 std::string single_line = current->GetBuffer();
212 current->bytes_in += single_line.length();
214 if (single_line.length() > 512)
215 single_line.resize(512);
217 EventHandler* old_comp = this->SE->GetRef(currfd);
219 this->Parser->ProcessBuffer(single_line,current);
221 * look for the user's record in case it's changed... if theyve quit,
222 * we cant do anything more with their buffer, so bail.
223 * there used to be an ugly, slow loop here. Now we have a reference
224 * table, life is much easier (and FASTER)
226 EventHandler* new_comp = this->SE->GetRef(currfd);
227 if (old_comp != new_comp)
233 /* The user is still here, flush their buffer */
234 current->FlushWriteBuf();
241 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
243 this->Log(DEBUG,"killing: %s",cu->nick);
244 userrec::QuitUser(this,cu,strerror(errno));
249 // result EAGAIN means nothing read
250 else if ((result == EAGAIN) || (result == -EAGAIN))
254 else if (result == 0)
256 this->Log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
257 userrec::QuitUser(this,cu,"Client exited");
258 this->Log(DEBUG,"Bailing from client exit");
264 * This function is called once a second from the mainloop.
265 * It is intended to do background checking on all the user structs, e.g.
266 * stuff like ping checks, registration timeouts, etc. This function is
267 * also responsible for checking if InspSocket derived classes are timed out.
269 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
271 CullList GlobalGoners(this);
273 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
274 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
276 if (count2 >= this->local_users.size())
279 userrec* curr = this->local_users[count2];
284 * registration timeout -- didnt send USER/NICK/HOST
285 * in the time specified in their connection class.
287 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
289 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
290 //ZapThisDns(curr->fd);
291 GlobalGoners.AddItem(curr,"Registration timeout");
295 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
296 * say this user is ok to proceed, fully connect them.
298 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
300 curr->dns_done = true;
301 //ZapThisDns(curr->fd);
302 this->stats->statsDnsBad++;
303 curr->FullConnect(&GlobalGoners);
306 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
308 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
309 curr->FullConnect(&GlobalGoners);
310 //ZapThisDns(curr->fd);
313 // It's time to PING this user. Send them a ping.
314 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
316 // This user didn't answer the last ping, remove them
319 GlobalGoners.AddItem(curr,"Ping timeout");
321 curr->nping = TIME+curr->pingmax;
324 curr->Write("PING :%s",this->Config->ServerName);
326 curr->nping = TIME+curr->pingmax;
330 * We can flush the write buffer as the last thing we do, because if they
331 * match any of the above conditions its no use flushing their buffer anyway.
334 curr->FlushWriteBuf();
335 if (*curr->GetWriteError())
337 GlobalGoners.AddItem(curr,curr->GetWriteError());
345 /* Remove all the queued users who are due to be quit, free memory used. */
346 GlobalGoners.Apply();