📄 lan_ip_taday.c
字号:
/*得到源IP地址*/
ptr = strtok(NULL,"|");
if (ptr == NULL) continue;
strcpy(s_1,ptr);
/*得到目的IP地址*/
ptr = strtok(NULL,"|");
if (ptr == NULL) continue;
strcpy(s_2,ptr);
/*得到长度*/
ptr = strtok(NULL,"|");
if (ptr == NULL) continue;
strcpy(s_3,ptr);
length= atol(s_3);
length *= ratio/10;
in_out = in_b_net(s_1,s_2); /* 判断是否在本地地址段组内*/
if ( in_out == 0) /*不在本地地址段组内*/
continue;
if ( (in_b_net(s_1,s_2)>0) && (in_b_net(s_2,s_1)>0) )
continue;
/*修改内存中的数据*/
/*add by tang in 1998.6.30*/
GetUserName(userID,userName);
printf("useid=%s,userName=%s\n",userID,userName);
if (strcmp(userName,"") == 0)
continue;
/*add end*/
if (in_out > 0 ) /* >0 代表出去,<0 代表进入*/
{
ip_num = no_b_net(userName); /*取本地地址数组下标*/
/*printf("ip_num=%d,s_2 in %d\n",ip_num,n_w(s_2));*/
if( n_w(s_2) == 1) /* 1 代表国内, 2 代表国外*/
ip_list[ip_num].n_out += length;
else ip_list[ip_num].w_out += length;
} else
{
ip_num = no_b_net(userName); /*取本地地址数组下标*/
/*printf("ip_num=%d,s_2 in %d\n",ip_num,n_w(s_2));*/
if( n_w(s_1) == 1) /* 1 代表国内, 2 代表国外*/
ip_list[ip_num].n_in += length;
else ip_list[ip_num].w_in += length;
}
} /* end while*/
}
int n_w(char *s_1)/*判国内国外: 1 代表国内, 2 代表国外*/
{
int i,j;
char b_1[20], b_2[20];
for (i=0;i<n_record;i++)
{
strcpy(b_1,n_net[i].b);
strcpy(b_2,n_net[i].e);
/* if ((inet_addr(n_net[i].b) <= inet_addr(s_1) ) &&
(inet_addr(n_net[i].e) >= inet_addr(s_1) ) )*/
if ( (ip_compare(b_1,s_1) <= 0 ) &&
(ip_compare(b_2,s_1) >= 0 ) )
return 1; /* 1 代表国内*/
}
/*修改, 不在国内就在国外*/
return 2; /* 2 代表国外 */
for (i=0;i<w_record;i++)
{
strcpy(b_1,w_net[i].b);
strcpy(b_2,w_net[i].e);
/* if ((inet_addr(w_net[i].b) <= inet_addr(s_1) ) &&
(inet_addr(w_net[i].e) >= inet_addr(s_1) ) )*/
if ( (ip_compare(b_1,s_1) <= 0 ) &&
(ip_compare(b_2,s_1) >= 0 ) )
return 2; /* 2 代表国外 */
}
}
int no_b_net(char* s_1) /*取本地地址数组下标*/
{
int j;
char s[20];
strcpy(s,s_1);
/*printf("ip_list_up=%d\n",ip_list_up);*/
for(j=0; j<ip_list_up;j++)
if (!strcmp(ip_list[j].ip,s))
return j;
if (j==ip_list_up){
strcpy(ip_list[j].ip,s);
ip_list_up++;
return j;
}
}
int in_b_net(char* s1, char* s2)/* 判断是否在本地地址段组内*/
{
int i,j;
char s_1[20],s_2[20], b_1[20], b_2[20];
unsigned long i1,i2,i3;
strcpy(s_1,s1);
strcpy(s_2,s2);
for (i=0;i<b_record;i++)
{
/*printf("b_record=%d\n",b_record);
i1 = inet_addr(b_net[i].b);
i2 = inet_addr(s_1);
i3 = inet_addr(b_net[i].e);*/
strcpy(b_1,b_net[i].b);
strcpy(b_2,b_net[i].e);
/*printf("<%s> - <%s> = %d\n",b_1,s_1,ip_compare(b_1,s_1));
printf("<%s> - <%s> = %d\n",s_1,b_2,ip_compare(s_1,b_2));*/
if ( (ip_compare(b_1,s_1) <= 0 ) &&
(ip_compare(s_1,b_2) <= 0 ) )
return i+1; /* > 0 从本地出去*/
}
for (i=0;i<b_record;i++)
{
/* if ((inet_addr(b_net[i].b) <= inet_addr(s_2) ) &&
(inet_addr(b_net[i].e) >= inet_addr(s_2) ) )*/
strcpy(b_1,b_net[i].b);
strcpy(b_2,b_net[i].e);
if ( (ip_compare(b_1,s_2) <= 0 ) &&
(ip_compare(s_2,b_2) <= 0 ) )
return -i-1; /* < 0 进入本地*/
}
return 0; /*不在*/
}
/*
转储总体文件以外的文件
方法:将文件名加上当天的日期
*/
void MaintainDataFile()
{
char command[150];
char today[20];
struct tm *now;
time_t day;
/*得到当前日期*/
time(&day);
now = localtime(&day);
sprintf(today,"%d-%d",now->tm_mon+1,now->tm_mday);
}
void net_init() /* david, read net.conf */
{
int i,j;
FILE *fp;
char buff[200];
char filename[80];
char flag[20];
char st_net[20];
char e_net[20];
char temp[80];
char *ptr;
long w=0, b=0, n=0;
/* printf("\n net_init start \n");*/
strcpy(filename,NET_CONF);
if((fp=fopen(filename,"r")) == NULL)
{
printf("not found %s \n",filename);
exit(-1);
}
/* printf("found %s\n",filename);*/
while ( ! feof(fp))
{
fgets(buff,200,fp);
/*printf("line=%s",buff);*/
ptr = strtok(buff," ");
if (ptr == NULL) continue;
strcpy(flag,ptr); /*flag*/
/*printf("flag=<%s>\n",flag);*/
ptr = strtok(NULL," ");
if (ptr == NULL) continue;
strcpy(temp,ptr); /*number*/
i = atol(temp);
/*printf(" i = %ld\n",i);*/
ptr = strtok(NULL," ");
if (ptr == NULL) continue;
strcpy(st_net,ptr); /*net start*/
/*printf("s_net=%s\n",st_net);*/
ptr = strtok(NULL," ");
if (ptr == NULL) continue;
strcpy(e_net,ptr); /*net end*/
/*printf("e_net=%s\n",e_net);*/
if (strcmp(flag,"b")==0)
{
b_net[i-1].num = i;
strcpy(b_net[i-1].b,st_net);
strcpy(b_net[i-1].e,e_net);
b_record++;
continue;
}
if (strcmp(flag,"n")==0)
{
n_net[i-1].num = i;
strcpy(n_net[i-1].b,st_net);
strcpy(n_net[i-1].e,e_net);
n_record++;
continue;
}
if (strcmp(flag,"w")==0)
{
w_net[i-1].num = i;
strcpy(w_net[i-1].b,st_net);
strcpy(w_net[i-1].e,e_net);
w_record++;
}
}/*end while */
/* 打印网段列表
for(j=0; j<b_record;j++)
printf("b %d b=%s, e=%s\n",b_net[j].num,b_net[j].b,b_net[j].e);
for(j=0; j<n_record;j++)
printf("n %d b=%s, e=%s\n",n_net[j].num,n_net[j].b,n_net[j].e);
for(j=0; j<w_record;j++)
printf("w %d b=%s, e=%s\n",w_net[j].num,w_net[j].b,w_net[j].e);
*/
fclose(fp);
/*printf("22\n");*/
}
cacu_ip()
{
int i;
FILE *fp;
char buffer[200], filename[80];
strcpy(filename,A_TMP);
fp=fopen(filename,"a+");
if (fp == NULL)
{
printf("open a_tmp error\n");
exit(-1);
}
for(i=0;i<ip_list_up;i++)
{
ip_list[i].n_sum = ip_list[i].n_in + ip_list[i].n_out;
ip_list[i].w_sum = ip_list[i].w_in + ip_list[i].w_out;
ip_list[i].total = ip_list[i].n_sum + ip_list[i].w_sum;
sprintf(buffer,"%s %10.0f %10.0f %10.0f %10.0f\n",ip_list[i].ip,
ip_list[i].n_in, ip_list[i].n_out,
ip_list[i].w_in, ip_list[i].w_out);
fputs(buffer,fp); /*追加a_tmp文件*/
/*调试打印*/
if (DEBUG_FLAG==1)
printf("ip=<%s>, n_in=%10.0f, n_out=%10.0f, w_in=%10.0f, w_out=%10.0f\n",ip_list[i].ip,
ip_list[i].n_in, ip_list[i].n_out,
ip_list[i].w_in, ip_list[i].w_out);
/*
printf(" ***** ip=%s,n_sum=%f,w_sum=%f,total=%f\n\n",ip_list[i].ip,
ip_list[i].n_sum, ip_list[i].w_sum,ip_list[i].total);
*/
}
fclose(fp);
}
/* -1 : s1 < s2 0 : s1 = s2*/
int ip_compare(char *s1,char *s2)
{
char *ptr;
char a11[10],a12[10],a13[10],a14[10],a21[10],a22[10],a23[10],a24[10];
char temp[20];
strcpy(temp,s1);
ptr = strtok(temp,".");
if (ptr == NULL) return 999;
strcpy(a11,ptr);
ptr = strtok(NULL,".");
if (ptr == NULL) return 999;
strcpy(a12,ptr);
ptr = strtok(NULL,".");
if (ptr == NULL) return 999;
strcpy(a13,ptr);
ptr = strtok(NULL,".");
if (ptr == NULL) return 999;
strcpy(a14,ptr);
strcpy(temp,s2);
ptr = strtok(temp,".");
if (ptr == NULL) return 999;
strcpy(a21,ptr);
ptr = strtok(NULL,".");
if (ptr == NULL) return 999;
strcpy(a22,ptr);
ptr = strtok(NULL,".");
if (ptr == NULL) return 999;
strcpy(a23,ptr);
ptr = strtok(NULL,".");
if (ptr == NULL) return 999;
strcpy(a24,ptr);
if (atol(a11)<atol(a21))
return -1;
if (atol(a11)>atol(a21))
return 1;
if (atol(a12)<atol(a22))
return -1;
if (atol(a12)>atol(a22))
return 1;
if (atol(a13)<atol(a23))
return -1;
if (atol(a13)>atol(a23))
return 1;
if (atol(a14)<atol(a24))
return -1;
if (atol(a14)>atol(a24))
return 1;
return 0;
}
DEBUG(char *s)
{
if (DEBUG_FLAG==1)
printf("%s");
}
read_ratio()
{
FILE *fp;
char buff[20];
if((fp=fopen("bl","r")) == NULL)
{
printf("not found bl \n");
exit(-1);
}
fgets(buff,200,fp);
ratio = atol(buff);
fclose(fp);
}
main(int argc, char *argv[])
{
read_ratio(); /* read from bl file*/
if (argc > 1)
{
if (strcmp(argv[1],"m")==0)
{
/*消息初始化*/
InitVar();
sleep(2);
}
if (strcmp(argv[1],"p")==0)
/*debug print*/
DEBUG_FLAG = 1;
}
printf("read user account file.\n");
ReadUserListFromFile(UserFile);
net_init(); /* read net.conf*/
AnalyzeIpDataFile();
cacu_ip(); /*按IP地址数组计算求和并打印*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -