]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/bancache.cpp
BanCache: Do one hash lookup in BanCacheManager::AddHit()
[user/henk/code/inspircd.git] / src / bancache.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2007-2008 Robin Burchell <robin+git@viroteck.net>
5  *   Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
6  *
7  * This file is part of InspIRCd.  InspIRCd is free software: you can
8  * redistribute it and/or modify it under the terms of the GNU General Public
9  * License as published by the Free Software Foundation, version 2.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20
21 /* $Core */
22
23 #include "inspircd.h"
24 #include "bancache.h"
25
26 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
27 {
28         BanCacheHit*& b = (*BanHash)[ip];
29         if (b != NULL) // can't have two cache entries on the same IP, sorry..
30                 return NULL;
31
32         b = new BanCacheHit(type, reason, (seconds ? seconds : 86400));
33         return b;
34 }
35
36 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
37 {
38         BanCacheHash::iterator i = this->BanHash->find(ip);
39
40         if (i == this->BanHash->end())
41                 return NULL; // free and safe
42         else
43         {
44                 if (ServerInstance->Time() > i->second->Expiry)
45                 {
46                         ServerInstance->Logs->Log("BANCACHE", DEBUG, "Hit on " + ip + " is out of date, removing!");
47                         RemoveHit(i->second);
48                         return NULL; // out of date
49                 }
50
51                 return i->second; // hit.
52         }
53 }
54
55 bool BanCacheManager::RemoveHit(BanCacheHit *b)
56 {
57         BanCacheHash::iterator i;
58
59         if (!b)
60                 return false; // I don't think so.
61
62         i = this->BanHash->find(b->IP);
63
64         if (i == this->BanHash->end())
65         {
66                 // err..
67                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
68         }
69         else
70         {
71                 this->BanHash->erase(i);
72         }
73
74         delete b;
75         return true;
76 }
77
78 unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
79 {
80         int removed = 0;
81
82         BanCacheHash::iterator safei;
83
84         if (positive)
85                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
86         else
87                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type);
88
89         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
90         {
91                 safei = n;
92                 safei++;
93
94                 BanCacheHit *b = n->second;
95
96                 /* Safe to delete items here through iterator 'n' */
97                 if (b->Type == type || !positive) // if removing negative hits, ignore type..
98                 {
99                         if ((positive && !b->Reason.empty()) || b->Reason.empty())
100                         {
101                                 /* we need to remove this one. */
102                                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + b->IP);
103                                 delete b;
104                                 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
105                                 removed++;
106                         }
107                 }
108
109                 /* End of safe section */
110                 n = safei;
111         }
112
113
114         return removed;
115 }
116
117 void BanCacheManager::RehashCache()
118 {
119         BanCacheHash* NewHash = new BanCacheHash();
120
121         BanCacheHash::iterator safei;
122         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
123         {
124                 safei = n;
125                 safei++;
126
127                 /* Safe to delete items here through iterator 'n' */
128                 BanCacheHit *b = n->second;
129
130                 if (ServerInstance->Time() > b->Expiry)
131                 {
132                         /* we need to remove this one. */
133                         delete b;
134                         BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
135                 }
136                 else
137                 {
138                         /* Actually inserts a std::pair */
139                         NewHash->insert(*n);
140                 }
141
142                 /* End of safe section */
143
144                 n = safei;
145         }
146
147         delete BanHash;
148         BanHash = NewHash;
149 }
150
151 BanCacheManager::~BanCacheManager()
152 {
153         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); ++n)
154                 delete n->second;
155         delete BanHash;
156 }