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"
23 #include "configreader.h"
26 #include <sys/errno.h>
27 #include <sys/ioctl.h>
28 #include <sys/utsname.h>
31 #include <ext/hash_map>
46 #include "inspstring.h"
47 #include "helperfuncs.h"
49 #include "socketengine.h"
50 #include "userprocess.h"
52 #include "command_parse.h"
53 #include "cull_list.h"
55 extern struct sockaddr_in client,server;
56 extern socklen_t length;
57 extern std::vector<Module*> modules;
58 extern std::vector<ircd_module*> factory;
60 extern time_t OLDTIME;
63 void InspIRCd::ProcessUser(userrec* cu)
67 if (cu->fd == FD_MAGIC_NUMBER)
70 log(DEBUG,"Processing user with fd %d",cu->fd);
72 if (this->Config->GetIOHook(cu->GetPort()))
79 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
80 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
82 catch (ModuleException& modexcept)
84 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
98 result = cu->ReadData(data, 65535);
101 log(DEBUG,"Read result: %d",result);
103 if ((result) && (result != -EAGAIN))
109 this->stats->statsRecv += result;
111 * perform a check on the raw buffer as an array (not a string!) to remove
112 * character 0 which is illegal in the RFC - replace them with spaces.
113 * XXX - no garauntee there's not \0's in the middle of the data,
114 * and no reason for it to be terminated either. -- Om
117 for (int checker = 0; checker < result; checker++)
119 if (data[checker] == 0)
127 currfd = current->fd;
129 // add the data to the users buffer
132 if (!current->AddBuffer(data))
134 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
135 if (current->registered == REG_ALL)
137 // Make sure they arn't flooding long lines.
138 if (TIME > current->reset_due)
140 current->reset_due = TIME + current->threshold;
141 current->lines_in = 0;
146 if (current->lines_in > current->flood)
148 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
149 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
150 userrec::QuitUser(this, current,"Excess flood");
155 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
161 WriteOpers("*** Excess flood from %s",current->GetIPString());
162 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
163 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
164 apply_lines(APPLY_ZLINES);
170 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
172 if (current->registered == REG_ALL)
174 userrec::QuitUser(this, current,"RecvQ exceeded");
178 WriteOpers("*** Excess flood from %s",current->GetIPString());
179 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
180 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
181 apply_lines(APPLY_ZLINES);
187 // while there are complete lines to process...
188 while (current->BufferIsReady())
190 if (TIME > current->reset_due)
192 current->reset_due = TIME + current->threshold;
193 current->lines_in = 0;
196 if (++current->lines_in > current->flood)
198 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
199 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
200 userrec::QuitUser(this, current,"Excess flood");
204 if ((++floodlines > current->flood) && (current->flood != 0))
206 if (current->registered == REG_ALL)
208 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
209 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
210 userrec::QuitUser(this, current,"Excess flood");
214 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
215 apply_lines(APPLY_ZLINES);
221 // use GetBuffer to copy single lines into the sanitized string
222 std::string single_line = current->GetBuffer();
223 current->bytes_in += single_line.length();
225 if (single_line.length() > 512)
226 single_line.resize(512);
228 userrec* old_comp = this->fd_ref_table[currfd];
230 this->Parser->ProcessBuffer(single_line,current);
232 * look for the user's record in case it's changed... if theyve quit,
233 * we cant do anything more with their buffer, so bail.
234 * there used to be an ugly, slow loop here. Now we have a reference
235 * table, life is much easier (and FASTER)
237 userrec* new_comp = this->fd_ref_table[currfd];
238 if ((currfd < 0) || (!this->fd_ref_table[currfd]) || (old_comp != new_comp))
244 /* The user is still here, flush their buffer */
245 current->FlushWriteBuf();
252 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
254 log(DEBUG,"killing: %s",cu->nick);
255 userrec::QuitUser(this,cu,strerror(errno));
260 // result EAGAIN means nothing read
261 else if ((result == EAGAIN) || (result == -EAGAIN))
265 else if (result == 0)
267 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
268 userrec::QuitUser(this,cu,"Client exited");
269 log(DEBUG,"Bailing from client exit");
274 void InspIRCd::DoSocketTimeouts(time_t TIME)
276 unsigned int numsockets = this->module_sockets.size();
277 SocketEngine* SE = this->SE;
279 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
281 InspSocket* s = (InspSocket*)*a;
282 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (this->socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
284 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
285 this->socket_ref[s->GetFd()] = NULL;
286 SE->DelFd(s->GetFd());
287 this->module_sockets.erase(a);
293 if (this->module_sockets.size() != numsockets)
299 * This function is called once a second from the mainloop.
300 * It is intended to do background checking on all the user structs, e.g.
301 * stuff like ping checks, registration timeouts, etc. This function is
302 * also responsible for checking if InspSocket derived classes are timed out.
304 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
306 CullList GlobalGoners;
308 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
309 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
311 if (count2 >= this->local_users.size())
314 userrec* curr = this->local_users[count2];
319 * registration timeout -- didnt send USER/NICK/HOST
320 * in the time specified in their connection class.
322 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
324 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
325 //ZapThisDns(curr->fd);
326 GlobalGoners.AddItem(curr,"Registration timeout");
330 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
331 * say this user is ok to proceed, fully connect them.
333 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
335 curr->dns_done = true;
336 //ZapThisDns(curr->fd);
337 this->stats->statsDnsBad++;
338 curr->FullConnect(&GlobalGoners);
341 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
343 log(DEBUG,"dns done, registered=3, and modules ready, OK");
344 curr->FullConnect(&GlobalGoners);
345 //ZapThisDns(curr->fd);
348 // It's time to PING this user. Send them a ping.
349 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
351 // This user didn't answer the last ping, remove them
354 GlobalGoners.AddItem(curr,"Ping timeout");
356 curr->nping = TIME+curr->pingmax;
359 curr->Write("PING :%s",this->Config->ServerName);
361 curr->nping = TIME+curr->pingmax;
365 * We can flush the write buffer as the last thing we do, because if they
366 * match any of the above conditions its no use flushing their buffer anyway.
369 curr->FlushWriteBuf();
370 if (*curr->GetWriteError())
372 GlobalGoners.AddItem(curr,curr->GetWriteError());
380 /* Remove all the queued users who are due to be quit, free memory used. */
381 GlobalGoners.Apply();