-
Notifications
You must be signed in to change notification settings - Fork 941
Expand file tree
/
Copy pathnetban.cpp
More file actions
463 lines (407 loc) · 10.8 KB
/
netban.cpp
File metadata and controls
463 lines (407 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
#include <math.h>
#include "netban.h"
bool CNetBan::StrAllnum(const char *pStr)
{
while(*pStr)
{
if(!(*pStr >= '0' && *pStr <= '9'))
return false;
pStr++;
}
return true;
}
CNetBan::CNetHash::CNetHash(const NETADDR *pAddr)
{
if(pAddr->type==NETTYPE_IPV4)
m_Hash = (pAddr->ip[0]+pAddr->ip[1]+pAddr->ip[2]+pAddr->ip[3])&0xFF;
else
m_Hash = (pAddr->ip[0]+pAddr->ip[1]+pAddr->ip[2]+pAddr->ip[3]+pAddr->ip[4]+pAddr->ip[5]+pAddr->ip[6]+pAddr->ip[7]+
pAddr->ip[8]+pAddr->ip[9]+pAddr->ip[10]+pAddr->ip[11]+pAddr->ip[12]+pAddr->ip[13]+pAddr->ip[14]+pAddr->ip[15])&0xFF;
m_HashIndex = 0;
}
CNetBan::CNetHash::CNetHash(const CNetRange *pRange)
{
m_Hash = 0;
m_HashIndex = 0;
for(int i = 0; pRange->m_LB.ip[i] == pRange->m_UB.ip[i]; ++i)
{
m_Hash += pRange->m_LB.ip[i];
++m_HashIndex;
}
m_Hash &= 0xFF;
}
int CNetBan::CNetHash::MakeHashArray(const NETADDR *pAddr, CNetHash aHash[17])
{
int Length = pAddr->type==NETTYPE_IPV4 ? 4 : 16;
aHash[0].m_Hash = 0;
aHash[0].m_HashIndex = 0;
for(int i = 1, Sum = 0; i <= Length; ++i)
{
Sum += pAddr->ip[i-1];
aHash[i].m_Hash = Sum&0xFF;
aHash[i].m_HashIndex = i%Length;
}
return Length;
}
template<class T, int HashCount>
typename CNetBan::CBan<T> *CNetBan::CBanPool<T, HashCount>::Add(const T *pData, const CBanInfo *pInfo, const CNetHash *pNetHash)
{
if(!m_pFirstFree)
return 0;
// create new ban
CBan<T> *pBan = m_pFirstFree;
pBan->m_Data = *pData;
pBan->m_Info = *pInfo;
pBan->m_NetHash = *pNetHash;
if(pBan->m_pNext)
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
if(pBan->m_pPrev)
pBan->m_pPrev->m_pNext = pBan->m_pNext;
else
m_pFirstFree = pBan->m_pNext;
// add it to the hash list
if(m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash])
m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]->m_pHashPrev = pBan;
pBan->m_pHashPrev = 0;
pBan->m_pHashNext = m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash];
m_paaHashList[pNetHash->m_HashIndex][pNetHash->m_Hash] = pBan;
// insert it into the used list
if(m_pFirstUsed)
{
for(CBan<T> *p = m_pFirstUsed; ; p = p->m_pNext)
{
if(p->m_Info.m_Expires == CBanInfo::EXPIRES_NEVER || (pInfo->m_Expires != CBanInfo::EXPIRES_NEVER && pInfo->m_Expires <= p->m_Info.m_Expires))
{
// insert before
pBan->m_pNext = p;
pBan->m_pPrev = p->m_pPrev;
if(p->m_pPrev)
p->m_pPrev->m_pNext = pBan;
else
m_pFirstUsed = pBan;
p->m_pPrev = pBan;
break;
}
if(!p->m_pNext)
{
// last entry
p->m_pNext = pBan;
pBan->m_pPrev = p;
pBan->m_pNext = 0;
break;
}
}
}
else
{
m_pFirstUsed = pBan;
pBan->m_pNext = pBan->m_pPrev = 0;
}
// update ban count
++m_CountUsed;
return pBan;
}
template<class T, int HashCount>
int CNetBan::CBanPool<T, HashCount>::Remove(CBan<T> *pBan)
{
if(pBan == 0)
return -1;
// remove from hash list
if(pBan->m_pHashNext)
pBan->m_pHashNext->m_pHashPrev = pBan->m_pHashPrev;
if(pBan->m_pHashPrev)
pBan->m_pHashPrev->m_pHashNext = pBan->m_pHashNext;
else
m_paaHashList[pBan->m_NetHash.m_HashIndex][pBan->m_NetHash.m_Hash] = pBan->m_pHashNext;
pBan->m_pHashNext = pBan->m_pHashPrev = 0;
// remove from used list
if(pBan->m_pNext)
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
if(pBan->m_pPrev)
pBan->m_pPrev->m_pNext = pBan->m_pNext;
else
m_pFirstUsed = pBan->m_pNext;
// add to recycle list
if(m_pFirstFree)
m_pFirstFree->m_pPrev = pBan;
pBan->m_pPrev = 0;
pBan->m_pNext = m_pFirstFree;
m_pFirstFree = pBan;
// update ban count
--m_CountUsed;
return 0;
}
template<class T, int HashCount>
void CNetBan::CBanPool<T, HashCount>::Update(CBan<CDataType> *pBan, const CBanInfo *pInfo)
{
pBan->m_Info = *pInfo;
// remove from used list
if(pBan->m_pNext)
pBan->m_pNext->m_pPrev = pBan->m_pPrev;
if(pBan->m_pPrev)
pBan->m_pPrev->m_pNext = pBan->m_pNext;
else
m_pFirstUsed = pBan->m_pNext;
// insert it into the used list
if(m_pFirstUsed)
{
for(CBan<T> *p = m_pFirstUsed; ; p = p->m_pNext)
{
if(p->m_Info.m_Expires == CBanInfo::EXPIRES_NEVER || (pInfo->m_Expires != CBanInfo::EXPIRES_NEVER && pInfo->m_Expires <= p->m_Info.m_Expires))
{
// insert before
pBan->m_pNext = p;
pBan->m_pPrev = p->m_pPrev;
if(p->m_pPrev)
p->m_pPrev->m_pNext = pBan;
else
m_pFirstUsed = pBan;
p->m_pPrev = pBan;
break;
}
if(!p->m_pNext)
{
// last entry
p->m_pNext = pBan;
pBan->m_pPrev = p;
pBan->m_pNext = 0;
break;
}
}
}
else
{
m_pFirstUsed = pBan;
pBan->m_pNext = pBan->m_pPrev = 0;
}
}
template<class T, int HashCount>
void CNetBan::CBanPool<T, HashCount>::Reset()
{
mem_zero(m_paaHashList, sizeof(m_paaHashList));
mem_zero(m_aBans, sizeof(m_aBans));
m_pFirstUsed = 0;
m_CountUsed = 0;
for(int i = 1; i < MAX_BANS-1; ++i)
{
m_aBans[i].m_pNext = &m_aBans[i+1];
m_aBans[i].m_pPrev = &m_aBans[i-1];
}
m_aBans[0].m_pNext = &m_aBans[1];
m_aBans[MAX_BANS-1].m_pPrev = &m_aBans[MAX_BANS-2];
m_pFirstFree = &m_aBans[0];
}
template<class T, int HashCount>
typename CNetBan::CBan<T> *CNetBan::CBanPool<T, HashCount>::Get(int Index) const
{
if(Index < 0 || Index >= Num())
return 0;
for(CNetBan::CBan<T> *pBan = m_pFirstUsed; pBan; pBan = pBan->m_pNext, --Index)
{
if(Index == 0)
return pBan;
}
return 0;
}
template<class T>
void CNetBan::MakeBanInfo(const CBan<T> *pBan, char *pBuf, unsigned BuffSize, int Type) const
{
if(pBan == 0 || pBuf == 0)
{
if(BuffSize > 0)
pBuf[0] = 0;
return;
}
// build type based part
char aBuf[256];
if(Type == MSGTYPE_PLAYER)
str_copy(aBuf, "You have been banned", sizeof(aBuf));
else
{
char aTemp[256];
switch(Type)
{
case MSGTYPE_LIST:
str_format(aBuf, sizeof(aBuf), "%s banned", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp))); break;
case MSGTYPE_BANADD:
str_format(aBuf, sizeof(aBuf), "banned %s", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp))); break;
case MSGTYPE_BANREM:
str_format(aBuf, sizeof(aBuf), "unbanned %s", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp))); break;
default:
aBuf[0] = 0;
}
}
// add info part
if(pBan->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER)
{
int Mins = ((pBan->m_Info.m_Expires-time_timestamp()) + 59) / 60;
if(Mins <= 1)
str_format(pBuf, BuffSize, "%s for 1 minute (%s)", aBuf, pBan->m_Info.m_aReason);
else
str_format(pBuf, BuffSize, "%s for %d minutes (%s)", aBuf, Mins, pBan->m_Info.m_aReason);
}
else
str_format(pBuf, BuffSize, "%s for life (%s)", aBuf, pBan->m_Info.m_aReason);
}
template<class T>
int CNetBan::Ban(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason)
{
// do not ban localhost
if(NetMatch(pData, &m_LocalhostIPV4) || NetMatch(pData, &m_LocalhostIPV6))
{
dbg_msg("net_ban", "ban failed (localhost)");
return -1;
}
int Stamp = Seconds > 0 ? time_timestamp()+Seconds : CBanInfo::EXPIRES_NEVER;
// set up info
CBanInfo Info = {0};
Info.m_Expires = Stamp;
str_copy(Info.m_aReason, pReason, sizeof(Info.m_aReason));
// check if it already exists
CNetHash NetHash(pData);
CBan<typename T::CDataType> *pBan = pBanPool->Find(pData, &NetHash);
if(pBan)
{
// adjust the ban
pBanPool->Update(pBan, &Info);
char aBuf[128];
MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_LIST);
dbg_msg("net_ban", aBuf);
return 1;
}
// add ban and print result
pBan = pBanPool->Add(pData, &Info, &NetHash);
if(pBan)
{
char aBuf[128];
MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_BANADD);
dbg_msg("net_ban", aBuf);
return 0;
}
else
dbg_msg("net_ban", "ban failed (full banlist)");
return -1;
}
template<class T>
int CNetBan::Unban(T *pBanPool, const typename T::CDataType *pData)
{
CNetHash NetHash(pData);
CBan<typename T::CDataType> *pBan = pBanPool->Find(pData, &NetHash);
if(pBan)
{
char aBuf[256];
MakeBanInfo(pBan, aBuf, sizeof(aBuf), MSGTYPE_BANREM);
pBanPool->Remove(pBan);
dbg_msg("net_ban", aBuf);
return 0;
}
else
dbg_msg("net_ban", "unban failed (invalid entry)");
return -1;
}
void CNetBan::Init()
{
m_BanAddrPool.Reset();
m_BanRangePool.Reset();
net_host_lookup("localhost", &m_LocalhostIPV4, NETTYPE_IPV4);
net_host_lookup("localhost", &m_LocalhostIPV6, NETTYPE_IPV6);
}
void CNetBan::Update()
{
int Now = time_timestamp();
// remove expired bans
char aBuf[256], aNetStr[256];
while(m_BanAddrPool.First() && m_BanAddrPool.First()->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER && m_BanAddrPool.First()->m_Info.m_Expires < Now)
{
str_format(aBuf, sizeof(aBuf), "ban %s expired", NetToString(&m_BanAddrPool.First()->m_Data, aNetStr, sizeof(aNetStr)));
dbg_msg("net_ban", aBuf);
m_BanAddrPool.Remove(m_BanAddrPool.First());
}
while(m_BanRangePool.First() && m_BanRangePool.First()->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER && m_BanRangePool.First()->m_Info.m_Expires < Now)
{
str_format(aBuf, sizeof(aBuf), "ban %s expired", NetToString(&m_BanRangePool.First()->m_Data, aNetStr, sizeof(aNetStr)));
dbg_msg("net_ban", aBuf);
m_BanRangePool.Remove(m_BanRangePool.First());
}
}
int CNetBan::BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason)
{
return Ban(&m_BanAddrPool, pAddr, Seconds, pReason);
}
int CNetBan::BanRange(const CNetRange *pRange, int Seconds, const char *pReason)
{
if(pRange->IsValid())
return Ban(&m_BanRangePool, pRange, Seconds, pReason);
dbg_msg("net_ban", "ban failed (invalid range)");
return -1;
}
int CNetBan::UnbanByAddr(const NETADDR *pAddr)
{
return Unban(&m_BanAddrPool, pAddr);
}
int CNetBan::UnbanByRange(const CNetRange *pRange)
{
if(pRange->IsValid())
return Unban(&m_BanRangePool, pRange);
dbg_msg("net_ban", "ban failed (invalid range)");
return -1;
}
int CNetBan::UnbanByIndex(int Index)
{
int Result;
char aBuf[256];
CBanAddr *pBan = m_BanAddrPool.Get(Index);
if(pBan)
{
NetToString(&pBan->m_Data, aBuf, sizeof(aBuf));
Result = m_BanAddrPool.Remove(pBan);
}
else
{
CBanRange *pBan = m_BanRangePool.Get(Index-m_BanAddrPool.Num());
if(pBan)
{
NetToString(&pBan->m_Data, aBuf, sizeof(aBuf));
Result = m_BanRangePool.Remove(pBan);
}
else
{
dbg_msg("net_ban", "unban failed (invalid index)");
return -1;
}
}
char aMsg[256];
str_format(aMsg, sizeof(aMsg), "unbanned index %i (%s)", Index, aBuf);
dbg_msg("net_ban", aMsg);
return Result;
}
void CNetBan::UnbanAll()
{
m_BanAddrPool.Reset();
m_BanRangePool.Reset();
}
bool CNetBan::IsBanned(const NETADDR *pAddr, char *pBuf, unsigned BufferSize) const
{
CNetHash aHash[17];
int Length = CNetHash::MakeHashArray(pAddr, aHash);
// check ban adresses
CBanAddr *pBan = m_BanAddrPool.Find(pAddr, &aHash[Length]);
if(pBan)
{
MakeBanInfo(pBan, pBuf, BufferSize, MSGTYPE_PLAYER);
return true;
}
// check ban ranges
for(int i = Length-1; i >= 0; --i)
{
for(CBanRange *pBan = m_BanRangePool.First(&aHash[i]); pBan; pBan = pBan->m_pHashNext)
{
if(NetMatch(&pBan->m_Data, pAddr, i, Length))
{
MakeBanInfo(pBan, pBuf, BufferSize, MSGTYPE_PLAYER);
return true;
}
}
}
return false;
}