Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CAChainTable Class Reference

#include <CAChainTable.hpp>

Collaboration diagram for CAChainTable:

Public Member Functions

 CAChainTable (void)
 
 ~CAChainTable (void)
 
CAChaingetEntry (UINT8 *a_chainId)
 
CAChaincreateEntry ()
 
void deleteEntry (UINT8 *a_chainId)
 
UINT32 getSize ()
 
CAChaingetFirstEntry ()
 
CAChaingetNextEntry ()
 

Private Member Functions

t_chaintableEntrygetEntryInternal (UINT8 *a_chainId)
 
void removeEntryInternal (t_chaintableEntry *a_entry)
 
void getNextEntryInternal (t_chaintableIterator *a_iterator)
 

Private Attributes

t_chaintableEntry ** m_pChainTable
 
CAMutexm_pMutex
 
UINT32 m_chaintableSize
 
t_chaintableIteratorm_pChaintableIterator
 

Detailed Description

Definition at line 52 of file CAChainTable.hpp.

Constructor & Destructor Documentation

◆ CAChainTable()

CAChainTable::CAChainTable ( void  )

Definition at line 39 of file CAChainTable.cpp.

39  {
40  m_pChainTable = new (t_chaintableEntry*[0x10000]);
41  for (UINT32 i = 0; i < 0x10000; i++) {
42  m_pChainTable[i] = NULL;
43  }
44  m_pMutex = new CAMutex();
45  m_chaintableSize = 0;
46  m_pChaintableIterator = NULL;
47  #ifdef DELAY_CHANNELS
48  m_initialBucketSize = DELAY_CHANNEL_TRAFFIC;
49  m_delayBucketGrow = DELAY_BUCKET_GROW;
50  m_delayBucketGrowInterval = DELAY_BUCKET_GROW_INTERVALL;
51  m_pDelayBucketMutex = new CAMutex();
52  m_pDelayBuckets = new SINT32[MAX_POLLFD];
53  for (UINT32 i = 0; i < MAX_POLLFD; i++) {
54  m_pDelayBuckets[i] = -1;
55  }
56  /* start the delay-buckets-refill loop */
57  m_delayBucketsLoopRun = true;
58  m_pDelayBucketsLoop = new CAThread((UINT8*)"CAChainTable: delay-buckets refill thread");
59  m_pDelayBucketsLoop->setMainLoop(lml_chaintableDelayBucketsLoop);
60  m_pDelayBucketsLoop->start(this);
61  #endif
62 }
#define MAX_POLLFD
Definition: StdAfx.h:192
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
t_chaintableIterator * m_pChaintableIterator
CAMutex * m_pMutex
UINT32 m_chaintableSize
t_chaintableEntry ** m_pChainTable

References m_chaintableSize, m_pChainTable, m_pChaintableIterator, m_pMutex, and MAX_POLLFD.

◆ ~CAChainTable()

CAChainTable::~CAChainTable ( void  )

Definition at line 64 of file CAChainTable.cpp.

64  {
65  /* use the iterator to clean the table */
66  getFirstEntry();
67  while (m_pChaintableIterator != NULL) {
68  /* chaintable iterator is removed, if last entry is reached */
70  /* entry is removed automatically, when the iterator points to the next
71  * entry
72  */
73  getNextEntry();
74  }
75  #ifdef DELAY_CHANNELS
76  m_delayBucketsLoopRun = false;
77  /* wait for the delay-buckets-refill loop */
78  m_pDelayBucketsLoop->join();
79  delete m_pDelayBucketsLoop;
80  m_pDelayBucketsLoop = NULL;
81  delete m_pDelayBucketMutex;
82  m_pDelayBucketMutex = NULL;
83  delete []m_pDelayBuckets;
84  m_pDelayBuckets = NULL;
85  #endif
86  delete m_pMutex;
87  m_pMutex = NULL;
88  delete []m_pChainTable;
89  m_pChainTable = NULL;
90 }
CAChain * getNextEntry()
CAChain * getFirstEntry()

References getFirstEntry(), getNextEntry(), m_pChainTable, m_pChaintableIterator, m_pMutex, and t_chaintableIterator::removeEntry.

Here is the call graph for this function:

Member Function Documentation

◆ createEntry()

CAChain * CAChainTable::createEntry ( )

Definition at line 172 of file CAChainTable.cpp.

172  {
173  m_pMutex->lock();
174  if (m_chaintableSize >= MAX_POLLFD) {
175  /* we cannot create more chains because we are not able to handle more
176  * sockets
177  */
178  m_pMutex->unlock();
179  return NULL;
180  }
181  /* create a unique chain-id */
183  do {
185  m_pMutex->unlock();
186  delete []chainId;
187  chainId = NULL;
188  return NULL;
189  }
190  }
191  while (getEntryInternal(chainId) != NULL);
192  t_chaintableEntry* newEntry = new t_chaintableEntry;
193  #ifndef DELAY_CHANNELS
194  newEntry->chain = new CAChain(chainId);
195  #else
196  /* find an unused delay-bucket */
197  m_pDelayBucketMutex->lock();
198  UINT32 i = 0;
199  bool bucketFound = false;
200  while ((!bucketFound) && (i < MAX_POLLFD)) {
201  if (m_pDelayBuckets[i] == -1) {
202  bucketFound = true;
203  }
204  else {
205  i++;
206  }
207  }
208  if (!bucketFound) {
209  /* we found no bucket -> this shouldn't happen because there cannot be
210  * more chains than buckets
211  */
212  m_pDelayBucketMutex->unlock();
213  delete newEntry;
214  newEntry = NULL;
215  delete []chainId;
216  chainId = NULL;
217  m_pMutex->unlock();
218  return NULL;
219  }
220  /* initalize our bucket */
221  m_pDelayBuckets[i] = (SINT32)m_initialBucketSize;
222  m_pDelayBucketMutex->unlock();
223  newEntry->chain = new CAChain(chainId, m_pDelayBucketMutex, &(m_pDelayBuckets[i]));
224  #endif
225  /* take the lower 16 bits as key for the hashtable */
226  UINT16 hashKey = (((UINT16)(chainId[CHAIN_ID_LENGTH - 2])) << 8) | (UINT16)(chainId[CHAIN_ID_LENGTH - 1]);
227  /* now add the entry at the begin of the hashtable-line */
228  newEntry->rightEntry = m_pChainTable[hashKey];
229  newEntry->rightEntryPointerOfLeftEntry = &(m_pChainTable[hashKey]);
230  m_pChainTable[hashKey] = newEntry;
231  if (newEntry->rightEntry != NULL) {
232  newEntry->rightEntry->rightEntryPointerOfLeftEntry = &(newEntry->rightEntry);
233  }
235  m_pMutex->unlock();
236  return (newEntry->chain);
237 }
SINT32 getRandom(UINT32 *val)
Gets 32 random bits.
Definition: CAUtil.cpp:346
unsigned short UINT16
Definition: basetypedefs.h:133
t_chaintableEntry * getEntryInternal(UINT8 *a_chainId)
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
CAChain * chain
t_chaintableEntry * rightEntry
t_chaintableEntry ** rightEntryPointerOfLeftEntry
UINT8 chainId[CHAIN_ID_LENGTH]
Definition: typedefsb.hpp:0
#define CHAIN_ID_LENGTH
Definition: typedefsb.hpp:40

References t_chaintableEntry::chain, CHAIN_ID_LENGTH, chainId, E_SUCCESS, getEntryInternal(), getRandom(), CAMutex::lock(), m_chaintableSize, m_pChainTable, m_pMutex, MAX_POLLFD, t_chaintableEntry::rightEntry, t_chaintableEntry::rightEntryPointerOfLeftEntry, and CAMutex::unlock().

Referenced by CALastMixB::loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ deleteEntry()

void CAChainTable::deleteEntry ( UINT8 a_chainId)

Definition at line 103 of file CAChainTable.cpp.

103  {
104  m_pMutex->lock();
105  t_chaintableEntry* chaintableEntry = getEntryInternal(a_chainId);
106  if (chaintableEntry != NULL) {
107  /* we have found an entry with the specified ID -> check whether the
108  * iterator points to it
109  */
110  if (m_pChaintableIterator != NULL) {
111  if (m_pChaintableIterator->currentEntry == chaintableEntry) {
112  /* don't remove the entry but set the remove-flag for the iterator */
114  }
115  else {
116  removeEntryInternal(chaintableEntry);
117  }
118  }
119  else {
120  removeEntryInternal(chaintableEntry);
121  }
122  }
123  m_pMutex->unlock();
124 }
void removeEntryInternal(t_chaintableEntry *a_entry)
t_chaintableEntry * currentEntry

References t_chaintableIterator::currentEntry, getEntryInternal(), CAMutex::lock(), m_pChaintableIterator, m_pMutex, t_chaintableIterator::removeEntry, removeEntryInternal(), and CAMutex::unlock().

Referenced by CALastMixB::loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getEntry()

CAChain * CAChainTable::getEntry ( UINT8 a_chainId)

Definition at line 92 of file CAChainTable.cpp.

92  {
93  m_pMutex->lock();
94  CAChain* returnedChain = NULL;
95  t_chaintableEntry* chaintableEntry = getEntryInternal(a_chainId);
96  if (chaintableEntry != NULL) {
97  returnedChain = chaintableEntry->chain;
98  }
99  m_pMutex->unlock();
100  return returnedChain;
101 }

References t_chaintableEntry::chain, getEntryInternal(), CAMutex::lock(), m_pMutex, and CAMutex::unlock().

Referenced by CALastMixB::loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getEntryInternal()

t_chaintableEntry * CAChainTable::getEntryInternal ( UINT8 a_chainId)
private

Definition at line 248 of file CAChainTable.cpp.

248  {
249  /* mutex must be already locked */
250  /* take the lower 16 bits as key for the hashtable */
251  UINT16 hashKey = (((UINT16)(a_chainId[CHAIN_ID_LENGTH - 2])) << 8) | (UINT16)(a_chainId[CHAIN_ID_LENGTH - 1]);
252  bool entryFound = false;
253  t_chaintableEntry* currentEntry = m_pChainTable[hashKey];
254  while ((currentEntry != NULL) && !entryFound) {
255  /* a removed entry could be in the table, if the iterator points to it ->
256  * we have to check it
257  */
258  bool entryVirtualRemoved = false;
259  if (m_pChaintableIterator != NULL) {
261  entryVirtualRemoved = true;
262  /* skip this entry */
263  currentEntry = currentEntry->rightEntry;
264  }
265  }
266  if (!entryVirtualRemoved) {
267  /* compare the Chain-IDs (lower 2 bytes are identical because of the same
268  * hashkey -> don't compare them)
269  */
270  if (memcmp(currentEntry->chain->getChainId(), a_chainId, CHAIN_ID_LENGTH - 2) == 0) {
271  /* we have found the entry */
272  entryFound = true;
273  }
274  else {
275  currentEntry = currentEntry->rightEntry;
276  }
277  }
278  }
279  return currentEntry;
280 }
UINT8 * getChainId()
Definition: CAChain.cpp:123

References t_chaintableEntry::chain, CHAIN_ID_LENGTH, t_chaintableIterator::currentEntry, CAChain::getChainId(), m_pChainTable, m_pChaintableIterator, t_chaintableIterator::removeEntry, and t_chaintableEntry::rightEntry.

Referenced by createEntry(), deleteEntry(), and getEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getFirstEntry()

CAChain * CAChainTable::getFirstEntry ( )

Definition at line 126 of file CAChainTable.cpp.

126  {
127  CAChain* firstChain = NULL;
128  m_pMutex->lock();
129  if (m_pChaintableIterator == NULL) {
131  }
132  else {
133  /* look whether we shall remove our last entry */
136  }
137  }
142  if (m_pChaintableIterator->currentEntry == NULL) {
143  /* no entries in the table */
144  delete m_pChaintableIterator;
145  m_pChaintableIterator = NULL;
146  }
147  else {
149  }
150  m_pMutex->unlock();
151  return firstChain;
152 }
void getNextEntryInternal(t_chaintableIterator *a_iterator)

References t_chaintableEntry::chain, t_chaintableIterator::currentEntry, getNextEntryInternal(), CAMutex::lock(), m_pChaintableIterator, m_pMutex, t_chaintableIterator::nextHashkey, t_chaintableIterator::removeEntry, removeEntryInternal(), and CAMutex::unlock().

Referenced by CALastMixB::loop(), and ~CAChainTable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNextEntry()

CAChain * CAChainTable::getNextEntry ( )

Definition at line 154 of file CAChainTable.cpp.

154  {
155  CAChain* nextChain = NULL;
156  m_pMutex->lock();
157  if (m_pChaintableIterator != NULL) {
159  if (m_pChaintableIterator->currentEntry == NULL) {
160  /* no more entries in the table */
161  delete m_pChaintableIterator;
162  m_pChaintableIterator = NULL;
163  }
164  else {
166  }
167  }
168  m_pMutex->unlock();
169  return nextChain;
170 }

References t_chaintableEntry::chain, t_chaintableIterator::currentEntry, getNextEntryInternal(), CAMutex::lock(), m_pChaintableIterator, m_pMutex, and CAMutex::unlock().

Referenced by CALastMixB::loop(), and ~CAChainTable().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getNextEntryInternal()

void CAChainTable::getNextEntryInternal ( t_chaintableIterator a_iterator)
private

Definition at line 297 of file CAChainTable.cpp.

297  {
298  /* mutex must be already locked */
299  t_chaintableEntry* nextEntry;
300  if (a_iterator->currentEntry == NULL) {
301  nextEntry = NULL;
302  }
303  else {
304  nextEntry = a_iterator->currentEntry->rightEntry;
305  /* look whether we shall remove our last entry */
306  if (a_iterator->removeEntry) {
307  removeEntryInternal(a_iterator->currentEntry);
308  a_iterator->removeEntry = false;
309  }
310  }
311  if (nextEntry == NULL) {
312  /* we have to start at the current hashtable-line until we find a
313  * hashtable-line with entries or reach again the top of the table
314  */
315  do {
316  a_iterator->currentEntry = m_pChainTable[a_iterator->nextHashkey];
317  (a_iterator->nextHashkey)++;
318  }
319  while ((a_iterator->currentEntry == NULL) && (a_iterator->nextHashkey != 0));
320  if (a_iterator->nextHashkey == 0) {
321  /* we have reached the top of the table again */
322  a_iterator->currentEntry = NULL;
323  }
324  }
325  else {
326  /* we have another entry in the same hashtable-line */
327  a_iterator->currentEntry = nextEntry;
328  }
329 }

References t_chaintableIterator::currentEntry, m_pChainTable, t_chaintableIterator::nextHashkey, t_chaintableIterator::removeEntry, removeEntryInternal(), and t_chaintableEntry::rightEntry.

Referenced by getFirstEntry(), and getNextEntry().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ getSize()

UINT32 CAChainTable::getSize ( )

Definition at line 239 of file CAChainTable.cpp.

239  {
240  UINT32 chaintableSize;
241  m_pMutex->lock();
242  chaintableSize = m_chaintableSize;
243  m_pMutex->unlock();
244  return chaintableSize;
245 }

References CAMutex::lock(), m_chaintableSize, m_pMutex, and CAMutex::unlock().

Referenced by CALastMixB::loop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ removeEntryInternal()

void CAChainTable::removeEntryInternal ( t_chaintableEntry a_entry)
private

Definition at line 282 of file CAChainTable.cpp.

282  {
283  /* mutex must be already locked */
284  *(a_entry->rightEntryPointerOfLeftEntry) = a_entry->rightEntry;
285  if (a_entry->rightEntry != NULL) {
287  }
288  /* delete the chain */
289  delete a_entry->chain;
290  a_entry->chain = NULL;
291  /* delete the table-entry */
292  delete a_entry;
293  a_entry = NULL;
295 }

References t_chaintableEntry::chain, m_chaintableSize, t_chaintableEntry::rightEntry, and t_chaintableEntry::rightEntryPointerOfLeftEntry.

Referenced by deleteEntry(), getFirstEntry(), and getNextEntryInternal().

Here is the caller graph for this function:

Member Data Documentation

◆ m_chaintableSize

UINT32 CAChainTable::m_chaintableSize
private

Definition at line 69 of file CAChainTable.hpp.

Referenced by CAChainTable(), createEntry(), getSize(), and removeEntryInternal().

◆ m_pChainTable

t_chaintableEntry** CAChainTable::m_pChainTable
private

◆ m_pChaintableIterator

t_chaintableIterator* CAChainTable::m_pChaintableIterator
private

◆ m_pMutex

CAMutex* CAChainTable::m_pMutex
private

The documentation for this class was generated from the following files: