|
Mixe for Privacy and Anonymity in the Internet
|
The purpose of this class is storing the IPs of JAP users who tried to hack/attack the payment system. More...
#include <CATempIPBlockList.hpp>
Public Member Functions | |
| CATempIPBlockList (UINT64 validTimeMillis) | |
| ~CATempIPBlockList () | |
| SINT32 | insertIP (const UINT8 ip[4]) |
| inserts an IP into the blocklist | |
| SINT32 | checkIP (const UINT8 ip[4]) |
| check whether an IP is blocked | |
| void | setValidTimeMillis (UINT64 millis) |
| set the time (in Milliseconds) that each blocked IP should stay valid in the list | |
| UINT32 | count () |
Static Private Member Functions | |
| static THREAD_RETURN | cleanupThreadMainLoop (void *param) |
| the cleanup thread main loop | |
Private Attributes | |
| volatile bool | m_bRunCleanupThread |
| as long as true the clenaupthread does his job. | |
| CAThread * | m_pCleanupThread |
| this thread cleans up the hashtable and removes old entries | |
| UINT64 | m_validTimeMillis |
| the time that each blocked IP should stay in the List | |
| PTEMPIPBLOCKLIST * | m_hashTable |
| the buffer where the entries are stored | |
| CAMutex * | m_pMutex |
| Used for locking the datastructure to make it threadsafe. | |
| UINT32 | m_iEntries |
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
Definition at line 61 of file CATempIPBlockList.hpp.
| CATempIPBlockList::CATempIPBlockList | ( | UINT64 | validTimeMillis | ) |
Definition at line 37 of file CATempIPBlockList.cpp.
References cleanupThreadMainLoop(), m_bRunCleanupThread, m_hashTable, m_iEntries, m_pCleanupThread, m_pMutex, m_validTimeMillis, CAThread::setMainLoop(), and CAThread::start().
{
m_validTimeMillis = validTimeMillis;
m_iEntries = 0;
m_hashTable=new PTEMPIPBLOCKLIST[0x10000];
memset(m_hashTable,0,0x10000*sizeof(PTEMPIPBLOCKLIST));
m_pMutex = new CAMutex();
// launch cleanup thread
m_pCleanupThread = new CAThread((UINT8*)"Cleanup Thread");
m_bRunCleanupThread=true;
m_pCleanupThread->setMainLoop(cleanupThreadMainLoop);
m_pCleanupThread->start(this);
}
Definition at line 56 of file CATempIPBlockList.cpp.
References CAThread::join(), CAMutex::lock(), m_bRunCleanupThread, m_hashTable, m_pCleanupThread, m_pMutex, _tempipblocklist_t::next, CAMsg::printMsg(), and CAMutex::unlock().
{
CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList terminating...\n");
//Now stop the cleanup thread...
m_bRunCleanupThread=false;
m_pCleanupThread->join(); //wait for cleanupthread to wakeup and exit
m_pMutex->lock();
//its safe to delete it because we have the lock...
for(UINT32 i=0;i<=0xFFFF;i++)
{
PTEMPIPBLOCKLIST entry=m_hashTable[i];
PTEMPIPBLOCKLIST tmpEntry;
while(entry!=NULL)
{
tmpEntry=entry;
entry=entry->next;
delete tmpEntry;
tmpEntry = NULL;
}
}
delete [] m_hashTable;
m_hashTable = NULL;
m_pMutex->unlock();
delete m_pMutex;
m_pMutex = NULL;
}
| SINT32 CATempIPBlockList::checkIP | ( | const UINT8 | ip[4] | ) |
check whether an IP is blocked
| 1,if | the IP is blocked |
| 0,if | the IP is not blocked |
| E_SUCCESS,if | the IP is not blocked |
| E_UNKNOWN,if | the IP is blocked |
Definition at line 144 of file CATempIPBlockList.cpp.
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.
Referenced by fm_loopAcceptUsers().
{
UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
m_pMutex->lock();
PTEMPIPBLOCKLIST entry = m_hashTable[hashvalue];
PTEMPIPBLOCKLIST previous = NULL;
while(entry) {
if(memcmp(entry->ip,ip,2)==0) {
// we have found the entry
// additional check: is it still valid?
UINT64 now;
getcurrentTimeMillis(now);
if(entry->validTimeMillis <= now)
{
// entry can be removed
if(previous==NULL) {
m_hashTable[hashvalue] = entry->next;
}
else {
previous->next = entry->next;
}
delete entry;
entry = NULL;
m_iEntries--;
m_pMutex->unlock();
return E_SUCCESS;
}
else
{
m_pMutex->unlock();
return E_UNKNOWN;
}
}
previous = entry;
entry = entry->next;
}
m_pMutex->unlock();
return E_SUCCESS;
}
| THREAD_RETURN CATempIPBlockList::cleanupThreadMainLoop | ( | void * | param | ) | [static, private] |
the cleanup thread main loop
Definition at line 189 of file CATempIPBlockList.cpp.
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().
{
INIT_STACK;
BEGIN_STACK("CATempIPBlockList::cleanupThreadMainLoop");
CATempIPBlockList * instance;
instance = (CATempIPBlockList *)param;
while(instance->m_bRunCleanupThread)
{
// do cleanup
UINT64 now;
getcurrentTimeMillis(now);
instance->m_pMutex->lock();
for(UINT32 i=0;i<=0xFFFF;i++)
{
PTEMPIPBLOCKLIST entry=instance->m_hashTable[i];
PTEMPIPBLOCKLIST previous = NULL;
while(entry!=NULL)
{
if(entry->validTimeMillis <= now)
{
// entry can be removed
if(previous==NULL)
{
CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList: removing entry...\n");
instance->m_hashTable[i] = entry->next;
previous=entry->next;
delete entry;
entry=previous;
previous=NULL;
}
else
{
previous->next = entry->next;
delete entry;
entry = previous->next;
}
instance->m_iEntries--;
}
else {
// entry is still valid
previous = entry;
entry = entry->next;
}
}
}
instance->m_pMutex->unlock();
// let the thread sleep for 1 minute
sSleep(CLEANUP_THREAD_SLEEP_INTERVAL);
}
FINISH_STACK("CATempIPBlockList::cleanupThreadMainLoop");
THREAD_RETURN_SUCCESS;
}
| UINT32 CATempIPBlockList::count | ( | ) | [inline] |
Definition at line 90 of file CATempIPBlockList.hpp.
References m_iEntries.
Referenced by fm_loopAcceptUsers().
{
return m_iEntries;
}
| SINT32 CATempIPBlockList::insertIP | ( | const UINT8 | ip[4] | ) |
inserts an IP into the blocklist
| E_SUCCESS | if successful |
| E_UNKNOWN | if IP was already in blocklist |
Definition at line 91 of file CATempIPBlockList.cpp.
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(), and fm_loopAcceptUsers().
{
UINT64 now;
getcurrentTimeMillis(now);
PTEMPIPBLOCKLIST newEntry = new TEMPIPBLOCKLISTENTRY;
memcpy(newEntry->ip,ip,2);
newEntry->validTimeMillis = now + m_validTimeMillis;
newEntry->next=NULL;
UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
m_pMutex->lock();
if(m_hashTable[hashvalue]==NULL) {
m_hashTable[hashvalue] = newEntry;
m_iEntries++;
}
else
{
PTEMPIPBLOCKLIST temp = m_hashTable[hashvalue];
for(;;)
{
if(memcmp(temp->ip,ip,2)==0)
{
// we have found the entry
delete newEntry;
m_pMutex->unlock();
return E_UNKNOWN;
}
if (temp->next)
{
temp = temp->next;
}
else
{
temp->next = newEntry;
m_iEntries++;
break;
}
}
}
m_pMutex->unlock();
return E_SUCCESS;
}
| void CATempIPBlockList::setValidTimeMillis | ( | UINT64 | millis | ) |
set the time (in Milliseconds) that each blocked IP should stay valid in the list
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().
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().
UINT32 CATempIPBlockList::m_iEntries [private] |
Definition at line 114 of file CATempIPBlockList.hpp.
Referenced by CATempIPBlockList(), checkIP(), cleanupThreadMainLoop(), count(), and insertIP().
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().
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().
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().
1.7.6.1