00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #include "StdAfx.h"
00030 #ifndef ONLY_LOCAL_PROXY
00031 #include "CAControlChannelDispatcher.hpp"
00032 #include "CAAbstractControlChannel.hpp"
00033
00034 SINT32 CAControlChannelDispatcher::registerControlChannel(CAAbstractControlChannel* pControlChannel)
00035 {
00036 if(pControlChannel==NULL||pControlChannel->getID()>255)
00037 return E_UNKNOWN;
00038 m_pcsRegisterChannel->lock();
00039 pControlChannel->setDispatcher(this);
00040 m_arControlChannels[pControlChannel->getID()]= pControlChannel;
00041 m_pcsRegisterChannel->unlock();
00042 return E_SUCCESS;
00043 }
00044
00045 SINT32 CAControlChannelDispatcher::removeControlChannel(UINT32 id)
00046 {
00047 if(id>255)
00048 return E_UNKNOWN;
00049 m_pcsRegisterChannel->lock();
00050 delete m_arControlChannels[id];
00051 m_arControlChannels[id]=NULL;
00052 m_pcsRegisterChannel->unlock();
00053 return E_SUCCESS;
00054 }
00055
00057 void CAControlChannelDispatcher::deleteAllControlChannels()
00058 {
00059 m_pcsRegisterChannel->lock();
00060 for(UINT32 i=0;i<256;i++)
00061 {
00062 delete m_arControlChannels[i];
00063 m_arControlChannels[i]=NULL;
00064 }
00065 m_pcsRegisterChannel->unlock();
00066 }
00067
00068 bool CAControlChannelDispatcher::proccessMixPacket(const MIXPACKET* pPacket)
00069 {
00070 if(pPacket->channel < 256 && pPacket->channel > 0)
00071 {
00072 m_pcsRegisterChannel->lock();
00073 CAAbstractControlChannel* pControlChannel=m_arControlChannels[pPacket->channel];
00074 if (pControlChannel != NULL)
00075 {
00076 bool ret = (pControlChannel->proccessMessage(pPacket->data,pPacket->flags) == E_SUCCESS);
00077 m_pcsRegisterChannel->unlock();
00078 return ret;
00079 }
00080 else
00081 {
00082 m_pcsRegisterChannel->unlock();
00083 return true;
00084 }
00085 }
00086 return false;
00087 }
00088
00089 SINT32 CAControlChannelDispatcher::encryptMessage(const UINT8* in,UINT32 inlen, UINT8* out,UINT32* outlen)
00090 {
00091 if(m_pGCMCtxEnc!=NULL)
00092 {
00093 m_pcsEnc->lock();
00094 UINT32 iv=htonl(m_nEncMsgCounter);
00095 m_nEncMsgCounter++;
00096 memcpy(m_pEncMsgIV+8,&iv,4);
00097 ::gcm_encrypt_64k(m_pGCMCtxEnc, m_pEncMsgIV ,12, in,inlen,NULL,0,out,out+inlen);
00098 *outlen=inlen+16;
00099 m_pcsEnc->unlock();
00100 }
00101 else
00102 {
00103 memcpy(out,in,inlen);
00104 *outlen=inlen;
00105 }
00106 return E_SUCCESS;
00107 }
00108
00109 SINT32 CAControlChannelDispatcher::decryptMessage(const UINT8* in,UINT32 inlen, UINT8* out,UINT32* outlen)
00110 {
00111 if(m_pGCMCtxDec!=NULL)
00112 {
00113 m_pcsDec->lock();
00114 UINT32 iv=htonl(m_nDecMsgCounter);
00115 m_nDecMsgCounter++;
00116 memcpy(m_pDecMsgIV+8,&iv,4);
00117 ::gcm_decrypt_64k(m_pGCMCtxDec, m_pDecMsgIV ,12, in,inlen-16,in+inlen-16,16,NULL,0,out);
00118 *outlen=inlen-16;
00119 m_pcsDec->unlock();
00120 }
00121 else
00122 {
00123 memcpy(out,in,inlen);
00124 *outlen=inlen;
00125 }
00126 return E_SUCCESS;
00127 }
00128
00129 SINT32 CAControlChannelDispatcher::sendMessages(UINT32 id,const UINT8* msg,UINT32 msglen)
00130 {
00131 #ifdef DEBUG
00132 CAMsg::printMsg(LOG_DEBUG,"dispatch - sendMesg()\n");
00133 #endif
00134 if(msg==NULL)
00135 return E_UNKNOWN;
00136 m_pcsSendMsg->lock();
00137 m_pMixPacket->channel=id;
00138 UINT32 aktIndex=0;
00139
00140
00141 memset(m_pMixPacket->data, 0, DATA_SIZE);
00142 while(msglen>0)
00143 {
00144 if(msglen>DATA_SIZE)
00145 {
00146 m_pMixPacket->flags=DATA_SIZE;
00147 }
00148 else
00149 {
00150 m_pMixPacket->flags=(UINT16)msglen;
00151 }
00152 memcpy(m_pMixPacket->data,msg+aktIndex,m_pMixPacket->flags);
00153 m_pSendQueue->add(m_pQueueEntry,sizeof(tQueueEntry));
00154 aktIndex+=m_pMixPacket->flags;
00155 msglen-=m_pMixPacket->flags;
00156 }
00157 m_pcsSendMsg->unlock();
00158 return E_SUCCESS;
00159 }
00160 #endif //ONLY_LOCAL_PROXY