Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
cNetAddrTracker.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_cNetAddrTracker_H
7 #define _INC_cNetAddrTracker_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cNetAddr.h"
13 #include "cNetHostRange.h"
17 
18 namespace GrayLib
19 {
21 
22  class cClientAddrObj; // persisting def record. will be defined some time later.
23 
24  class GRAYLIB_LINK cNetAddrTracker : public cThreadLockableRef // cRefBase IUnknown
25  {
31 
32  friend class cClientAddrObj;
33 
34  public:
35  static const TIMESECD_t k_PING_WINDOW_LEN = (5 * 60); // seconds
36  static const int k_PINGS_PER_WINDOW = 5 * 10;
37  static const TIMESECD_t k_BLOCK_LEN = (60); // seconds to block for some bad action.
38 
42  bool m_bTrusted;
43 
44  protected:
50 
51  // Track normal but excessive and anti social/hacking behaviors.
52  int m_iPings;
54 
57 
58  public:
59  cNetAddrTracker(const cNetAddress& addr);
60  virtual ~cNetAddrTracker();
61 
62  cString get_Name() const
63  {
64  return cString(m_addr.get_HostStr());
65  }
66  HASHCODE_t get_HashCode() const noexcept
67  {
68  return m_addr.get_HashCode();
69  }
70 
71  cStringA get_DescString() const;
72  bool LookupHostName();
73 
74  HRESULT DisposeThis();
75 
77  bool IsNeverDecay() const;
78  void BumpDecayTimeSec(TIMESECD_t iSeconds);
79 
81  bool IsBlocked() const;
82  bool SetBlocked(TIMESECD_t iTimeDecaySeconds, const char* pszReason);
83 
84  bool onTickAddr(int iRefCount);
85 
87  int get_Pings() const
88  {
89  return m_iPings;
90  }
91 
93  bool IncPingBlock(bool bPreAccept = false);
94  bool IncBadPassword(const char* pszAttackedAccountName);
95 
96  void put_Account(cRefBase* pAccount)
97  {
100  m_iPings = 0;
101  m_iBadPasswords = 0;
102  m_pAccount = pAccount;
103  }
104  cRefBasePtr get_Account() const
105  {
107  return m_pAccount; // associate this with a CAccount.
108  }
109  };
110 
112 
114  : public cSingleton<cNetAddrMgr>
115  , public cThreadLockArrayHash<cNetAddrTracker> // overload this so i can mess with protected stuff.
116  {
125 
126  friend class cSingleton<cNetAddrMgr>;
128 
129  protected:
130  cNetAddrMgr();
131  ~cNetAddrMgr();
132 
133  public:
135  {
137  return this->Add(pClientAddr);
138  }
139  bool RemoveLogAddr(cNetAddrTracker* pClientAddr)
140  {
142  return this->RemoveArgKey(pClientAddr);
143  }
144 
145  virtual cNetAddrTrackPtr CreateAddrTracker(const cNetAddress& addr);
146  cNetAddrTrackPtr FindAddrTracker(const cNetAddress& addr, bool bCreate = true);
147 
149  void onTickAddrArray();
150 
152  };
153 }
154 #endif // _INC_cNetAddrTracker_H
#define GRAYLIB_LINK
Definition: GrayLibBase.h:35
INT32 HRESULT
_WIN32 style error codes. INT32
Definition: SysTypes.h:465
#define CHEAPOBJECT_IMPL
Definition: cHeapObject.h:32
Definition: cClientAddrObj.h:17
Definition: cNetAddrTracker.h:116
UNITTEST_FRIEND(cNetAddrTracker)
bool RemoveLogAddr(cNetAddrTracker *pClientAddr)
Definition: cNetAddrTracker.h:139
ITERATE_t AddLogAddr(cNetAddrTracker *pClientAddr)
Definition: cNetAddrTracker.h:134
Definition: cNetAddrTracker.h:25
cTimeInt m_timeCreated
When was this record created?
Definition: cNetAddrTracker.h:45
cTimeInt m_timeWindowStart
My ping check window started at this time.
Definition: cNetAddrTracker.h:48
int m_iPings
How many pings have we seen since m_timeWindowStart.
Definition: cNetAddrTracker.h:52
cStringA m_sIPHost
Look up this IP host name. reverse DNS lookup. (Maybe async lookup)
Definition: cNetAddrTracker.h:40
int m_iBadPasswords
How many bad passwords since m_timeWindowStart.
Definition: cNetAddrTracker.h:53
cRefBasePtr m_pAccount
associate this IP address with an account. (allow auto log in based on IP?)
Definition: cNetAddrTracker.h:56
cTimeInt m_timeBlockTil
Blocked until this time.
Definition: cNetAddrTracker.h:47
bool m_bTrusted
This is a trusted IP. (don't ever block it. e.g. localhost)
Definition: cNetAddrTracker.h:42
HASHCODE_t get_HashCode() const noexcept
Definition: cNetAddrTracker.h:66
void put_Account(cRefBase *pAccount)
Definition: cNetAddrTracker.h:96
cTimeInt m_timeDecay
Time this record should be gone. (may be set to NEVER)
Definition: cNetAddrTracker.h:46
int get_Pings() const
Transient Activity tracking info.
Definition: cNetAddrTracker.h:87
cStringA m_sDescription
Description of why this IP is blocked. or why we track it.
Definition: cNetAddrTracker.h:41
cNetAddress m_addr
The remote address we are tracking. IP4 OR IP6.
Definition: cNetAddrTracker.h:39
cTimeInt m_timeLastEvent
Last time we last interacted with this IP.
Definition: cNetAddrTracker.h:49
cString get_Name() const
Definition: cNetAddrTracker.h:62
Definition: cNetAddr.h:31
HASHCODE_t get_HashCode() const noexcept
Definition: cNetAddr.h:185
cStringA get_HostStr() const
Definition: cNetAddr.cpp:232
Definition: cRefPtr.h:22
Definition: cSingleton.h:127
Definition: cThreadArray.h:240
Definition: cThreadLockRef.h:20
< similar to the MFC CTime and cTimeSpan, not as accurate or large ranged as COleDateTime
Definition: cTimeInt.h:101
Definition: cMesh.h:22
cRefPtr< cNetAddrTracker > cNetAddrTrackPtr
cIUnkPtr
Definition: cNetAddrTracker.h:111
UNITTEST2_PREDEF(cQuadtree)
int ITERATE_t
like size_t but signed
Definition: Index.h:28
int TIMESECD_t
signed delta seconds. like TIMESEC_t. redefined in TimeUnits.h.
Definition: cTimeSys.h:19
__DECL_IMPORT cRefPtr cRefBasePtr
Definition: cRefPtr.h:376
UINT_PTR HASHCODE_t
could hold a pointer converted to a number? maybe 64 or 32 bit ? same as size_t.
Definition: GrayCore.h:116
cStringT< GChar_t > cString
Definition: cString.h:636