2007-11-03 21:19:39 +00:00
|
|
|
/* +------------------------------------+
|
|
|
|
* | Inspire Internet Relay Chat Daemon |
|
|
|
|
* +------------------------------------+
|
|
|
|
*
|
2010-01-11 03:07:32 +00:00
|
|
|
* InspIRCd: (C) 2002-2010 InspIRCd Development Team
|
2009-03-15 12:42:35 +00:00
|
|
|
* See: http://wiki.inspircd.org/Credits
|
2007-11-03 21:19:39 +00:00
|
|
|
*
|
|
|
|
* This program is free but copyrighted software; see
|
|
|
|
* the file COPYING for details.
|
|
|
|
*
|
|
|
|
* ---------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
2012-04-14 18:03:25 -07:00
|
|
|
#ifndef BANCACHE_H
|
|
|
|
#define BANCACHE_H
|
2007-11-03 21:19:39 +00:00
|
|
|
|
2008-09-20 20:40:29 +00:00
|
|
|
/** Stores a cached ban entry.
|
|
|
|
* Each ban has one of these hashed in a hash_map to make for faster removal
|
|
|
|
* of already-banned users in the case that they try to reconnect. As no wildcard
|
|
|
|
* matching is done on these IPs, the speed of the system is improved. These cache
|
|
|
|
* entries expire every few hours, which is a reasonable expiry for any reasonable
|
|
|
|
* sized network.
|
|
|
|
*/
|
2009-10-18 16:01:33 +00:00
|
|
|
class CoreExport BanCacheHit
|
2007-11-03 22:53:51 +00:00
|
|
|
{
|
|
|
|
public:
|
2008-09-20 20:40:29 +00:00
|
|
|
/** Type of cached ban
|
|
|
|
*/
|
2007-11-04 00:44:28 +00:00
|
|
|
std::string Type;
|
2008-09-20 20:40:29 +00:00
|
|
|
/** Reason, shown as quit message
|
|
|
|
*/
|
2007-11-04 00:44:28 +00:00
|
|
|
std::string Reason;
|
2008-09-20 20:40:29 +00:00
|
|
|
/** IP to match against, no wildcards here (of course)
|
|
|
|
*/
|
2007-11-04 00:44:28 +00:00
|
|
|
std::string IP;
|
2008-09-20 20:40:29 +00:00
|
|
|
/** Time that the ban expires at
|
|
|
|
*/
|
2008-01-06 01:15:58 +00:00
|
|
|
time_t Expiry;
|
2007-11-04 00:44:28 +00:00
|
|
|
|
2009-09-26 14:13:13 +00:00
|
|
|
BanCacheHit(const std::string &ip, const std::string &type, const std::string &reason)
|
2007-11-04 00:44:28 +00:00
|
|
|
{
|
|
|
|
this->Type = type;
|
|
|
|
this->Reason = reason;
|
|
|
|
this->IP = ip;
|
2008-11-01 23:02:23 +00:00
|
|
|
this->Expiry = ServerInstance->Time() + 86400; // a day. this might seem long, but entries will be removed as glines/etc expire.
|
2007-11-04 00:44:28 +00:00
|
|
|
}
|
2008-01-06 01:50:34 +00:00
|
|
|
|
|
|
|
// overridden to allow custom time
|
2009-09-26 14:13:13 +00:00
|
|
|
BanCacheHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
|
2008-01-06 01:50:34 +00:00
|
|
|
{
|
|
|
|
this->Type = type;
|
|
|
|
this->Reason = reason;
|
|
|
|
this->IP = ip;
|
2008-11-01 23:02:23 +00:00
|
|
|
this->Expiry = ServerInstance->Time() + seconds;
|
2008-01-06 01:50:34 +00:00
|
|
|
}
|
2007-11-03 22:56:40 +00:00
|
|
|
};
|
2007-11-03 22:53:51 +00:00
|
|
|
|
2008-09-20 20:40:29 +00:00
|
|
|
/* A container of ban cache items.
|
|
|
|
* must be defined after class BanCacheHit.
|
|
|
|
*/
|
2009-08-15 11:32:49 +00:00
|
|
|
#if defined(WINDOWS) && !defined(HASHMAP_DEPRECATED)
|
2008-02-05 02:41:22 +00:00
|
|
|
typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash_compare<std::string, std::less<std::string> > > BanCacheHash;
|
2009-08-15 11:32:49 +00:00
|
|
|
#else
|
|
|
|
typedef nspace::hash_map<std::string, BanCacheHit*, nspace::hash<std::string> > BanCacheHash;
|
2007-11-04 11:04:03 +00:00
|
|
|
#endif
|
2007-11-03 22:53:51 +00:00
|
|
|
|
2008-09-20 20:40:29 +00:00
|
|
|
/** A manager for ban cache, which allocates and deallocates and checks cached bans.
|
|
|
|
*/
|
2009-10-18 16:01:33 +00:00
|
|
|
class CoreExport BanCacheManager
|
2007-11-03 22:53:51 +00:00
|
|
|
{
|
|
|
|
private:
|
2008-09-20 20:40:29 +00:00
|
|
|
BanCacheHash* BanHash;
|
2007-11-03 22:53:51 +00:00
|
|
|
public:
|
2007-11-04 00:44:28 +00:00
|
|
|
|
|
|
|
/** Creates and adds a Ban Cache item.
|
|
|
|
* @param ip The IP the item is for.
|
|
|
|
* @param type The type of ban cache item. std::string. .empty() means it's a negative match (user is allowed freely).
|
|
|
|
* @param reason The reason for the ban. Left .empty() if it's a negative match.
|
|
|
|
*/
|
|
|
|
BanCacheHit *AddHit(const std::string &ip, const std::string &type, const std::string &reason);
|
2008-01-16 21:43:03 +00:00
|
|
|
|
|
|
|
// Overridden to allow an optional number of seconds before expiry
|
|
|
|
BanCacheHit *AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds);
|
2007-11-03 22:53:51 +00:00
|
|
|
BanCacheHit *GetHit(const std::string &ip);
|
2007-11-04 00:44:28 +00:00
|
|
|
bool RemoveHit(BanCacheHit *b);
|
2007-11-03 22:53:51 +00:00
|
|
|
|
2007-11-15 15:59:05 +00:00
|
|
|
/** Removes all entries of a given type, either positive or negative. Returns the number of hits removed.
|
|
|
|
* @param type The type of bancache entries to remove (e.g. 'G')
|
|
|
|
* @param positive Remove either positive (true) or negative (false) hits.
|
|
|
|
*/
|
2008-01-06 01:15:58 +00:00
|
|
|
unsigned int RemoveEntries(const std::string &type, bool positive);
|
2007-11-15 15:59:05 +00:00
|
|
|
|
2009-09-26 14:13:13 +00:00
|
|
|
BanCacheManager()
|
2007-11-03 22:53:51 +00:00
|
|
|
{
|
|
|
|
this->BanHash = new BanCacheHash();
|
|
|
|
}
|
2009-10-19 04:09:38 +00:00
|
|
|
~BanCacheManager();
|
2007-11-05 19:52:16 +00:00
|
|
|
void RehashCache();
|
2007-11-03 22:56:40 +00:00
|
|
|
};
|
2007-11-03 22:53:51 +00:00
|
|
|
|
2007-11-03 21:19:39 +00:00
|
|
|
#endif
|