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>
50 #include "inspstring.h"
52 #include "helperfuncs.h"
54 #include "socketengine.h"
55 #include "userprocess.h"
57 #include "command_parse.h"
58 #include "cull_list.h"
60 extern struct sockaddr_in client,server;
61 extern socklen_t length;
62 extern std::vector<Module*> modules;
63 extern std::vector<ircd_module*> factory;
64 extern std::vector<InspSocket*> module_sockets;
66 extern time_t OLDTIME;
67 extern std::vector<userrec*> local_users;
68 extern InspSocket* socket_ref[MAX_DESCRIPTORS];
70 extern InspIRCd* ServerInstance;
71 extern ServerConfig *Config;
72 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
78 void ProcessUser(userrec* cu)
82 if (cu->fd == FD_MAGIC_NUMBER)
85 log(DEBUG,"Processing user with fd %d",cu->fd);
87 if (Config->GetIOHook(cu->port))
94 MOD_RESULT = Config->GetIOHook(cu->port)->OnRawSocketRead(cu->fd,data,65535,result2);
95 log(DEBUG,"Data result returned by module: %d",MOD_RESULT);
97 catch (ModuleException& modexcept)
99 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
113 result = cu->ReadData(data, 65535);
116 log(DEBUG,"Read result: %d",result);
118 if ((result) && (result != -EAGAIN))
124 ServerInstance->stats->statsRecv += result;
126 * perform a check on the raw buffer as an array (not a string!) to remove
127 * character 0 which is illegal in the RFC - replace them with spaces.
128 * XXX - no garauntee there's not \0's in the middle of the data,
129 * and no reason for it to be terminated either. -- Om
132 for (int checker = 0; checker < result; checker++)
134 if (data[checker] == 0)
142 currfd = current->fd;
144 // add the data to the users buffer
147 if (!current->AddBuffer(data))
149 // AddBuffer returned false, theres too much data in the user's buffer and theyre up to no good.
150 if (current->registered == 7)
152 // Make sure they arn't flooding long lines.
153 if (TIME > current->reset_due)
155 current->reset_due = TIME + current->threshold;
156 current->lines_in = 0;
161 if (current->lines_in > current->flood)
163 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
164 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
165 kill_link(current,"Excess flood");
170 WriteServ(currfd, "NOTICE %s :Your previous line was too long and was not delivered (Over 512chars) Please shorten it.", current->nick);
176 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
177 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
178 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
179 apply_lines(APPLY_ZLINES);
185 if (current->recvq.length() > (unsigned)Config->NetBufferSize)
187 if (current->registered == 7)
189 kill_link(current,"RecvQ exceeded");
193 WriteOpers("*** Excess flood from %s",(char*)inet_ntoa(current->ip4));
194 log(DEFAULT,"Excess flood from: %s",(char*)inet_ntoa(current->ip4));
195 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
196 apply_lines(APPLY_ZLINES);
202 // while there are complete lines to process...
203 while (current->BufferIsReady())
205 if (TIME > current->reset_due)
207 current->reset_due = TIME + current->threshold;
208 current->lines_in = 0;
211 if (++current->lines_in > current->flood)
213 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
214 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
215 kill_link(current,"Excess flood");
219 if ((++floodlines > current->flood) && (current->flood != 0))
221 if (current->registered == 7)
223 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
224 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
225 kill_link(current,"Excess flood");
229 add_zline(120,Config->ServerName,"Flood from unregistered connection",(char*)inet_ntoa(current->ip4));
230 apply_lines(APPLY_ZLINES);
236 // use GetBuffer to copy single lines into the sanitized string
237 std::string single_line = current->GetBuffer();
238 current->bytes_in += single_line.length();
240 if (single_line.length() > 512)
241 single_line.resize(512);
243 userrec* old_comp = fd_ref_table[currfd];
245 ServerInstance->Parser->ProcessBuffer(single_line,current);
247 * look for the user's record in case it's changed... if theyve quit,
248 * we cant do anything more with their buffer, so bail.
249 * there used to be an ugly, slow loop here. Now we have a reference
250 * table, life is much easier (and FASTER)
252 userrec* new_comp = fd_ref_table[currfd];
253 if ((currfd < 0) || (!fd_ref_table[currfd]) || (old_comp != new_comp))
259 /* The user is still here, flush their buffer */
260 current->FlushWriteBuf();
267 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
269 log(DEBUG,"killing: %s",cu->nick);
270 kill_link(cu,strerror(errno));
275 // result EAGAIN means nothing read
276 else if ((result == EAGAIN) || (result == -EAGAIN))
280 else if (result == 0)
282 log(DEBUG,"InspIRCd: Exited: %s",cu->nick);
283 kill_link(cu,"Client exited");
284 log(DEBUG,"Bailing from client exit");
289 void DoSocketTimeouts(time_t TIME)
291 unsigned int numsockets = module_sockets.size();
292 SocketEngine* SE = ServerInstance->SE;
294 for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
296 InspSocket* s = (InspSocket*)*a;
297 if ((s) && (s->GetFd() >= 0) && (s->GetFd() < MAX_DESCRIPTORS) && (socket_ref[s->GetFd()] != NULL) && (s->Timeout(TIME)))
299 log(DEBUG,"userprocess.cpp: Socket poll returned false, close and bail");
300 socket_ref[s->GetFd()] = NULL;
301 SE->DelFd(s->GetFd());
302 module_sockets.erase(a);
308 if (module_sockets.size() != numsockets)
314 * This function is called once a second from the mainloop.
315 * It is intended to do background checking on all the user structs, e.g.
316 * stuff like ping checks, registration timeouts, etc. This function is
317 * also responsible for checking if InspSocket derived classes are timed out.
319 void DoBackgroundUserStuff(time_t TIME)
321 CullList GlobalGoners;
325 /* XXX: IT IS NOT SAFE TO USE AN ITERATOR HERE. DON'T EVEN THINK ABOUT IT. */
326 for (unsigned long count2 = 0; count2 != local_users.size(); count2++)
328 if (count2 >= local_users.size())
331 userrec* curr = local_users[count2];
337 if ((cfd > 0) && (cfd < MAX_DESCRIPTORS) && (fd_ref_table[cfd] == curr) && (curr))
340 * registration timeout -- didnt send USER/NICK/HOST
341 * in the time specified in their connection class.
343 if (((unsigned)TIME > (unsigned)curr->timeout) && (curr->registered != 7))
345 log(DEBUG,"InspIRCd: registration timeout: %s",curr->nick);
346 ZapThisDns(curr->fd);
347 GlobalGoners.AddItem(curr,"Registration timeout");
352 * user has signed on with USER/NICK/PASS, and dns has completed, all the modules
353 * say this user is ok to proceed, fully connect them.
355 if ((TIME > curr->signon) && (curr->registered == 3) && (AllModulesReportReady(curr)))
357 curr->dns_done = true;
358 ZapThisDns(curr->fd);
359 ServerInstance->stats->statsDnsBad++;
360 FullConnectUser(curr,&GlobalGoners);
364 if ((curr->dns_done) && (curr->registered == 3) && (AllModulesReportReady(curr)))
366 log(DEBUG,"dns done, registered=3, and modules ready, OK");
367 FullConnectUser(curr,&GlobalGoners);
368 ZapThisDns(curr->fd);
372 // It's time to PING this user. Send them a ping.
373 if ((TIME > curr->nping) && (curr->registered == 7))
375 // This user didn't answer the last ping, remove them
378 GlobalGoners.AddItem(curr,"Ping timeout");
380 curr->nping = TIME+curr->pingmax;
384 Write(curr->fd,"PING :%s",Config->ServerName);
386 curr->nping = TIME+curr->pingmax;
391 * We can flush the write buffer as the last thing we do, because if they
392 * match any of the above conditions its no use flushing their buffer anyway.
395 /* Check again that theyre still here (something above may have changed that) */
396 if ((cfd > 0) && (cfd < MAX_DESCRIPTORS) && (fd_ref_table[cfd] == curr) && (curr))
398 curr->FlushWriteBuf();
399 if (*curr->GetWriteError())
401 GlobalGoners.AddItem(curr,curr->GetWriteError());
408 /* Remove all the queued users who are due to be quit, free memory used. */
409 GlobalGoners.Apply();