Mixe for Privacy and Anonymity in the Internet
Public Member Functions | Private Attributes | Static Private Attributes
CAMuxSocket Class Reference

#include <CAMuxSocket.hpp>

Collaboration diagram for CAMuxSocket:
[legend]

List of all members.

Public Member Functions

 CAMuxSocket ()
 ~CAMuxSocket ()
SINT32 getHashKey ()
 Returns a Hashkey which uniquely identifies this socket.
SINT32 accept (UINT16 port)
SINT32 accept (const CASocketAddr &oAddr)
 Waits for an incoming connection on oAddr.
SINT32 connect (CASocketAddr &psa)
SINT32 connect (CASocketAddr &psa, UINT retry, UINT32 time)
SINT32 close ()
 Closes the underlying socket.
SINT32 send (MIXPACKET *pPacket)
 Sends a MixPacket over the Network.
SINT32 send (MIXPACKET *pPacket, UINT8 *buff)
SINT32 prepareForSend (MIXPACKET *inoutPacket)
SINT32 receive (MIXPACKET *pPacket)
 Receives a whole MixPacket.
SINT32 receive (MIXPACKET *pPacket, UINT32 timeout)
 Trys to receive a Mix-Packet.
SINT32 receiveFully (UINT8 *buff, UINT32 len)
 Receives some "plain" bytes from the underlying socket - just a convenient function...
SINT32 receiveFully (UINT8 *buff, UINT32 len, UINT32 msTimeOut)
CASocketgetCASocket ()
SOCKET getSocket ()
SINT32 setCrypt (bool b)
bool getIsEncrypted ()
SINT32 setKey (UINT8 *key, UINT32 keyLen)
 Sets the symmetric keys used for de-/encrypting the Mux connection.
SINT32 setSendKey (UINT8 *key, UINT32 keyLen)
SINT32 setReceiveKey (UINT8 *key, UINT32 keyLen)

Private Attributes

CASocket m_Socket
UINT32 m_aktBuffPos
UINT8m_Buff
CASymCipher m_oCipherIn
CASymCipher m_oCipherOut
bool m_bIsCrypted
CAMutex m_csSend
CAMutex m_csReceive
t_hashkeylistEntrym_pHashKeyEntry

Static Private Attributes

static t_hashkeylistEntryms_phashkeylistAvailableHashKeys = NULL
static SINT32 ms_nMaxHashKeyValue = 0
static CAMutexms_pcsHashKeyList = new CAMutex()

Detailed Description

Definition at line 45 of file CAMuxSocket.hpp.


Constructor & Destructor Documentation

Definition at line 62 of file CAMuxSocket.cpp.

References close(), CAMutex::lock(), m_Buff, m_pHashKeyEntry, ms_pcsHashKeyList, ms_phashkeylistAvailableHashKeys, __t_hash_key_entry__::next, and CAMutex::unlock().

Here is the call graph for this function:


Member Function Documentation

Definition at line 84 of file CAMuxSocket.cpp.

References CASocket::accept(), CASocket::close(), CASocket::create(), E_SUCCESS, E_UNKNOWN, CASocket::listen(), m_aktBuffPos, m_Socket, and CASocket::setReuseAddr().

Referenced by CAMiddleMix::init(), and CALastMix::init().

  {
    CASocket oSocket;
    oSocket.create();
    oSocket.setReuseAddr(true);
    if(oSocket.listen(port)!=E_SUCCESS)
      return E_UNKNOWN;
    if(oSocket.accept(m_Socket)!=E_SUCCESS)
      return E_UNKNOWN;
    oSocket.close();
    //m_Socket.setRecvLowWat(MIXPACKET_SIZE);
    m_aktBuffPos=0;
    return E_SUCCESS;
  }

Here is the call graph for this function:

Waits for an incoming connection on oAddr.

Return values:
E_SUCCESS,ifsuccessful
E_SOCKET_BIND,E_SOCKET_LISTEN
E_UNKOWN

Definition at line 104 of file CAMuxSocket.cpp.

References CASocket::accept(), CASocket::close(), CASocket::create(), E_SUCCESS, E_UNKNOWN, CASocketAddr::getType(), CASocket::listen(), m_aktBuffPos, m_Socket, and CASocket::setReuseAddr().

  {
    CASocket oSocket;
    oSocket.create(oAddr.getType());
    oSocket.setReuseAddr(true);
    SINT32 ret=oSocket.listen(oAddr);
    if(ret!=E_SUCCESS)
      return ret;
    ret=oSocket.accept(m_Socket);
    if(ret!=E_SUCCESS)
      return E_UNKNOWN;
    oSocket.close();
    //m_Socket.setRecvLowWat(MIXPACKET_SIZE);
    m_aktBuffPos=0;
    return E_SUCCESS;
  }

Here is the call graph for this function:

Closes the underlying socket.

Definition at line 133 of file CAMuxSocket.cpp.

References CASocket::close(), m_aktBuffPos, and m_Socket.

Referenced by CALocalProxy::clean(), CAMiddleMix::clean(), CALastMix::clean(), CAFirstMix::clean(), CAFirstMixB::loop(), CALastMixB::loop(), CAMiddleMix::loop(), and ~CAMuxSocket().

  {
    m_aktBuffPos=0;
    return m_Socket.close();
  }

Here is the call graph for this function:

Definition at line 121 of file CAMuxSocket.cpp.

Referenced by CAMiddleMix::connectToNextMix(), CAFirstMix::connectToNextMix(), and CALocalProxy::init().

  {
    return connect(psa,1,0);
  }
SINT32 CAMuxSocket::connect ( CASocketAddr psa,
UINT  retry,
UINT32  time 
)

Definition at line 126 of file CAMuxSocket.cpp.

References CASocket::connect(), m_aktBuffPos, and m_Socket.

  {
    //m_Socket.setRecvLowWat(MIXPACKET_SIZE);
    m_aktBuffPos=0;
    return m_Socket.connect(psa,retry,time);
  }

Here is the call graph for this function:

bool CAMuxSocket::getIsEncrypted ( ) [inline]

Definition at line 88 of file CAMuxSocket.hpp.

References m_bIsCrypted.

        {
          return m_bIsCrypted;
        }

Definition at line 85 of file CAMuxSocket.hpp.

References CASocket::getSocket(), and m_Socket.

Referenced by CASocketGroupEpoll::add(), CASocketGroup::add(), CASocketGroup::isSignaled(), CAFirstMixB::loop(), and CASocketGroup::remove().

{return m_Socket.getSocket();}

Here is the call graph for this function:

Definition at line 189 of file CAMuxSocket.cpp.

References t_MixPacket::channel, CASymCipher::crypt1(), t_MixPacket::flags, CAMutex::lock(), m_csSend, m_oCipherOut, MIXPACKET_SIZE, and CAMutex::unlock().

Referenced by CAFirstMix::doUserLogin_internal(), CAFirstMixB::loop(), and CAFirstMixA::sendToUsers().

  { 
    m_csSend.lock();
    pinoutPacket->channel=htonl(pinoutPacket->channel);
    pinoutPacket->flags=htons(pinoutPacket->flags);
    m_oCipherOut.crypt1(((UINT8*)pinoutPacket),((UINT8*)pinoutPacket),16);
    m_csSend.unlock();
    return MIXPACKET_SIZE;
  }

Here is the call graph for this function:

Receives a whole MixPacket.

Blocks until a packet is received or a socket error occurs.

Parameters:
pPacketon return stores the received MixPacket
Return values:
SOCKET_ERROR,incase of an error
MIXPACKET_SIZEotherwise

Definition at line 206 of file CAMuxSocket.cpp.

References CASymCipher::crypt1(), E_SUCCESS, E_UNKNOWN, CAMutex::lock(), m_bIsCrypted, m_csReceive, m_oCipherIn, m_Socket, MIXPACKET_SIZE, CAMsg::printMsg(), CAClientSocket::receiveFully(), SOCKET_ERROR, and CAMutex::unlock().

Referenced by CAFirstMix::doUserLogin_internal(), fm_loopReadFromMix(), lm_loopReadFromMix(), CAFirstMixB::loop(), CAFirstMixA::loop(), CALocalProxy::loop(), mm_loopReadFromMixAfter(), and mm_loopReadFromMixBefore().

  {
    SINT32 retLock = m_csReceive.lock();
    if (retLock != E_SUCCESS)
    {
      CAMsg::printMsg(LOG_CRIT,
        "Could not lock MuxSocket receive method! Error code: %d\n", retLock);
      return E_UNKNOWN;
    }
    
    if(m_Socket.receiveFully((UINT8*)pPacket,MIXPACKET_SIZE)!=E_SUCCESS)
    {
      m_csReceive.unlock();
      return SOCKET_ERROR;
    }
    if(m_bIsCrypted)
      m_oCipherIn.crypt1((UINT8*)pPacket,(UINT8*)pPacket,16);
    pPacket->channel=ntohl(pPacket->channel);
    pPacket->flags=ntohs(pPacket->flags);
    m_csReceive.unlock();
    return MIXPACKET_SIZE;
  }

Here is the call graph for this function:

SINT32 CAMuxSocket::receive ( MIXPACKET pPacket,
UINT32  msTimeout 
)

Trys to receive a Mix-Packet.

If after timout milliseconds not a whole packet is available E_AGAIN will be returned. In this case you should later try to get the rest of the packet

Definition at line 236 of file CAMuxSocket.cpp.

References CASocketGroup::add(), add64(), t_MixPacket::channel, CASymCipher::crypt1(), diff64(), E_AGAIN, E_NOT_CONNECTED, E_SUCCESS, E_UNKNOWN, t_MixPacket::flags, GET_NET_ERROR, GET_NET_ERROR_STR, getcurrentTimeMillis(), CASocket::isClosed(), isEqual64(), isGreater64(), len, CAMutex::lock(), m_aktBuffPos, m_bIsCrypted, m_Buff, m_csReceive, m_oCipherIn, m_Socket, MIXPACKET_SIZE, CAMsg::printMsg(), CASocket::receive(), CASocketGroup::select(), and CAMutex::unlock().

  {
    if (m_Socket.isClosed())
    {
      return E_NOT_CONNECTED;
    }
    
    SINT32 retLock = m_csReceive.lock();
    if (retLock != E_SUCCESS)
    {
      CAMsg::printMsg(LOG_CRIT,
        "Could not lock MuxSocket timed receive method! Error code: %d\n", retLock);
      return E_UNKNOWN;
    }
    SINT32 len=MIXPACKET_SIZE-m_aktBuffPos;
    SINT32 ret=m_Socket.receive(m_Buff+m_aktBuffPos,len);
    if(ret<=0&&ret!=E_AGAIN) //if socket was set in non-blocking mode
    {
      m_csReceive.unlock();
      return E_UNKNOWN;
    }
    if(ret==len) //whole packet recieved
    {
      if(m_bIsCrypted)
        m_oCipherIn.crypt1(m_Buff,m_Buff,16);
      memcpy(pPacket,m_Buff,MIXPACKET_SIZE);
      pPacket->channel=ntohl(pPacket->channel);
      pPacket->flags=ntohs(pPacket->flags);
      m_aktBuffPos=0;
      m_csReceive.unlock();
      return MIXPACKET_SIZE;
    }
    if(ret>0) //some new bytes arrived
      m_aktBuffPos+=ret;
    if(msTimeout==0) //we should not wait any more
    {
      m_csReceive.unlock();
      return E_AGAIN;
    }
    UINT64 timeE;
    UINT64 timeC;
    getcurrentTimeMillis(timeE);
    add64(timeE,msTimeout);
    UINT32 dt=msTimeout;
    CASingleSocketGroup oSocketGroup(false);
    oSocketGroup.add(*this);
    for(;;)
      {
        if (m_Socket.isClosed())
        {
          m_csReceive.unlock();
          return E_NOT_CONNECTED;
        }       
        ret=oSocketGroup.select(dt);
        if(ret!=1)
        {
          m_csReceive.unlock();
          return E_UNKNOWN;
        }
        len=MIXPACKET_SIZE-m_aktBuffPos;
        if (m_Socket.isClosed())
        {
          m_csReceive.unlock();
          return E_NOT_CONNECTED;
        }       
        ret=m_Socket.receive(m_Buff+m_aktBuffPos,len);
        if(ret<=0&&ret!=E_AGAIN)
        {
          if (m_Socket.isClosed())
          {
            CAMsg::printMsg(LOG_ERR, "Error while receiving from socket. Socket is closed! Receive returned: %i Reason: %s (%i)\n", ret, GET_NET_ERROR_STR(GET_NET_ERROR), GET_NET_ERROR);
          }
          else
          {
            CAMsg::printMsg(LOG_ERR, "Error while receiving from socket. Receive returned: %i Reason: %s (%i)\n", ret, GET_NET_ERROR_STR(GET_NET_ERROR), GET_NET_ERROR);
          }
          m_csReceive.unlock();
          return E_UNKNOWN;
        }
        if(ret==len)
        {
          if(m_bIsCrypted)
            m_oCipherIn.crypt1(m_Buff,m_Buff,16);
          memcpy(pPacket,m_Buff,MIXPACKET_SIZE);
          pPacket->channel=ntohl(pPacket->channel);
          pPacket->flags=ntohs(pPacket->flags);
          m_aktBuffPos=0;
          m_csReceive.unlock();
          return MIXPACKET_SIZE;
        }
        if(ret>0)
          m_aktBuffPos+=ret;
        getcurrentTimeMillis(timeC);
        if(isGreater64(timeC,timeE)||isEqual64(timeC,timeE))
          break;
        dt=diff64(timeE,timeC);
      }
    m_csReceive.unlock();
    return E_AGAIN;
  }

Here is the call graph for this function:

SINT32 CAMuxSocket::receiveFully ( UINT8 buff,
UINT32  len 
) [inline]

Receives some "plain" bytes from the underlying socket - just a convenient function...

Definition at line 69 of file CAMuxSocket.hpp.

References m_Socket, and CAClientSocket::receiveFully().

Referenced by CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), and CAFirstMix::processKeyExchange().

      {
        return m_Socket.receiveFully(buff,len);
      }

Here is the call graph for this function:

SINT32 CAMuxSocket::receiveFully ( UINT8 buff,
UINT32  len,
UINT32  msTimeOut 
) [inline]

Definition at line 74 of file CAMuxSocket.hpp.

References m_Socket, and CASocket::receiveFullyT().

      {
        return m_Socket.receiveFullyT(buff,len, msTimeOut);
      }

Here is the call graph for this function:

Sends a MixPacket over the Network.

Will block until the whole packet is send.

Parameters:
pPacketMixPacket to send
Return values:
MIXPACKET_SIZEif MixPacket was successful send
E_UNKNOWNotherwise

Definition at line 144 of file CAMuxSocket.cpp.

References t_MixPacket::channel, CASymCipher::crypt1(), E_SUCCESS, E_UNKNOWN, t_MixPacket::flags, GET_NET_ERROR, CAMutex::lock(), m_bIsCrypted, m_csSend, m_oCipherOut, m_Socket, MIXPACKET_SIZE, CAMsg::printMsg(), CASocket::sendFully(), and CAMutex::unlock().

Referenced by fm_loopSendToMix(), lm_loopSendToMix(), CALocalProxy::loop(), mm_loopReadFromMixAfter(), mm_loopReadFromMixBefore(), mm_loopSendToMixAfter(), mm_loopSendToMixBefore(), and CALocalProxy::processKeyExchange().

  {
    m_csSend.lock();
    int ret;
    UINT8 tmpBuff[16];
    memcpy(tmpBuff,pPacket,16);
    pPacket->channel=htonl(pPacket->channel);
    pPacket->flags=htons(pPacket->flags);
    if(m_bIsCrypted)
      m_oCipherOut.crypt1(((UINT8*)pPacket),((UINT8*)pPacket),16);
    ret=m_Socket.sendFully(((UINT8*)pPacket),MIXPACKET_SIZE);
    if(ret!=E_SUCCESS)
      {
        #ifdef _DEBUG
          CAMsg::printMsg(LOG_DEBUG,"MuxSocket-Send-Error!\n");
          CAMsg::printMsg(LOG_DEBUG,"SOCKET-ERROR: %i\n",GET_NET_ERROR);
        #endif
        ret=E_UNKNOWN;
      }
    else
    {
      ret=MIXPACKET_SIZE;
    }
    memcpy(pPacket,tmpBuff,16);
    m_csSend.unlock();
    return ret;
  }

Here is the call graph for this function:

SINT32 CAMuxSocket::send ( MIXPACKET pPacket,
UINT8 buff 
)

Definition at line 172 of file CAMuxSocket.cpp.

References t_MixPacket::channel, CASymCipher::crypt1(), t_MixPacket::flags, CAMutex::lock(), m_bIsCrypted, m_csSend, m_oCipherOut, MIXPACKET_SIZE, and CAMutex::unlock().

  {
    m_csSend.lock();
    int ret;
    UINT8 tmpBuff[16];
    memcpy(tmpBuff,pPacket,16);
    pPacket->channel=htonl(pPacket->channel);
    pPacket->flags=htons(pPacket->flags);
    if(m_bIsCrypted)
      m_oCipherOut.crypt1(((UINT8*)pPacket),((UINT8*)pPacket),16);
    memcpy(buff,((UINT8*)pPacket),MIXPACKET_SIZE);
    ret=MIXPACKET_SIZE;
    memcpy(pPacket,tmpBuff,16);
    m_csSend.unlock();
    return ret;
  }

Here is the call graph for this function:

Definition at line 73 of file CAMuxSocket.cpp.

References E_SUCCESS, CAMutex::lock(), m_bIsCrypted, m_csReceive, m_csSend, and CAMutex::unlock().

Referenced by CAFirstMix::doUserLogin_internal(), CAMiddleMix::loop(), CALocalProxy::processKeyExchange(), CALastMix::processKeyExchange(), and CAFirstMix::processKeyExchange().

Here is the call graph for this function:

SINT32 CAMuxSocket::setKey ( UINT8 key,
UINT32  keyLen 
) [inline]

Sets the symmetric keys used for de-/encrypting the Mux connection.

Parameters:
keybuffer conntaining the key bits
keyLensize of the buffer (keys) if keylen=16, then the key is used for incomming and outgoing direction (key only) if keylen=32, then the first bytes are used for incoming and the last bytes are used for outgoing
Return values:
E_SUCCESSif successful
E_UNKNOWNotherwise

Definition at line 102 of file CAMuxSocket.hpp.

References E_SUCCESS, E_UNKNOWN, m_oCipherIn, m_oCipherOut, and CASymCipher::setKey().

        {
          if(keyLen==16)
            {
              m_oCipherIn.setKey(key);
              m_oCipherOut.setKey(key);
            }
          else if(keyLen==32)
            {
              m_oCipherOut.setKey(key);
              m_oCipherIn.setKey(key+16);
            }
        else
            return E_UNKNOWN;
          return E_SUCCESS;
        }

Here is the call graph for this function:

SINT32 CAMuxSocket::setReceiveKey ( UINT8 key,
UINT32  keyLen 
) [inline]

Definition at line 135 of file CAMuxSocket.hpp.

References E_SUCCESS, E_UNKNOWN, m_oCipherIn, CASymCipher::setIVs(), and CASymCipher::setKey().

Referenced by CAFirstMix::doUserLogin_internal(), CALocalProxy::processKeyExchange(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), and CAFirstMix::processKeyExchange().

      {
        if(keyLen==16)
        {
          m_oCipherIn.setKey(key);
        }
        else if(keyLen==32)
        {
          m_oCipherIn.setKey(key);
          m_oCipherIn.setIVs(key+16);
        }
        else
        {
          return E_UNKNOWN;
        }
        return E_SUCCESS;
      }

Here is the call graph for this function:

SINT32 CAMuxSocket::setSendKey ( UINT8 key,
UINT32  keyLen 
) [inline]

Definition at line 119 of file CAMuxSocket.hpp.

References E_SUCCESS, E_UNKNOWN, m_oCipherOut, CASymCipher::setIVs(), and CASymCipher::setKey().

Referenced by CAFirstMix::doUserLogin_internal(), CALocalProxy::processKeyExchange(), CAMiddleMix::processKeyExchange(), CALastMix::processKeyExchange(), and CAFirstMix::processKeyExchange().

        {
          if(keyLen==16)
            {
              m_oCipherOut.setKey(key);
            }
          else if(keyLen==32)
            {
              m_oCipherOut.setKey(key);
              m_oCipherOut.setIVs(key+16);
            }
          else
            return E_UNKNOWN;
          return E_SUCCESS;
        }

Here is the call graph for this function:


Member Data Documentation

Definition at line 155 of file CAMuxSocket.hpp.

Referenced by accept(), CAMuxSocket(), close(), connect(), and receive().

bool CAMuxSocket::m_bIsCrypted [private]

Definition at line 159 of file CAMuxSocket.hpp.

Referenced by CAMuxSocket(), getIsEncrypted(), receive(), send(), and setCrypt().

Definition at line 156 of file CAMuxSocket.hpp.

Referenced by CAMuxSocket(), receive(), and ~CAMuxSocket().

Definition at line 161 of file CAMuxSocket.hpp.

Referenced by receive(), and setCrypt().

Definition at line 160 of file CAMuxSocket.hpp.

Referenced by prepareForSend(), send(), and setCrypt().

Definition at line 157 of file CAMuxSocket.hpp.

Referenced by receive(), setKey(), and setReceiveKey().

Definition at line 158 of file CAMuxSocket.hpp.

Referenced by prepareForSend(), send(), setKey(), and setSendKey().

Definition at line 162 of file CAMuxSocket.hpp.

Referenced by CAMuxSocket(), getHashKey(), and ~CAMuxSocket().

Definition at line 154 of file CAMuxSocket.hpp.

Referenced by accept(), close(), connect(), getCASocket(), getSocket(), receive(), receiveFully(), and send().

Definition at line 165 of file CAMuxSocket.hpp.

Referenced by CAMuxSocket().

CAMutex * CAMuxSocket::ms_pcsHashKeyList = new CAMutex() [static, private]

Definition at line 166 of file CAMuxSocket.hpp.

Referenced by CAMuxSocket(), and ~CAMuxSocket().

Definition at line 164 of file CAMuxSocket.hpp.

Referenced by CAMuxSocket(), and ~CAMuxSocket().


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