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"
22 #include "configreader.h"
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
30 #include <ext/hash_map>
44 #include "inspstring.h"
45 #include "helperfuncs.h"
47 #include "socketengine.h"
48 #include "userprocess.h"
51 #include "command_parse.h"
52 #include "cull_list.h"
54 extern struct sockaddr_in client,server;
55 extern socklen_t length;
58 extern time_t OLDTIME;
61 void InspIRCd::ProcessUser(userrec* cu)
65 if (cu->fd == FD_MAGIC_NUMBER)
68 log(DEBUG,"Processing user with fd %d",cu->fd);
70 if (this->Config->GetIOHook(cu->GetPort()))
77 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
78 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
80 catch (ModuleException& modexcept)
82 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
96 result = cu->ReadData(data, 65535);
99 log(DEBUG,"Read result: %d",result);
101 if ((result) && (result != -EAGAIN))
107 this->stats->statsRecv += result;
109 * perform a check on the raw buffer as an array (not a string!) to remove
110 * character 0 which is illegal in the RFC - replace them with spaces.
111 * XXX - no garauntee there's not \0's in the middle of the data,
112 * and no reason for it to be terminated either. -- Om
115 for (int checker = 0; checker < result; checker++)
117 if (data[checker] == 0)
125 currfd = current->fd;
127 // add the data to the users buffer
130 if (!current->AddBuffer(data))
132 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
133 if (current->registered == REG_ALL)
135 // Make sure they arn't flooding long lines.
136 if (TIME > current->reset_due)
138 current->reset_due = TIME + current->threshold;
139 current->lines_in = 0;
144 if (current->lines_in > current->flood)
146 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
147 this->WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
148 userrec::QuitUser(this, current,"Excess flood");
153 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
159 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
160 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
161 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
162 apply_lines(APPLY_ZLINES);
168 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
170 if (current->registered == REG_ALL)
172 userrec::QuitUser(this, current,"RecvQ exceeded");
176 this->WriteOpers("*** Excess flood from %s",current->GetIPString());
177 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
178 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
179 apply_lines(APPLY_ZLINES);
185 // while there are complete lines to process...
186 while (current->BufferIsReady())
188 if (TIME > current->reset_due)
190 current->reset_due = TIME + current->threshold;
191 current->lines_in = 0;
194 if (++current->lines_in > current->flood)
196 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
197 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
198 userrec::QuitUser(this, current,"Excess flood");
202 if ((++floodlines > current->flood) && (current->flood != 0))
204 if (current->registered == REG_ALL)
206 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
207 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
208 userrec::QuitUser(this, current,"Excess flood");
212 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
213 apply_lines(APPLY_ZLINES);
219 // use GetBuffer to copy single lines into the sanitized string
220 std::string single_line = current->GetBuffer();
221 current->bytes_in += single_line.length();
223 if (single_line.length() > 512)
224 single_line.resize(512);
226 userrec* old_comp = this->fd_ref_table[currfd];
228 this->Parser->ProcessBuffer(single_line,current);
230 * look for the user's record in case it's changed... if theyve quit,
231 * we cant do anything more with their buffer, so bail.
232 * there used to be an ugly, slow loop here. Now we have a reference
233 * table, life is much easier (and FASTER)
235 userrec* new_comp = this->fd_ref_table[currfd];
236 if ((currfd < 0) || (!this->fd_ref_table[currfd]) || (old_comp != new_comp))
242 /* The user is still here, flush their buffer */
243 current->FlushWriteBuf();
250 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
252 log(DEBUG,"killing: %s",cu->nick);
253 userrec::QuitUser(this,cu,strerror(errno));
258 // result EAGAIN means nothing read
259 else if ((result == EAGAIN) || (result == -EAGAIN))
263 else if (result == 0)
265 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
266 userrec::QuitUser(this,cu,"Client exited");
267 log(DEBUG,"Bailing from client exit");
272 void InspIRCd::DoSocketTimeouts(time_t TIME)
274 unsigned int numsockets = this->module_sockets.size();
275 SocketEngine* SE = this->SE;
277 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
279 InspSocket* s = (InspSocket*)*a;
280 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (this->socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
282 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
283 this->socket_ref[s->GetFd()] = NULL;
284 SE->DelFd(s->GetFd());
285 this->module_sockets.erase(a);
291 if (this->module_sockets.size() != numsockets)
297 * This function is called once a second from the mainloop.
298 * It is intended to do background checking on all the user structs, e.g.
299 * stuff like ping checks, registration timeouts, etc. This function is
300 * also responsible for checking if InspSocket derived classes are timed out.
302 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
304 CullList GlobalGoners(this);
306 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
307 for (unsigned long count2 = 0; count2 != this->local_users.size(); count2++)
309 if (count2 >= this->local_users.size())
312 userrec* curr = this->local_users[count2];
317 * registration timeout -- didnt send USER/NICK/HOST
318 * in the time specified in their connection class.
320 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
322 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
323 //ZapThisDns(curr->fd);
324 GlobalGoners.AddItem(curr,"Registration timeout");
328 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
329 * say this user is ok to proceed, fully connect them.
331 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
333 curr->dns_done = true;
334 //ZapThisDns(curr->fd);
335 this->stats->statsDnsBad++;
336 curr->FullConnect(&GlobalGoners);
339 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
341 log(DEBUG,"dns done, registered=3, and modules ready, OK");
342 curr->FullConnect(&GlobalGoners);
343 //ZapThisDns(curr->fd);
346 // It's time to PING this user. Send them a ping.
347 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
349 // This user didn't answer the last ping, remove them
352 GlobalGoners.AddItem(curr,"Ping timeout");
354 curr->nping = TIME+curr->pingmax;
357 curr->Write("PING :%s",this->Config->ServerName);
359 curr->nping = TIME+curr->pingmax;
363 * We can flush the write buffer as the last thing we do, because if they
364 * match any of the above conditions its no use flushing their buffer anyway.
367 curr->FlushWriteBuf();
368 if (*curr->GetWriteError())
370 GlobalGoners.AddItem(curr,curr->GetWriteError());
378 /* Remove all the queued users who are due to be quit, free memory used. */
379 GlobalGoners.Apply();