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