📄 lan_ip_taday.c
字号:
/*****************************************************************************
发消息命令scs-listen转存ipdata.dat,
读IP流量文件IpDataFile[20] = "ipdata.dat.tmp"
分析数据,将结果追加到a_tmp文件。等待?_night分析
******************************************************************************/
#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 LIMIT_LOGIN_LOCATION 0x0001/*限制用户在某些地址登陆*/
#define HOST_TYPE 0 /*地址为主机地址*/
/* david */
#define K 1024
#define NET_NUM 100
#define IP_NUM 1000
struct {
int num;
char b[20], e[20];
}b_net[NET_NUM],n_net[NET_NUM],w_net[NET_NUM];
struct {
char ip[20];
double n_in, n_out, w_in, w_out, n_sum, w_sum, total;
} ip_list[IP_NUM];
int b_record=0, n_record=0, w_record=0 , ip_list_up=0;
int DEBUG_FLAG = 0;
float ratio=1;
/*end for david*/
#define PrintError printf
/*响应转储程序的请求,转储程序在每天运行一次*/
#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 IpDataFile[80] = "ipdata1.dat.tmp";/*出入数据细节文件*/
char NET_CONF[80] = "net.conf";
char A_TMP[80] = "a_tmp";
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;/*输出消息标识*/
#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);
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];/*各个网段的名字,如国内网段,国外网段,免费网段等*/
void ReadNetPartFile();
/*用户列表定义*/
#define UserIDFile "system/userID.sys"
char userFile[20] = "system/user.file";
#define USER_HASH_LEN 250
/*地址列表定义,该地址为主机地址或网络地址*/
typedef struct _AddressTYPE
{
int type;/*0---主机地址,1---网络地址*/
struct in_addr addr;/*主机或网络地址*/
struct in_addr netMask;/*网络屏蔽码*/
struct _AddressTYPE* next;
struct _AddressTYPE* prev;
}AddressType;
#define USER_NAME_LEN 11
#define USER_PASS_LEN 11
/*用户列表定义*/
typedef struct _UserTYPE
{
int userID; /*用户的标识号,对于每个用户来说,该值唯一*/
char userName[USER_NAME_LEN];/*用户名称*/
char userPassword[USER_PASS_LEN];/*用户密码*/
int level;/*用户的级别,0--进行使用量限制,1---不进行使用量限制*/
int groupID;/*该用户的组ID*/
long remainData;/*该用户剩余的数据量*/
float remainMoney;/*该用户剩余的钱数*/
int refrenceNum;/*该用户目前被引用数*/
int state;/*该用户当前状态,0---未上网,1---上网*/
int flag;/*该用户的表示,如flag&LIMIT_LOCATION=true,则进行检查它的登陆位置*/
long addUserDate;/*用户被加入的时间*/
AddressType* loginAddressList;/*当前用户已经从几处登陆*/
AddressType* allowLoginAddressList;/*若限制用户登陆地点,则用本地址列表进行判断*/
struct _UserTYPE* nextInHash;
struct _UserTYPE* prevInHash;
struct _UserTYPE* nextInList;
struct _UserTYPE* prevInList;
}UserType;
UserType* UserAccount[USER_HASH_LEN];/*用户HASH表*/
UserType* UserHead,*UserTail;/*用户链表表头和表尾*/
#include "ta_day.h"
/*
转换IP地址,从字符串到in_addr结构,或相反
flag = 0, from string to struct
flag = 1, from struct to string
*/
/*
初始化消息队列
本程序中有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);
}
}
/*
初始化变量
*/
void InitVar()
{
FILE* fptr;
char str[200];
char tmp[50];
int rtn;
char * ch1,*ch2,*ch3;
int pid;
InitMessage();
/*得到本进程的ID号*/
pid = getpid();
/*给计费进程发送消息,令计费进程转储数据文件*/
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);
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("read user account file.\n");
ReadUserListFromFile(UserFile);
}
/*
保存一天的数据量,本函数将一天的数据存入统计文件中.
该统计文件的构成:
1.名称:公共路径/groupID/userID.sum
2.大小:12*32*SumDataSize
12-----指12个月,每个月1行
32-----每个月最多31天,此外第0个元素为一个月的总数
3.该文件为二进制文件
4.该文件在增加用户时创建
5.该文件在删除用户时,被移到delete目录中
*/
void SaveOneDayData()
{
FILE * fptr;
char fileName[100];
SumDataType data,*dataPtr = &data;
int i,j;
while (1)
{
if (1)
{/*数据发生变化*/
/* sprintf(fileName,"%s%d/%d.%s",*/
if ((fptr = fopen(fileName,"rb+")) == NULL)
{
PrintError("don't open file %s\n",fileName);
continue;
}
/*1.修改当天的数据*/
/*注意:以后可用将*32改为向左移5位*/
/*移动文件指针到相应位置*/
fseek(fptr,((CurrentMonth-1)*32 + CurrentDay)*SumDataSize,SEEK_SET);
/*读取文件中的数据*/
fread(dataPtr,SumDataSize,1,fptr);
/*修改数据*/
for (i = 0; i < TIME_STAGE_NUM; i++)
for (j = 0; j < NET_PART_NUM; j++)
{/*
data.inPackets[i][j] += user->data.inPackets[i][j];
data.inPacketsBytes[i][j] += user->data.inPacketsBytes[i][j];
data.outPackets[i][j] += user->data.outPackets[i][j];
data.outPacketsBytes[i][j] += user->data.outPacketsBytes[i][j];
*/}
/*重新定位*/
fseek(fptr,-SumDataSize,SEEK_CUR);
/*再写入*/
fwrite(dataPtr,SumDataSize,1,fptr);
/*2.修改月总数数据*/
/*移动文件指针到相应位置*/
fseek(fptr,(CurrentMonth-1)*32*SumDataSize,SEEK_SET);
/*读取文件中的数据*/
fread(dataPtr,SumDataSize,1,fptr);
/*修改数据*/
for (i = 0; i < TIME_STAGE_NUM; i++)
for (j = 0; j < NET_PART_NUM; j++)
{/*
data.inPackets[i][j] += user->data.inPackets[i][j];
data.inPacketsBytes[i][j] += user->data.inPacketsBytes[i][j];
data.outPackets[i][j] += user->data.outPackets[i][j];
data.outPacketsBytes[i][j] += user->data.outPacketsBytes[i][j];
*/}
/*重新定位*/
fseek(fptr,-SumDataSize,SEEK_CUR);
/*再写入*/
fwrite(dataPtr,SumDataSize,1,fptr);
fclose(fptr);
/*将数据清0*/
/* memset(&user->data,0,SumDataSize);*/
}
/* user = user->nextInList;*/
}
}
/*add by tang in 1998.6.30*/
void GetUserName(char* userIDStr,char* userName)
{
int userID;
UserType *user;
userID = atoi(userIDStr);
user = FindUserByID(userID);
if (user == NULL)
strcpy(userName,"");
else strcpy(userName,user->userName);
}
/*add end */
/*
分析IP流量细节文件
IP流量细节文件格式:source Ip|destination Ip|length
*/
void AnalyzeIpDataFile()
{
FILE* fptr;
char buff[200];
char result[30];
char* startField, *ptr;
int ip_num;
struct in_addr srcIp,dstIp;
struct in_addr *srcIpPtr = &srcIp,*dstIpPtr = &dstIp;
int in_out; /* >0 代表出去,<0 代表进入*/
long length;
char s_1[20],s_2[20],s_3[20];
char in_out_flag[5],userID[20],userName[20];
if ((fptr = fopen(IpDataFile,"r")) == NULL)
{/*正常传入的IP包数据细节文件指针*/
PrintError("Don't open file:%s.\n",IpDataFile);
return;
}
/*printf("get file : %s\n",IpDataFile);*/
while (!feof(fptr))
{
fgets(buff,200,fptr);
if (feof(fptr))
break;
/*add by tang in 1998.6.30*/
/*得到进入外出标识*/
ptr = strtok(buff,"|");
if (ptr == NULL) continue;
strcpy(in_out_flag,ptr);
/*得到用户ID*/
ptr = strtok(NULL,"|");
if (ptr == NULL) continue;
strcpy(userID,ptr);
/*add end*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -