1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #include "configreader.h"
19 #include "socketengine.h"
21 #include "command_parse.h"
22 #include "cull_list.h"
24 void InspIRCd::ProcessUser(userrec* cu)
28 if (cu->GetFd() == FD_MAGIC_NUMBER)
31 if (this->Config->GetIOHook(cu->GetPort()))
38 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
40 catch (CoreException& modexcept)
42 this->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
56 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
59 this->Log(DEBUG,"Read result: %d",result);
61 if ((result) && (result != -EAGAIN))
67 this->stats->statsRecv += result;
69 * perform a check on the raw buffer as an array (not a string!) to remove
70 * character 0 which is illegal in the RFC - replace them with spaces.
71 * XXX - no garauntee there's not \0's in the middle of the data,
72 * and no reason for it to be terminated either. -- Om
75 for (int checker = 0; checker < result; checker++)
77 if (ReadBuffer[checker] == 0)
78 ReadBuffer[checker] = ' ';
82 ReadBuffer[result] = '\0';
85 currfd = current->GetFd();
87 // add the data to the users buffer
90 if (!current->AddBuffer(ReadBuffer))
92 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
93 if (current->registered == REG_ALL)
95 // Make sure they arn't flooding long lines.
96 if (TIME > current->reset_due)
98 current->reset_due = TIME + current->threshold;
99 current->lines_in = 0;
104 if (current->lines_in > current->flood)
106 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
107 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
108 current->SetWriteError("Excess flood");
113 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
119 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
120 this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
121 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
122 XLines->apply_lines(APPLY_ZLINES);
128 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
130 if (current->registered == REG_ALL)
132 current->SetWriteError("RecvQ exceeded");
136 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
137 this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
138 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
139 XLines->apply_lines(APPLY_ZLINES);
145 // while there are complete lines to process...
146 while (current->BufferIsReady())
148 if (TIME > current->reset_due)
150 current->reset_due = TIME + current->threshold;
151 current->lines_in = 0;
154 if (++current->lines_in > current->flood)
156 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
157 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
158 current->SetWriteError("Excess flood");
162 if ((++floodlines > current->flood) && (current->flood != 0))
164 if (current->registered == REG_ALL)
166 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
167 SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
168 current->SetWriteError("Excess flood");
172 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
173 XLines->apply_lines(APPLY_ZLINES);
179 // use GetBuffer to copy single lines into the sanitized string
180 std::string single_line = current->GetBuffer();
181 current->bytes_in += single_line.length();
183 if (single_line.length() > 512)
184 single_line.resize(512);
186 EventHandler* old_comp = this->SE->GetRef(currfd);
188 this->Parser->ProcessBuffer(single_line,current);
190 * look for the user's record in case it's changed... if theyve quit,
191 * we cant do anything more with their buffer, so bail.
192 * there used to be an ugly, slow loop here. Now we have a reference
193 * table, life is much easier (and FASTER)
195 EventHandler* new_comp = this->SE->GetRef(currfd);
197 if (new_comp != old_comp)
204 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
206 this->Log(DEBUG,"killing: %s",cu->nick);
207 cu->SetWriteError(strerror(errno));
212 // result EAGAIN means nothing read
213 else if ((result == EAGAIN) || (result == -EAGAIN))
217 else if (result == 0)
219 cu->SetWriteError("Client exited");
220 this->Log(DEBUG,"Bailing from client exit");
226 * This function is called once a second from the mainloop.
227 * It is intended to do background checking on all the user structs, e.g.
228 * stuff like ping checks, registration timeouts, etc.
230 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
232 /* Is it time yet? */
233 if (TIME < next_call)
237 CullList GlobalGoners(this);
239 /* Time we actually need to call this again */
240 const time_t DUMMY_VALUE = 32768;
241 next_call = TIME + DUMMY_VALUE;
243 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
244 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
246 if (count2 >= this->local_users.size())
249 userrec* curr = this->local_users[count2];
254 * registration timeout -- didnt send USER/NICK/HOST
255 * in the time specified in their connection class.
257 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
259 this->Log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
260 GlobalGoners.AddItem(curr,"Registration timeout");
265 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
266 next_call = curr->timeout;
270 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
271 * say this user is ok to proceed, fully connect them.
273 bool ready = AllModulesReportReady(curr);
274 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
276 curr->dns_done = true;
277 this->stats->statsDnsBad++;
278 curr->FullConnect(&GlobalGoners);
283 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
284 next_call = curr->signon;
287 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
289 this->Log(DEBUG,"dns done, registered=3, and modules ready, OK");
290 curr->FullConnect(&GlobalGoners);
295 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
296 next_call = curr->signon + this->Config->dns_timeout;
299 // It's time to PING this user. Send them a ping.
300 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
302 // This user didn't answer the last ping, remove them
305 /* Everybody loves boobies. */
306 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
307 std::string boobies = "Ping timeout: " + ConvToStr(time) + " second" + (time > 1 ? "s" : "");
308 GlobalGoners.AddItem(curr, boobies);
310 curr->nping = TIME+curr->pingmax;
313 curr->Write("PING :%s",this->Config->ServerName);
315 curr->nping = TIME+curr->pingmax;
319 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
320 next_call = curr->nping;
325 /* If theres nothing to do, trigger in the next second, something might come up */
326 time_t delta = next_call - TIME;
327 if (delta == DUMMY_VALUE)
329 next_call = TIME + 1;
333 /* Remove all the queued users who are due to be quit, free memory used. */
334 GlobalGoners.Apply();