?? analyze.c
字號:
/*****************************************************************************
用戶數據分析程序
用于分析用戶登陸數據文件,生成每個用戶自己的數據文件。
注意:如果定義了宏ANALYZE_BY_IP,則按照IP流量文件分析數據,否則按照用戶登錄文件分析流量
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/msg.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <netinet/ip_var.h>
#include <netinet/tcp.h>
#include <netinet/tcpip.h>
#include <signal.h>
#define PrintError printf
#define WAIT_TIMEOUT 10
/*響應轉儲程序的請求,轉儲程序在每天運行一次*/
#define MAINTAIN_DATA 900/*轉儲數據,將數據文件關閉再從新打開*/
#define MAINTAIN_DATA_FILE_SUCCESS 901/*轉儲數據成功*/
/*輸入輸出消息*/
#define SCS_INMSGKEY 500/*與JF進程進行通訊*/
#define SCS_OUTMSGKEY 400
#define SCS_CGIINMSGKEY 460/*與USER進程進行通訊*/
#define SCS_CGIOUTMSGKEY 450
#define MESSAGE_PRIORITY 0
struct msg_scs {
long mtype;
int command;
char value[200];
};
char * InitFile="scs_init.file";/*系統初始化讀取的文件名*/
char InternalNetFile[30] = "internalnet.sys";/*內部網段文件名稱,*/
char LeadIpInfoFile[30] = "leadipinfo.sys";/*領導IP信息文件名稱,*/
char IpDataControlFile[30] = "ipdatacontrol.dat";/*正常IP包數據控制文件名稱*/
char IpDataFile[20] = "ipdata.dat";/*出入數據細節文件*/
char IpSumDataFile[30] = "ipsumdata.dat";/*正常傳入傳出的IP包數據總體文件名稱*/
char LeadIpDataFile[30] = "leadipdata.dat";/*領導IP包數據文件名稱*/
char BlockIpDataFile[30]="blockipdata.dat";/*應被封鎖的IP包數據文件名稱,因為有可能仍有一些數據傳輸.*/
char CurTimeIDFile[30] = "CurTimeID.sys";/*保存全局變量CurTimeID的文件名稱,*/
FILE* IpDataControlFptr;/*正常IP包數據控制文件指針*/
FILE* IpDataFptr;/*正常傳入的IP包數據詳細文件指針*/
FILE* IpSumDataFptr;/*正常傳入傳出的IP包總體數據文件指針*/
FILE* LeadIpDataFptr;/*領導IP包數據文件指針*/
FILE* BlockIpDataFptr;/*應被封鎖的IP包數據文件指針,因為有可能仍有一些數據傳輸.*/
struct msg_scs InMessage;/*用于輸入消息*/
struct msg_scs OutMessage;/*用于輸出消息*/
int CGIInMessageID;/*與UMG進程進行通訊的輸入消息標識*/
int CGIOutMessageID;/*與UMG進程進行通訊的輸出消息標識*/
int MyInMessageID;/*輸入消息標識*/
int BackInMessageID;/*輸入消息標識*/
int MyOutMessageID;/*輸出消息標識*/
int BackOutMessageID;/*輸出消息標識*/
char *SumSuffix = ".sum";/*每個用戶月統計文件后綴*/
#define NET_PART_NUM 1/*網絡分為幾個段落,如國內段,國外段,免費段等*/
#define NET_PART_1 0 /*確省段落,一般應為國外網段*/
#define NET_PART_2 1
#define NET_PART_3 2
#define TIME_STAGE_NUM 1 /*每天分為多少個時間段*/
#define TIME_STAGE_1 0 /*時間段1*/
#define TIME_STAGE_2 1 /*時間段2*/
#define TIME_STAGE_3 2 /*時間段3*/
/*
統計數據結構
進入的數據包個數包括:在時間段n內,有網段m進入的包個數
*/
typedef struct _SumDataType
{
long inPackets[TIME_STAGE_NUM][NET_PART_NUM];/*進入的數據包個數*/
long inPacketsBytes[TIME_STAGE_NUM][NET_PART_NUM];/*進入的數據包字節數*/
long outPackets[TIME_STAGE_NUM][NET_PART_NUM];/*出去的數據包個數*/
long outPacketsBytes[TIME_STAGE_NUM][NET_PART_NUM];/*出去的數據包字節數*/
long connectTime;/*連接時間*/
} SumDataType;
int SumDataSize = sizeof(SumDataType);
/*用戶列表定義*/
#define USER_HASH_LEN 250
typedef struct _UserTYPE
{
int userID; /*用戶的標識號,對于每個用戶來說,該值唯一*/
int groupID;/*用戶的組ID*/
SumDataType data; /*一天的數據結構*/
int change;/*是否數據發生了變化*/
struct _UserTYPE* nextInHash;
struct _UserTYPE* prevInHash;
struct _UserTYPE* nextInList;
struct _UserTYPE* prevInList;
}UserType;
UserType* UserAccount[USER_HASH_LEN];/*用戶HASH表*/
UserType* UserHead,*UserTail;/*用戶鏈表表頭和表尾*/
int CurrentMonth;/*當前月份*/
int CurrentDay;/*當前日期*/
int CurrentTimeStage;/*當前時間段*/
char PATH[100];/*文件的路徑*/
char *NetPartFile = "system/netpart.sys";/*網段文件*/
char *UserFile = "system/user.file";
/*
網絡或主機地址
*/
typedef struct _InternalNetTYPE
{
struct in_addr netAddress;/*內部網的網絡地址*/
struct in_addr netMask; /*內部網絡的網絡屏蔽碼*/
int type;/*標識本地址是網絡地址,還是IP地址,0--net address,1--ip address*/
int flag;/* 該內部網絡的標識*/
struct _InternalNetTYPE* next;
}InternalNetType;
InternalNetType* NetPartList[NET_PART_NUM];/*各個網段鏈表的頭指針*/
char NetPartName[NET_PART_NUM][50];/*各個網段的名字,如國內網段,國外網段,免費網段等*/
#ifdef ZOOM-OP
char * ZoomRatioFile = "system/zoomratio.sys";
float ZoomRatio[2] = {1.0,1.0};
#endif
void ReadNetPartFile();
/*
轉換IP地址,從字符串到in_addr結構,或相反
flag = 0, from string to struct
flag = 1, from struct to string
*/
void TransIpAddress(int flag ,char* str,struct in_addr *ip)
{
int tmp[4];
if (flag == 0)
{
sscanf(str,"%d.%d.%d.%d",&tmp[0],&tmp[1],&tmp[2],&tmp[3]);
(ip->s_net) = tmp[0];
(ip->s_host) = tmp[1];
(ip->s_lh) = tmp[2];
(ip->s_impno) = tmp[3];
}
else
{
sprintf(str,"%d.%d.%d.%d",
ip->s_net,
ip->s_host,
ip->s_lh,
ip->s_impno);
}
}
/*
從一個字符串中得到第一個域,該域不含空白字符
返回值:為輸入字符串中第一個域后的第一個字符的指針
*/
char* GetOneField(char* str,char* result)
{
char* ch1,*ch2,*ch3;
ch1 = str;
while (*ch1 == ' ' || *ch1 =='\t')
ch1++;
ch2 = ch1;
ch3 = result;
while ((*ch2 != '|') &&(*ch2 != '#') && (*ch2 != ' ') && (*ch2 != '\t') && (*ch2 != '\n') && (*ch2 != '\r') && (*ch2 != 0))
{
*ch3 = *ch2;
ch2++;
ch3++;
}
*ch3 = 0;
while ((*ch2 != '|') && (*ch2 != '\0'))
ch2++;
ch2++;
return ch2;
}
/*
初始化消息隊列
本程序中有2個消息隊列,一個消息對列用于得到輸入消息,MyInMessageID
一個消息隊列用于輸出消息,MyOutMessageID
*/
void InitMessage()
{
printf("enter InitMessage..............\n");
MyInMessageID = msgget(SCS_INMSGKEY, 0777|IPC_CREAT);
BackInMessageID = MyInMessageID;
if (MyInMessageID == -1)
{
PrintError(" in msgget failed!\n ");
exit(1);
}
MyOutMessageID = msgget(SCS_OUTMSGKEY, 0777|IPC_CREAT);
BackOutMessageID = MyOutMessageID;
if (MyOutMessageID == -1)
{
PrintError(" out msgget failed!\n ");
exit(1);
}
CGIInMessageID = msgget(SCS_CGIINMSGKEY, 0777|IPC_CREAT);
if (CGIInMessageID == -1)
{
PrintError(" in msgget failed!CGI In Message\n ");
exit(1);
}
CGIOutMessageID = msgget(SCS_CGIOUTMSGKEY, 0777|IPC_CREAT);
if (CGIOutMessageID == -1)
{
PrintError(" out msgget failed!CGI OUT MESSGAE\n ");
exit(1);
}
}
#ifdef ZOOM-OP
/*
讀取縮放用戶數據的比例文件
*/
void ReadZoomRatioFromFile()
{
FILE* fptr;
char str[100];
float ratio;
if ((fptr = fopen(ZoomRatioFile,"r")) == NULL)
{
PrintError("Don't open zoom ratio file:%s\n",ZoomRatioFile);
return;
}
fgets(str,100,fptr);
sscanf(str,"%f",&ratio);
if ((ratio > 0.01) && (ratio < 10.0))
{
ZoomRatio[0] = ratio;
}
fgets(str,100,fptr);
sscanf(str,"%f",&ratio);
if ((ratio > 0.01) && (ratio < 10.0))
{
ZoomRatio[1] = ratio;
}
/*
printf("zoom=%f,%f\n",ZoomRatio[0],ZoomRatio[1]);
*/
}
#endif
void TimeCheck(int signo)
{
printf("wait message from jf or user,but timeout...\nso ,quit..\n");
exit(1);
}
/*
初始化變量
*/
void InitVar()
{
FILE* fptr;
char str[200];
char tmp[50];
int rtn;
char * ch1,*ch2,*ch3;
int pid;
signal(SIGALRM,TimeCheck);
alarm(WAIT_TIMEOUT);
#ifdef ZOOM-OP
ReadZoomRatioFromFile();
#endif
InitMessage();
/*得到本進程的ID號*/
pid = getpid();
/*給計費進程發送消息,令計費進程轉儲數據文件*/
printf("Send MAINTAIN_DATA command to jf .\n");
OutMessage.mtype = pid;
OutMessage.command = MAINTAIN_DATA;
MyOutMessageID = BackOutMessageID;
msgsnd(MyOutMessageID, &OutMessage, sizeof (struct msg_scs),0);
/*等待計費進程的返回消息,再繼續*/
MyInMessageID = BackInMessageID;
rtn = msgrcv(MyInMessageID, &InMessage, sizeof(struct msg_scs), pid, 0);
printf("Get Message From jf.\n");
if (rtn <= 0)
{
PrintError("Maintain recieve message Error!\n");
exit(1);
}
if (InMessage.command != MAINTAIN_DATA_FILE_SUCCESS)
{
PrintError("Maintain recieve message Error FROM JF!command != MAINTAIN_DATA_FILE_SUCCESS\n");
exit(1);
}
/*給用戶管理進程發消息,保存用戶帳戶文件*/
printf("Send command MAINTAIN_DATA to user.\n");
msgsnd(CGIOutMessageID, &OutMessage, sizeof (struct msg_scs),0);
/*等待用戶管理進程的返回消息,再繼續*/
rtn = msgrcv(CGIInMessageID, &InMessage, sizeof(struct msg_scs), pid, 0);
printf("Get message from user.\n");
if (rtn <= 0)
{
PrintError("Maintain recieve message Error!\n");
exit(1);
}
if (InMessage.command != MAINTAIN_DATA_FILE_SUCCESS)
{
PrintError("Maintain recieve message Error FROM USER!command != MAINTAIN_DATA_FILE_SUCCESS\n");
exit(1);
}
alarm(0);
/*初始化數據文件*/
/*首先讀取各系統變量的值*/
if ((fptr = fopen(InitFile,"r")) == NULL)
{
printf("Can't open Init File:%s.\nQuit!\n",InitFile);
fflush(stdout);
exit(1);
}
/*(1).內部網段文件名稱,InternalNetFile*/
fgets(str,150,fptr);
GetOneField(str,InternalNetFile);
/*(2).領導信息文件名稱,LeaderInfoFile*/
fgets(str,150,fptr);
GetOneField(str,LeadIpInfoFile);
/*(3).正常IP包數據控制文件名稱,IpDataControlFile*/
fgets(str,150,fptr);
GetOneField(str,IpDataControlFile);
/*(4).正常傳入傳出的IP包數據細節文件名稱,IpDataFile*/
fgets(str,150,fptr);
GetOneField(str,IpDataFile);
/*(6).正常傳入傳出的IP包數據總體文件名稱,IpSumDataFile*/
fgets(str,150,fptr);
GetOneField(str,IpSumDataFile);
/*(7).領導IP包數據文件名稱,LeaderIpDataFile*/
fgets(str,150,fptr);
GetOneField(str,LeadIpDataFile);
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -