]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/inetaccess/antiflood.cpp
Добавление исходников
[stg.git] / projects / stargazer / plugins / authorization / inetaccess / antiflood.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@ua.fm>
23  */
24
25
26 #include <string.h>
27
28 #include "antiflood.h"
29 #include "common.h"
30
31 //-----------------------------------------------------------------------------
32 int FloodIPCompare(void * p1, void * p2)
33 {
34 /*
35 óÒÁ×ÎÉ×ÁÅÍ Ä×Á éð × ÓÔÒÕËÔÒÕ Ó ÁÎÔÉÆÌÕÄÏÍ
36  * */
37
38 FLOOD_NODE * n1, *n2;
39
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;
44 }
45 //-----------------------------------------------------------------------------
46 ANTIFLOOD::ANTIFLOOD():
47 floodTree(FloodIPCompare)
48 {
49 /*
50 óÒÅÄÎÅÅ ÎÁÉÍÅÎØÛÅÅ ÒÁÚÒÅÛÅÎÎÏÅ ×ÒÅÍÑ ÍÅÖÄÕ ÐÁËÅÔÁÍÉ. ÍÓ.
51  * */
52
53 avrgTime = 500;
54 }
55 //-----------------------------------------------------------------------------
56 bool ANTIFLOOD::AllowIP(uint32_t ip, bool * logged)
57 {
58 /*
59 ðÒÏ×ÅÒËÁ ÔÏÇÏ ÎÅ ÓÌÉÛËÏÍ ÌÉ ÞÁÓÔÏ ÐÒÉÈÏÄÑÔ ÐÁËÅÔÙ Ó ÕËÁÚÁÎÎÏÇÏ ÁÊÐÉÛÎÉËÁ
60  * */
61
62 BSPNODE * findNode;
63 FLOOD_NODE floodNode;
64
65 floodNode.ip = ip;
66 findNode = floodTree.Find(&floodNode);
67
68 gettimeofday(&tv, NULL);
69 currentTime = ((uint64_t)tv.tv_sec)*1000 + ((uint64_t)tv.tv_usec)/1000;
70
71 if (findNode == NULL)
72     {
73     AddNode(ip);
74     printfd(__FILE__, "AddNode(%X)\n", ip);
75     return true;
76     }
77 else
78     {
79     printfd(__FILE__, "UpdateNodeTime(%X)\n", findNode->record);
80     UpdateNodeTime((FLOOD_NODE*)(findNode->record));
81     }
82
83 if (currentTime - CalcAvrgNodeTime((FLOOD_NODE*)findNode->record) < avrgTime)
84     {
85     if (((FLOOD_NODE*)findNode->record)->logged == false)
86         {
87         *logged = false;
88         ((FLOOD_NODE*)findNode->record)->logged = true;
89         //floodNode.logged = true;
90         }
91     else
92         {
93         *logged = true;
94         }
95     return false;
96     }
97
98 ((FLOOD_NODE*)findNode->record)->logged = false;
99 return true;
100 }
101 //-----------------------------------------------------------------------------
102 void ANTIFLOOD::UpdateNodeTime(FLOOD_NODE * node)
103 {
104 /*
105 ïÂÎÏ×ÌÑÅÍ ×ÒÅÍÑ ÐÏÓÌÅÄÎÅÇÏ ÐÒÅÛÅÄÛÅÇÏ ÐÁËÅÔÁ
106  * */
107
108 node->timeIP[node->pos] = currentTime;
109
110 node->pos++;
111 if (node->pos >= FLOOD_LBL_MAX)
112     node->pos = 0;
113
114 }
115 //-----------------------------------------------------------------------------
116 void ANTIFLOOD::Clean()
117 {
118 /*
119 þÍÓÔËÁ ÄÅÒÅ×Á ÓÏ ÓÌÉÛËÏÍ ÓÔÁÒÙÍÉ ÄÁÎÎÙÍÉ
120  * */
121
122 BSPNODE * bspNode = floodTree.Max(floodTree.GetRoot());
123 FLOOD_NODE * n;
124 BSPNODE * delNode;
125 currentTime = ((uint64_t)tv.tv_sec)*1000 + ((uint64_t)tv.tv_usec)/1000;
126
127 while (bspNode)
128     {
129     n = (FLOOD_NODE*)bspNode->record;
130     if (currentTime - CalcAvrgNodeTime(n) > 2 * 60)
131         {
132         delNode = floodTree.Delete(bspNode);
133         delete (FLOOD_NODE*)delNode->record;
134         delete delNode;
135         }
136     bspNode = floodTree.Max(floodTree.GetRoot());
137     }
138
139 printfd(__FILE__, "after clean max=%X\n", floodTree.Max(floodTree.GetRoot()));
140 printfd(__FILE__, "after clean min=%X\n", floodTree.Min(floodTree.GetRoot()));
141
142 }
143 //-----------------------------------------------------------------------------
144 void ANTIFLOOD::AddNode(uint32_t ip)
145 {
146 /*
147 äÏÂÁ×ÌÑÅÍ ÎÏ×ÙÊ éð × ÄÅÒÅ×Ï
148  * */
149
150 FLOOD_NODE * fn;
151 BSPNODE * node;
152
153 fn = new FLOOD_NODE;
154
155 fn->pos = 0;
156 fn->ip = ip;
157 fn->logged = false;
158
159 memset(fn->timeIP, 0, sizeof(uint64_t) * FLOOD_LBL_MAX);
160 fn->timeIP[0] = currentTime;
161
162 node = new BSPNODE;
163 node->record = fn;
164
165 floodTree.Add(node);
166 }
167 //-----------------------------------------------------------------------------
168 uint64_t ANTIFLOOD::CalcAvrgNodeTime(FLOOD_NODE * fn)
169 {
170 /*
171 ÷ÙÞÉÓÌÑÅÍ ÓÒÅÄÎÅÅ ×ÒÅÍÑ ÐÏÓÌÅÄÎÉÈ ÐÒÅÛÅÄÛÉÈ ÐÁËÅÔÏ×
172  * */
173
174 uint64_t t = 0;
175 for (int i = 0; i < FLOOD_LBL_MAX; i++)
176     t += fn->timeIP[i];
177
178 printfd(__FILE__, "node time %lld\n", t/FLOOD_LBL_MAX);
179 printfd(__FILE__, "current time %lld\n", currentTime);
180
181 return t/FLOOD_LBL_MAX;
182 }
183 //-----------------------------------------------------------------------------
184 void ANTIFLOOD::SetAvrgTime(uint64_t t)
185 {
186 avrgTime = t;
187 }
188 //-----------------------------------------------------------------------------
189
190