当前位置: 技术问答>linux和unix
linux下运行C程序的错误信息为 segmentation fault
来源: 互联网 发布时间:2016-02-28
本文导语: 我在运行用C写的ping代码出现segmentation fault 原代码是 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define PACKET_SIZE 4096 #define MAX_WAIT_TIME...
我在运行用C写的ping代码出现segmentation fault
原代码是
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd,datalen=56;
int nsend=0,nreceived=0;
struct sockaddr_in dest_addr;
pid_t pid;
struct sockaddr_in from;
struct timeval tvrecv;
void statistics(int signo);
unsigned short cal_chksum(unsigned short *addr,int len);
int pack(int pack_no);
void send_packet(void);
void recv_packet(void);
int unpack(char *buf,int len);
void tv_sub(struct timeval *out,struct timeval *in);
void statistics(int signo)
{ printf("n--------------------PING statistics-------------------n");
printf("%d packets transmitted, %d received , %%%d lostn",nsend,nreceived,
(nsend-nreceived)/nsend*100);
close(sockfd);
exit(1);
}
/*校验和算法*/
unsigned short cal_chksum(unsigned short *addr,int len)
{ int nleft=len;
int sum=0;
unsigned short *w=addr;
unsigned short answer=0;
/*把ICMP报头二进制数据以2字节为单位累加起来*/
while(nleft>1)
{ sum+=*w++;
nleft-=2;
}
/*若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个2字节数据的高字节,这个2字节数据的低字节为0,继续累加*/
if( nleft==1)
{ *(unsigned char *)(&answer)=*(unsigned char *)w;
sum+=answer;
}
sum=(sum>>16)+(sum&0xffff);
sum+=(sum>>16);
answer=~sum;
return answer;
}
/*设置ICMP报头*/
int pack(int pack_no)
{ int i,packsize;
struct icmp *icmp;
struct timeval *tval;
icmp=(struct icmp*)sendpacket;
icmp->icmp_type=ICMP_ECHO;
icmp->icmp_code=0;
icmp->icmp_cksum=0;
icmp->icmp_seq=pack_no;
icmp->icmp_id=pid;
packsize=8+datalen;
tval= (struct timeval *)icmp->icmp_data;
gettimeofday(tval,NULL); /*记录发送时间*/
icmp->icmp_cksum=cal_chksum( (unsigned short *)icmp,packsize); /*校验算法*/
return packsize;
}
/*发送三个ICMP报文*/
void send_packet()
{ int packetsize;
while( nsendh_length);
}
else /*是ip地址*/
memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length);
/*获取main的进程id,用于设置ICMP的标志符*/
pid=getpid();
printf("PING %s(%s): %d bytes data in ICMP packets.n",argv[1],
inet_ntoa(dest_addr.sin_addr),datalen);
send_packet(); /*发送所有ICMP报文*/
recv_packet(); /*接收所有ICMP报文*/
statistics(SIGALRM); /*进行统计*/
return 0;
}
/*两个timeval结构相减*/
void tv_sub(struct timeval *out,struct timeval *in)
{ if( (out->tv_usec-=in->tv_usec)tv_sec;
out->tv_usec+=1000000;
}
out->tv_sec-=in->tv_sec;
}
有知道的帮帮忙好吗 非常非常感谢!
原代码是
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
#define MAX_NO_PACKETS 3
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
int sockfd,datalen=56;
int nsend=0,nreceived=0;
struct sockaddr_in dest_addr;
pid_t pid;
struct sockaddr_in from;
struct timeval tvrecv;
void statistics(int signo);
unsigned short cal_chksum(unsigned short *addr,int len);
int pack(int pack_no);
void send_packet(void);
void recv_packet(void);
int unpack(char *buf,int len);
void tv_sub(struct timeval *out,struct timeval *in);
void statistics(int signo)
{ printf("n--------------------PING statistics-------------------n");
printf("%d packets transmitted, %d received , %%%d lostn",nsend,nreceived,
(nsend-nreceived)/nsend*100);
close(sockfd);
exit(1);
}
/*校验和算法*/
unsigned short cal_chksum(unsigned short *addr,int len)
{ int nleft=len;
int sum=0;
unsigned short *w=addr;
unsigned short answer=0;
/*把ICMP报头二进制数据以2字节为单位累加起来*/
while(nleft>1)
{ sum+=*w++;
nleft-=2;
}
/*若ICMP报头为奇数个字节,会剩下最后一字节。把最后一个字节视为一个2字节数据的高字节,这个2字节数据的低字节为0,继续累加*/
if( nleft==1)
{ *(unsigned char *)(&answer)=*(unsigned char *)w;
sum+=answer;
}
sum=(sum>>16)+(sum&0xffff);
sum+=(sum>>16);
answer=~sum;
return answer;
}
/*设置ICMP报头*/
int pack(int pack_no)
{ int i,packsize;
struct icmp *icmp;
struct timeval *tval;
icmp=(struct icmp*)sendpacket;
icmp->icmp_type=ICMP_ECHO;
icmp->icmp_code=0;
icmp->icmp_cksum=0;
icmp->icmp_seq=pack_no;
icmp->icmp_id=pid;
packsize=8+datalen;
tval= (struct timeval *)icmp->icmp_data;
gettimeofday(tval,NULL); /*记录发送时间*/
icmp->icmp_cksum=cal_chksum( (unsigned short *)icmp,packsize); /*校验算法*/
return packsize;
}
/*发送三个ICMP报文*/
void send_packet()
{ int packetsize;
while( nsendh_length);
}
else /*是ip地址*/
memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length);
/*获取main的进程id,用于设置ICMP的标志符*/
pid=getpid();
printf("PING %s(%s): %d bytes data in ICMP packets.n",argv[1],
inet_ntoa(dest_addr.sin_addr),datalen);
send_packet(); /*发送所有ICMP报文*/
recv_packet(); /*接收所有ICMP报文*/
statistics(SIGALRM); /*进行统计*/
return 0;
}
/*两个timeval结构相减*/
void tv_sub(struct timeval *out,struct timeval *in)
{ if( (out->tv_usec-=in->tv_usec)tv_sec;
out->tv_usec+=1000000;
}
out->tv_sec-=in->tv_sec;
}
有知道的帮帮忙好吗 非常非常感谢!
|
用GDB很容易发现core dump 发生在
memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length);
改为:
memcpy( (char *)&dest_addr,(char *)&inaddr,sizeof(dest_addr));
后没有core dump了,可是如果参数是ip地址,是没有返回包的,
从输出的信息可以发现,你取到的ip是错的,
改为
memcpy( (char *)&dest_addr.sin_addr,(char *)&inaddr,sizeof(dest_addr
));
就可以了!
memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length);
改为:
memcpy( (char *)&dest_addr,(char *)&inaddr,sizeof(dest_addr));
后没有core dump了,可是如果参数是ip地址,是没有返回包的,
从输出的信息可以发现,你取到的ip是错的,
改为
memcpy( (char *)&dest_addr.sin_addr,(char *)&inaddr,sizeof(dest_addr
));
就可以了!
|
if( inaddr=inet_addr(argv[1])==INADDR_NONE)
改为
if( (inaddr = inet_addr(argv[1])) == INADDR_NONE)
最好改为
if( INADDR_NONE == (inaddr = inet_addr(argv[1])) )
改为
if( (inaddr = inet_addr(argv[1])) == INADDR_NONE)
最好改为
if( INADDR_NONE == (inaddr = inet_addr(argv[1])) )