Mixe for Privacy and Anonymity in the Internet
CAControlChannelDispatcher.cpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2000, The JAP-Team
3 All rights reserved.
4 Redistribution and use in source and binary forms, with or without modification,
5 are permitted provided that the following conditions are met:
6 
7  - Redistributions of source code must retain the above copyright notice,
8  this list of conditions and the following disclaimer.
9 
10  - Redistributions in binary form must reproduce the above copyright notice,
11  this list of conditions and the following disclaimer in the documentation and/or
12  other materials provided with the distribution.
13 
14  - Neither the name of the University of Technology Dresden, Germany nor the names of its contributors
15  may be used to endorse or promote products derived from this software without specific
16  prior written permission.
17 
18 
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
20 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21 AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
27 */
28 
29 #include "StdAfx.h"
30 #if !defined ONLY_LOCAL_PROXY || defined INCLUDE_LAST_MIX
33 
35  {
36  if(pControlChannel==NULL||pControlChannel->getID()>255)
37  return E_UNKNOWN;
39  pControlChannel->setDispatcher(this);
40  m_arControlChannels[pControlChannel->getID()]= pControlChannel;
42  return E_SUCCESS;
43  }
44 
46  {
47  if(id>255)
48  return E_UNKNOWN;
50  delete m_arControlChannels[id];
51  m_arControlChannels[id]=NULL;
53  return E_SUCCESS;
54  }
55 
58  {
60  for(UINT32 i=0;i<256;i++)
61  {
62  delete m_arControlChannels[i];
63  m_arControlChannels[i]=NULL;
64  }
66  }
67 
69  {
70  if(pPacket->channel < 256 && pPacket->channel > 0)
71  {
73  CAAbstractControlChannel* pControlChannel=m_arControlChannels[pPacket->channel];
74  if (pControlChannel != NULL)
75  {
76  bool ret = (pControlChannel->proccessMessage(pPacket->data,pPacket->flags) == E_SUCCESS);
78  return ret;
79  }
80  else
81  {
83  return true;
84  }
85  }
86  return false;
87  }
88 
90  {
91  if(m_pGCMCtxEnc!=NULL)
92  {
93  m_pcsEnc->lock();
94  m_pEncMsgIV[2]=htonl(m_nEncMsgCounter);
96  ::gcm_encrypt_64k(m_pGCMCtxEnc, m_pEncMsgIV , in,inlen,out,(UINT32*)(out+inlen));
97  *outlen=inlen+16;
98  m_pcsEnc->unlock();
99  }
100  else
101  {
102  memcpy(out,in,inlen);
103  *outlen=inlen;
104  }
105  return E_SUCCESS;
106  }
107 
109  {
110  if(m_pGCMCtxDec!=NULL)
111  {
112  m_pcsDec->lock();
113  UINT32 iv=htonl(m_nDecMsgCounter);
115  memcpy(m_pDecMsgIV+8,&iv,4);
116  ::gcm_decrypt_64k(m_pGCMCtxDec,(UINT32*) m_pDecMsgIV , in,inlen-16,in+inlen-16,out);
117  *outlen=inlen-16;
118  m_pcsDec->unlock();
119  }
120  else
121  {
122  memcpy(out,in,inlen);
123  *outlen=inlen;
124  }
125  return E_SUCCESS;
126  }
127 
129  {
130  #ifdef DEBUG
131  CAMsg::printMsg(LOG_DEBUG,"dispatch - sendMesg()\n");
132  #endif
133  if(msg==NULL)
134  return E_UNKNOWN;
135  m_pcsSendMsg->lock();
136  m_pMixPacket->channel=id;
137  UINT32 aktIndex=0;
138 
139  //prevent data garbage from old messages to be transmitted again
140  memset(m_pMixPacket->data, 0, DATA_SIZE);
141  while(msglen>0)
142  {
143  if(msglen>DATA_SIZE)
144  {
146  }
147  else
148  {
149  m_pMixPacket->flags=(UINT16)msglen;
150  }
151  memcpy(m_pMixPacket->data,msg+aktIndex,m_pMixPacket->flags);
153  aktIndex+=m_pMixPacket->flags;
154  msglen-=m_pMixPacket->flags;
155  }
156  m_pcsSendMsg->unlock();
157  return E_SUCCESS;
158  }
159 #endif //ONLY_LOCAL_PROXY
unsigned short UINT16
Definition: basetypedefs.h:133
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
The base of each control channel.
virtual SINT32 proccessMessage(const UINT8 *msg, UINT32 msglen)=0
Processes some bytes of a message we got from the communication channel.
UINT32 getID() const
Returns the id of this control channel.
SINT32 setDispatcher(CAControlChannelDispatcher *pDispatcher)
Sets the Dispatcher.
void deleteAllControlChannels(void)
Deregisters all control channels and calls delete on every registered control channel object.
CAAbstractControlChannel ** m_arControlChannels
SINT32 decryptMessage(const UINT8 *in, UINT32 inlen, UINT8 *out, UINT32 *outlen)
Decrypts a control channel message, which has to be of form: cipher text auth tag - 16 bytes.
SINT32 registerControlChannel(CAAbstractControlChannel *pControlChannel)
Registers a control channel for receiving messages.
SINT32 sendMessages(UINT32 id, const UINT8 *msg, UINT32 msglen)
SINT32 encryptMessage(const UINT8 *in, UINT32 inlen, UINT8 *out, UINT32 *outlen)
Encrypts a control channel message.
bool proccessMixPacket(const MIXPACKET *pPacket)
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
SINT32 unlock()
Definition: CAMutex.hpp:52
SINT32 lock()
Definition: CAMutex.hpp:41
SINT32 add(const void *buff, UINT32 size)
Adds data to the Queue.
Definition: CAQueue.cpp:76
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
HCHANNEL channel
Definition: typedefs.hpp:117
UINT8 data[DATA_SIZE]
Definition: typedefs.hpp:121
UINT16 flags
Definition: typedefs.hpp:118
Definition: typedefs.hpp:169
#define DATA_SIZE
Definition: typedefs.hpp:69