Mixe for Privacy and Anonymity in the Internet
CATempIPBlockList.cpp
Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000, The JAP-Team 
00003 All rights reserved.
00004 Redistribution and use in source and binary forms, with or without modification, 
00005 are permitted provided that the following conditions are met:
00006 
00007   - Redistributions of source code must retain the above copyright notice, 
00008     this list of conditions and the following disclaimer.
00009 
00010   - Redistributions in binary form must reproduce the above copyright notice, 
00011     this list of conditions and the following disclaimer in the documentation and/or 
00012     other materials provided with the distribution.
00013 
00014   - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors 
00015     may be used to endorse or promote products derived from this software without specific 
00016     prior written permission. 
00017 
00018   
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS 
00020 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
00021 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
00022 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00023 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00024 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
00025 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
00026 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
00027 */
00028 #include "StdAfx.h"
00029 
00030 #ifndef ONLY_LOCAL_PROXY
00031 
00032 #include "CATempIPBlockList.hpp"
00033 #include "CAUtil.hpp"
00034 #include "CAMsg.hpp"
00035 
00036 
00037 CATempIPBlockList::CATempIPBlockList(UINT64 validTimeMillis)
00038   {
00039     m_validTimeMillis = validTimeMillis;
00040     m_iEntries = 0;
00041 
00042     m_hashTable=new PTEMPIPBLOCKLIST[0x10000];
00043     memset(m_hashTable,0,0x10000*sizeof(PTEMPIPBLOCKLIST));
00044   
00045     m_pMutex = new CAMutex();
00046   
00047     // launch cleanup thread
00048     m_pCleanupThread = new CAThread((UINT8*)"Cleanup Thread");
00049     m_bRunCleanupThread=true;
00050     m_pCleanupThread->setMainLoop(cleanupThreadMainLoop);
00051     m_pCleanupThread->start(this);
00052   }
00053 
00054 
00055 
00056 CATempIPBlockList::~CATempIPBlockList()
00057   {
00058     CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList terminating...\n");
00059     //Now stop the cleanup thread...
00060     m_bRunCleanupThread=false;
00061     m_pCleanupThread->join(); //wait for cleanupthread to wakeup and exit
00062     m_pMutex->lock();
00063     //its safe to delete it because we have the lock...
00064     for(UINT32 i=0;i<=0xFFFF;i++) 
00065       {
00066         PTEMPIPBLOCKLIST entry=m_hashTable[i];
00067         PTEMPIPBLOCKLIST tmpEntry;
00068         while(entry!=NULL)
00069           {
00070             tmpEntry=entry;
00071             entry=entry->next;
00072             delete tmpEntry;
00073             tmpEntry = NULL;
00074           }
00075       }
00076     delete [] m_hashTable;
00077     m_hashTable = NULL;
00078     m_pMutex->unlock();
00079     delete m_pMutex;
00080     m_pMutex = NULL;
00081   }
00082 
00083 
00084 
00091 SINT32 CATempIPBlockList::insertIP(const UINT8 ip[4])
00092 {
00093   UINT64 now;
00094   getcurrentTimeMillis(now);
00095   
00096   PTEMPIPBLOCKLIST newEntry = new TEMPIPBLOCKLISTENTRY;
00097   memcpy(newEntry->ip,ip,2);
00098   newEntry->validTimeMillis = now + m_validTimeMillis;
00099   newEntry->next=NULL;  
00100   
00101   UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
00102   m_pMutex->lock();
00103   
00104   if(m_hashTable[hashvalue]==NULL) {
00105     m_hashTable[hashvalue] = newEntry;
00106     m_iEntries++;
00107   }
00108   else 
00109     {
00110       PTEMPIPBLOCKLIST temp = m_hashTable[hashvalue];
00111       for(;;) 
00112         {
00113           if(memcmp(temp->ip,ip,2)==0) 
00114             {
00115               // we have found the entry
00116               delete newEntry;
00117               m_pMutex->unlock();
00118               return E_UNKNOWN;
00119             }
00120           if (temp->next)
00121             {
00122               temp = temp->next;
00123             }
00124           else
00125             {
00126               temp->next = newEntry;
00127               m_iEntries++;
00128               break;
00129             }
00130         }
00131     }
00132   m_pMutex->unlock(); 
00133   return E_SUCCESS;
00134 }
00135 
00136 
00137 
00144 SINT32 CATempIPBlockList::checkIP(const UINT8 ip[4])
00145 {
00146   UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
00147   m_pMutex->lock();
00148   PTEMPIPBLOCKLIST entry = m_hashTable[hashvalue];
00149   PTEMPIPBLOCKLIST previous = NULL;
00150   while(entry) {
00151     if(memcmp(entry->ip,ip,2)==0) {
00152       // we have found the entry
00153       // additional check: is it still valid?
00154       UINT64 now;
00155       getcurrentTimeMillis(now);
00156       if(entry->validTimeMillis <= now) 
00157       {
00158         // entry can be removed
00159         if(previous==NULL) {
00160           m_hashTable[hashvalue] = entry->next;
00161         }
00162         else {
00163           previous->next = entry->next;
00164         }
00165         delete entry;
00166         entry = NULL;
00167         m_iEntries--;
00168         m_pMutex->unlock();
00169         return E_SUCCESS;
00170       }
00171       else 
00172       {
00173         m_pMutex->unlock();
00174         return E_UNKNOWN;
00175       }
00176     }
00177     previous = entry;
00178     entry = entry->next;
00179   }
00180   m_pMutex->unlock();
00181   return E_SUCCESS;
00182 }
00183 
00184 
00185 
00189 THREAD_RETURN CATempIPBlockList::cleanupThreadMainLoop(void *param)
00190   {
00191     INIT_STACK;
00192     BEGIN_STACK("CATempIPBlockList::cleanupThreadMainLoop");
00193     
00194     CATempIPBlockList * instance;
00195     instance = (CATempIPBlockList *)param;
00196     while(instance->m_bRunCleanupThread) 
00197       {       
00198         // do cleanup
00199         UINT64 now;
00200         getcurrentTimeMillis(now);
00201         instance->m_pMutex->lock();
00202         for(UINT32 i=0;i<=0xFFFF;i++) 
00203           {
00204             PTEMPIPBLOCKLIST entry=instance->m_hashTable[i];
00205             PTEMPIPBLOCKLIST previous = NULL;
00206             while(entry!=NULL)
00207               {
00208                 if(entry->validTimeMillis <= now) 
00209                   {
00210                     // entry can be removed
00211                     if(previous==NULL)
00212                       {
00213                         CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList: removing entry...\n");
00214                         instance->m_hashTable[i] = entry->next;
00215                         previous=entry->next;
00216                         delete entry;
00217                         entry=previous;
00218                         previous=NULL;
00219                     }
00220                 else
00221                   {
00222                     previous->next = entry->next;
00223                     delete entry;
00224                     entry = previous->next;
00225                   }
00226 
00227                 instance->m_iEntries--;
00228               }
00229           else {
00230           // entry is still valid
00231           previous = entry;
00232           entry = entry->next;
00233         }
00234       }
00235     }
00236     instance->m_pMutex->unlock();
00237 
00238     // let the thread sleep for 1 minute
00239     sSleep(CLEANUP_THREAD_SLEEP_INTERVAL);
00240   }
00241   
00242   FINISH_STACK("CATempIPBlockList::cleanupThreadMainLoop");
00243   
00244   THREAD_RETURN_SUCCESS;
00245 }
00246 #endif //ONLY_LOCAL_PROXY