Mixe for Privacy and Anonymity in the Internet
packetintro.cpp
Go to the documentation of this file.
1 /*$Id: packetintro.cpp 2739 2007-10-16 11:56:26Z rolf $*/
2 /*********************************************************************************************************/
3 /* */
4 /* tramo V0.9, server traffic monitor, (c) SDTFA 05/2007 */
5 /* */
6 /* PacketIntro, Send packet count to tramo process */
7 /* */
8 /* Date CRQ Revision Change description Autor */
9 /*-------------------------------------------------------------------------------------------------------*/
10 /* 16.05.07 1.0 file created Freddy */
11 /* */
12 /*********************************************************************************************************/
13 
14 #ifdef SDTFA
15 
16 #include "packetintro.h"
17 #include "StdAfx.h"
18 #include "CAMsg.hpp"
19 
20 #include <stdlib.h>
21 #include <sys/shm.h>
22 #include <errno.h>
23 #include <assert.h>
24 
25 static void SDTFA_NeedMixSharedMemory(void);
26 static SDTFA_MixShared *SDTFA_TryMixSharedMemory(void);
27 
28 static SDTFA_MixShared *SDTFA_MixShm = NULL;
34 void SDTFA_IncrementShmPacketCount(void)
35 {
36 // create/attach shared memory (if not done)
37 if (!MixShm)
38  SDTFA_NeedMixSharedMemory();
39 // atomic access (32 bit)
40 assert(sizeof(MixShm->PacketCount)==4);
41 MixShm->PacketCount++;
42 }
43 
48 static void SDTFA_NeedMixSharedMemory(void)
49 {
50 // try to create/attach shared memory of mix process
51 if (!SDTFA_TryMixSharedMemory())
52  {
53  // if not creatable, use dummy
54  MixShm=(SDTFA_MixShared*)malloc(sizeof(SDTFA_MixShared));
55  MixShm->PacketCount=0;
56  }
57 }
58 
63 static SDTFA_MixShared *SDTFA_TryMixSharedMemory(void)
64 {
65 int mid; void *base;
66 CAMsg::printMsg(LOG_DEBUG,"Create/attach shared memory of mix process.\n");
67 assert(sizeof(key_t)==4);
68 if ((mid=shmget(SDTFA_MIX_SHARED_KEY,sizeof(SDTFA_MixShared),SHM_MODE))<0)
69  {
70  // no such file or directory ?
71  if (errno==ENOENT)
72  {
73  // cant attach, try to create
74  if ((mid=shmget(SDTFA_MIX_SHARED_KEY,sizeof(SDTFA_MixShared),IPC_CREAT|SHM_MODE))<0)
75  {
76  CAMsg::printMsg(LOG_ERR,"Cant create shared memory of mix process.\n");
77  return(NULL);
78  }
79  }
80  else
81  {
82  CAMsg::printMsg(LOG_ERR,"Cant attach to shared memory of mix process.\n");
83  return(NULL);
84  }
85  }
86 // locate the shared memory
87 if ((base=shmat(mid,NULL,0))==(void*)(-1))
88  {
89  CAMsg::printMsg(LOG_ERR,"Cant locate shared memory.\n");
90  return(NULL);
91  }
92 // success
93 MixShm=(SDTFA_MixShared*)base;
94 return(MixShm);
95 }
96 
97 #endif
static SINT32 printMsg(UINT32 typ, const char *format,...)
Writes a given message to the log.
Definition: CAMsg.cpp:251