|
Mixe for Privacy and Anonymity in the Internet
|
00001 /*$Id: packetintro.cpp 2739 2007-10-16 11:56:26Z rolf $*/ 00002 /*********************************************************************************************************/ 00003 /* */ 00004 /* tramo V0.9, server traffic monitor, (c) SDTFA 05/2007 */ 00005 /* */ 00006 /* PacketIntro, Send packet count to tramo process */ 00007 /* */ 00008 /* Date CRQ Revision Change description Autor */ 00009 /*-------------------------------------------------------------------------------------------------------*/ 00010 /* 16.05.07 1.0 file created Freddy */ 00011 /* */ 00012 /*********************************************************************************************************/ 00013 00014 #ifdef SDTFA 00015 00016 #include "packetintro.h" 00017 #include "StdAfx.h" 00018 #include "CAMsg.hpp" 00019 00020 #include <stdlib.h> 00021 #include <sys/shm.h> 00022 #include <errno.h> 00023 #include <assert.h> 00024 00025 static void SDTFA_NeedMixSharedMemory(void); 00026 static SDTFA_MixShared *SDTFA_TryMixSharedMemory(void); 00027 00028 static SDTFA_MixShared *SDTFA_MixShm = NULL; 00034 void SDTFA_IncrementShmPacketCount(void) 00035 { 00036 // create/attach shared memory (if not done) 00037 if (!MixShm) 00038 SDTFA_NeedMixSharedMemory(); 00039 // atomic access (32 bit) 00040 assert(sizeof(MixShm->PacketCount)==4); 00041 MixShm->PacketCount++; 00042 } 00043 00048 static void SDTFA_NeedMixSharedMemory(void) 00049 { 00050 // try to create/attach shared memory of mix process 00051 if (!SDTFA_TryMixSharedMemory()) 00052 { 00053 // if not creatable, use dummy 00054 MixShm=(SDTFA_MixShared*)malloc(sizeof(SDTFA_MixShared)); 00055 MixShm->PacketCount=0; 00056 } 00057 } 00058 00063 static SDTFA_MixShared *SDTFA_TryMixSharedMemory(void) 00064 { 00065 int mid; void *base; 00066 CAMsg::printMsg(LOG_DEBUG,"Create/attach shared memory of mix process.\n"); 00067 assert(sizeof(key_t)==4); 00068 if ((mid=shmget(SDTFA_MIX_SHARED_KEY,sizeof(SDTFA_MixShared),SHM_MODE))<0) 00069 { 00070 // no such file or directory ? 00071 if (errno==ENOENT) 00072 { 00073 // cant attach, try to create 00074 if ((mid=shmget(SDTFA_MIX_SHARED_KEY,sizeof(SDTFA_MixShared),IPC_CREAT|SHM_MODE))<0) 00075 { 00076 CAMsg::printMsg(LOG_ERR,"Cant create shared memory of mix process.\n"); 00077 return(NULL); 00078 } 00079 } 00080 else 00081 { 00082 CAMsg::printMsg(LOG_ERR,"Cant attach to shared memory of mix process.\n"); 00083 return(NULL); 00084 } 00085 } 00086 // locate the shared memory 00087 if ((base=shmat(mid,NULL,0))==(void*)(-1)) 00088 { 00089 CAMsg::printMsg(LOG_ERR,"Cant locate shared memory.\n"); 00090 return(NULL); 00091 } 00092 // success 00093 MixShm=(SDTFA_MixShared*)base; 00094 return(MixShm); 00095 } 00096 00097 #endif
1.7.6.1