LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 714|回复: 5

请教一个SegFault的问题。

[复制链接]
发表于 2006-11-24 10:57:33 | 显示全部楼层 |阅读模式
我在运行该程序时,第9行进入tcp_read();函数,在该函数中,47行之前都可以运行,第47行也能够把“lable”打因出来。但是tcp_read()结束后回到调用的程序时出现“Segment Fault”的错误。第10行的“tcp_read”打印不了。请问大虾们,这是怎么回事啊?

1--void tcp_read();
2--void
3--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(struct ip        *ipx)
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--}
 楼主| 发表于 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--}
回复 支持 反对

使用道具 举报

发表于 2006-11-24 10:59:46 | 显示全部楼层
1,12行函数声明和定义都不一样.....

学C 没钱途,学JAVA去吧
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-24 11:03:57 | 显示全部楼层
第12行写错了,现在改了。但是改了后还是一样的问题啊。
回复 支持 反对

使用道具 举报

发表于 2006-11-24 11:56:26 | 显示全部楼层
pcap close之后,之前返回的ptr也无效了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-11-24 12:29:26 | 显示全部楼层
Post by x11
pcap close之后,之前返回的ptr也无效了

如果ptr无效了,但是为什么pcap_close(pd)后的eptr = (struct ether_header *) ptr;
还可以执行呢,还可以得到eptr内部的成员?
而且我把pcap_close(pd)放到lable后,同样是SegFault的问题。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表