Mixe for Privacy and Anonymity in the Internet
CAIPList.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 #ifndef ONLY_LOCAL_PROXY
00030 #include "CAIPList.hpp"
00031 #include "CAMsg.hpp"
00032 #include "CAUtil.hpp"
00033 #include "CACmdLnOptions.hpp"
00034 
00036 
00039 CAIPList::CAIPList()
00040   { 
00041     m_pMutex=new CAMutex();
00042     m_HashTable=new PIPLIST[0x10000];
00043     memset((void*)m_HashTable,0,0x10000*sizeof(PIPLIST));
00044     m_allowedConnections=MAX_IP_CONNECTIONS;
00045 #if defined (_DEBUG)
00046     m_Random=new UINT8[56];
00047     getRandom(m_Random,56);
00048 #endif
00049   }
00050 
00055 CAIPList::CAIPList(UINT32 allowedConnections)
00056   {
00057     m_pMutex=new CAMutex();
00058     m_HashTable=new PIPLIST[0x10000];
00059     memset((void*)m_HashTable,0,0x10000*sizeof(PIPLIST));
00060     m_allowedConnections=allowedConnections;
00061 #if defined (_DEBUG)
00062     m_Random=new UINT8[56];
00063     getRandom(m_Random,56);
00064 #endif
00065   }
00066 
00068 CAIPList::~CAIPList()
00069   {
00070     for(UINT32 i=0;i<=0xFFFF;i++)
00071       {
00072         VOLATILE_PIPLIST entry=m_HashTable[i];
00073         PIPLIST tmpEntry;
00074         while(entry!=NULL)
00075           { 
00076             tmpEntry=entry;
00077             entry=entry->next;
00078             delete tmpEntry;
00079             tmpEntry = NULL;
00080           }
00081       }
00082 #ifdef _DEUBG
00083     delete[] m_Random;
00084     m_Random = NULL;
00085 #endif
00086     delete[] m_HashTable;
00087     m_HashTable = NULL;
00088     delete m_pMutex;
00089     m_pMutex = NULL;
00090   }
00091 
00103 SINT32 CAIPList::insertIP(const UINT8 ip[4])
00104   {
00105 #ifdef PAYMENT
00106     return E_SUCCESS;
00107 #else     
00108     UINT16 hashvalue=(ip[2]<<8)|ip[3];
00109     SINT32 ret;
00110     m_pMutex->lock();
00111     PIPLIST entry=m_HashTable[hashvalue];
00112     if(entry==NULL)
00113       {//Hashkey nicht in der Hashtabelle gefunden --> neuer Eintrag in Hashtabelle
00114 #ifndef PSEUDO_LOG
00115 #ifdef _DEBUG
00116         UINT8 hash[16];
00117         memcpy(m_Random,ip,4);
00118         MD5(m_Random,56,hash);
00119         CAMsg::printMsg(LOG_DEBUG,"Inserting new IP-Address: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X !\n",hash[0],hash[1],hash[2],hash[3],hash[4],hash[5],hash[6],hash[7],hash[8],hash[9],hash[10],hash[11],hash[12],hash[13],hash[14],hash[15]);
00120 #endif
00121 #else
00122         CAMsg::printMsg(LOG_DEBUG,"Inserting new IP-Address: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
00123 #endif
00124         entry=new IPLISTENTRY;
00125         memcpy(entry->ip,ip,2);
00126         entry->count=1;
00127         entry->next=NULL;
00128         m_HashTable[hashvalue]=entry;
00129         ret = entry->count;
00130 #ifdef DEBUG
00131 #ifndef PSEUDO_LOG
00132 #ifdef DEBUG
00133         CAMsg::printMsg(LOG_DEBUG,"New IP-Address inserted: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X !\n",hash[0],hash[1],hash[2],hash[3],hash[4],hash[5],hash[6],hash[7],hash[8],hash[9],hash[10],hash[11],hash[12],hash[13],hash[14],hash[15]);
00134 #endif
00135 #else
00136         CAMsg::printMsg(LOG_DEBUG,"New IP-Address inserted: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
00137 #endif
00138 #endif
00139         m_pMutex->unlock();
00140         return ret;
00141       }
00142     else
00143       {//Hashkey in Hashtabelle gefunden --> suche in Ueberlaufliste nach Eintrag bzw. lege neuen Eitnrag an
00144         PIPLIST last;
00145         do
00146           {
00147             if(memcmp(entry->ip,ip,2)==0) //we have found the entry
00148               {
00149                 #ifdef PSEUDO_LOG
00150                   CAMsg::printMsg(LOG_DEBUG,"Inserting IP-Address: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
00151                 #endif
00152                 if(entry->count>=m_allowedConnections) //an Attack...
00153                   {
00154                     //#if !defined(PSEUDO_LOG)&&defined(FIREWALL_SUPPORT)
00155                       CAMsg::printMsg(LOG_CRIT,"Possible flooding attack from: %u.%u.x.x !\n",ip[0],ip[1],ip[2],ip[3]);
00156                     //#endif
00157                     m_pMutex->unlock();
00158                     return E_UNKNOWN;
00159                   }
00160                 entry->count++;
00161                 ret = entry->count;
00162                 #ifdef PSEUDO_LOG
00163                   CAMsg::printMsg(LOG_DEBUG,"IP-Address inserted: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
00164                 #endif
00165                 m_pMutex->unlock();
00166                 return ret;
00167               }
00168             last=entry;
00169             entry=entry->next;
00170           } while(entry!=NULL);
00171 //Nicht in der Ueberlaufliste gefunden
00172         last->next=new IPLISTENTRY;
00173         entry=last->next;
00174         memcpy(entry->ip,ip,2);
00175         entry->count=1;
00176         entry->next=NULL;
00177         ret = entry->count;
00178         m_pMutex->unlock();
00179         return ret;
00180       } 
00181 #endif      
00182   }
00183 
00189   SINT32 CAIPList::removeIP(const UINT8 ip[4])
00190   { 
00191 #ifdef PAYMENT  
00192   return E_SUCCESS;
00193 #else
00194     UINT16 hashvalue=(ip[2]<<8)|ip[3];
00195     SINT32 ret;
00196     m_pMutex->lock();
00197     PIPLIST entry=m_HashTable[hashvalue];
00198     if(entry==NULL)
00199       {
00200         m_pMutex->unlock();
00201         CAMsg::printMsg(LOG_INFO,"Try to remove IP which is not in the hashtable of the IP-list - possible inconsistences in IPList!\n");
00202         return 0;
00203       }
00204     else
00205       {
00206         PIPLIST before=NULL;
00207         while(entry!=NULL)
00208         {
00209           if(memcmp(entry->ip,ip,2)==0)
00210           {
00211             entry->count--;
00212             if(entry->count==0)
00213             {           
00214               #ifndef PSEUDO_LOG
00215                 #if defined (_DEBUG)
00216                   UINT8 hash[16];
00217                   memcpy(m_Random,ip,4);
00218                   MD5(m_Random,56,hash);
00219                   CAMsg::printMsg(LOG_DEBUG,"Removing IP-Address: %02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X !\n",hash[0],hash[1],hash[2],hash[3],hash[4],hash[5],hash[6],hash[7],hash[8],hash[9],hash[10],hash[11],hash[12],hash[13],hash[14],hash[15]);
00220                 #endif
00221               #else
00222                 CAMsg::printMsg(LOG_DEBUG,"Removing IP-Address: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
00223               #endif
00224               if(before==NULL)
00225                 m_HashTable[hashvalue]=entry->next;
00226               else
00227                 before->next=entry->next;
00228               delete entry;
00229               entry = NULL;
00230               m_pMutex->unlock();
00231               return 0;
00232             }
00233             ret = entry->count;
00234             m_pMutex->unlock();
00235             return ret;
00236           }
00237           before=entry;
00238           entry=entry->next;
00239         }
00240         m_pMutex->unlock();
00241         CAMsg::printMsg(LOG_INFO,"Try to remove IP which is not in list - possible inconsistences in IPList!\n");
00242         return 0;
00243       }
00244 #endif      
00245   }
00246 #endif //ONLY_LOCAL_PROXY