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;
61 extern std::vector<userrec*> local_users;
64 void InspIRCd::ProcessUser(userrec* cu)
68 if (cu->fd == FD_MAGIC_NUMBER)
71 log(DEBUG,"Processing user with fd %d",cu->fd);
73 if (this->Config->GetIOHook(cu->GetPort()))
80 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
81 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
83 catch (ModuleException& modexcept)
85 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
99 result = cu->ReadData(data, 65535);
102 log(DEBUG,"Read result: %d",result);
104 if ((result) && (result != -EAGAIN))
110 this->stats->statsRecv += result;
112 * perform a check on the raw buffer as an array (not a string!) to remove
113 * character 0 which is illegal in the RFC - replace them with spaces.
114 * XXX - no garauntee there's not \0's in the middle of the data,
115 * and no reason for it to be terminated either. -- Om
118 for (int checker = 0; checker < result; checker++)
120 if (data[checker] == 0)
128 currfd = current->fd;
130 // add the data to the users buffer
133 if (!current->AddBuffer(data))
135 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
136 if (current->registered == REG_ALL)
138 // Make sure they arn't flooding long lines.
139 if (TIME > current->reset_due)
141 current->reset_due = TIME + current->threshold;
142 current->lines_in = 0;
147 if (current->lines_in > current->flood)
149 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
150 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
151 userrec::QuitUser(current,"Excess flood");
156 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
162 WriteOpers("*** Excess flood from %s",current->GetIPString());
163 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
164 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
165 apply_lines(APPLY_ZLINES);
171 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
173 if (current->registered == REG_ALL)
175 userrec::QuitUser(current,"RecvQ exceeded");
179 WriteOpers("*** Excess flood from %s",current->GetIPString());
180 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
181 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
182 apply_lines(APPLY_ZLINES);
188 // while there are complete lines to process...
189 while (current->BufferIsReady())
191 if (TIME > current->reset_due)
193 current->reset_due = TIME + current->threshold;
194 current->lines_in = 0;
197 if (++current->lines_in > current->flood)
199 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
200 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
201 userrec::QuitUser(current,"Excess flood");
205 if ((++floodlines > current->flood) && (current->flood != 0))
207 if (current->registered == REG_ALL)
209 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
210 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
211 userrec::QuitUser(current,"Excess flood");
215 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
216 apply_lines(APPLY_ZLINES);
222 // use GetBuffer to copy single lines into the sanitized string
223 std::string single_line = current->GetBuffer();
224 current->bytes_in += single_line.length();
226 if (single_line.length() > 512)
227 single_line.resize(512);
229 userrec* old_comp = this->fd_ref_table[currfd];
231 this->Parser->ProcessBuffer(single_line,current);
233 * look for the user's record in case it's changed... if theyve quit,
234 * we cant do anything more with their buffer, so bail.
235 * there used to be an ugly, slow loop here. Now we have a reference
236 * table, life is much easier (and FASTER)
238 userrec* new_comp = this->fd_ref_table[currfd];
239 if ((currfd < 0) || (!this->fd_ref_table[currfd]) || (old_comp != new_comp))
245 /* The user is still here, flush their buffer */
246 current->FlushWriteBuf();
253 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
255 log(DEBUG,"killing: %s",cu->nick);
256 userrec::QuitUser(cu,strerror(errno));
261 // result EAGAIN means nothing read
262 else if ((result == EAGAIN) || (result == -EAGAIN))
266 else if (result == 0)
268 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
269 userrec::QuitUser(cu,"Client exited");
270 log(DEBUG,"Bailing from client exit");
275 void InspIRCd::DoSocketTimeouts(time_t TIME)
277 unsigned int numsockets = this->module_sockets.size();
278 SocketEngine* SE = this->SE;
280 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
282 InspSocket* s = (InspSocket*)*a;
283 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (this->socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
285 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
286 this->socket_ref[s->GetFd()] = NULL;
287 SE->DelFd(s->GetFd());
288 this->module_sockets.erase(a);
294 if (this->module_sockets.size() != numsockets)
300 * This function is called once a second from the mainloop.
301 * It is intended to do background checking on all the user structs, e.g.
302 * stuff like ping checks, registration timeouts, etc. This function is
303 * also responsible for checking if InspSocket derived classes are timed out.
305 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
307 CullList GlobalGoners;
309 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
310 for (unsigned long count2 = 0; count2 != local_users.size(); count2++)
312 if (count2 >= local_users.size())
315 userrec* curr = local_users[count2];
320 * registration timeout -- didnt send USER/NICK/HOST
321 * in the time specified in their connection class.
323 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
325 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
326 //ZapThisDns(curr->fd);
327 GlobalGoners.AddItem(curr,"Registration timeout");
331 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
332 * say this user is ok to proceed, fully connect them.
334 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
336 curr->dns_done = true;
337 //ZapThisDns(curr->fd);
338 this->stats->statsDnsBad++;
339 curr->FullConnect(&GlobalGoners);
342 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
344 log(DEBUG,"dns done, registered=3, and modules ready, OK");
345 curr->FullConnect(&GlobalGoners);
346 //ZapThisDns(curr->fd);
349 // It's time to PING this user. Send them a ping.
350 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
352 // This user didn't answer the last ping, remove them
355 GlobalGoners.AddItem(curr,"Ping timeout");
357 curr->nping = TIME+curr->pingmax;
360 curr->Write("PING :%s",this->Config->ServerName);
362 curr->nping = TIME+curr->pingmax;
366 * We can flush the write buffer as the last thing we do, because if they
367 * match any of the above conditions its no use flushing their buffer anyway.
370 curr->FlushWriteBuf();
371 if (*curr->GetWriteError())
373 GlobalGoners.AddItem(curr,curr->GetWriteError());
381 /* Remove all the queued users who are due to be quit, free memory used. */
382 GlobalGoners.Apply();