Mixe for Privacy and Anonymity in the Internet
CAIPList.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 #include "StdAfx.h"
29 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_FIRST_MIX
30 #include "CAIPList.hpp"
31 #include "CAMsg.hpp"
32 #include "CAUtil.hpp"
33 #include "CACmdLnOptions.hpp"
34 
36 
40  {
41  m_pMutex=new CAMutex();
42  m_HashTable=new PIPLIST[0x10000];
43  memset((void*)m_HashTable,0,0x10000*sizeof(PIPLIST));
45 #if defined (_DEBUG)
46  m_Random=new UINT8[56];
47  getRandom(m_Random,56);
48 #endif
49  }
50 
55 CAIPList::CAIPList(UINT32 allowedConnections)
56  {
57  m_pMutex=new CAMutex();
58  m_HashTable=new PIPLIST[0x10000];
59  memset((void*)m_HashTable,0,0x10000*sizeof(PIPLIST));
60  m_allowedConnections=allowedConnections;
61 #if defined (_DEBUG)
62  m_Random=new UINT8[56];
63  getRandom(m_Random,56);
64 #endif
65  }
66 
69  {
70  for(UINT32 i=0;i<=0xFFFF;i++)
71  {
73  PIPLIST tmpEntry;
74  while(entry!=NULL)
75  {
76  tmpEntry=entry;
77  entry=entry->next;
78  delete tmpEntry;
79  tmpEntry = NULL;
80  }
81  }
82 #ifdef _DEBUG
83  delete[] m_Random;
84  m_Random = NULL;
85 #endif
86  delete[] m_HashTable;
87  m_HashTable = NULL;
88  delete m_pMutex;
89  m_pMutex = NULL;
90  }
91 
104  {
105 #ifdef PAYMENT
106  return E_SUCCESS;
107 #else
108  UINT16 hashvalue=(ip[2]<<8)|ip[3];
109  SINT32 ret;
110  m_pMutex->lock();
111  PIPLIST entry=m_HashTable[hashvalue];
112  if(entry==NULL)
113  {//Hashkey nicht in der Hashtabelle gefunden --> neuer Eintrag in Hashtabelle
114 #ifndef PSEUDO_LOG
115 #ifdef _DEBUG
116  UINT8 hash[16];
117  memcpy(m_Random,ip,4);
118  MD5(m_Random,56,hash);
119  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]);
120 #endif
121 #else
122  CAMsg::printMsg(LOG_DEBUG,"Inserting new IP-Address: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
123 #endif
124  entry=new IPLISTENTRY;
125  memcpy(entry->ip,ip,2);
126  entry->count=1;
127  entry->next=NULL;
128  m_HashTable[hashvalue]=entry;
129  ret = entry->count;
130 #ifdef DEBUG
131 #ifndef PSEUDO_LOG
132 #ifdef DEBUG
133  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]);
134 #endif
135 #else
136  CAMsg::printMsg(LOG_DEBUG,"New IP-Address inserted: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
137 #endif
138 #endif
139  m_pMutex->unlock();
140  return ret;
141  }
142  else
143  {//Hashkey in Hashtabelle gefunden --> suche in Ueberlaufliste nach Eintrag bzw. lege neuen Eitnrag an
144  PIPLIST last;
145  do
146  {
147  if(memcmp(entry->ip,ip,2)==0) //we have found the entry
148  {
149  #ifdef PSEUDO_LOG
150  CAMsg::printMsg(LOG_DEBUG,"Inserting IP-Address: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
151  #endif
152  if(entry->count>=m_allowedConnections) //an Attack...
153  {
154  //#if !defined(PSEUDO_LOG)&&defined(FIREWALL_SUPPORT)
155  CAMsg::printMsg(LOG_CRIT,"Possible flooding attack from: %u.%u.x.x !\n",ip[0],ip[1],ip[2],ip[3]);
156  //#endif
157  m_pMutex->unlock();
158  return E_UNKNOWN;
159  }
160  entry->count++;
161  ret = entry->count;
162  #ifdef PSEUDO_LOG
163  CAMsg::printMsg(LOG_DEBUG,"IP-Address inserted: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
164  #endif
165  m_pMutex->unlock();
166  return ret;
167  }
168  last=entry;
169  entry=entry->next;
170  } while(entry!=NULL);
171 //Nicht in der Ueberlaufliste gefunden
172  last->next=new IPLISTENTRY;
173  entry=last->next;
174  memcpy(entry->ip,ip,2);
175  entry->count=1;
176  entry->next=NULL;
177  ret = entry->count;
178  m_pMutex->unlock();
179  return ret;
180  }
181 #endif
182  }
183 
190  {
191 #ifdef PAYMENT
192  return E_SUCCESS;
193 #else
194  UINT16 hashvalue=(ip[2]<<8)|ip[3];
195  SINT32 ret;
196  m_pMutex->lock();
197  PIPLIST entry=m_HashTable[hashvalue];
198  if(entry==NULL)
199  {
200  m_pMutex->unlock();
201  CAMsg::printMsg(LOG_INFO,"Try to remove IP which is not in the hashtable of the IP-list - possible inconsistences in IPList!\n");
202  return 0;
203  }
204  else
205  {
206  PIPLIST before=NULL;
207  while(entry!=NULL)
208  {
209  if(memcmp(entry->ip,ip,2)==0)
210  {
211  entry->count--;
212  if(entry->count==0)
213  {
214  #ifndef PSEUDO_LOG
215  #if defined (_DEBUG)
216  UINT8 hash[16];
217  memcpy(m_Random,ip,4);
218  MD5(m_Random,56,hash);
219  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]);
220  #endif
221  #else
222  CAMsg::printMsg(LOG_DEBUG,"Removing IP-Address: {%u.%u.%u.%u} !\n",ip[0],ip[1],ip[2],ip[3]);
223  #endif
224  if(before==NULL)
225  m_HashTable[hashvalue]=entry->next;
226  else
227  before->next=entry->next;
228  delete entry;
229  entry = NULL;
230  m_pMutex->unlock();
231  return 0;
232  }
233  ret = entry->count;
234  m_pMutex->unlock();
235  return ret;
236  }
237  before=entry;
238  entry=entry->next;
239  }
240  m_pMutex->unlock();
241  CAMsg::printMsg(LOG_INFO,"Try to remove IP which is not in list - possible inconsistences in IPList!\n");
242  return 0;
243  }
244 #endif
245  }
246 #endif //ONLY_LOCAL_PROXY
struct _iplist_t IPLISTENTRY
Definition: CAIPList.hpp:32
#define MAX_IP_CONNECTIONS
The default value of allowed insertions, until insertIP() will return an error.
Definition: CAIPList.hpp:47
volatile PIPLIST VOLATILE_PIPLIST
Definition: CAIPList.hpp:34
SINT32 getRandom(UINT32 *val)
Gets 32 random bits.
Definition: CAUtil.cpp:346
unsigned short UINT16
Definition: basetypedefs.h:133
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
SINT32 insertIP(const UINT8 ip[4])
Inserts the IP-Address into the list.
Definition: CAIPList.cpp:103
~CAIPList()
Deletes the IPList and frees all used resources.
Definition: CAIPList.cpp:68
UINT32 m_allowedConnections
Definition: CAIPList.hpp:70
CAMutex * m_pMutex
Definition: CAIPList.hpp:76
volatile VOLATILE_PIPLIST * m_HashTable
Definition: CAIPList.hpp:71
CAIPList()
TODO: Fix LOG_TRAFFIC output which is not done anymore, as per default no log message are ommited....
Definition: CAIPList.cpp:39
SINT32 removeIP(const UINT8 ip[4])
Removes the IP-Address from the list.
Definition: CAIPList.cpp:189
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
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
This structure is used for building the IP-List.
Definition: CAIPList.hpp:40
UINT8 ip[2]
Next element, NULL if element is the last one.
Definition: CAIPList.hpp:42
VOLATILE_PIPLIST next
Definition: CAIPList.hpp:41
volatile UINT8 count
First two Bytes of the IP-Address.
Definition: CAIPList.hpp:43