⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 analyze.c

📁 计费部分源程序。我认为该软件极好
💻 C
📖 第 1 页 / 共 3 页
字号:
/*****************************************************************************
				用户数据分析程序
用于分析用户登陆数据文件,生成每个用户自己的数据文件。
注意:如果定义了宏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 + -