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;
59 extern std::vector<InspSocket*> module_sockets;
61 extern time_t OLDTIME;
62 extern std::vector<userrec*> local_users;
63 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
65 extern InspIRCd* ServerInstance;
66 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
69 extern user_hash clientlist;
70 extern chan_hash chanlist;
72 void ProcessUser(userrec* cu)
76 if (cu->fd == FD_MAGIC_NUMBER)
79 log(DEBUG,"Processing user with fd %d",cu->fd);
81 if (ServerInstance->Config->GetIOHook(cu->GetPort()))
88 MOD_RESULT = ServerInstance->Config->GetIOHook(cu->GetPort())->OnRawSocketRead(cu->fd,data,65535,result2);
89 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
91 catch (ModuleException& modexcept)
93 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
107 result = cu->ReadData(data, 65535);
110 log(DEBUG,"Read result: %d",result);
112 if ((result) && (result != -EAGAIN))
118 ServerInstance->stats->statsRecv += result;
120 * perform a check on the raw buffer as an array (not a string!) to remove
121 * character 0 which is illegal in the RFC - replace them with spaces.
122 * XXX - no garauntee there's not \0's in the middle of the data,
123 * and no reason for it to be terminated either. -- Om
126 for (int checker = 0; checker < result; checker++)
128 if (data[checker] == 0)
136 currfd = current->fd;
138 // add the data to the users buffer
141 if (!current->AddBuffer(data))
143 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
144 if (current->registered == REG_ALL)
146 // Make sure they arn't flooding long lines.
147 if (TIME > current->reset_due)
149 current->reset_due = TIME + current->threshold;
150 current->lines_in = 0;
155 if (current->lines_in > current->flood)
157 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
158 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
159 userrec::QuitUser(current,"Excess flood");
164 current->WriteServ("NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
170 WriteOpers("*** Excess flood from %s",current->GetIPString());
171 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
172 add_zline(120,ServerInstance->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
173 apply_lines(APPLY_ZLINES);
179 if (current->recvq.length() > (unsigned)ServerInstance->Config->NetBufferSize)
181 if (current->registered == REG_ALL)
183 userrec::QuitUser(current,"RecvQ exceeded");
187 WriteOpers("*** Excess flood from %s",current->GetIPString());
188 log(DEFAULT,"Excess flood from: %s",current->GetIPString());
189 add_zline(120,ServerInstance->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
190 apply_lines(APPLY_ZLINES);
196 // while there are complete lines to process...
197 while (current->BufferIsReady())
199 if (TIME > current->reset_due)
201 current->reset_due = TIME + current->threshold;
202 current->lines_in = 0;
205 if (++current->lines_in > current->flood)
207 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
208 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
209 userrec::QuitUser(current,"Excess flood");
213 if ((++floodlines > current->flood) && (current->flood != 0))
215 if (current->registered == REG_ALL)
217 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
218 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
219 userrec::QuitUser(current,"Excess flood");
223 add_zline(120,ServerInstance->Config->ServerName,"Flood from unregistered connection",current->GetIPString());
224 apply_lines(APPLY_ZLINES);
230 // use GetBuffer to copy single lines into the sanitized string
231 std::string single_line = current->GetBuffer();
232 current->bytes_in += single_line.length();
234 if (single_line.length() > 512)
235 single_line.resize(512);
237 userrec* old_comp = fd_ref_table[currfd];
239 ServerInstance->Parser->ProcessBuffer(single_line,current);
241 * look for the user's record in case it's changed... if theyve quit,
242 * we cant do anything more with their buffer, so bail.
243 * there used to be an ugly, slow loop here. Now we have a reference
244 * table, life is much easier (and FASTER)
246 userrec* new_comp = fd_ref_table[currfd];
247 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
253 /* The user is still here, flush their buffer */
254 current->FlushWriteBuf();
261 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
263 log(DEBUG,"killing: %s",cu->nick);
264 userrec::QuitUser(cu,strerror(errno));
269 // result EAGAIN means nothing read
270 else if ((result == EAGAIN) || (result == -EAGAIN))
274 else if (result == 0)
276 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
277 userrec::QuitUser(cu,"Client exited");
278 log(DEBUG,"Bailing from client exit");
283 void DoSocketTimeouts(time_t TIME)
285 unsigned int numsockets = module_sockets.size();
286 SocketEngine* SE = ServerInstance->SE;
288 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
290 InspSocket* s = (InspSocket*)*a;
291 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
293 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
294 socket_ref[s->GetFd()] = NULL;
295 SE->DelFd(s->GetFd());
296 module_sockets.erase(a);
302 if (module_sockets.size() != numsockets)
308 * This function is called once a second from the mainloop.
309 * It is intended to do background checking on all the user structs, e.g.
310 * stuff like ping checks, registration timeouts, etc. This function is
311 * also responsible for checking if InspSocket derived classes are timed out.
313 void DoBackgroundUserStuff(time_t TIME)
315 CullList GlobalGoners;
317 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
318 for (unsigned long count2 = 0; count2 != local_users.size(); count2++)
320 if (count2 >= local_users.size())
323 userrec* curr = local_users[count2];
328 * registration timeout -- didnt send USER/NICK/HOST
329 * in the time specified in their connection class.
331 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != REG_ALL))
333 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
334 //ZapThisDns(curr->fd);
335 GlobalGoners.AddItem(curr,"Registration timeout");
339 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
340 * say this user is ok to proceed, fully connect them.
342 if ((TIME > curr->signon) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
344 curr->dns_done = true;
345 //ZapThisDns(curr->fd);
346 ServerInstance->stats->statsDnsBad++;
347 curr->FullConnect(&GlobalGoners);
350 if ((curr->dns_done) && (curr->registered == REG_NICKUSER) && (AllModulesReportReady(curr)))
352 log(DEBUG,"dns done, registered=3, and modules ready, OK");
353 curr->FullConnect(&GlobalGoners);
354 //ZapThisDns(curr->fd);
357 // It's time to PING this user. Send them a ping.
358 if ((TIME > curr->nping) && (curr->registered == REG_ALL))
360 // This user didn't answer the last ping, remove them
363 GlobalGoners.AddItem(curr,"Ping timeout");
365 curr->nping = TIME+curr->pingmax;
368 curr->Write("PING :%s",ServerInstance->Config->ServerName);
370 curr->nping = TIME+curr->pingmax;
374 * We can flush the write buffer as the last thing we do, because if they
375 * match any of the above conditions its no use flushing their buffer anyway.
378 curr->FlushWriteBuf();
379 if (*curr->GetWriteError())
381 GlobalGoners.AddItem(curr,curr->GetWriteError());
389 /* Remove all the queued users who are due to be quit, free memory used. */
390 GlobalGoners.Apply();