]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_connflood.cpp
Allocate id properly
[user/henk/code/inspircd.git] / src / modules / m_connflood.cpp
1 /*   +------------------------------------+
2  *   | Inspire Internet Relay Chat Daemon |
3  *   +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                    E-mail:
7  *             <brain@chatspike.net>
8  *             <Craig@chatspike.net>
9  *
10  *     --- This module contributed by pippijn ---
11  * 
12  * Written by Craig Edwards, Craig McLure, and others.
13  * This program is free but copyrighted software; see
14  * the file COPYING for details.
15  *
16  * ---------------------------------------------------
17  */
18
19 using namespace std;
20
21 #include "users.h"
22 #include "modules.h"
23
24 /* $ModDesc: Connection throttle */
25
26 int conns = 0, throttled = 0;
27 extern time_t TIME;
28
29 class ModuleConnFlood : public Module
30 {
31 private:
32         int seconds, maxconns, timeout, boot_wait;
33         time_t first;
34         std::string quitmsg;
35
36         ConfigReader* conf;
37         Server *Srv;
38
39 public:
40         ModuleConnFlood(Server* Me) : Module::Module(Me)
41         {
42                 Srv = Me;
43                 InitConf();
44         }
45
46         virtual ~ModuleConnFlood()
47         {
48         }
49
50         virtual Version GetVersion()
51         {
52                 return Version(1,0,0,0,0);
53         }
54
55         void Implements(char* List)
56         {
57                 List[I_OnRehash] = List[I_OnUserRegister] = 1;
58         }
59    
60         void InitConf()
61         {
62                 /* read configuration variables */
63                 conf = new ConfigReader;
64                 /* throttle configuration */
65                 seconds = conf->ReadInteger("connflood", "seconds", 0, true);
66                 maxconns = conf->ReadInteger("connflood", "maxconns", 0, true);
67                 timeout = conf->ReadInteger("connflood", "timeout", 0, true);
68                 quitmsg = conf->ReadValue("connflood", "quitmsg", 0);
69
70                 /* seconds to wait when the server just booted */
71                 boot_wait = conf->ReadInteger("connflood", "bootwait", 0, true);
72
73                 first = TIME;
74         }
75  
76         virtual void OnUserRegister(userrec* user)
77         {
78                 time_t next = TIME;
79                 if (!first)
80                         first = next - boot_wait;
81
82                 /* time difference between first and latest connection */
83                 time_t tdiff = next - first;
84
85                 /* increase connection count */
86                 conns++;
87
88                 if (throttled == 1)
89                 {
90                         if (tdiff > seconds + timeout)
91                         {
92                                 /* expire throttle */
93                                 throttled = 0;
94                                 Srv->SendOpers("*** Connection throttle deactivated");
95                                 return;
96                         }
97                         Srv->QuitUser(user, quitmsg);
98                         return;
99                 }
100
101                 if (tdiff <= seconds)
102                 {
103                         if (conns >= maxconns)
104                         {
105                                 throttled = 1;
106                                 Srv->SendOpers("*** Connection throttle activated");
107                                 Srv->QuitUser(user, quitmsg);
108                                 return;
109                         }
110                 }
111                 else
112                 {
113                         conns = 1;
114                         first = next;
115                 }
116         }
117
118         virtual void OnRehash(const std::string &parameter)
119         {
120                 InitConf();
121         }
122
123 };
124
125
126 class ModuleConnFloodFactory : public ModuleFactory
127 {
128 public:
129         ModuleConnFloodFactory()
130         {
131         }
132
133         ~ModuleConnFloodFactory()
134         {
135         }
136     
137         virtual Module * CreateModule(Server* Me)
138         {
139                 return new ModuleConnFlood(Me);
140         }
141 };
142
143
144 extern "C" void * init_module( void )
145 {
146         return new ModuleConnFloodFactory;
147 }