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 extern user_hash clientlist;
65 extern chan_hash chanlist;
67 void InspIRCd::ProcessUser(userrec* cu)
71 if (cu->fd == FD_MAGIC_NUMBER)
74 log(DEBUG,"Processing user with fd %d",cu->fd);
76 if (this->Config->GetIOHook(cu->GetPort()))
83 MOD_RESULT = this->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
84 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
86 catch (ModuleException& modexcept)
88 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
102 result = cu->ReadData(data, 65535);
105 log(DEBUG,"Read result: %d",result);
107 if ((result) && (result != -EAGAIN))
113 this->stats->statsRecv += result;
115 * perform a check on the raw buffer as an array (not a string!) to remove
116 * character 0 which is illegal in the RFC - replace them with spaces.
117 * XXX - no garauntee there's not \0's in the middle of the data,
118 * and no reason for it to be terminated either. -- Om
121 for (int checker = 0; checker < result; checker++)
123 if (data[checker] == 0)
131 currfd = current->fd;
133 // add the data to the users buffer
136 if (!current->AddBuffer(data))
138 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
139 if (current->registered == REG_ALL)
141 // Make sure they arn't flooding long lines.
142 if (TIME > current->reset_due)
144 current->reset_due = TIME + current->threshold;
145 current->lines_in = 0;
150 if (current->lines_in > current->flood)
152 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
153 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
154 userrec::QuitUser(current,"Excess flood");
159 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
165 WriteOpers("*** Excess flood from %s",current->GetIPString());
166 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
167 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
168 apply_lines(APPLY_ZLINES);
174 if (current->recvq.length() > (unsigned)this->Config->NetBufferSize)
176 if (current->registered == REG_ALL)
178 userrec::QuitUser(current,"RecvQ exceeded");
182 WriteOpers("*** Excess flood from %s",current->GetIPString());
183 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
184 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
185 apply_lines(APPLY_ZLINES);
191 // while there are complete lines to process...
192 while (current->BufferIsReady())
194 if (TIME > current->reset_due)
196 current->reset_due = TIME + current->threshold;
197 current->lines_in = 0;
200 if (++current->lines_in > current->flood)
202 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
203 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
204 userrec::QuitUser(current,"Excess flood");
208 if ((++floodlines > current->flood) && (current->flood != 0))
210 if (current->registered == REG_ALL)
212 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
213 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
214 userrec::QuitUser(current,"Excess flood");
218 add_zline(120,this->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
219 apply_lines(APPLY_ZLINES);
225 // use GetBuffer to copy single lines into the sanitized string
226 std::string single_line = current->GetBuffer();
227 current->bytes_in += single_line.length();
229 if (single_line.length() > 512)
230 single_line.resize(512);
232 userrec* old_comp = this->fd_ref_table[currfd];
234 this->Parser->ProcessBuffer(single_line,current);
236 * look for the user's record in case it's changed... if theyve quit,
237 * we cant do anything more with their buffer, so bail.
238 * there used to be an ugly, slow loop here. Now we have a reference
239 * table, life is much easier (and FASTER)
241 userrec* new_comp = this->fd_ref_table[currfd];
242 if ((currfd < 0) || (!this->fd_ref_table[currfd]) || (old_comp != new_comp))
248 /* The user is still here, flush their buffer */
249 current->FlushWriteBuf();
256 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
258 log(DEBUG,"killing: %s",cu->nick);
259 userrec::QuitUser(cu,strerror(errno));
264 // result EAGAIN means nothing read
265 else if ((result == EAGAIN) || (result == -EAGAIN))
269 else if (result == 0)
271 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
272 userrec::QuitUser(cu,"Client exited");
273 log(DEBUG,"Bailing from client exit");
278 void InspIRCd::DoSocketTimeouts(time_t TIME)
280 unsigned int numsockets = this->module_sockets.size();
281 SocketEngine* SE = this->SE;
283 for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
285 InspSocket* s = (InspSocket*)*a;
286 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (this->socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
288 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
289 this->socket_ref[s->GetFd()] = NULL;
290 SE->DelFd(s->GetFd());
291 this->module_sockets.erase(a);
297 if (this->module_sockets.size() != numsockets)
303 * This function is called once a second from the mainloop.
304 * It is intended to do background checking on all the user structs, e.g.
305 * stuff like ping checks, registration timeouts, etc. This function is
306 * also responsible for checking if InspSocket derived classes are timed out.
308 void InspIRCd::DoBackgroundUserStuff(time_t TIME)
310 CullList GlobalGoners;
312 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
313 for (unsigned long count2 = 0; count2 != local_users.size(); count2++)
315 if (count2 >= local_users.size())
318 userrec* curr = local_users[count2];
323 * registration timeout -- didnt send USER/NICK/HOST
324 * in the time specified in their connection class.
326 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
328 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
329 //ZapThisDns(curr->fd);
330 GlobalGoners.AddItem(curr,"Registration timeout");
334 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
335 * say this user is ok to proceed, fully connect them.
337 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
339 curr->dns_done = true;
340 //ZapThisDns(curr->fd);
341 this->stats->statsDnsBad++;
342 curr->FullConnect(&GlobalGoners);
345 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
347 log(DEBUG,"dns done, registered=3, and modules ready, OK");
348 curr->FullConnect(&GlobalGoners);
349 //ZapThisDns(curr->fd);
352 // It's time to PING this user. Send them a ping.
353 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
355 // This user didn't answer the last ping, remove them
358 GlobalGoners.AddItem(curr,"Ping timeout");
360 curr->nping = TIME+curr->pingmax;
363 curr->Write("PING :%s",this->Config->ServerName);
365 curr->nping = TIME+curr->pingmax;
369 * We can flush the write buffer as the last thing we do, because if they
370 * match any of the above conditions its no use flushing their buffer anyway.
373 curr->FlushWriteBuf();
374 if (*curr->GetWriteError())
376 GlobalGoners.AddItem(curr,curr->GetWriteError());
384 /* Remove all the queued users who are due to be quit, free memory used. */
385 GlobalGoners.Apply();