]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
Move static map of extensions into ServerInstance, add const-correctness
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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()       {
32
33                 InitConf();
34                 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
35                 ServerInstance->Modules->Attach(eventlist, this, 2);
36         }
37
38         virtual ~ModuleConnFlood()
39         {
40         }
41
42         virtual Version GetVersion()
43         {
44                 return Version("Connection throttle", VF_VENDOR,API_VERSION);
45         }
46
47         void InitConf()
48         {
49                 /* read configuration variables */
50                 conf = new ConfigReader;
51                 /* throttle configuration */
52                 seconds = conf->ReadInteger("connflood", "seconds", 0, true);
53                 maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);
54                 timeout = conf->ReadInteger("connflood", "timeout", 0, true);
55                 quitmsg = conf->ReadValue("connflood", "quitmsg", 0);
56
57                 /* seconds to wait when the server just booted */
58                 boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);
59
60                 first = ServerInstance->Time();
61         }
62
63         virtual ModResult OnUserRegister(User* user)
64         {
65                 time_t next = ServerInstance->Time();
66
67                 if ((ServerInstance->startup_time + boot_wait) > next)
68                         return MOD_RES_PASSTHRU;
69
70                 /* time difference between first and latest connection */
71                 time_t tdiff = next - first;
72
73                 /* increase connection count */
74                 conns++;
75
76                 if (throttled == 1)
77                 {
78                         if (tdiff > seconds + timeout)
79                         {
80                                 /* expire throttle */
81                                 throttled = 0;
82                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle deactivated");
83                                 return MOD_RES_PASSTHRU;
84                         }
85
86                         ServerInstance->Users->QuitUser(user, quitmsg);
87                         return MOD_RES_DENY;
88                 }
89
90                 if (tdiff <= seconds)
91                 {
92                         if (conns >= maxconns)
93                         {
94                                 throttled = 1;
95                                 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle activated");
96                                 ServerInstance->Users->QuitUser(user, quitmsg);
97                                 return MOD_RES_DENY;
98                         }
99                 }
100                 else
101                 {
102                         conns = 1;
103                         first = next;
104                 }
105                 return MOD_RES_PASSTHRU;
106         }
107
108         virtual void OnRehash(User* user)
109         {
110                 InitConf();
111         }
112
113 };
114
115 MODULE_INIT(ModuleConnFlood)