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