DDraceNetwork Documentation
Loading...
Searching...
No Matches
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 {
48 net_addr_str(pData, aAddrStr, sizeof(aAddrStr), false);
50 return pBuffer;
51 }
52
53 const char *NetToString(const CNetRange *pData, char *pBuffer, unsigned BufferSize) const
54 {
56 net_addr_str(&pData->m_LB, aAddrStr1, sizeof(aAddrStr1), false);
57 net_addr_str(&pData->m_UB, aAddrStr2, sizeof(aAddrStr2), false);
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);
71
72 static int MakeHashArray(const NETADDR *pAddr, CNetHash aHash[17]);
73 };
74
75 struct CBanInfo
76 {
77 enum
78 {
81 };
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
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]; }
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 nullptr;
128 }
129 CBan<CDataType> *Get(int Index) const;
130
131 private:
132 enum
133 {
134 MAX_BANS = 2048,
135 };
136
142
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() = default;
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 == nullptr || pBuf == nullptr)
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
232 if(!pBan->m_Info.m_VerbatimReason && pBan->m_Info.m_Expires != CBanInfo::EXPIRES_NEVER)
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
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
@ MAX_BANS
Definition netban.h:134
void InsertUsed(CBan< CDataType > *pBan)
Definition netban.cpp:47
bool IsFull() const
Definition netban.h:115
CBan< CDataType > * m_pFirstUsed
Definition netban.h:140
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:415
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:463
bool NetMatch(const NETADDR *pAddr1, const NETADDR *pAddr2) const
Definition netban.h:29
virtual ~CNetBan()=default
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
void Update()
Definition netban.cpp:293
static void ConUnban(class IConsole::IResult *pResult, void *pUser)
Definition netban.cpp:446
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
@ MSGTYPE_BANADD
Definition netban.h:169
@ MSGTYPE_PLAYER
Definition netban.h:167
@ MSGTYPE_LIST
Definition netban.h:168
@ MSGTYPE_BANREM
Definition netban.h:170
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:543
CBan< NETADDR > CBanAddr
Definition netban.h:148
static void ConBans(class IConsole::IResult *pResult, void *pUser)
Definition netban.cpp:485
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:592
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:430
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:477
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:47
Definition console.h:18
Definition storage.h:21
Definition vmath.h:15
int mem_comp(const void *a, const void *b, size_t size)
Definition system.cpp:190
void net_addr_str(const NETADDR *addr, char *string, int max_length, bool add_port)
Definition system.cpp:1182
int str_copy(char *dst, const char *src, int dst_size)
Definition system.cpp:3039
int64_t time_timestamp()
Definition system.cpp:2919
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
@ EXPIRES_NEVER
Definition netban.h:79
@ REASON_LENGTH
Definition netban.h:80
char m_aReason[REASON_LENGTH]
Definition netban.h:83
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:72
unsigned int type
Definition types.h:73
#define str_format
Definition system.cpp:3110
constexpr auto NETADDR_MAXSTRSIZE
Definition types.h:51
constexpr auto NETTYPE_IPV4
Definition types.h:54