Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Private Member Functions | Private Attributes | List of all members
CATempIPBlockList Class Reference

The purpose of this class is storing the IPs of JAP users who tried to hack/attack the payment system. More...

#include <CATempIPBlockList.hpp>

Collaboration diagram for CATempIPBlockList:

Public Member Functions

 CATempIPBlockList (UINT64 validTimeMillis)
 
 ~CATempIPBlockList ()
 
SINT32 insertIP (const UINT8 ip[4])
 inserts an IP into the blocklist More...
 
SINT32 checkIP (const UINT8 ip[4])
 check whether an IP is blocked More...
 
void setValidTimeMillis (UINT64 millis)
 set the time (in Milliseconds) that each blocked IP should stay valid in the list More...
 
UINT32 count ()
 

Static Private Member Functions

static THREAD_RETURN cleanupThreadMainLoop (void *param)
 the cleanup thread main loop More...
 

Private Attributes

volatile bool m_bRunCleanupThread
 as long as true the clenaupthread does his job. More...
 
CAThreadm_pCleanupThread
 this thread cleans up the hashtable and removes old entries More...
 
UINT64 m_validTimeMillis
 the time that each blocked IP should stay in the List More...
 
PTEMPIPBLOCKLISTm_hashTable
 the buffer where the entries are stored More...
 
CAMutexm_pMutex
 Used for locking the datastructure to make it threadsafe. More...
 
UINT32 m_iEntries
 

Detailed Description

The purpose of this class is storing the IPs of JAP users who tried to hack/attack the payment system.

Their IP should stay in this block list for a limited time period (e.g. 10 minutes or so). During this time a JAP cannot connect to the mixcascade from this IP.

The implementation uses Mutex locking and is thus threadsafe

Author
Bastian Voigt bavoi.nosp@m.gt@i.nosp@m.nf.fu.nosp@m.-ber.nosp@m.lin.d.nosp@m.e

Definition at line 61 of file CATempIPBlockList.hpp.

Constructor & Destructor Documentation

◆ CATempIPBlockList()

CATempIPBlockList::CATempIPBlockList ( UINT64  validTimeMillis)

Definition at line 37 of file CATempIPBlockList.cpp.

38  {
39  m_validTimeMillis = validTimeMillis;
40  m_iEntries = 0;
41 
42  m_hashTable=new PTEMPIPBLOCKLIST[0x10000];
43  memset(m_hashTable,0,0x10000*sizeof(PTEMPIPBLOCKLIST));
44 
45  m_pMutex = new CAMutex();
46 
47  // launch cleanup thread
48  m_pCleanupThread = new CAThread((UINT8*)"Cleanup Thread");
51  m_pCleanupThread->start(this);
52  }
unsigned char UINT8
Definition: basetypedefs.h:135
UINT64 m_validTimeMillis
the time that each blocked IP should stay in the List
CAMutex * m_pMutex
Used for locking the datastructure to make it threadsafe.
CAThread * m_pCleanupThread
this thread cleans up the hashtable and removes old entries
PTEMPIPBLOCKLIST * m_hashTable
the buffer where the entries are stored
volatile bool m_bRunCleanupThread
as long as true the clenaupthread does his job.
static THREAD_RETURN cleanupThreadMainLoop(void *param)
the cleanup thread main loop
SINT32 start(void *param, bool bDaemon=false, bool bSilent=false)
Starts the execution of the main function of this thread.
Definition: CAThread.cpp:115
SINT32 setMainLoop(THREAD_MAIN_TYP fnc)
Sets the main function which will be executed within this thread.
Definition: CAThread.hpp:148

References cleanupThreadMainLoop(), m_bRunCleanupThread, m_hashTable, m_iEntries, m_pCleanupThread, m_pMutex, m_validTimeMillis, CAThread::setMainLoop(), and CAThread::start().

Here is the call graph for this function:

◆ ~CATempIPBlockList()

CATempIPBlockList::~CATempIPBlockList ( )

Definition at line 56 of file CATempIPBlockList.cpp.

57  {
58  CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList terminating...\n");
59  //Now stop the cleanup thread...
60  m_bRunCleanupThread=false;
61  m_pCleanupThread->join(); //wait for cleanupthread to wakeup and exit
62  delete m_pCleanupThread;
63  m_pCleanupThread=NULL;
64  m_pMutex->lock();
65  //its safe to delete it because we have the lock...
66  for(UINT32 i=0;i<=0xFFFF;i++)
67  {
69  PTEMPIPBLOCKLIST tmpEntry;
70  while(entry!=NULL)
71  {
72  tmpEntry=entry;
73  entry=entry->next;
74  delete tmpEntry;
75  tmpEntry = NULL;
76  }
77  }
78  delete [] m_hashTable;
79  m_hashTable = NULL;
80  m_pMutex->unlock();
81  delete m_pMutex;
82  m_pMutex = NULL;
83  CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList terminated!\n");
84  }
unsigned int UINT32
Definition: basetypedefs.h:131
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
SINT32 join()
Waits for the main function to finish execution.
Definition: CAThread.cpp:187
struct _tempipblocklist_t * next
Next element, NULL if element is the last one.

References CAThread::join(), CAMutex::lock(), m_bRunCleanupThread, m_hashTable, m_pCleanupThread, m_pMutex, _tempipblocklist_t::next, CAMsg::printMsg(), and CAMutex::unlock().

Here is the call graph for this function:

Member Function Documentation

◆ checkIP()

SINT32 CATempIPBlockList::checkIP ( const UINT8  ip[4])

check whether an IP is blocked

Return values
1,ifthe IP is blocked
0,ifthe IP is not blocked
E_SUCCESS,ifthe IP is not blocked
E_UNKNOWN,ifthe IP is blocked

Definition at line 147 of file CATempIPBlockList.cpp.

148 {
149  UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
150  m_pMutex->lock();
151  PTEMPIPBLOCKLIST entry = m_hashTable[hashvalue];
152  PTEMPIPBLOCKLIST previous = NULL;
153  while(entry) {
154  if(memcmp(entry->ip,ip,2)==0) {
155  // we have found the entry
156  // additional check: is it still valid?
157  UINT64 now;
159  if(entry->validTimeMillis <= now)
160  {
161  // entry can be removed
162  if(previous==NULL) {
163  m_hashTable[hashvalue] = entry->next;
164  }
165  else {
166  previous->next = entry->next;
167  }
168  delete entry;
169  entry = NULL;
170  m_iEntries--;
171  m_pMutex->unlock();
172  return E_SUCCESS;
173  }
174  else
175  {
176  m_pMutex->unlock();
177  return E_UNKNOWN;
178  }
179  }
180  previous = entry;
181  entry = entry->next;
182  }
183  m_pMutex->unlock();
184  return E_SUCCESS;
185 }
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:252
unsigned short UINT16
Definition: basetypedefs.h:133
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
UINT64 validTimeMillis
Entry is valid until getCurrentTimeMillis() > validTimeMillis.
UINT8 ip[2]
First two Bytes of the IP-Address.

References E_SUCCESS, E_UNKNOWN, getcurrentTimeMillis(), _tempipblocklist_t::ip, CAMutex::lock(), m_hashTable, m_iEntries, m_pMutex, _tempipblocklist_t::next, CAMutex::unlock(), and _tempipblocklist_t::validTimeMillis.

Here is the call graph for this function:

◆ cleanupThreadMainLoop()

THREAD_RETURN CATempIPBlockList::cleanupThreadMainLoop ( void *  param)
staticprivate

the cleanup thread main loop

Definition at line 192 of file CATempIPBlockList.cpp.

193  {
194  INIT_STACK;
195  BEGIN_STACK("CATempIPBlockList::cleanupThreadMainLoop");
196 
197  CATempIPBlockList * instance;
198  instance = (CATempIPBlockList *)param;
199  while(instance->m_bRunCleanupThread)
200  {
201  // do cleanup
202  UINT64 now;
204  instance->m_pMutex->lock();
205  for(UINT32 i=0;i<=0xFFFF;i++)
206  {
207  PTEMPIPBLOCKLIST entry=instance->m_hashTable[i];
208  PTEMPIPBLOCKLIST previous = NULL;
209  while(entry!=NULL)
210  {
211  if(entry->validTimeMillis <= now)
212  {
213  // entry can be removed
214  if(previous==NULL)
215  {
216  CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList: removing entry...\n");
217  instance->m_hashTable[i] = entry->next;
218  previous=entry->next;
219  delete entry;
220  entry=previous;
221  previous=NULL;
222  }
223  else
224  {
225  previous->next = entry->next;
226  delete entry;
227  entry = previous->next;
228  }
229 
230  instance->m_iEntries--;
231  }
232  else
233  {
234  // entry is still valid
235  previous = entry;
236  entry = entry->next;
237  }
238  }
239  }
240  instance->m_pMutex->unlock();
241 
242  // let the thread sleep for 1 minute
244  }
245 
246  FINISH_STACK("CATempIPBlockList::cleanupThreadMainLoop");
247 
249 }
#define INIT_STACK
Definition: CAThread.hpp:48
#define BEGIN_STACK(methodName)
Definition: CAThread.hpp:49
#define FINISH_STACK(methodName)
Definition: CAThread.hpp:50
SINT32 sSleep(UINT32 sec)
Sleeps sec Seconds.
Definition: CAUtil.cpp:425
#define THREAD_RETURN_SUCCESS
Definition: StdAfx.h:542
#define CLEANUP_THREAD_SLEEP_INTERVAL
Definition: StdAfx.h:217
The purpose of this class is storing the IPs of JAP users who tried to hack/attack the payment system...

References BEGIN_STACK, CLEANUP_THREAD_SLEEP_INTERVAL, FINISH_STACK, getcurrentTimeMillis(), INIT_STACK, CAMutex::lock(), m_bRunCleanupThread, m_hashTable, m_iEntries, m_pMutex, _tempipblocklist_t::next, CAMsg::printMsg(), sSleep(), THREAD_RETURN_SUCCESS, CAMutex::unlock(), and _tempipblocklist_t::validTimeMillis.

Referenced by CATempIPBlockList().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ count()

UINT32 CATempIPBlockList::count ( )
inline

Definition at line 90 of file CATempIPBlockList.hpp.

91  {
92  return m_iEntries;
93  }

References m_iEntries.

◆ insertIP()

SINT32 CATempIPBlockList::insertIP ( const UINT8  ip[4])

inserts an IP into the blocklist

Return values
E_SUCCESSif successful
E_UNKNOWNif IP was already in blocklist

Definition at line 94 of file CATempIPBlockList.cpp.

95 {
96  UINT64 now;
98 
100  memcpy(newEntry->ip,ip,2);
101  newEntry->validTimeMillis = now + m_validTimeMillis;
102  newEntry->next=NULL;
103 
104  UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
105  m_pMutex->lock();
106 
107  if(m_hashTable[hashvalue]==NULL) {
108  m_hashTable[hashvalue] = newEntry;
109  m_iEntries++;
110  }
111  else
112  {
113  PTEMPIPBLOCKLIST temp = m_hashTable[hashvalue];
114  for(;;)
115  {
116  if(memcmp(temp->ip,ip,2)==0)
117  {
118  // we have found the entry
119  delete newEntry;
120  m_pMutex->unlock();
121  return E_UNKNOWN;
122  }
123  if (temp->next)
124  {
125  temp = temp->next;
126  }
127  else
128  {
129  temp->next = newEntry;
130  m_iEntries++;
131  break;
132  }
133  }
134  }
135  m_pMutex->unlock();
136  return E_SUCCESS;
137 }
struct _tempipblocklist_t TEMPIPBLOCKLISTENTRY

References E_SUCCESS, E_UNKNOWN, getcurrentTimeMillis(), _tempipblocklist_t::ip, CAMutex::lock(), m_hashTable, m_iEntries, m_pMutex, m_validTimeMillis, _tempipblocklist_t::next, CAMutex::unlock(), and _tempipblocklist_t::validTimeMillis.

Referenced by CAFirstMix::doUserLogin_internal().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setValidTimeMillis()

void CATempIPBlockList::setValidTimeMillis ( UINT64  millis)

set the time (in Milliseconds) that each blocked IP should stay valid in the list

Member Data Documentation

◆ m_bRunCleanupThread

volatile bool CATempIPBlockList::m_bRunCleanupThread
private

as long as true the clenaupthread does his job.

If false the thread will exit.

Definition at line 97 of file CATempIPBlockList.hpp.

Referenced by CATempIPBlockList(), cleanupThreadMainLoop(), and ~CATempIPBlockList().

◆ m_hashTable

PTEMPIPBLOCKLIST* CATempIPBlockList::m_hashTable
private

the buffer where the entries are stored

Definition at line 109 of file CATempIPBlockList.hpp.

Referenced by CATempIPBlockList(), checkIP(), cleanupThreadMainLoop(), insertIP(), and ~CATempIPBlockList().

◆ m_iEntries

UINT32 CATempIPBlockList::m_iEntries
private

◆ m_pCleanupThread

CAThread* CATempIPBlockList::m_pCleanupThread
private

this thread cleans up the hashtable and removes old entries

Definition at line 100 of file CATempIPBlockList.hpp.

Referenced by CATempIPBlockList(), and ~CATempIPBlockList().

◆ m_pMutex

CAMutex* CATempIPBlockList::m_pMutex
private

Used for locking the datastructure to make it threadsafe.

Definition at line 112 of file CATempIPBlockList.hpp.

Referenced by CATempIPBlockList(), checkIP(), cleanupThreadMainLoop(), insertIP(), and ~CATempIPBlockList().

◆ m_validTimeMillis

UINT64 CATempIPBlockList::m_validTimeMillis
private

the time that each blocked IP should stay in the List

Definition at line 106 of file CATempIPBlockList.hpp.

Referenced by CATempIPBlockList(), and insertIP().


The documentation for this class was generated from the following files: