The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

m_connflood.cpp

Go to the documentation of this file.
00001 /*       +------------------------------------+
00002  *       | Inspire Internet Relay Chat Daemon |
00003  *       +------------------------------------+
00004  *
00005  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
00006  * See: http://www.inspircd.org/wiki/index.php/Credits
00007  *
00008  * This program is free but copyrighted software; see
00009  *            the file COPYING for details.
00010  *
00011  * ---------------------------------------------------
00012  */
00013 
00014 #include "inspircd.h"
00015 
00016 /* $ModDesc: Connection throttle */
00017 
00018 int conns = 0, throttled = 0;
00019 
00020 class ModuleConnFlood : public Module
00021 {
00022 private:
00023         int seconds, maxconns, timeout, boot_wait;
00024         time_t first;
00025         std::string quitmsg;
00026 
00027         ConfigReader* conf;
00028 
00029 
00030 public:
00031         ModuleConnFlood(InspIRCd* Me) : Module(Me)
00032         {
00033 
00034                 InitConf();
00035                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
00036                 ServerInstance->Modules->Attach(eventlist, this, 2);
00037         }
00038 
00039         virtual ~ModuleConnFlood()
00040         {
00041         }
00042 
00043         virtual Version GetVersion()
00044         {
00045                 return Version("$Id: m_connflood.cpp 10291 2008-08-25 20:35:51Z w00t $", VF_VENDOR,API_VERSION);
00046         }
00047 
00048         void InitConf()
00049         {
00050                 /* read configuration variables */
00051                 conf = new ConfigReader(ServerInstance);
00052                 /* throttle configuration */
00053                 seconds = conf->ReadInteger("connflood", "seconds", 0, true);
00054                 maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);
00055                 timeout = conf->ReadInteger("connflood", "timeout", 0, true);
00056                 quitmsg = conf->ReadValue("connflood", "quitmsg", 0);
00057 
00058                 /* seconds to wait when the server just booted */
00059                 boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);
00060 
00061                 first = ServerInstance->Time();
00062         }
00063 
00064         virtual int OnUserRegister(User* user)
00065         {
00066                 time_t next = ServerInstance->Time();
00067 
00068                 if ((ServerInstance->startup_time + boot_wait) > next)
00069                         return 0;
00070 
00071                 /* time difference between first and latest connection */
00072                 time_t tdiff = next - first;
00073 
00074                 /* increase connection count */
00075                 conns++;
00076 
00077                 if (throttled == 1)
00078                 {
00079                         if (tdiff > seconds + timeout)
00080                         {
00081                                 /* expire throttle */
00082                                 throttled = 0;
00083                                 ServerInstance->SNO->WriteToSnoMask('A', "Connection throttle deactivated");
00084                                 return 0;
00085                         }
00086 
00087                         ServerInstance->Users->QuitUser(user, quitmsg);
00088                         return 1;
00089                 }
00090 
00091                 if (tdiff <= seconds)
00092                 {
00093                         if (conns >= maxconns)
00094                         {
00095                                 throttled = 1;
00096                                 ServerInstance->SNO->WriteToSnoMask('A', "Connection throttle activated");
00097                                 ServerInstance->Users->QuitUser(user, quitmsg);
00098                                 return 1;
00099                         }
00100                 }
00101                 else
00102                 {
00103                         conns = 1;
00104                         first = next;
00105                 }
00106                 return 0;
00107         }
00108 
00109         virtual void OnRehash(User* user, const std::string &parameter)
00110         {
00111                 InitConf();
00112         }
00113 
00114 };
00115 
00116 MODULE_INIT(ModuleConnFlood)