|
主函数里调用test_tcp()
void
test_tcp()
{
.......
tcp_read(ipt); //ipt是全局struct ip 型指针
........;
}
void tcp_read(struct ip *ipx)
{
int len;
char *ptr,buf[255];
struct ether_header *eptr;
struct tcphdr *tcphead;
struct my_sql mysql;
struct ip *tcp_check(char *,int);
int i=0;
for ( ; ; ) {
ptr = next_pcap(&len); //返回下一个捕获数据包的地址
printf("ptr=%p\n",ptr);
pcap_close(pd);
switch (datalink) {
case DLT_NULL: /* loopback header = 4 bytes */
tcp_check(ptr+4,len-4);break;
case DLT_EN10MB:
eptr = (struct ether_header *) ptr;
if (ntohs(eptr->ether_type) != ETHERTYPE_IP){
perror("Ethernet type:");
exit }
int i;
char *hwadd=eptr->ether_shost;
for(i=0;i<5;++i)
printf("%2x:",hwadd);
printf("%2x\n",hwadd);
ipx=tcp_check(ptr+14,len-14); //校验ip头部
printf("ipx%p\n",ipx);
inet_ntop(AF_INET,&(ipx->ip_src),buf,sizeof(buf));
printf("ip_src: %s\n",buf);
inet_ntop(AF_INET,&(ipx->ip_dst),buf,sizeof(buf));
printf("dst: %s\n",buf);
tcphead=(struct tcphdr *)(ptr+4*ipx->ip_hl);
printf("tcphead%p\n",tcphead);
printf("srcport%d\n",ntohs(tcphead->source));
printf("dstport%d\n",ntohs(tcphead->dest));
goto lable;
default:
perror("unsupported datalink:");
exit(1);
}
}
lable:
printf("lable\n");
}
调用tcp_read()返回有段错误,怎么回事?? |
|