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 * ---------------------------------------------------
15 #include "configreader.h"
20 #include "socketengine.h"
21 #include "command_parse.h"
23 void InspIRCd::ProcessUser(userrec* cu)
27 if (cu->GetFd() == FD_MAGIC_NUMBER)
30 if (this->Config->GetIOHook(cu->GetPort()))
37 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
39 catch (CoreException& modexcept)
41 this->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
55 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
58 if ((result) && (result != -EAGAIN))
64 this->stats->statsRecv += result;
66 * perform a check on the raw buffer as an array (not a string!) to remove
67 * character 0 which is illegal in the RFC - replace them with spaces.
68 * XXX - no garauntee there's not \0's in the middle of the data,
69 * and no reason for it to be terminated either. -- Om
72 for (int checker = 0; checker < result; checker++)
74 if (ReadBuffer[checker] == 0)
75 ReadBuffer[checker] = ' ';
79 ReadBuffer[result] = '\0';
82 currfd = current->GetFd();
84 // add the data to the users buffer
87 if (!current->AddBuffer(ReadBuffer))
89 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
90 if (current->registered == REG_ALL)
92 // Make sure they arn't flooding long lines.
93 if (TIME > current->reset_due)
95 current->reset_due = TIME + current->threshold;
96 current->lines_in = 0;
101 if (current->lines_in > current->flood)
103 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
104 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
105 current->SetWriteError("Excess flood");
110 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
116 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
117 this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
118 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
119 XLines->apply_lines(APPLY_ZLINES);
125 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
127 if (current->registered == REG_ALL)
129 current->SetWriteError("RecvQ exceeded");
133 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
134 this->SNO->WriteToSnoMask('f',"Excess flood from: %s",current->GetIPString());
135 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
136 XLines->apply_lines(APPLY_ZLINES);
142 // while there are complete lines to process...
143 while (current->BufferIsReady())
145 if (TIME > current->reset_due)
147 current->reset_due = TIME + current->threshold;
148 current->lines_in = 0;
151 if (++current->lines_in > current->flood)
153 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
154 this->SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
155 current->SetWriteError("Excess flood");
159 if ((++floodlines > current->flood) && (current->flood != 0))
161 if (current->registered == REG_ALL)
163 this->Log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
164 SNO->WriteToSnoMask('f',"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
165 current->SetWriteError("Excess flood");
169 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
170 XLines->apply_lines(APPLY_ZLINES);
176 // use GetBuffer to copy single lines into the sanitized string
177 std::string single_line = current->GetBuffer();
178 current->bytes_in += single_line.length();
180 if (single_line.length() > MAXBUF - 2) /* MAXBUF is 514 to allow for neccessary line terminators */
181 single_line.resize(MAXBUF - 2); /* So to trim to 512 here, we use MAXBUF - 2 */
183 EventHandler* old_comp = this->SE->GetRef(currfd);
185 this->Parser->ProcessBuffer(single_line,current);
187 * look for the user's record in case it's changed... if theyve quit,
188 * we cant do anything more with their buffer, so bail.
189 * there used to be an ugly, slow loop here. Now we have a reference
190 * table, life is much easier (and FASTER)
192 EventHandler* new_comp = this->SE->GetRef(currfd);
194 if (new_comp != old_comp)
201 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
203 cu->SetWriteError(strerror(errno));
208 // result EAGAIN means nothing read
209 else if ((result == EAGAIN) || (result == -EAGAIN))
213 else if (result == 0)
215 cu->SetWriteError("Client exited");
221 * This function is called once a second from the mainloop.
222 * It is intended to do background checking on all the user structs, e.g.
223 * stuff like ping checks, registration timeouts, etc.
225 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
227 /* Is it time yet? */
228 if (TIME < next_call)
232 /* Time we actually need to call this again */
233 const time_t DUMMY_VALUE = 32768;
234 next_call = TIME + DUMMY_VALUE;
236 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
237 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
239 if (count2 >= this->local_users.size())
242 userrec* curr = this->local_users[count2];
247 * registration timeout -- didnt send USER/NICK/HOST
248 * in the time specified in their connection class.
250 if ((TIME > curr->timeout) && (curr->registered != REG_ALL))
253 GlobalCulls.AddItem(curr,"Registration timeout");
258 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
259 next_call = curr->timeout;
263 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
264 * say this user is ok to proceed, fully connect them.
266 bool ready = AllModulesReportReady(curr);
267 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
271 curr->WriteServ("NOTICE Auth :*** Could not resolve your hostname: Request timed out; using your IP address (%s) instead.", curr->GetIPString());
272 curr->dns_done = true;
274 this->stats->statsDnsBad++;
280 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
281 next_call = curr->signon;
284 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
291 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
292 next_call = curr->signon + this->Config->dns_timeout;
295 // It's time to PING this user. Send them a ping.
296 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
298 // This user didn't answer the last ping, remove them
301 /* Everybody loves boobies. */
302 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
303 std::string boobies = "Ping timeout: " + ConvToStr(time) + " second" + (time > 1 ? "s" : "");
305 GlobalCulls.AddItem(curr, boobies);
307 curr->nping = TIME+curr->pingmax;
310 curr->Write("PING :%s",this->Config->ServerName);
312 curr->nping = TIME+curr->pingmax;
316 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
317 next_call = curr->nping;
322 /* If theres nothing to do, trigger in the next second, something might come up */
323 time_t delta = next_call - TIME;
324 if (delta == DUMMY_VALUE)
326 next_call = TIME + 1;