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 * ---------------------------------------------------
17 #include "socketengine.h"
18 #include "command_parse.h"
20 void FloodQuitUserHandler::Call(userrec* current)
22 Server->Log(DEFAULT,"Excess flood from: %s@%s", current->ident, current->host);
23 Server->SNO->WriteToSnoMask('f',"Excess flood from: %s%s%s@%s",
24 current->registered == REG_ALL ? current->nick : "",
25 current->registered == REG_ALL ? "!" : "", current->ident, current->host);
26 userrec::QuitUser(Server, current, "Excess flood");
27 if (current->registered != REG_ALL)
29 Server->XLines->add_zline(120, Server->Config->ServerName, "Flood from unregistered connection", current->GetIPString());
30 Server->XLines->apply_lines(APPLY_ZLINES);
34 void ProcessUserHandler::Call(userrec* cu)
38 if (cu->GetFd() == FD_MAGIC_NUMBER)
41 char* ReadBuffer = Server->GetReadBuffer();
43 if (Server->Config->GetIOHook(cu->GetPort()))
50 MOD_RESULT = Server->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->GetFd(),ReadBuffer,sizeof(ReadBuffer),result2);
52 catch (CoreException& modexcept)
54 Server->Log(DEBUG, "%s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
68 result = cu->ReadData(ReadBuffer, sizeof(ReadBuffer));
71 if ((result) && (result != -EAGAIN))
77 Server->stats->statsRecv += result;
79 * perform a check on the raw buffer as an array (not a string!) to remove
80 * character 0 which is illegal in the RFC - replace them with spaces.
83 for (int checker = 0; checker < result; checker++)
85 if (ReadBuffer[checker] == 0)
86 ReadBuffer[checker] = ' ';
90 ReadBuffer[result] = '\0';
93 currfd = current->GetFd();
95 // add the data to the users buffer
98 if (!current->AddBuffer(ReadBuffer))
100 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
101 if (current->registered == REG_ALL)
103 // Make sure they arn't flooding long lines.
104 if (Server->Time() > current->reset_due)
106 current->reset_due = Server->Time() + current->threshold;
107 current->lines_in = 0;
112 if (current->flood && current->lines_in > current->flood)
113 Server->FloodQuitUser(current);
116 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over %d chars) Please shorten it.", current->nick, MAXBUF-2);
117 current->recvq.clear();
121 Server->FloodQuitUser(current);
126 // while there are complete lines to process...
127 while (current->BufferIsReady())
129 if (Server->Time() > current->reset_due)
131 current->reset_due = Server->Time() + current->threshold;
132 current->lines_in = 0;
135 if (++current->lines_in > current->flood && current->flood)
137 Server->FloodQuitUser(current);
141 if ((++floodlines > current->flood) && (current->flood != 0))
143 Server->FloodQuitUser(current);
147 // use GetBuffer to copy single lines into the sanitized string
148 std::string single_line = current->GetBuffer();
149 current->bytes_in += single_line.length();
151 if (single_line.length() > MAXBUF - 2) /* MAXBUF is 514 to allow for neccessary line terminators */
152 single_line.resize(MAXBUF - 2); /* So to trim to 512 here, we use MAXBUF - 2 */
154 Server->Parser->ProcessBuffer(single_line, current);
160 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
162 userrec::QuitUser(Server, cu, errno ? strerror(errno) : "EOF from client");
167 // result EAGAIN means nothing read
168 else if ((result == EAGAIN) || (result == -EAGAIN))
172 else if (result == 0)
174 userrec::QuitUser(Server, cu, "Connection closed");
180 * This function is called once a second from the mainloop.
181 * It is intended to do background checking on all the user structs, e.g.
182 * stuff like ping checks, registration timeouts, etc.
184 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
186 /* Is it time yet? */
187 if (TIME < next_call)
191 /* Time we actually need to call this again */
192 const time_t DUMMY_VALUE = 32768;
193 next_call = TIME + DUMMY_VALUE;
195 for (std::vector<userrec*>::iterator count2 = local_users.begin(); count2 != local_users.end(); count2++)
197 userrec* curr = *count2;
200 * registration timeout -- didnt send USER/NICK/HOST
201 * in the time specified in their connection class.
203 if ((TIME > curr->timeout) && (curr->registered != REG_ALL))
206 userrec::QuitUser(this, curr, "Registration timeout");
211 if ((curr->registered != REG_ALL) && (next_call > (time_t)curr->timeout))
212 next_call = curr->timeout;
215 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
216 * say this user is ok to proceed, fully connect them.
218 bool ready = AllModulesReportReady(curr);
219 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (ready))
223 curr->WriteServ("NOTICE Auth :*** Could not resolve your hostname: Request timed out; using your IP address (%s) instead.", curr->GetIPString());
224 curr->dns_done = true;
226 this->stats->statsDnsBad++;
232 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon))
233 next_call = curr->signon;
236 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (ready))
243 if ((curr->registered == REG_NICKUSER) && (ready) && (next_call > curr->signon + this->Config->dns_timeout))
244 next_call = curr->signon + this->Config->dns_timeout;
247 // It's time to PING this user. Send them a ping.
248 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
250 // This user didn't answer the last ping, remove them
253 /* Everybody loves boobies. */
254 time_t time = this->Time(false) - (curr->nping - curr->pingmax);
255 char message[MAXBUF];
256 snprintf(message, MAXBUF, "Ping timeout: %ld second%s", (long)time, time > 1 ? "s" : "");
259 curr->nping = TIME+curr->pingmax;
260 userrec::QuitUser(this, curr, message);
263 curr->Write("PING :%s",this->Config->ServerName);
265 curr->nping = TIME+curr->pingmax;
269 if ((curr->registered == REG_ALL) && (next_call > curr->nping))
270 next_call = curr->nping;
274 /* If theres nothing to do, trigger in the next second, something might come up */
275 time_t delta = next_call - TIME;
276 if (delta == DUMMY_VALUE)
278 next_call = TIME + 1;