|

楼主 |
发表于 2006-11-24 10:57:35
|
显示全部楼层
请教一个SegFault的问题。
我在运行该程序时,第9行进入tcp_read();函数,在该函数中,47行之前都可以运行,第47行也能够把“lable”打因出来。但是tcp_read()结束后回到调用的程序时出现“Segment Fault”的错误。第10行的“tcp_read”打印不了。请问大虾们,这是怎么回事啊?
1--void tcp_read();
2--struct ip *ipx;
3--void test_tcp(void)
4--{
5-- struct ip *iptest,*ip;
6-- struct tcphdr *tcphead;
7-- char *ptr;
8-- printf("test start!\n");
9-- tcp_read();
10-- printf("tcp_read");
11--}
12--void tcp_read()
13--{
14-- int len;
15-- char *ptr,buf[255];
16-- struct ether_header *eptr;
17-- struct tcphdr *tcphead;
18-- struct ip *tcp_check(char *,int);
19-- for ( ; ; ) {
20-- ptr = next_pcap(&len);
21-- printf("ptr=%p\n",ptr);
22-- pcap_close(pd);
23-- eptr = (struct ether_header *) ptr;
24-- if (ntohs(eptr->ether_type) != ETHERTYPE_IP){
25-- perror("Ethernet type:");
26-- exit(1);
27-- }
28-- int i;
29-- char *hwadd=eptr->ether_shost;
30-- for(i=0;i<5;++i)
31-- printf("%2x:",hwadd);
32-- printf("%2x\n",hwadd);
33--
34-- ipx=tcp_check(ptr+14,len-14);
35-- printf("ipx%p\n",ipx);
36-- inet_ntop(AF_INET,&(ipx->ip_src),buf,sizeof(buf));
37-- printf("ip_src=%s\n",buf);
38--
39-- inet_ntop(AF_INET,&(ipx->ip_dst),buf,sizeof(buf));
40-- printf("dst=%s\n",buf);
41-- tcphead=(struct tcphdr *)(ptr+4*ipx->ip_hl);
42-- printf("tcphead%p\n",tcphead);
43-- printf("src port%d\n",ntohs(tcphead->source));
44-- printf("dst port%d\n",ntohs(tcphead->dest));
45-- goto lable;
46-- }
47--label:printf("lable\n");
48--} |
|