Mixe for Privacy and Anonymity in the Internet
Functions
CALastMix.cpp File Reference
#include "StdAfx.h"
#include "CALastMix.hpp"
#include "CALastMixChannelList.hpp"
#include "CALibProxytest.hpp"
#include "CASocketGroup.hpp"
#include "CASingleSocketGroup.hpp"
#include "CAMsg.hpp"
#include "CACmdLnOptions.hpp"
#include "CASocketAddrUnix.hpp"
#include "CACertStore.hpp"
#include "CABase64.hpp"
#include "CAPool.hpp"
#include "xml/DOM_Output.hpp"
#include "CAStatusManager.hpp"
#include "CAControlChannelDispatcher.hpp"
Include dependency graph for CALastMix.cpp:

Go to the source code of this file.

Functions

THREAD_RETURN lm_loopLog (void *param)
 
THREAD_RETURN lm_loopSendToMix (void *param)
 How to end this thread: 0. More...
 
THREAD_RETURN lm_loopReadFromMix (void *pParam)
 

Function Documentation

◆ lm_loopLog()

THREAD_RETURN lm_loopLog ( void *  param)

Definition at line 591 of file CALastMix.cpp.

592  {
593  CALastMix* pLastMix=static_cast<CALastMix*>(param);
594  UINT32 countLog=0;
595  UINT8 buff[256];
596  while(pLastMix->m_bRunLog)
597  {
598  if((countLog%10)==0)
599  {
600  logMemoryUsage();
601  }
602  if(countLog==0)
603  {
604  CAMsg::printMsg(LOG_DEBUG,"Uploaded Packets: %u\n",pLastMix->m_logUploadedPackets);
605  CAMsg::printMsg(LOG_DEBUG,"Downloaded Packets: %u\n",pLastMix->m_logDownloadedPackets);
606  print64(buff,(UINT64&)pLastMix->m_logUploadedBytes);
607  CAMsg::printMsg(LOG_DEBUG,"Uploaded Bytes : %s\n",buff);
608  print64(buff,(UINT64&)pLastMix->m_logDownloadedBytes);
609  CAMsg::printMsg(LOG_DEBUG,"Downloaded Bytes : %s\n",buff);
610  countLog=30;
611  }
612  sSleep(30);
613  countLog--;
614  }
616  }
SINT32 sSleep(UINT32 sec)
Sleeps sec Seconds.
Definition: CAUtil.cpp:425
void logMemoryUsage()
Log information about the current memory (heap) usage.
Definition: CAUtil.cpp:177
void print64(UINT8 *buff, UINT64 num)
Definition: CAUtil.hpp:482
#define THREAD_RETURN_SUCCESS
Definition: StdAfx.h:542
unsigned char UINT8
Definition: basetypedefs.h:135
unsigned int UINT32
Definition: basetypedefs.h:131
volatile bool m_bRunLog
Definition: CALastMix.hpp:189
volatile UINT32 m_logDownloadedPackets
Definition: CALastMix.hpp:192
volatile UINT64 m_logDownloadedBytes
Definition: CALastMix.hpp:193
volatile UINT64 m_logUploadedBytes
Definition: CALastMix.hpp:191
volatile UINT32 m_logUploadedPackets
Definition: CALastMix.hpp:190
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251

◆ lm_loopReadFromMix()

THREAD_RETURN lm_loopReadFromMix ( void *  pParam)

ToDo: check if keep-alive is really correct here - should it not be moved upwards?

Definition at line 731 of file CALastMix.cpp.

732  {
733  #ifdef ENABLE_GPERFTOOLS_CPU_PROFILER
734  ProfilerRegisterThread();
735  #endif
736  CALastMix* pLastMix = static_cast<CALastMix*>(pParam);
737  CAMuxSocket* pMuxSocket=pLastMix->m_pMuxIn;
738  CAQueue* pQueue=pLastMix->m_pQueueReadFromMix;
739  tQueueEntry* pQueueEntry=new tQueueEntry;
740  MIXPACKET* pMixPacket=&pQueueEntry->packet;
741  CASingleSocketGroup* pSocketGroup=new CASingleSocketGroup(false);
742  pSocketGroup->add(*pMuxSocket);
743  #ifdef USE_POOL
744  CAPool* pPool=new CAPool(MIX_POOL_SIZE);
745  #endif
746  UINT64 keepaliveNow,keepaliveLast;
747  UINT32 u32KeepAliveRecvInterval=pLastMix->m_u32KeepAliveRecvInterval;
748  getcurrentTimeMillis(keepaliveLast);
749  while(!pLastMix->m_bRestart)
750  {
752  {
753 #ifdef DEBUG
754  CAMsg::printMsg(LOG_DEBUG,"CAFirstMix::Queue is full!\n");
755 #endif
756  msSleep(200);
757  getcurrentTimeMillis(keepaliveLast);
758  continue;
759  }
760  //check if the connection is broken because we did not receive a Keep_alive-Message
761  getcurrentTimeMillis(keepaliveNow);
762  UINT32 keepaliveDiff=diff64(keepaliveNow,keepaliveLast);
763  if(keepaliveDiff>u32KeepAliveRecvInterval)
764  {
765  CAMsg::printMsg(LOG_ERR,"CALastMix::loopReadFromMix() -- restart because of KeepAlive-Traffic Timeout!\n");
766  pLastMix->m_bRestart=true;
768  break;
769  }
770  SINT32 ret=pSocketGroup->select(MIX_POOL_TIMEOUT);
771  if(ret < 0)
772  {
773  if (ret == E_TIMEDOUT)
774  {
775  #ifdef USE_POOL
776  pMixPacket->flags=CHANNEL_DUMMY;
777  pMixPacket->channel=DUMMY_CHANNEL;
778  getRandom(pMixPacket->data,DATA_SIZE);
779  #ifdef LOG_PACKET_TIMES
780  setZero64(pQueueEntry->timestamp_proccessing_start);
781  #endif
782  #else
783  continue;
784  #endif
785  }
786  else
787  {
788  /* another error occured (happens sometimes while debugging because
789  * of interruption, if a breakpoint is reached -> poll() returns
790  * errorcode EINTR)
791  * Note: Any Error on select() does not mean, that the underliny connections have some error state, because
792  * in this case select() returns the socket and than this socket returns the error
793  */
794  continue;
795  }
796  }
797  else if(ret>0)
798  {
799  ret=pMuxSocket->receive(pMixPacket); //receives a whole MixPacket
800  #ifdef LOG_PACKET_TIMES
801  getcurrentTimeMicros(pQueueEntry->timestamp_proccessing_start);
802  #endif
803  #ifdef DATA_RETENTION_LOG
804  pQueueEntry->dataRetentionLogEntry.t_in=htonl(time(NULL));
805  #endif
806  if(ret!=MIXPACKET_SIZE)
807  {//something goes wrong...
808  CAMsg::printMsg(LOG_ERR,"CALastMix::lm_loopReadFromMix - received returned: %i\n",ret);
809  pLastMix->m_bRestart=true;
811  break;
812  }
813  }
814  #ifdef USE_POOL
815  #ifdef LOG_PACKET_TIMES
816  getcurrentTimeMicros(pQueueEntry->pool_timestamp_in);
817  #endif
818  pPool->pool((tPoolEntry*) pQueueEntry);
819  #ifdef LOG_PACKET_TIMES
820  getcurrentTimeMicros(pQueueEntry->pool_timestamp_out);
821  #endif
822  #endif
823  pQueue->add(pQueueEntry,sizeof(tQueueEntry));
824  getcurrentTimeMillis(keepaliveLast);
825  }
826  delete pQueueEntry;
827  pQueueEntry = NULL;
828  delete pSocketGroup;
829  pSocketGroup = NULL;
830  #ifdef USE_POOL
831  delete pPool;
832  pPool = NULL;
833  #endif
835  }
#define MONITORING_FIRE_NET_EVENT(e_type)
SINT32 getcurrentTimeMillis(UINT64 &u64Time)
Gets the current Systemtime in milli seconds.
Definition: CAUtil.cpp:252
SINT32 getcurrentTimeMicros(UINT64 &u64Time)
Gets the current Systemtime in micros seconds.
Definition: CAUtil.cpp:280
SINT32 getRandom(UINT32 *val)
Gets 32 random bits.
Definition: CAUtil.cpp:346
SINT32 msSleep(UINT32 ms)
Sleeps ms milliseconds.
Definition: CAUtil.cpp:406
UINT32 diff64(const UINT64 &bigop, const UINT64 &smallop)
Definition: CAUtil.hpp:398
void setZero64(UINT64 &op1)
Definition: CAUtil.hpp:355
#define MAX_READ_FROM_PREV_MIX_QUEUE_SIZE
Definition: StdAfx.h:226
#define MIX_POOL_TIMEOUT
Definition: StdAfx.h:245
#define DUMMY_CHANNEL
Definition: StdAfx.h:246
#define MIX_POOL_SIZE
Definition: StdAfx.h:243
signed int SINT32
Definition: basetypedefs.h:132
CAQueue * m_pQueueReadFromMix
Definition: CALastMix.hpp:154
CAMuxSocket * m_pMuxIn
Definition: CALastMix.hpp:152
volatile bool m_bRestart
Definition: CALastMix.hpp:151
UINT32 m_u32KeepAliveRecvInterval
Definition: CAMix.hpp:186
SINT32 receive(MIXPACKET *pPacket)
Receives a whole MixPacket.
This class implements the pool strategie of a Mix.
Definition: CAPool.hpp:43
SINT32 pool(tPoolEntry *pPoolEntry)
Definition: CAPool.cpp:83
This is a simple FIFO-Queue.
Definition: CAQueue.hpp:50
SINT32 add(const void *buff, UINT32 size)
Adds data to the Queue.
Definition: CAQueue.cpp:76
UINT32 getSize()
Returns the size of stored data in byte.
Definition: CAQueue.hpp:101
SINT32 add(SOCKET &s)
#define E_TIMEDOUT
Definition: errorcodes.hpp:10
@ ev_net_prevConnectionClosed
HCHANNEL channel
Definition: typedefs.hpp:117
UINT8 data[DATA_SIZE]
Definition: typedefs.hpp:121
UINT16 flags
Definition: typedefs.hpp:118
Definition: typedefs.hpp:169
MIXPACKET packet
Definition: typedefs.hpp:170
#define MIXPACKET_SIZE
Definition: typedefs.hpp:40
#define CHANNEL_DUMMY
Definition: typedefs.hpp:50
struct t_queue_entry tQueueEntry
Definition: typedefs.hpp:188
#define DATA_SIZE
Definition: typedefs.hpp:69

◆ lm_loopSendToMix()

THREAD_RETURN lm_loopSendToMix ( void *  param)

How to end this thread: 0.

set m_bRestart=true;

  1. Close connection to next mix
  2. put a byte in the Mix-Output-Queue

Definition at line 623 of file CALastMix.cpp.

624  {
625  #ifdef ENABLE_GPERFTOOLS_CPU_PROFILER
626  ProfilerRegisterThread();
627  #endif
628 
629  CALastMix* pLastMix = static_cast<CALastMix*>(param);
630  CAQueue* pQueue=pLastMix->m_pQueueSendToMix;
631  CAMuxSocket* pMuxSocket=pLastMix->m_pMuxIn;
632  SINT32 ret;
633  UINT32 len;
634  UINT32 u32KeepAliveSendInterval=pLastMix->m_u32KeepAliveSendInterval;
635 #ifndef USE_POOL
636  tQueueEntry* pQueueEntry=new tQueueEntry;
637  MIXPACKET* pMixPacket=&pQueueEntry->packet;
638 
639  while(!pLastMix->m_bRestart)
640  {
641  len=sizeof(tQueueEntry);
642  ret=pQueue->getOrWait((UINT8*)pQueueEntry,&len,u32KeepAliveSendInterval);
643  if(ret==E_TIMEDOUT)
644  {
645  pMixPacket->flags=CHANNEL_DUMMY;
646  pMixPacket->channel=DUMMY_CHANNEL;
647  getRandom(pMixPacket->data,DATA_SIZE);
648  }
649  else if(ret!=E_SUCCESS||len!=sizeof(tQueueEntry))
650  {
651  CAMsg::printMsg(LOG_ERR,"CALastMix::lm_loopSendToMix - Error in dequeueing MixPaket\n");
652  CAMsg::printMsg(LOG_ERR,"ret=%i len=%i\n",ret,len);
654  break;
655  }
656 #ifdef ANON_DEBUG_MODE
657  if (pMixPacket->flags&CHANNEL_DEBUG)
658  {
659  UINT8 base64Payload[DATA_SIZE << 1];
660  EVP_EncodeBlock(base64Payload, pMixPacket->data, DATA_SIZE);//base64 encoding (without newline!)
661  CAMsg::printMsg(LOG_DEBUG, "Send Downstream AN.ON packet to previous Mix debug: %s\n", base64Payload);
662  }
663 #endif
664  if (pMuxSocket->send(pMixPacket) != MIXPACKET_SIZE)
665  {
666  CAMsg::printMsg(LOG_ERR,"CALastMix::lm_loopSendToMix - Error in sending MixPaket\n");
668  break;
669  }
670 #ifdef LOG_PACKET_TIMES
671  if(!isZero64(pQueueEntry->timestamp_proccessing_start))
672  {
673  getcurrentTimeMicros(pQueueEntry->timestamp_proccessing_end);
674  pLastMix->m_pLogPacketStats->addToTimeingStats(*pQueueEntry,CHANNEL_DATA,false);
675  }
676 #endif
677  }
678  delete pQueueEntry;
679  pQueueEntry = NULL;
680 #else
681  CAPool* pPool=new CAPool(MIX_POOL_SIZE);
682  tPoolEntry* pPoolEntry=new tPoolEntry;
683  MIXPACKET* pMixPacket=&pPoolEntry->packet;
684  while(!pLastMix->m_bRestart)
685  {
686  len=sizeof(tQueueEntry);
687  SINT32 ret=pQueue->getOrWait((UINT8*)pPoolEntry,&len,MIX_POOL_TIMEOUT);
688  if(ret==E_TIMEDOUT)
689  {
690  pMixPacket->flags=CHANNEL_DUMMY;
691  pMixPacket->channel=DUMMY_CHANNEL;
692  getRandom(pMixPacket->data,DATA_SIZE);
693  #ifdef LOG_PACKET_TIMES
694  setZero64(pPoolEntry->timestamp_proccessing_start);
695  #endif
696  }
697  else if(ret!=E_SUCCESS||len!=sizeof(tQueueEntry))
698  {
699  break;
700  }
701  #ifdef LOG_PACKET_TIMES
702  getcurrentTimeMicros(pPoolEntry->pool_timestamp_in);
703  #endif
704  pPool->pool(pPoolEntry);
705  #ifdef LOG_PACKET_TIMES
706  getcurrentTimeMicros(pPoolEntry->pool_timestamp_out);
707  #endif
708  if(pMuxSocket->send(pMixPacket)!=MIXPACKET_SIZE)
709  break;
710 #ifdef LOG_PACKET_TIMES
711  if(!isZero64(pPoolEntry->timestamp_proccessing_start))
712  {
713  getcurrentTimeMicros(pPoolEntry->timestamp_proccessing_end);
714  pLastMix->m_pLogPacketStats->addToTimeingStats(*pPoolEntry,CHANNEL_DATA,false);
715  }
716 #endif
717  }
718  delete pPoolEntry;
719  pPoolEntry = NULL;
720  delete pPool;
721  pPool = NULL;
722 #endif
723  CAMsg::printMsg(LOG_DEBUG,"Exiting Thread SendToMix\n");
725  }
bool isZero64(UINT64 &op1)
Definition: CAUtil.hpp:464
CAQueue * m_pQueueSendToMix
Definition: CALastMix.hpp:153
UINT32 m_u32KeepAliveSendInterval
Definition: CAMix.hpp:187
SINT32 send(MIXPACKET *pPacket)
Sends a MixPacket over the Network.
SINT32 getOrWait(UINT8 *pbuff, UINT32 *psize)
Gets data from the Queue or waits until some data is available, if the Queue is empty.
Definition: CAQueue.cpp:209
const SINT32 E_SUCCESS
Definition: errorcodes.hpp:2
#define CHANNEL_DATA
Definition: typedefs.hpp:42
#define CHANNEL_DEBUG
Definition: typedefs.hpp:51
UINT16 len
Definition: typedefs.hpp:0
tQueueEntry tPoolEntry
Definition: typedefs.hpp:192