对网络数据包进行匹配时出现的问题??
想法:用pcap_next()获取的包,分析出五元组后,进行匹配建立链表(匹配正、反向:如果匹配到,就正向或反向加1;如果没匹配到,就链到最后面)
代码: //p1是指针链表,p2是新分析出来的数据(是一个结构体指针)
while(((p1->next)!=NULL)&&(flag != 1))
{
if((p2->b_proto==p1->b_proto)\
&&(strcmp(p2->b_saddr,p1->b_saddr)==0)&&(p2->b_sport==p1->b_sport)\
&&(strcmp(p2->b_daddr,p1->b_daddr)==0)&&(p2->b_dport==p1->b_dport))
{
fflag = 1;
flag = 1;
}
else if(flag != 1)
{
if((p2->b_proto==p1->b_proto)\
&&(strcmp(p2->b_saddr,p1->b_daddr)==0)&&(p2->b_sport==p1->b_dport)\
&&(strcmp(p2->b_daddr,p1->b_saddr)==0)&&(p2->b_dport==p1->b_sport))
{
bflag = 1;
flag = 1;
}
}
if(flag!=1)
{
p1 = p1->next;
}
}
if(fflag ==1 )
{
p1->b_fpackets = p1->b_fpackets+1;
flag = 1;
}
else if(bflag ==1)
{
p1->b_bpackets = p1->b_bpackets+1;
flag = 1;
}
else
{
p2->b_fpackets = 1;
p2->b_bpackets = 0;
p1->next = p2;
p2->next=NULL;
}
}
结果:
this is the 1 packets! 协议 正向数量 反向数量
172.16.10.170 220.181.20.177 17029 20480 6 //分析出来的每一个包
the list is:
172.16.10.170 220.181.20.177 17029 20480 6 1 0 //链表的内容
this is the 2 packets!
220.181.20.177 172.16.10.170 20480 17029 6
the list is:
172.16.10.170 220.181.20.177 17029 20480 6 1 0
220.181.20.177 172.16.10.170 20480 17029 6 1 0 ??**这时反向没有匹配到
this is the 3 packets!
172.16.10.170 220.181.20.177 17029 20480 6
the list is:
172.16.10.170 220.181.20.177 17029 20480 6 2 0 !!**这时正向匹配到
220.181.20.177 172.16.10.170 20480 17029 6 1 0
......
this is the 6 packets!
220.181.20.177 172.16.10.170 20480 17029 6
the list is:
172.16.10.170 220.181.20.177 17029 20480 6 4 1 !!**这里反向匹配到
220.181.20.177 172.16.10.170 20480 17029 6 1 0
后面还有这种情况.......
一时不知这种情况产生的原因,请仁兄赐教!