]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/bancache.cpp
Fix mistakenly using Clang instead of GCC on older FreeBSD versions.
[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)
27 {
28         BanCacheHit *b;
29
30         if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
31                 return NULL;
32
33         b = new BanCacheHit(ip, type, reason);
34
35         this->BanHash->insert(std::make_pair(ip, b));
36         return b;
37 }
38
39 BanCacheHit *BanCacheManager::AddHit(const std::string &ip, const std::string &type, const std::string &reason, time_t seconds)
40 {
41         BanCacheHit *b;
42
43         if (this->BanHash->find(ip) != this->BanHash->end()) // can't have two cache entries on the same IP, sorry..
44                 return NULL;
45
46         b = new BanCacheHit(ip, type, reason, seconds);
47
48         this->BanHash->insert(std::make_pair(ip, b));
49         return b;
50 }
51
52 BanCacheHit *BanCacheManager::GetHit(const std::string &ip)
53 {
54         BanCacheHash::iterator i = this->BanHash->find(ip);
55
56         if (i == this->BanHash->end())
57                 return NULL; // free and safe
58         else
59         {
60                 if (ServerInstance->Time() > i->second->Expiry)
61                 {
62                         ServerInstance->Logs->Log("BANCACHE", DEBUG, "Hit on " + ip + " is out of date, removing!");
63                         RemoveHit(i->second);
64                         return NULL; // out of date
65                 }
66
67                 return i->second; // hit.
68         }
69 }
70
71 bool BanCacheManager::RemoveHit(BanCacheHit *b)
72 {
73         BanCacheHash::iterator i;
74
75         if (!b)
76                 return false; // I don't think so.
77
78         i = this->BanHash->find(b->IP);
79
80         if (i == this->BanHash->end())
81         {
82                 // err..
83                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveHit(): I got asked to remove a hit that wasn't in the hash(?)");
84         }
85         else
86         {
87                 this->BanHash->erase(i);
88         }
89
90         delete b;
91         return true;
92 }
93
94 unsigned int BanCacheManager::RemoveEntries(const std::string &type, bool positive)
95 {
96         int removed = 0;
97
98         BanCacheHash::iterator safei;
99
100         if (positive)
101                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing positive hits for " + type);
102         else
103                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing negative hits for " + type);
104
105         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
106         {
107                 safei = n;
108                 safei++;
109
110                 BanCacheHit *b = n->second;
111
112                 /* Safe to delete items here through iterator 'n' */
113                 if (b->Type == type || !positive) // if removing negative hits, ignore type..
114                 {
115                         if ((positive && !b->Reason.empty()) || b->Reason.empty())
116                         {
117                                 /* we need to remove this one. */
118                                 ServerInstance->Logs->Log("BANCACHE", DEBUG, "BanCacheManager::RemoveEntries(): Removing a hit on " + b->IP);
119                                 delete b;
120                                 BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
121                                 removed++;
122                         }
123                 }
124
125                 /* End of safe section */
126                 n = safei;
127         }
128
129
130         return removed;
131 }
132
133 void BanCacheManager::RehashCache()
134 {
135         BanCacheHash* NewHash = new BanCacheHash();
136
137         BanCacheHash::iterator safei;
138         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); )
139         {
140                 safei = n;
141                 safei++;
142
143                 /* Safe to delete items here through iterator 'n' */
144                 BanCacheHit *b = n->second;
145
146                 if (ServerInstance->Time() > b->Expiry)
147                 {
148                         /* we need to remove this one. */
149                         delete b;
150                         BanHash->erase(n); // WORD TO THE WISE: don't use RemoveHit here, because we MUST remove the iterator in a safe way.
151                 }
152                 else
153                 {
154                         /* Actually inserts a std::pair */
155                         NewHash->insert(*n);
156                 }
157
158                 /* End of safe section */
159
160                 n = safei;
161         }
162
163         delete BanHash;
164         BanHash = NewHash;
165 }
166
167 BanCacheManager::~BanCacheManager()
168 {
169         for (BanCacheHash::iterator n = BanHash->begin(); n != BanHash->end(); ++n)
170                 delete n->second;
171         delete BanHash;
172 }