]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
fix some unitialised vectors and tidy up a bit.
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Connection throttle */
17
18 int conns = 0, throttled = 0;
19
20 class ModuleConnFlood : public Module
21 {
22 private:
23         int seconds, maxconns, timeout, boot_wait;
24         time_t first;
25         std::string quitmsg;
26
27         ConfigReader* conf;
28         
29
30 public:
31         ModuleConnFlood(InspIRCd* Me) : Module(Me)
32         {
33                 
34                 InitConf();
35                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
36                 ServerInstance->Modules->Attach(eventlist, this, 2);
37         }
38
39         virtual ~ModuleConnFlood()
40         {
41         }
42
43         virtual Version GetVersion()
44         {
45                 return Version(1,2,0,0,VF_VENDOR,API_VERSION);
46         }
47
48    
49         void InitConf()
50         {
51                 /* read configuration variables */
52                 conf = new ConfigReader(ServerInstance);
53                 /* throttle configuration */
54                 seconds = conf->ReadInteger("connflood", "seconds", 0, true);
55                 maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);
56                 timeout = conf->ReadInteger("connflood", "timeout", 0, true);
57                 quitmsg = conf->ReadValue("connflood", "quitmsg", 0);
58
59                 /* seconds to wait when the server just booted */
60                 boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);
61
62                 first = ServerInstance->Time();
63         }
64  
65         virtual int OnUserRegister(User* user)
66         {
67                 time_t next = ServerInstance->Time();
68                 
69                 if ((ServerInstance->startup_time + boot_wait) > next)
70                         return 0;
71                 
72                 /* time difference between first and latest connection */
73                 time_t tdiff = next - first;
74
75                 /* increase connection count */
76                 conns++;
77
78                 if (throttled == 1)
79                 {
80                         if (tdiff > seconds + timeout)
81                         {
82                                 /* expire throttle */
83                                 throttled = 0;
84                                 ServerInstance->SNO->WriteToSnoMask('A', "Connection throttle deactivated");
85                                 return 0;
86                         }
87
88                         ServerInstance->Users->QuitUser(user, quitmsg);
89                         return 1;
90                 }
91
92                 if (tdiff <= seconds)
93                 {
94                         if (conns >= maxconns)
95                         {
96                                 throttled = 1;
97                                 ServerInstance->SNO->WriteToSnoMask('A', "Connection throttle activated");
98                                 ServerInstance->Users->QuitUser(user, quitmsg);
99                                 return 1;
100                         }
101                 }
102                 else
103                 {
104                         conns = 1;
105                         first = next;
106                 }
107                 return 0;
108         }
109
110         virtual void OnRehash(User* user, const std::string &parameter)
111         {
112                 InitConf();
113         }
114
115 };
116
117 MODULE_INIT(ModuleConnFlood)