Mixe for Privacy and Anonymity in the Internet
CAReplayCtrlChannelMsgProc.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 #include "StdAfx.h"
29 #ifdef REPLAY_DETECTION
32 #include "CAMixWithReplayDB.hpp"
33 #include "CAMix.hpp"
35 #include "CACmdLnOptions.hpp"
36 #include "CAFirstMix.hpp"
37 #include "CADatabase.hpp"
38 
40  {
41  CAMsg::printMsg(LOG_DEBUG,"CAReplayCtrlChannelMsgProc - constructor - this=%p\n",this);
45  m_pMix=pMix;
46  m_docTemplate=NULL;
49 
50  if(pDispatcher!=NULL)
51  {
52  #ifdef DEBUG
53  CAMsg::printMsg(LOG_DEBUG,"CAReplayCtrlChannelMsgProc - constructor - registering downstream replay control channel\n",this);
54  #endif
56  CAMsg::printMsg(LOG_DEBUG,"m_pDownstreamReplayControlChannel= %p\n",m_pDownstreamReplayControlChannel);
58  }
60  if(pDispatcher!=NULL)
61  {
62  #ifdef DEBUG
63  CAMsg::printMsg(LOG_DEBUG,"CAReplayCtrlChannelMsgProc - constructor - registering upstream replay control channel\n",this);
64  #endif
67  }
68  }
69 
71  {
72  #ifdef DEBUG
73  CAMsg::printMsg(LOG_DEBUG,"CAReplayCtrlChannelMsgProc::~CAReplayCtrlChannelMsgProc()\n");
74  #endif
77  if(pDispatcher!=NULL)
78  {
82  }
84  if(pDispatcher!=NULL)
85  {
89  }
90  }
91 
92 /* not used and not necesary
93 SINT32 CAReplayCtrlChannelMsgProc::proccessGetTimestamps(const CAReplayControlChannel* pReceiver) const
94  {
95  //Only for the first mix get timestamps is supported for the moment!
96  if(m_pMix->getType()!=CAMix::FIRST_MIX)
97  {
98  return E_UNKNOWN;
99  }
100 
101  CAFirstMix* pMix=(CAFirstMix*)m_pMix;
102  DOM_Document docTemplate=DOM_Document::createDocument();
103  DOM_Element elemMixes=docTemplate.createElement("Mixes");
104  docTemplate.appendChild(elemMixes);
105  tMixParameters* mixParameters=pMix->getMixParameters();
106  time_t aktTime=time(NULL);
107  for(SINT32 i=0;i<pMix->getMixCount()-1;i++)
108  {
109  DOM_Element elemMix=docTemplate.createElement("Mix");
110  setDOMElementAttribute(elemMix,"id",mixParameters[i].m_strMixID);
111  DOM_Element elemReplay=docTemplate.createElement("Replay");
112  elemMix.appendChild(elemReplay);
113  DOM_Element elemReplayOffset=docTemplate.createElement("ReplayOffset");
114  setDOMElementValue(elemReplayOffset,(UINT32) (mixParameters[i].m_u32ReplayOffset+aktTime-pMix->m_u64LastTimestampReceived));
115  elemReplay.appendChild(elemReplayOffset);
116  elemMixes.appendChild(elemMix);
117  }
118  return pReceiver->sendXMLMessage(docTemplate);
119  }
120 */
121 // possibility for the first mix to request the timestamps
123  {
124  UINT8 buff[255];
125  pglobalOptions->getMixId(buff,255);
126  if(strMixID==NULL||strncmp((char*)strMixID,(char*)buff,255)==0)//our own replay timestamp is requested
127  {
128  //First Mixes does not have to send his timestamp!
130  return E_SUCCESS;
131 
133 
134  DOMElement* elemReplayTimestamp=NULL;
135  if (getDOMChildByName(m_docTemplate->getDocumentElement(),"ReplayOffset",elemReplayTimestamp,true)!=E_SUCCESS){
136  return E_UNKNOWN;
137  }
138  setDOMElementValue(elemReplayTimestamp,(UINT32) (time(NULL)-m_pMix->m_u64ReferenceTime));
139 
140  return pReceiver->sendXMLMessage(m_docTemplate);
141  }
142  else if(m_pUpstreamReplayControlChannel!=NULL&&strMixID!=NULL)//the replay timestamp of some other mix is requested
143  {
144  XERCES_CPP_NAMESPACE::DOMDocument* doc=createDOMDocument();
145  DOMElement *elemGet=createDOMElement(doc,"GetTimestamp");
146  setDOMElementAttribute(elemGet,"id",strMixID);
147  doc->appendChild(elemGet);
148 
150  if (doc != NULL)
151  {
152  doc->release();
153  doc = NULL;
154  }
155 
156  return return_value;
157  }
158  return E_SUCCESS;
159  }
160 
162  {
163  UINT8 buff[255];
164  pglobalOptions->getMixId(buff,255);
165 
166  #ifdef DEBUG
167  CAMsg::printMsg(LOG_DEBUG,"Start replay timestamp propagation\n");
168  #endif
169 
171  return E_UNKNOWN;
172 
173  DOMElement* elemReplayTimestamp=NULL;
174  if (getDOMChildByName(m_docTemplate->getDocumentElement(),"ReplayOffset",elemReplayTimestamp,true)!=E_SUCCESS){
175  return E_UNKNOWN;
176  }
177  setDOMElementValue(elemReplayTimestamp,(UINT32) (time(NULL)-m_pMix->m_u64ReferenceTime));
178 
180 
181  #ifdef DEBUG
182  CAMsg::printMsg(LOG_DEBUG,"Replay timestamp propagation finished\n");
183  #endif
184 
185  return E_SUCCESS;
186  }
187 
189  {
191  UINT32 propagationInterval=pReplayMsgProc->m_u32PropagationInterval;
192  while(pReplayMsgProc->m_bRun)
193  {
194  if(propagationInterval==0)
195  {
196  pReplayMsgProc->propagateCurrentReplayTimestamp();
197  propagationInterval=pReplayMsgProc->m_u32PropagationInterval;
198  }
199  sSleep(60);
200  propagationInterval--;
201  }
203  }
204 
206  {
207  m_u32PropagationInterval=minutesPropagationInterval;
210  m_bRun=true;
211  return m_pThreadTimestampPropagation->start(this);
212  }
213 
215  {
216  m_bRun=false;
218  {
222  }
224  return E_SUCCESS;
225  }
226 
233  {
234  //NOT for the first mix! Only propagateCurrentReplayTimestamps is supported for the moment!
236  {
237  return E_SUCCESS;
238  }
239 
240  if(m_docTemplate!=NULL)
241  {
242  return E_UNKNOWN;
243  }
244 
245  UINT8 buff[255];
246  pglobalOptions->getMixId(buff,255);
247 
249  DOMElement *elemMix=createDOMElement(m_docTemplate,"Mix");
250  setDOMElementAttribute(elemMix,"id",buff);
251  DOMElement *elemReplay=createDOMElement(m_docTemplate,"Replay");
252  elemMix->appendChild(elemReplay);
253  DOMElement *elemReplayOffset=createDOMElement(m_docTemplate,"ReplayOffset");
254 // setDOMElementValue(elemReplayOffset,(UINT32) (time(NULL)-m_pMix->m_u64ReferenceTime));
255  elemReplay->appendChild(elemReplayOffset);
256  DOMElement *elemReplayBase=createDOMElement(m_docTemplate,"ReplayBase");
257  setDOMElementValue(elemReplayBase,(UINT32) (REPLAY_BASE));
258  elemReplay->appendChild(elemReplayBase);
259  m_docTemplate->appendChild(elemMix);
260 
261  return E_SUCCESS;
262  }
263 
264 
266  {
267  //if not first mix just forwards them down the drain...
268  #ifdef DEBUG
269  CAMsg::printMsg(LOG_DEBUG,"CAReplayCtrlChannelMsgProc::proccessGotTimestamp() \n");
270  #endif
272  {
273  XERCES_CPP_NAMESPACE::DOMDocument* doc=createDOMDocument();
274  DOMElement *elemMix=createDOMElement(doc,"Mix");
275  setDOMElementAttribute(elemMix,"id",strMixID);
276  DOMElement *elemReplay=createDOMElement(doc,"Replay");
277  elemMix->appendChild(elemReplay);
278  DOMElement *elemReplayOffset=createDOMElement(doc,"ReplayOffset");
279  setDOMElementValue(elemReplayOffset,(UINT32) offset);
280  elemReplay->appendChild(elemReplayOffset);
281  doc->appendChild(elemMix);
282 
284  if (doc != NULL)
285  {
286  doc->release();
287  doc = NULL;
288  }
289 
290  return return_value;
291  }
292 
293  //First mix --> update mix parameters
295  #ifdef DEBUG
296  CAMsg::printMsg(LOG_DEBUG,"CAReplayCtrlChannelMsgProc::proccessGotTimestamp() - MixID: %s\n",strMixID);
297  CAMsg::printMsg(LOG_DEBUG,"CAReplayCtrlChannelMsgProc::proccessGotTimestamp() - LastTimestamp DIFF: %d\n",time(NULL)-pMix->m_u64LastTimestampReceived);
298  #endif
299 
300  tMixParameters params;
301  UINT32 len=strlen((char*)strMixID);
302  params.m_strMixID=new UINT8[len+1];
303  memcpy(params.m_strMixID,strMixID,len+1);
304  params.m_u32ReplayOffset=offset;
305  pMix->setMixParameters(params);
306 
307  pMix->m_u64LastTimestampReceived=time(NULL);
308  delete[] params.m_strMixID;
309  params.m_strMixID = NULL;
310  return E_SUCCESS;
311  }
312 
314  {
315  if(strMixID==NULL||strlen((const char*)strMixID)>400)
316  return E_UNKNOWN;
317 
318  XERCES_CPP_NAMESPACE::DOMDocument* doc=createDOMDocument();
319  DOMElement *elemGet=createDOMElement(doc,"GetTimestamp");
320  setDOMElementAttribute(elemGet,"id",strMixID);
321  doc->appendChild(elemGet);
322 
324  if (doc != NULL)
325  {
326  doc->release();
327  doc = NULL;
328  }
329 
330  return return_value;
331  }
332 
333 #endif //REPLAY_DETECTION
#define REPLAY_BASE
THREAD_RETURN rp_loopPropagateTimestamp(void *param)
SINT32 setDOMElementAttribute(DOMNode *pElem, const char *attrName, const char *value)
Definition: CAUtil.cpp:831
SINT32 setDOMElementValue(DOMElement *pElem, SINT32 value)
Definition: CAUtil.cpp:939
SINT32 sSleep(UINT32 sec)
Sleeps sec Seconds.
Definition: CAUtil.cpp:425
XERCES_CPP_NAMESPACE::DOMDocument * createDOMDocument()
Parses a timestamp in JDBC timestamp escape format (as it comes from the BI) and outputs the value in...
Definition: CAUtil.cpp:1568
DOMElement * createDOMElement(XERCES_CPP_NAMESPACE::DOMDocument *pOwnerDoc, const char *const name)
Creates a new DOMElement with the given name which belongs to the DOMDocument owernDoc.
Definition: CAUtil.cpp:814
SINT32 getDOMChildByName(const DOMNode *pNode, const char *const name, DOMElement *&child, bool deep)
Definition: CAUtil.cpp:458
#define THREAD_RETURN
Definition: StdAfx.h:540
#define THREAD_RETURN_SUCCESS
Definition: StdAfx.h:542
signed int SINT32
Definition: basetypedefs.h:132
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
SINT32 sendXMLMessage(const XERCES_CPP_NAMESPACE::DOMDocument *pDocMsg) const
Call to send a XML message via this control channel.
UINT32 getID() const
Returns the id of this control channel.
This class "dispatches" messages which it receives via proccessMixPacket() to the associated control ...
SINT32 registerControlChannel(CAAbstractControlChannel *pControlChannel)
Registers a control channel for receiving messages.
CAControlChannelDispatcher * getDownstreamControlChannelDispatcher() const
Definition: CAMix.hpp:115
virtual tMixType getType() const =0
@ FIRST_MIX
Definition: CAMix.hpp:53
CAControlChannelDispatcher * getUpstreamControlChannelDispatcher() const
Definition: CAMix.hpp:120
This class (interface) adds the functionality used by last and middle mixes to detecte replays.
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251
A Control channel for the exchange of the current replay detection timestamps.
SINT32 proccessGotTimestamp(const CAReplayControlChannel *pReceiver, const UINT8 *strMixID, const UINT32 offset) const
Proccesses a received replay timestamp rt from mix strMixID.
SINT32 initTimestampsMessageTemplate()
We initialise the template used to generate the idividual responses to gettimestamps requests accordi...
CAReplayControlChannel * m_pDownstreamReplayControlChannel
SINT32 stopTimeStampPorpagation()
Stops the timestamp propagation.
SINT32 propagateCurrentReplayTimestamp()
Propagates downstream the current replay timestamp.
CAReplayCtrlChannelMsgProc(const CAMixWithReplayDB *pMix)
Initialises the replay control channel messages processor with the necessary information.
friend THREAD_RETURN rp_loopPropagateTimestamp(void *param)
SINT32 proccessGetTimestamp(const CAReplayControlChannel *pReceiver, const UINT8 *strMixID) const
Proccesses a getTimeStamps request on a reply control channel.
XERCES_CPP_NAMESPACE::DOMDocument * m_docTemplate
CAReplayControlChannel * m_pUpstreamReplayControlChannel
SINT32 sendGetTimestamp(const UINT8 *strMixID)
Sends upstram a request for the replay timestamp for the given mix.
SINT32 startTimeStampPorpagation(UINT32 minutesPropagationIntervall)
Sends the current replay timestamp periodically on the downstream replay control channel.
SINT32 start(void *param, bool bDaemon=false, bool bSilent=false)
Starts the execution of the main function of this thread.
Definition: CAThread.cpp:115
SINT32 setMainLoop(THREAD_MAIN_TYP fnc)
Sets the main function which will be executed within this thread.
Definition: CAThread.hpp:148
SINT32 join()
Waits for the main function to finish execution.
Definition: CAThread.cpp:187
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define E_UNKNOWN
Definition: errorcodes.hpp:3
CAMix * pMix
Definition: proxytest.cpp:75
UINT32 m_u32ReplayOffset
Definition: typedefs.hpp:218
UINT8 * m_strMixID
Definition: typedefs.hpp:216
UINT16 len
Definition: typedefs.hpp:0