Mixe for Privacy and Anonymity in the Internet
CAMuxSocket.hpp
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 
00029 #ifndef __CAMUXSOCKET__
00030 #define __CAMUXSOCKET__
00031 #include "CASocket.hpp"
00032 #include "CASymCipher.hpp"
00033 #include "CAMutex.hpp"
00034 #include "CATLSClientSocket.hpp"
00035 
00036 struct __t_hash_key_entry__
00037   {
00038     struct __t_hash_key_entry__* next;
00039     SINT32 hashkey;
00040   };
00041 
00042 typedef struct __t_hash_key_entry__ t_hashkeylistEntry;
00043 
00044 
00045 class CAMuxSocket
00046   {
00047     public:
00048       CAMuxSocket();
00049       ~CAMuxSocket();
00050 
00052       SINT32 getHashKey()
00053         {
00054           return m_pHashKeyEntry->hashkey;
00055         }
00056 
00057       SINT32 accept(UINT16 port);
00058       SINT32 accept(const CASocketAddr& oAddr);
00059       SINT32 connect(CASocketAddr& psa);
00060       SINT32 connect(CASocketAddr& psa,UINT retry,UINT32 time);
00061       SINT32 close();
00062       SINT32 send(MIXPACKET *pPacket);
00063       SINT32 send(MIXPACKET *pPacket,UINT8* buff);
00064       SINT32 prepareForSend(MIXPACKET* inoutPacket);
00065       SINT32 receive(MIXPACKET *pPacket);
00066       SINT32 receive(MIXPACKET *pPacket,UINT32 timeout);
00067       
00069       SINT32 receiveFully(UINT8* buff,UINT32 len)
00070       {
00071         return m_Socket.receiveFully(buff,len);
00072       }
00073         
00074       SINT32 receiveFully(UINT8* buff,UINT32 len, UINT32 msTimeOut)
00075       {
00076         return m_Socket.receiveFullyT(buff,len, msTimeOut);
00077       }
00078         
00079       //int close(HCHANNEL channel_id);
00080       //int close(HCHANNEL channel_id,UINT8* buff);
00081 #ifdef LOG_CRIME
00082       UINT32 sigCrime(HCHANNEL channel_id,MIXPACKET* sigPacket);
00083 #endif
00084       CASocket* getCASocket(){return &m_Socket;}
00085       SOCKET getSocket(){return m_Socket.getSocket();}
00086 
00087       SINT32 setCrypt(bool b);
00088       bool getIsEncrypted()
00089         {
00090           return m_bIsCrypted;
00091         }
00092 
00102       SINT32 setKey(UINT8* key,UINT32 keyLen)
00103         {
00104           if(keyLen==16)
00105             {
00106               m_oCipherIn.setKey(key);
00107               m_oCipherOut.setKey(key);
00108             }
00109           else if(keyLen==32)
00110             {
00111               m_oCipherOut.setKey(key);
00112               m_oCipherIn.setKey(key+16);
00113             }
00114         else
00115             return E_UNKNOWN;
00116           return E_SUCCESS;
00117         }
00118 
00119       SINT32 setSendKey(UINT8* key,UINT32 keyLen)
00120         {
00121           if(keyLen==16)
00122             {
00123               m_oCipherOut.setKey(key);
00124             }
00125           else if(keyLen==32)
00126             {
00127               m_oCipherOut.setKey(key);
00128               m_oCipherOut.setIVs(key+16);
00129             }
00130           else
00131             return E_UNKNOWN;
00132           return E_SUCCESS;
00133         }
00134 
00135       SINT32 setReceiveKey(UINT8* key,UINT32 keyLen)
00136       {
00137         if(keyLen==16)
00138         {
00139           m_oCipherIn.setKey(key);
00140         }
00141         else if(keyLen==32)
00142         {
00143           m_oCipherIn.setKey(key);
00144           m_oCipherIn.setIVs(key+16);
00145         }
00146         else
00147         {
00148           return E_UNKNOWN;
00149         }
00150         return E_SUCCESS;
00151       }
00152 
00153     private:
00154         CASocket    m_Socket;
00155         UINT32      m_aktBuffPos;
00156         UINT8*      m_Buff;
00157         CASymCipher   m_oCipherIn;
00158         CASymCipher   m_oCipherOut;
00159         bool      m_bIsCrypted;
00160         CAMutex     m_csSend;
00161         CAMutex     m_csReceive;
00162         t_hashkeylistEntry*     m_pHashKeyEntry;
00163 
00164         static t_hashkeylistEntry* ms_phashkeylistAvailableHashKeys; //stores he avilalbe hashkeys -> if this list is empty new entires are create on the fly
00165         static SINT32 ms_nMaxHashKeyValue; // the maximum value of a hash key
00166         static CAMutex* ms_pcsHashKeyList;
00167 
00168   };
00169 #endif