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::FloodQuitUser(userrec* current)
25 this->Log(DEFAULT,"Excess flood from: %s@%s", current->ident, current->host);
26 this->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
27 current->registered == REG_ALL ? current->nick : "",
28 current->registered == REG_ALL ? "!" : "", current->ident, current->host);
29 current->SetWriteError("Excess flood");
30 if (current->registered != REG_ALL)
32 XLines->add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
33 XLines->apply_lines(APPLY_ZLINES);
37 void InspIRCd::ProcessUser(userrec* cu)
41 if (cu->GetFd() == FD_MAGIC_NUMBER)
44 if (this->Config->GetIOHook(cu->GetPort()))
51 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
53 catch (CoreException& modexcept)
55 this->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
69 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
72 if ((result) && (result != -EAGAIN))
78 this->stats->statsRecv += result;
80 * perform a check on the raw buffer as an array (not a string!) to remove
81 * character 0 which is illegal in the RFC - replace them with spaces.
82 * XXX - no garauntee there's not \0's in the middle of the data,
83 * and no reason for it to be terminated either. -- Om
86 for (int checker = 0; checker < result; checker++)
88 if (ReadBuffer[checker] == 0)
89 ReadBuffer[checker] = ' ';
93 ReadBuffer[result] = '\0';
96 currfd = current->GetFd();
98 // add the data to the users buffer
101 if (!current->AddBuffer(ReadBuffer))
103 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
104 if (current->registered == REG_ALL)
106 // Make sure they arn't flooding long lines.
107 if (TIME > current->reset_due)
109 current->reset_due = TIME + current->threshold;
110 current->lines_in = 0;
115 if (current->flood && current->lines_in > current->flood)
116 FloodQuitUser(current);
119 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
120 current->recvq.clear();
124 FloodQuitUser(current);
129 // while there are complete lines to process...
130 while (current->BufferIsReady())
132 if (TIME > current->reset_due)
134 current->reset_due = TIME + current->threshold;
135 current->lines_in = 0;
138 if (++current->lines_in > current->flood && current->flood)
140 FloodQuitUser(current);
144 if ((++floodlines > current->flood) && (current->flood != 0))
146 FloodQuitUser(current);
150 // use GetBuffer to copy single lines into the sanitized string
151 std::string single_line = current->GetBuffer();
152 current->bytes_in += single_line.length();
154 if (single_line.length() > MAXBUF - 2) /* MAXBUF is 514 to allow for neccessary line terminators */
155 single_line.resize(MAXBUF - 2); /* So to trim to 512 here, we use MAXBUF - 2 */
157 this->Parser->ProcessBuffer(single_line, current);
163 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
165 userrec::QuitUser(this, cu, errno ? strerror(errno) : "EOF from client");
170 // result EAGAIN means nothing read
171 else if ((result == EAGAIN) || (result == -EAGAIN))
175 else if (result == 0)
177 userrec::QuitUser(this, cu, "Connection closed");
183 * This function is called once a second from the mainloop.
184 * It is intended to do background checking on all the user structs, e.g.
185 * stuff like ping checks, registration timeouts, etc.
187 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
189 /* Is it time yet? */
190 if (TIME < next_call)
194 /* Time we actually need to call this again */
195 const time_t DUMMY_VALUE = 32768;
196 next_call = TIME + DUMMY_VALUE;
198 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
199 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); ++count2)
201 userrec* curr = *count2;
204 * registration timeout -- didnt send USER/NICK/HOST
205 * in the time specified in their connection class.
207 if ((TIME > curr->timeout) && (curr->registered != REG_ALL))
210 userrec::QuitUser(this, curr, "Registration timeout");
215 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
216 next_call = curr->timeout;
219 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
220 * say this user is ok to proceed, fully connect them.
222 bool ready = AllModulesReportReady(curr);
223 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
227 curr->WriteServ("NOTICE Auth :*** Could not resolve your hostname: Request timed out; using your IP address (%s) instead.", curr->GetIPString());
228 curr->dns_done = true;
230 this->stats->statsDnsBad++;
236 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
237 next_call = curr->signon;
240 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
247 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
248 next_call = curr->signon + this->Config->dns_timeout;
251 // It's time to PING this user. Send them a ping.
252 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
254 // This user didn't answer the last ping, remove them
257 /* Everybody loves boobies. */
258 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
259 char message[MAXBUF];
260 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
263 curr->nping = TIME+curr->pingmax;
264 userrec::QuitUser(this, curr, message);
267 curr->Write("PING :%s",this->Config->ServerName);
269 curr->nping = TIME+curr->pingmax;
273 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
274 next_call = curr->nping;
278 /* If theres nothing to do, trigger in the next second, something might come up */
279 time_t delta = next_call - TIME;
280 if (delta == DUMMY_VALUE)
282 next_call = TIME + 1;