]> git.netwichtig.de Git - user/henk/code/inspircd.git/commitdiff
Add bancache expiry stuff, currently records expire 60 seconds after creation. Live...
authorw00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7>
Sun, 6 Jan 2008 01:15:58 +0000 (01:15 +0000)
committerw00t <w00t@e03df62e-2008-0410-955e-edbf42e46eb7>
Sun, 6 Jan 2008 01:15:58 +0000 (01:15 +0000)
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@8640 e03df62e-2008-0410-955e-edbf42e46eb7

include/bancache.h
src/bancache.cpp

index cabd8536500b8cfee470605587b9ad3110ea01e3..75d1a67b5b765e93a748c6a6de88a495e764cf21 100644 (file)
@@ -24,6 +24,7 @@ class CoreExport BanCacheHit : public classbase
        std::string Type;
        std::string Reason;
        std::string IP;
+       time_t Expiry;
 
        BanCacheHit(InspIRCd *Instance, const std::string &ip, const std::string &type, const std::string &reason)
        {
@@ -31,6 +32,7 @@ class CoreExport BanCacheHit : public classbase
                this->Type = type;
                this->Reason = reason;
                this->IP = ip;
+               this->Expiry = time(NULL) + 60; // XXX changeme
        }
 };
 
@@ -61,7 +63,7 @@ class CoreExport BanCacheManager : public classbase
         * @param type The type of bancache entries to remove (e.g. 'G')
         * @param positive Remove either positive (true) or negative (false) hits.
         */
-       int RemoveEntries(const std::string &type, bool positive);
+       unsigned int RemoveEntries(const std::string &type, bool positive);
 
        BanCacheManager(InspIRCd *Instance)
        {
index 05c014de334a1e29f1676ba195d1569dc0fe121e..24578b379dc120d86a0875b00acdb95ec7d5bd92 100644 (file)
@@ -20,7 +20,6 @@ BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &t
 {
        BanCacheHit *b;
 
-
        if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
                return NULL;
 
@@ -37,7 +36,16 @@ BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
        if (i == this->BanHash->end())
                return NULL; // free and safe
        else
+       {
+               if (time(NULL) > i->second->Expiry)
+               {
+                       ServerInstance->Log(DEBUG, "Hit on " + ip + " is out of date, removing!");
+                       RemoveHit(i->second);
+                       return NULL; // out of date
+               }
+
                return i->second; // hit.
+       }
 }
 
 bool BanCacheManager::RemoveHit(BanCacheHit *b)
@@ -63,7 +71,7 @@ bool BanCacheManager::RemoveHit(BanCacheHit *b)
        return true;
 }
 
-int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
+unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
 {
        int removed = 0;
 
@@ -84,7 +92,7 @@ int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
                                /* we need to remove this one. */
                                delete b;
                                b = NULL;
-                               BanHash->erase(n);
+                               BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
                                removed++;
                        }
                }
@@ -108,9 +116,20 @@ void BanCacheManager::RehashCache()
                safei++;
 
                /* Safe to delete items here through iterator 'n' */
+               BanCacheHit *b = n->second;
 
-               /* Actually inserts a std::pair */
-               NewHash->insert(*n);
+               if (time(NULL) > b->Expiry)
+               {
+                       /* we need to remove this one. */
+                       delete b;
+                       b = NULL;
+                       BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
+               }
+               else
+               {
+                       /* Actually inserts a std::pair */
+                       NewHash->insert(*n);
+               }
 
                /* End of safe section */