2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 * Author : Boris Mikhailenko <stg34@ua.fm>
28 #include "antiflood.h"
31 //-----------------------------------------------------------------------------
32 int FloodIPCompare(void * p1, void * p2)
35 óÒÁ×ÎÉ×ÁÅÍ Ä×Á éð × ÓÔÒÕËÔÒÕ Ó ÁÎÔÉÆÌÕÄÏÍ
40 n1 = (FLOOD_NODE *)p1;
41 n2 = (FLOOD_NODE *)p2;
42 printfd(__FILE__, "FloodIPCompare %X %X\n", n1->ip, n2->ip);
43 return n1->ip - n2->ip;
45 //-----------------------------------------------------------------------------
46 ANTIFLOOD::ANTIFLOOD():
47 floodTree(FloodIPCompare)
50 óÒÅÄÎÅÅ ÎÁÉÍÅÎØÛÅÅ ÒÁÚÒÅÛÅÎÎÏÅ ×ÒÅÍÑ ÍÅÖÄÕ ÐÁËÅÔÁÍÉ. ÍÓ.
55 //-----------------------------------------------------------------------------
56 bool ANTIFLOOD::AllowIP(uint32_t ip, bool * logged)
59 ðÒÏ×ÅÒËÁ ÔÏÇÏ ÎÅ ÓÌÉÛËÏÍ ÌÉ ÞÁÓÔÏ ÐÒÉÈÏÄÑÔ ÐÁËÅÔÙ Ó ÕËÁÚÁÎÎÏÇÏ ÁÊÐÉÛÎÉËÁ
66 findNode = floodTree.Find(&floodNode);
68 gettimeofday(&tv, NULL);
69 currentTime = ((uint64_t)tv.tv_sec)*1000 + ((uint64_t)tv.tv_usec)/1000;
74 printfd(__FILE__, "AddNode(%X)\n", ip);
79 printfd(__FILE__, "UpdateNodeTime(%X)\n", findNode->record);
80 UpdateNodeTime((FLOOD_NODE*)(findNode->record));
83 if (currentTime - CalcAvrgNodeTime((FLOOD_NODE*)findNode->record) < avrgTime)
85 if (((FLOOD_NODE*)findNode->record)->logged == false)
88 ((FLOOD_NODE*)findNode->record)->logged = true;
89 //floodNode.logged = true;
98 ((FLOOD_NODE*)findNode->record)->logged = false;
101 //-----------------------------------------------------------------------------
102 void ANTIFLOOD::UpdateNodeTime(FLOOD_NODE * node)
105 ïÂÎÏ×ÌÑÅÍ ×ÒÅÍÑ ÐÏÓÌÅÄÎÅÇÏ ÐÒÅÛÅÄÛÅÇÏ ÐÁËÅÔÁ
108 node->timeIP[node->pos] = currentTime;
111 if (node->pos >= FLOOD_LBL_MAX)
115 //-----------------------------------------------------------------------------
116 void ANTIFLOOD::Clean()
119 þÍÓÔËÁ ÄÅÒÅ×Á ÓÏ ÓÌÉÛËÏÍ ÓÔÁÒÙÍÉ ÄÁÎÎÙÍÉ
122 BSPNODE * bspNode = floodTree.Max(floodTree.GetRoot());
125 currentTime = ((uint64_t)tv.tv_sec)*1000 + ((uint64_t)tv.tv_usec)/1000;
129 n = (FLOOD_NODE*)bspNode->record;
130 if (currentTime - CalcAvrgNodeTime(n) > 2 * 60)
132 delNode = floodTree.Delete(bspNode);
133 delete (FLOOD_NODE*)delNode->record;
136 bspNode = floodTree.Max(floodTree.GetRoot());
139 printfd(__FILE__, "after clean max=%X\n", floodTree.Max(floodTree.GetRoot()));
140 printfd(__FILE__, "after clean min=%X\n", floodTree.Min(floodTree.GetRoot()));
143 //-----------------------------------------------------------------------------
144 void ANTIFLOOD::AddNode(uint32_t ip)
147 äÏÂÁ×ÌÑÅÍ ÎÏ×ÙÊ éð × ÄÅÒÅ×Ï
159 memset(fn->timeIP, 0, sizeof(uint64_t) * FLOOD_LBL_MAX);
160 fn->timeIP[0] = currentTime;
167 //-----------------------------------------------------------------------------
168 uint64_t ANTIFLOOD::CalcAvrgNodeTime(FLOOD_NODE * fn)
171 ÷ÙÞÉÓÌÑÅÍ ÓÒÅÄÎÅÅ ×ÒÅÍÑ ÐÏÓÌÅÄÎÉÈ ÐÒÅÛÅÄÛÉÈ ÐÁËÅÔÏ×
175 for (int i = 0; i < FLOOD_LBL_MAX; i++)
178 printfd(__FILE__, "node time %lld\n", t/FLOOD_LBL_MAX);
179 printfd(__FILE__, "current time %lld\n", currentTime);
181 return t/FLOOD_LBL_MAX;
183 //-----------------------------------------------------------------------------
184 void ANTIFLOOD::SetAvrgTime(uint64_t t)
188 //-----------------------------------------------------------------------------