2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /* $ModDesc: Throttles the connections of IP ranges who try to connect flood. */
25 class ModuleConnectBan : public Module
29 unsigned int threshold;
30 unsigned int banduration;
31 unsigned int ipv4_cidr;
32 unsigned int ipv6_cidr;
35 Implementation eventlist[] = { I_OnUserConnect, I_OnGarbageCollect, I_OnRehash };
36 ServerInstance->Modules->Attach(eventlist, this, 3);
40 virtual ~ModuleConnectBan()
44 virtual Version GetVersion()
46 return Version("Throttles the connections of IP ranges who try to connect flood.", VF_VENDOR);
49 virtual void OnRehash(User* user)
51 ConfigTag* tag = ServerInstance->Config->ConfValue("connectban");
53 ipv4_cidr = tag->getInt("ipv4cidr", 32);
57 ipv6_cidr = tag->getInt("ipv6cidr", 128);
61 threshold = tag->getInt("threshold", 10);
65 banduration = ServerInstance->Duration(tag->getString("duration", "10m"));
70 virtual void OnUserConnect(LocalUser *u)
75 switch (u->client_sa.sa.sa_family)
85 irc::sockets::cidr_mask mask(u->client_sa, range);
86 i = connects.find(mask);
88 if (i != connects.end())
92 if (i->second >= threshold)
94 // Create zline for set duration.
95 ZLine* zl = new ZLine(ServerInstance->Time(), banduration, ServerInstance->Config->ServerName, "Your IP range has been attempting to connect too many times in too short a duration. Wait a while, and you will be able to connect.", mask.str());
96 if (ServerInstance->XLines->AddLine(zl,NULL))
97 ServerInstance->XLines->ApplyLines();
101 std::string maskstr = mask.str();
102 std::string timestr = ServerInstance->TimeString(zl->expiry);
103 ServerInstance->SNO->WriteGlobalSno('x',"Module m_connectban added Z:line on *@%s to expire on %s: Connect flooding",
104 maskstr.c_str(), timestr.c_str());
105 ServerInstance->SNO->WriteGlobalSno('a', "Connect flooding from IP range %s (%d)", maskstr.c_str(), threshold);
115 virtual void OnGarbageCollect()
117 ServerInstance->Logs->Log("m_connectban",DEBUG, "Clearing map.");
122 MODULE_INIT(ModuleConnectBan)