Mixe for Privacy and Anonymity in the Internet
CAPool.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 #include "CAPool.hpp"
00030 #include "CAUtil.hpp"
00031 #include "CAMsg.hpp"
00032 #ifdef USE_POOL
00033 CAPool::CAPool(UINT32 poolsize)
00034   {
00035     m_uPoolSize=poolsize;
00036     m_uRandMax=0xFFFFFFFF;
00037     m_uRandMax-=m_uRandMax%m_uPoolSize;
00038     m_pPoolList=new tPoolListEntry;
00039     getRandom(m_pPoolList->poolentry.packet.data,DATA_SIZE);
00040     m_pPoolList->poolentry.packet.flags=CHANNEL_DUMMY;
00041     m_pPoolList->poolentry.packet.channel=DUMMY_CHANNEL;
00042 #ifdef LOG_PACKET_TIMES
00043     setZero64(m_pPoolList->poolentry.timestamp_proccessing_start);
00044     setZero64(m_pPoolList->poolentry.pool_timestamp_in);
00045 #endif        
00046     m_pPoolList->next=NULL;
00047     m_pLastEntry=m_pPoolList;
00048     m_arChannelIDs=new HCHANNEL[poolsize];
00049     m_arChannelIDs[0]=0;
00050     for(UINT32 i=1;i<poolsize;i++)
00051       {
00052         tPoolListEntry* tmpEntry=new tPoolListEntry;
00053         getRandom(tmpEntry->poolentry.packet.data,DATA_SIZE);
00054         tmpEntry->poolentry.packet.flags=CHANNEL_DUMMY;
00055         tmpEntry->poolentry.packet.channel=DUMMY_CHANNEL;
00056         #ifdef LOG_PACKET_TIMES
00057           setZero64(tmpEntry->poolentry.timestamp_proccessing_start);
00058           setZero64(tmpEntry->poolentry.pool_timestamp_in);
00059         #endif        
00060         tmpEntry->next=m_pPoolList;
00061         m_pPoolList=tmpEntry;
00062         m_arChannelIDs[i]=0;
00063       }
00064     m_pEntry=new tPoolListEntry;  
00065   }
00066   
00067 CAPool::~CAPool()
00068   {
00069     tPoolListEntry* tmpEntry;
00070     while(m_pPoolList!=NULL)
00071       {
00072         tmpEntry=m_pPoolList;
00073         m_pPoolList=m_pPoolList->next;
00074         delete tmpEntry;
00075         tmpEntry = NULL;
00076       }
00077     delete m_pEntry;
00078     m_pEntry = NULL;
00079     delete[] m_arChannelIDs;
00080     m_arChannelIDs = NULL;
00081   }
00082   
00083 SINT32 CAPool::pool(tPoolEntry* pPoolEntry)
00084   {
00085     UINT32 v;
00086     for(;;)
00087       {
00088         getRandom(&v);
00089         if(v<m_uRandMax)
00090           {
00091             v%=m_uPoolSize;
00092             break;
00093           }
00094       }
00095     HCHANNEL id=m_arChannelIDs[v];
00096     m_arChannelIDs[v]=pPoolEntry->packet.channel;
00097     tPoolListEntry* pPrevEntry=NULL;    
00098     tPoolListEntry* pEntryOut=m_pPoolList;
00099     while(pEntryOut->poolentry.packet.channel!=id)
00100       {
00101         pPrevEntry=pEntryOut;
00102         pEntryOut=pEntryOut->next;  
00103       }
00104 
00105     if(pEntryOut==m_pPoolList) //first element to remove)
00106       {
00107         memcpy(&m_pEntry->poolentry,pPoolEntry,sizeof(tPoolEntry));
00108         memcpy(pPoolEntry,&pEntryOut->poolentry,sizeof(tPoolEntry));
00109         m_pPoolList=m_pPoolList->next;
00110         m_pLastEntry->next=m_pEntry;
00111         m_pLastEntry=m_pEntry;
00112         m_pLastEntry->next=NULL;
00113         m_pEntry=pEntryOut;
00114       }
00115     else
00116       {
00117         memcpy(&m_pEntry->poolentry,pPoolEntry,sizeof(tPoolEntry));
00118         memcpy(pPoolEntry,&pEntryOut->poolentry,sizeof(tPoolEntry));
00119         m_pLastEntry->next=m_pEntry;
00120         m_pLastEntry=m_pEntry;
00121         m_pLastEntry->next=NULL;
00122         pPrevEntry->next=pEntryOut->next;
00123         m_pEntry=pEntryOut;   
00124       }   
00125     return E_SUCCESS;
00126   }
00127 #endif