2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
5 * Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2006 Craig Edwards <craigedwards@brainbox.cc>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 /* $ModDesc: Connection throttle */
26 class ModuleConnFlood : public Module
29 int seconds, timeout, boot_wait;
31 unsigned int maxconns;
38 : conns(0), throttled(false)
45 Implementation eventlist[] = { I_OnRehash, I_OnUserRegister };
46 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
49 virtual Version GetVersion()
51 return Version("Connection throttle", VF_VENDOR);
56 /* read configuration variables */
57 ConfigTag* tag = ServerInstance->Config->ConfValue("connflood");
58 /* throttle configuration */
59 seconds = tag->getInt("seconds");
60 maxconns = tag->getInt("maxconns");
61 timeout = tag->getInt("timeout");
62 quitmsg = tag->getString("quitmsg");
64 /* seconds to wait when the server just booted */
65 boot_wait = tag->getInt("bootwait");
67 first = ServerInstance->Time();
70 virtual ModResult OnUserRegister(LocalUser* user)
73 return MOD_RES_PASSTHRU;
75 time_t next = ServerInstance->Time();
77 if ((ServerInstance->startup_time + boot_wait) > next)
78 return MOD_RES_PASSTHRU;
80 /* time difference between first and latest connection */
81 time_t tdiff = next - first;
83 /* increase connection count */
88 if (tdiff > seconds + timeout)
92 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle deactivated");
93 return MOD_RES_PASSTHRU;
96 ServerInstance->Users->QuitUser(user, quitmsg);
100 if (tdiff <= seconds)
102 if (conns >= maxconns)
105 ServerInstance->SNO->WriteGlobalSno('a', "Connection throttle activated");
106 ServerInstance->Users->QuitUser(user, quitmsg);
115 return MOD_RES_PASSTHRU;
118 virtual void OnRehash(User* user)
125 MODULE_INIT(ModuleConnFlood)