Mixe for Privacy and Anonymity in the Internet
CATempIPBlockList.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 
30 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_FIRST_MIX
31 
32 #include "CATempIPBlockList.hpp"
33 #include "CAUtil.hpp"
34 #include "CAMsg.hpp"
35 
36 
38  {
39  m_validTimeMillis = validTimeMillis;
40  m_iEntries = 0;
41 
42  m_hashTable=new PTEMPIPBLOCKLIST[0x10000];
43  memset(m_hashTable,0,0x10000*sizeof(PTEMPIPBLOCKLIST));
44 
45  m_pMutex = new CAMutex();
46 
47  // launch cleanup thread
48  m_pCleanupThread = new CAThread((UINT8*)"Cleanup Thread");
51  m_pCleanupThread->start(this);
52  }
53 
54 
55 
57  {
58  CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList terminating...\n");
59  //Now stop the cleanup thread...
60  m_bRunCleanupThread=false;
61  m_pCleanupThread->join(); //wait for cleanupthread to wakeup and exit
62  delete m_pCleanupThread;
63  m_pCleanupThread=NULL;
64  m_pMutex->lock();
65  //its safe to delete it because we have the lock...
66  for(UINT32 i=0;i<=0xFFFF;i++)
67  {
69  PTEMPIPBLOCKLIST tmpEntry;
70  while(entry!=NULL)
71  {
72  tmpEntry=entry;
73  entry=entry->next;
74  delete tmpEntry;
75  tmpEntry = NULL;
76  }
77  }
78  delete [] m_hashTable;
79  m_hashTable = NULL;
80  m_pMutex->unlock();
81  delete m_pMutex;
82  m_pMutex = NULL;
83  CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList terminated!\n");
84  }
85 
86 
87 
95 {
96  UINT64 now;
98 
100  memcpy(newEntry->ip,ip,2);
101  newEntry->validTimeMillis = now + m_validTimeMillis;
102  newEntry->next=NULL;
103 
104  UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
105  m_pMutex->lock();
106 
107  if(m_hashTable[hashvalue]==NULL) {
108  m_hashTable[hashvalue] = newEntry;
109  m_iEntries++;
110  }
111  else
112  {
113  PTEMPIPBLOCKLIST temp = m_hashTable[hashvalue];
114  for(;;)
115  {
116  if(memcmp(temp->ip,ip,2)==0)
117  {
118  // we have found the entry
119  delete newEntry;
120  m_pMutex->unlock();
121  return E_UNKNOWN;
122  }
123  if (temp->next)
124  {
125  temp = temp->next;
126  }
127  else
128  {
129  temp->next = newEntry;
130  m_iEntries++;
131  break;
132  }
133  }
134  }
135  m_pMutex->unlock();
136  return E_SUCCESS;
137 }
138 
139 
140 
148 {
149  UINT16 hashvalue=((ip[2]<<8)|ip[3]) % 0x10000;
150  m_pMutex->lock();
151  PTEMPIPBLOCKLIST entry = m_hashTable[hashvalue];
152  PTEMPIPBLOCKLIST previous = NULL;
153  while(entry) {
154  if(memcmp(entry->ip,ip,2)==0) {
155  // we have found the entry
156  // additional check: is it still valid?
157  UINT64 now;
159  if(entry->validTimeMillis <= now)
160  {
161  // entry can be removed
162  if(previous==NULL) {
163  m_hashTable[hashvalue] = entry->next;
164  }
165  else {
166  previous->next = entry->next;
167  }
168  delete entry;
169  entry = NULL;
170  m_iEntries--;
171  m_pMutex->unlock();
172  return E_SUCCESS;
173  }
174  else
175  {
176  m_pMutex->unlock();
177  return E_UNKNOWN;
178  }
179  }
180  previous = entry;
181  entry = entry->next;
182  }
183  m_pMutex->unlock();
184  return E_SUCCESS;
185 }
186 
187 
188 
193  {
194  INIT_STACK;
195  BEGIN_STACK("CATempIPBlockList::cleanupThreadMainLoop");
196 
197  CATempIPBlockList * instance;
198  instance = (CATempIPBlockList *)param;
199  while(instance->m_bRunCleanupThread)
200  {
201  // do cleanup
202  UINT64 now;
204  instance->m_pMutex->lock();
205  for(UINT32 i=0;i<=0xFFFF;i++)
206  {
207  PTEMPIPBLOCKLIST entry=instance->m_hashTable[i];
208  PTEMPIPBLOCKLIST previous = NULL;
209  while(entry!=NULL)
210  {
211  if(entry->validTimeMillis <= now)
212  {
213  // entry can be removed
214  if(previous==NULL)
215  {
216  CAMsg::printMsg(LOG_DEBUG, "CATmpIPBlockList: removing entry...\n");
217  instance->m_hashTable[i] = entry->next;
218  previous=entry->next;
219  delete entry;
220  entry=previous;
221  previous=NULL;
222  }
223  else
224  {
225  previous->next = entry->next;
226  delete entry;
227  entry = previous->next;
228  }
229 
230  instance->m_iEntries--;
231  }
232  else
233  {
234  // entry is still valid
235  previous = entry;
236  entry = entry->next;
237  }
238  }
239  }
240  instance->m_pMutex->unlock();
241 
242  // let the thread sleep for 1 minute
244  }
245 
246  FINISH_STACK("CATempIPBlockList::cleanupThreadMainLoop");
247 
249 }
250 #endif //ONLY_LOCAL_PROXY
struct _tempipblocklist_t TEMPIPBLOCKLISTENTRY
#define INIT_STACK
Definition: CAThread.hpp:48
#define BEGIN_STACK(methodName)
Definition: CAThread.hpp:49
#define FINISH_STACK(methodName)
Definition: CAThread.hpp:50
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:252
SINT32 sSleep(UINT32 sec)
Sleeps sec Seconds.
Definition: CAUtil.cpp:425
#define THREAD_RETURN
Definition: StdAfx.h:540
#define THREAD_RETURN_SUCCESS
Definition: StdAfx.h:542
#define CLEANUP_THREAD_SLEEP_INTERVAL
Definition: StdAfx.h:217
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
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
The purpose of this class is storing the IPs of JAP users who tried to hack/attack the payment system...
UINT64 m_validTimeMillis
the time that each blocked IP should stay in the List
CAMutex * m_pMutex
Used for locking the datastructure to make it threadsafe.
CAThread * m_pCleanupThread
this thread cleans up the hashtable and removes old entries
PTEMPIPBLOCKLIST * m_hashTable
the buffer where the entries are stored
SINT32 insertIP(const UINT8 ip[4])
inserts an IP into the blocklist
volatile bool m_bRunCleanupThread
as long as true the clenaupthread does his job.
CATempIPBlockList(UINT64 validTimeMillis)
static THREAD_RETURN cleanupThreadMainLoop(void *param)
the cleanup thread main loop
SINT32 checkIP(const UINT8 ip[4])
check whether an IP is blocked
SINT32 start(void *param, bool bDaemon=false, bool bSilent=false)
Starts the execution of the main function of this thread.
Definition: CAThread.cpp:115
SINT32 setMainLoop(THREAD_MAIN_TYP fnc)
Sets the main function which will be executed within this thread.
Definition: CAThread.hpp:148
SINT32 join()
Waits for the main function to finish execution.
Definition: CAThread.cpp:187
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
struct _tempipblocklist_t * next
Next element, NULL if element is the last one.
UINT64 validTimeMillis
Entry is valid until getCurrentTimeMillis() > validTimeMillis.
UINT8 ip[2]
First two Bytes of the IP-Address.