DDraceNetwork Docs
netban.h
Go to the documentation of this file.
1#ifndef ENGINE_SHARED_NETBAN_H
2#define ENGINE_SHARED_NETBAN_H
3
4#include <base/system.h>
5#include <engine/console.h>
6
7inline int NetComp(const NETADDR *pAddr1, const NETADDR *pAddr2)
8{
9 return mem_comp(pAddr1, pAddr2, pAddr1->type == NETTYPE_IPV4 ? 8 : 20);
10}
11
13{
14public:
17
18 bool IsValid() const { return m_LB.type == m_UB.type && NetComp(&m_LB, &m_UB) < 0; }
19};
20
21inline int NetComp(const CNetRange *pRange1, const CNetRange *pRange2)
22{
23 return NetComp(&pRange1->m_LB, &pRange2->m_LB) || NetComp(&pRange1->m_UB, &pRange2->m_UB);
24}
25
27{
28protected:
29 bool NetMatch(const NETADDR *pAddr1, const NETADDR *pAddr2) const
30 {
31 return NetComp(pAddr1, pAddr2) == 0;
32 }
33
34 bool NetMatch(const CNetRange *pRange, const NETADDR *pAddr, int Start, int Length) const
35 {
36 return pRange->m_LB.type == pAddr->type && (Start == 0 || mem_comp(&pRange->m_LB.ip[0], &pAddr->ip[0], Start) == 0) &&
37 mem_comp(&pRange->m_LB.ip[Start], &pAddr->ip[Start], Length - Start) <= 0 && mem_comp(&pRange->m_UB.ip[Start], &pAddr->ip[Start], Length - Start) >= 0;
38 }
39
40 bool NetMatch(const CNetRange *pRange, const NETADDR *pAddr) const
41 {
42 return NetMatch(pRange, pAddr, 0, pRange->m_LB.type == NETTYPE_IPV4 ? 4 : 16);
43 }
44
45 const char *NetToString(const NETADDR *pData, char *pBuffer, unsigned BufferSize) const
46 {
47 char aAddrStr[NETADDR_MAXSTRSIZE];
48 net_addr_str(pData, aAddrStr, sizeof(aAddrStr), false);
49 str_format(pBuffer, BufferSize, "'%s'", aAddrStr);
50 return pBuffer;
51 }
52
53 const char *NetToString(const CNetRange *pData, char *pBuffer, unsigned BufferSize) const
54 {
55 char aAddrStr1[NETADDR_MAXSTRSIZE], aAddrStr2[NETADDR_MAXSTRSIZE];
56 net_addr_str(&pData->m_LB, aAddrStr1, sizeof(aAddrStr1), false);
57 net_addr_str(&pData->m_UB, aAddrStr2, sizeof(aAddrStr2), false);
58 str_format(pBuffer, BufferSize, "'%s' - '%s'", aAddrStr1, aAddrStr2);
59 return pBuffer;
60 }
61
63 {
64 public:
65 int m_Hash;
66 int m_HashIndex; // matching parts for ranges, 0 for addr
67
68 CNetHash() = default;
69 CNetHash(const NETADDR *pAddr);
70 CNetHash(const CNetRange *pRange);
71
72 static int MakeHashArray(const NETADDR *pAddr, CNetHash aHash[17]);
73 };
74
75 struct CBanInfo
76 {
77 enum
78 {
81 };
82 int64_t m_Expires;
85 };
86
87 template<class T>
88 struct CBan
89 {
93
94 // hash list
97
98 // used or free list
101 };
102
103 template<class T, int HashCount>
105 {
106 public:
107 typedef T CDataType;
108
109 CBan<CDataType> *Add(const CDataType *pData, const CBanInfo *pInfo, const CNetHash *pNetHash);
110 int Remove(CBan<CDataType> *pBan);
111 void Update(CBan<CDataType> *pBan, const CBanInfo *pInfo);
112 void Reset();
113
114 int Num() const { return m_CountUsed; }
115 bool IsFull() const { return m_CountUsed == MAX_BANS; }
116
117 CBan<CDataType> *First() const { return m_pFirstUsed; }
118 CBan<CDataType> *First(const CNetHash *pNetHash) const { return m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]; }
119 CBan<CDataType> *Find(const CDataType *pData, const CNetHash *pNetHash) const
120 {
121 for(CBan<CDataType> *pBan = m_aapHashList[pNetHash->m_HashIndex][pNetHash->m_Hash]; pBan; pBan = pBan->m_pHashNext)
122 {
123 if(NetComp(&pBan->m_Data, pData) == 0)
124 return pBan;
125 }
126
127 return 0;
128 }
129 CBan<CDataType> *Get(int Index) const;
130
131 private:
132 enum
133 {
134 MAX_BANS = 2048,
135 };
136
142
143 void InsertUsed(CBan<CDataType> *pBan);
144 };
145
150
151 template<class T>
152 void MakeBanInfo(const CBan<T> *pBan, char *pBuf, unsigned BuffSize, int Type) const;
153 template<class T>
154 int Ban(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason, bool VerbatimReason);
155 template<class T>
156 int Unban(T *pBanPool, const typename T::CDataType *pData);
157
163
164public:
165 enum
166 {
171 };
172
173 class IConsole *Console() const { return m_pConsole; }
174 class IStorage *Storage() const { return m_pStorage; }
175
176 virtual ~CNetBan() {}
177 void Init(class IConsole *pConsole, class IStorage *pStorage);
178 void Update();
179
180 virtual int BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason, bool VerbatimReason);
181 virtual int BanRange(const CNetRange *pRange, int Seconds, const char *pReason);
182 int UnbanByAddr(const NETADDR *pAddr);
183 int UnbanByRange(const CNetRange *pRange);
184 int UnbanByIndex(int Index);
185 void UnbanAll();
186 bool IsBanned(const NETADDR *pOrigAddr, char *pBuf, unsigned BufferSize) const;
187
188 static void ConBan(class IConsole::IResult *pResult, void *pUser);
189 static void ConBanRange(class IConsole::IResult *pResult, void *pUser);
190 static void ConUnban(class IConsole::IResult *pResult, void *pUser);
191 static void ConUnbanRange(class IConsole::IResult *pResult, void *pUser);
192 static void ConUnbanAll(class IConsole::IResult *pResult, void *pUser);
193 static void ConBans(class IConsole::IResult *pResult, void *pUser);
194 static void ConBansFind(class IConsole::IResult *pResult, void *pUser);
195 static void ConBansSave(class IConsole::IResult *pResult, void *pUser);
196};
197
198template<class T>
199void CNetBan::MakeBanInfo(const CBan<T> *pBan, char *pBuf, unsigned BuffSize, int Type) const
200{
201 if(pBan == 0 || pBuf == 0)
202 {
203 if(BuffSize > 0)
204 pBuf[0] = 0;
205 return;
206 }
207
208 // build type based part
209 char aBuf[256];
210 if(Type == MSGTYPE_PLAYER)
211 str_copy(aBuf, "You have been banned");
212 else
213 {
214 char aTemp[256];
215 switch(Type)
216 {
217 case MSGTYPE_LIST:
218 str_format(aBuf, sizeof(aBuf), "%s banned", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp)));
219 break;
220 case MSGTYPE_BANADD:
221 str_format(aBuf, sizeof(aBuf), "banned %s", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp)));
222 break;
223 case MSGTYPE_BANREM:
224 str_format(aBuf, sizeof(aBuf), "unbanned %s", NetToString(&pBan->m_Data, aTemp, sizeof(aTemp)));
225 break;
226 default:
227 aBuf[0] = 0;
228 }
229 }
230
231 // add info part
233 {
234 int Mins = ((pBan->m_Info.m_Expires - time_timestamp()) + 59) / 60;
235 if(Mins <= 1)
236 str_format(pBuf, BuffSize, "%s for 1 minute (%s)", aBuf, pBan->m_Info.m_aReason);
237 else
238 str_format(pBuf, BuffSize, "%s for %d minutes (%s)", aBuf, Mins, pBan->m_Info.m_aReason);
239 }
240 else
241 str_format(pBuf, BuffSize, "%s (%s)", aBuf, pBan->m_Info.m_aReason);
242}
243
244#endif
Definition: netban.h:105
CBan< CDataType > * Add(const CDataType *pData, const CBanInfo *pInfo, const CNetHash *pNetHash)
Definition: netban.cpp:84
CBan< CDataType > * First() const
Definition: netban.h:117
CBan< CDataType > * m_pFirstFree
Definition: netban.h:139
CBan< CDataType > * First(const CNetHash *pNetHash) const
Definition: netban.h:118
T CDataType
Definition: netban.h:107
CBan< CDataType > m_aBans[MAX_BANS]
Definition: netban.h:138
void Reset()
Definition: netban.cpp:177
@ MAX_BANS
Definition: netban.h:134
CBan< CDataType > * Find(const CDataType *pData, const CNetHash *pNetHash) const
Definition: netban.h:119
int Remove(CBan< CDataType > *pBan)
Definition: netban.cpp:118
CBan< CDataType > * Get(int Index) const
Definition: netban.cpp:196
int m_CountUsed
Definition: netban.h:141
void InsertUsed(CBan< CDataType > *pBan)
Definition: netban.cpp:47
bool IsFull() const
Definition: netban.h:115
CBan< CDataType > * m_pFirstUsed
Definition: netban.h:140
void Update(CBan< CDataType > *pBan, const CBanInfo *pInfo)
Definition: netban.cpp:154
CBan< CDataType > * m_aapHashList[HashCount][256]
Definition: netban.h:137
int Num() const
Definition: netban.h:114
Definition: netban.h:63
CNetHash()=default
int m_HashIndex
Definition: netban.h:66
int m_Hash
Definition: netban.h:65
static int MakeHashArray(const NETADDR *pAddr, CNetHash aHash[17])
Definition: netban.cpp:32
Definition: netban.h:27
void UnbanAll()
Definition: netban.cpp:170
class IConsole * m_pConsole
Definition: netban.h:158
virtual int BanAddr(const NETADDR *pAddr, int Seconds, const char *pReason, bool VerbatimReason)
Definition: netban.cpp:313
CBanPool< NETADDR, 1 > CBanAddrPool
Definition: netban.h:146
bool IsBanned(const NETADDR *pOrigAddr, char *pBuf, unsigned BufferSize) const
Definition: netban.cpp:372
bool NetMatch(const CNetRange *pRange, const NETADDR *pAddr) const
Definition: netban.h:40
static void ConBan(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:409
void MakeBanInfo(const CBan< T > *pBan, char *pBuf, unsigned BuffSize, int Type) const
Definition: netban.h:199
CBanRangePool m_BanRangePool
Definition: netban.h:161
static void ConUnbanRange(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:457
bool NetMatch(const NETADDR *pAddr1, const NETADDR *pAddr2) const
Definition: netban.h:29
class IStorage * Storage() const
Definition: netban.h:174
const char * NetToString(const NETADDR *pData, char *pBuffer, unsigned BufferSize) const
Definition: netban.h:45
CBanAddrPool m_BanAddrPool
Definition: netban.h:160
CBanPool< CNetRange, 16 > CBanRangePool
Definition: netban.h:147
virtual ~CNetBan()
Definition: netban.h:176
void Update()
Definition: netban.cpp:293
static void ConUnban(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:440
bool NetMatch(const CNetRange *pRange, const NETADDR *pAddr, int Start, int Length) const
Definition: netban.h:34
CBan< CNetRange > CBanRange
Definition: netban.h:149
int UnbanByIndex(int Index)
Definition: netban.cpp:341
int Ban(T *pBanPool, const typename T::CDataType *pData, int Seconds, const char *pReason, bool VerbatimReason)
Definition: netban.cpp:211
int UnbanByRange(const CNetRange *pRange)
Definition: netban.cpp:332
static void ConBansFind(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:532
CBan< NETADDR > CBanAddr
Definition: netban.h:148
static void ConBans(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:479
int Unban(T *pBanPool, const typename T::CDataType *pData)
Definition: netban.cpp:256
NETADDR m_LocalhostIpV6
Definition: netban.h:162
static void ConBansSave(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:581
@ MSGTYPE_BANADD
Definition: netban.h:169
@ MSGTYPE_PLAYER
Definition: netban.h:167
@ MSGTYPE_LIST
Definition: netban.h:168
@ MSGTYPE_BANREM
Definition: netban.h:170
class IStorage * m_pStorage
Definition: netban.h:159
NETADDR m_LocalhostIpV4
Definition: netban.h:162
const char * NetToString(const CNetRange *pData, char *pBuffer, unsigned BufferSize) const
Definition: netban.h:53
virtual int BanRange(const CNetRange *pRange, int Seconds, const char *pReason)
Definition: netban.cpp:318
static void ConBanRange(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:424
void Init(class IConsole *pConsole, class IStorage *pStorage)
Definition: netban.cpp:273
int UnbanByAddr(const NETADDR *pAddr)
Definition: netban.cpp:327
class IConsole * Console() const
Definition: netban.h:173
static void ConUnbanAll(class IConsole::IResult *pResult, void *pUser)
Definition: netban.cpp:471
Definition: netban.h:13
NETADDR m_UB
Definition: netban.h:16
bool IsValid() const
Definition: netban.h:18
NETADDR m_LB
Definition: netban.h:15
Definition: console.h:45
Definition: console.h:18
Definition: storage.h:20
int mem_comp(const void *a, const void *b, size_t size)
Definition: system.cpp:184
bool net_addr_str(const NETADDR *addr, char *string, int max_length, int add_port)
Definition: system.cpp:1147
int str_copy(char *dst, const char *src, int dst_size)
Definition: system.cpp:2761
int64_t time_timestamp()
Definition: system.cpp:2641
int NetComp(const NETADDR *pAddr1, const NETADDR *pAddr2)
Definition: netban.h:7
Definition: netban.h:76
bool m_VerbatimReason
Definition: netban.h:84
int64_t m_Expires
Definition: netban.h:82
char m_aReason[REASON_LENGTH]
Definition: netban.h:83
@ EXPIRES_NEVER
Definition: netban.h:79
@ REASON_LENGTH
Definition: netban.h:80
Definition: netban.h:89
T m_Data
Definition: netban.h:90
CBan * m_pHashNext
Definition: netban.h:95
CBanInfo m_Info
Definition: netban.h:91
CBan * m_pNext
Definition: netban.h:99
CNetHash m_NetHash
Definition: netban.h:92
CBan * m_pPrev
Definition: netban.h:100
CBan * m_pHashPrev
Definition: netban.h:96
Definition: types.h:67
unsigned char ip[16]
Definition: types.h:69
unsigned int type
Definition: types.h:68
#define str_format
Definition: system.cpp:2830
@ NETADDR_MAXSTRSIZE
Definition: types.h:45
@ NETTYPE_IPV4
Definition: types.h:50