Mixe for Privacy and Anonymity in the Internet
CALastMixBChannelList.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2006, The JAP-Team 
00003  * All rights reserved.
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, 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
00011  *     notice, this list of conditions and the following disclaimer in the
00012  *     documentation and/or other materials provided with the distribution.
00013  *
00014  *   - Neither the name of the University of Technology Dresden, Germany nor
00015  *     the names of its contributors may be used to endorse or promote
00016  *     products derived from this software without specific prior written
00017  *     permission. 
00018  *
00019  *  
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
00022  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00023  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
00024  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030  * POSSIBILITY OF SUCH DAMAGE
00031  */
00032 #include "../StdAfx.h"
00033 #include "CALastMixBChannelList.hpp"
00034 #include "../CAUtil.hpp"
00035 
00036 CALastMixBChannelList::CALastMixBChannelList() {
00037   m_pChannelTable=new (t_lastMixBChannelListEntry*[0x10000]);
00038   for (UINT32 i = 0; i < 0x10000; i++) {
00039     m_pChannelTable[i] = NULL;
00040   }
00041   m_channelTableSize = 0;
00042   m_pMutex = new CAMutex();
00043 }
00044 
00045 CALastMixBChannelList::~CALastMixBChannelList() {
00046   delete m_pMutex;
00047   m_pMutex = NULL;
00048   delete []m_pChannelTable;
00049   m_pChannelTable = NULL;
00050 }
00051 
00052 t_lastMixBChannelListEntry* CALastMixBChannelList::add(HCHANNEL a_channelId, CASymCipher* a_channelCipher, CAChain* a_associatedChain) {
00053   m_pMutex->lock();
00054   /* check whether we have not already an entry with the same ID */
00055   if (getEntryInternal(a_channelId) != NULL) {
00056     m_pMutex->unlock();
00057     return NULL;
00058   }
00059   t_lastMixBChannelListEntry* newEntry = new t_lastMixBChannelListEntry;
00060   newEntry->channelId = a_channelId;
00061   newEntry->channelCipher = a_channelCipher;
00062   newEntry->associatedChain = a_associatedChain;
00063   newEntry->associatedChannelList = this;
00064   newEntry->firstResponseDeadline = NULL;
00065   /* take the lower 16 bits of the ID as key for the hashtable */
00066   UINT16 hashKey = (UINT16)(a_channelId & 0x0000FFFF);
00067   /* now add the entry at the begin of the hashtable-line */
00068   newEntry->rightEntry = m_pChannelTable[hashKey];
00069   newEntry->rightEntryPointerOfLeftEntry = &(m_pChannelTable[hashKey]);
00070   m_pChannelTable[hashKey] = newEntry;
00071   if (newEntry->rightEntry != NULL) {
00072     newEntry->rightEntry->rightEntryPointerOfLeftEntry = &(newEntry->rightEntry);
00073   }
00074   m_channelTableSize++;
00075   m_pMutex->unlock();
00076   return newEntry;
00077 }
00078 
00079 void CALastMixBChannelList::removeFromTable(t_lastMixBChannelListEntry* a_channelEntry) {
00080   m_pMutex->lock();
00081   if (a_channelEntry->associatedChannelList == this) {
00082     /* only remove entries which are in the table */
00083     *(a_channelEntry->rightEntryPointerOfLeftEntry) = a_channelEntry->rightEntry;
00084     if (a_channelEntry->rightEntry != NULL) {
00085       a_channelEntry->rightEntry->rightEntryPointerOfLeftEntry = a_channelEntry->rightEntryPointerOfLeftEntry;
00086     }
00087     a_channelEntry->rightEntry = NULL;
00088     a_channelEntry->rightEntryPointerOfLeftEntry = NULL;
00089     a_channelEntry->associatedChannelList = NULL;
00090   }
00091   m_pMutex->unlock();
00092 }
00093 
00094 t_lastMixBChannelListEntry* CALastMixBChannelList::get(HCHANNEL a_channelId) {
00095   t_lastMixBChannelListEntry* returnedEntry = NULL;
00096   m_pMutex->lock();
00097   returnedEntry = getEntryInternal(a_channelId);
00098   m_pMutex->unlock();
00099   return returnedEntry;
00100 }
00101 
00102 
00103 t_lastMixBChannelListEntry* CALastMixBChannelList::getEntryInternal(HCHANNEL a_channelId) {
00104   /* mutex must be already locked */
00105   /* take the lower 16 bits of the ID as key for the hashtable */
00106   UINT16 hashKey = (UINT16)(a_channelId & 0x0000FFFF);
00107   bool entryFound = false;
00108   t_lastMixBChannelListEntry* currentEntry = m_pChannelTable[hashKey];
00109   while ((currentEntry != NULL) && !entryFound) {
00110     /* compare the Channel-IDs */
00111     if (currentEntry->channelId == a_channelId) {
00112       /* we have found the entry */
00113       entryFound = true;
00114     }
00115     else {
00116       currentEntry = currentEntry->rightEntry;
00117     }
00118   }
00119   return currentEntry;
00120 }