Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Static Private Member Functions | Private Attributes
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:
[legend]

List of all members.

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.
CAThreadm_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
PTEMPIPBLOCKLISTm_hashTable
 the buffer where the entries are stored
CAMutexm_pMutex
 Used for locking the datastructure to make it threadsafe.
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 <bavoigt@inf.fu-berlin.de>

Definition at line 61 of file CATempIPBlockList.hpp.


Constructor & Destructor Documentation

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);
  }

Here is the call graph for this function:

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;
  }

Here is the call graph for this function:


Member Function Documentation

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 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;
}

Here is the call graph for this function:

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;
}

Here is the call graph for this function:

Definition at line 90 of file CATempIPBlockList.hpp.

References m_iEntries.

Referenced by fm_loopAcceptUsers().

    {
      return m_iEntries;
    }

inserts an IP into the blocklist

Return values:
E_SUCCESSif successful
E_UNKNOWNif 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;
}

Here is the call graph for this function:

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


Member Data Documentation

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().

the buffer where the entries are stored

Definition at line 109 of file CATempIPBlockList.hpp.

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

this thread cleans up the hashtable and removes old entries

Definition at line 100 of file CATempIPBlockList.hpp.

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

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().

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: