资源简介
LINUX操作系统下的PING命令完整实现,带有路由追踪的功能和CTRL+C终止的功能。
代码片段和文件信息
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define PACKET_SIZE 4096
#define MAX_WAIT_TIME 5
char sendpacket[PACKET_SIZE];
char recvpacket[PACKET_SIZE];
double max_time=0min_time=10000sum_time=0;
int show_pro=1iobyname=0;
struct timeval start_timeend_time;
int sockfddatalen=56;
int send_count=4send_interval=1nTTL=-1route_count=-1route_TTL=1;
int nsend=0nreceived=0npack=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);
int send_packet(void);
int recv_packet(void);
int unpack(char* buf int len);
void tv_sub(struct timeval *out struct timeval *in);
void cal_time(double rtt);
void commandhelp();
void send_recv();
void statistics(int signo)
{
printf(“\n-------------------PING statistics----------------\n“);
printf(“%d packets transmitted %d received %.1f%% lost\n “
nsendnreceived((float)nsend-(float)nreceived)/(float)nsend*100);
if( (max_time!=0) && (sum_time!=0) )
printf(“rtt min/avg/max = %.3f %.3f %.3f \n“
min_timesum_time/nreceivedmax_time);
close(sockfd);
exit(0);
}
/*校验和算法*/
unsigned short cal_chksum(unsigned short *addrint len)
{
int nleft=len;
int sum=0;
unsigned short *w=addr;
unsigned short answer=0;
while(nleft>1)/*把ICMP报头二进制数据以2字节为单位加起来*/
{
sum+=*w++;
nleft-=2;
}
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 ipacksize;
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(tvalNULL);/*记录发送时间*/
icmp->icmp_cksum=cal_chksum((unsigned short *)icmppacksize);/*校验算法*/
return packsize;
}
int send_packet()
{
int packetsize;
npack++;
packetsize=pack(npack);/*设置ICMP报头*/
if( sendto( sockfd sendpacket packetsize 0
(struct sockaddr *)&dest_addrsizeof(dest_addr)) <0 )
{
perror(“sendto error“);
return -1;
}
return 0;
}
/*接受报文*/
int recv_packet()
{
int nfromlen;
extern int errno;
signal(SIGALRMstatistics);
signal(SIGINTstatistics);
fromlen=sizeof(from);
alarm(MAX_WAIT_TIME);
if( ( n=recvfrom(sockfdrecvpacketsizeof(recvpacket)0
(struct sockaddr*)&from&fromlen))<0 )
{
if( errno==EINTR )
perror(“recvfrom error“);
return -1;
}
gettimeofday(&tvrecvNULL);/*记录接收时间*/
if(unpack(recvpacketn)==0)
return 0;
return -1;
}
/*剥去ICMP报头*/
int unpack(char *bufint len)
{
int iiphdrlen;
struct ip *ip;
struct
- 上一篇:单片机最小系统原理图
- 下一篇:使用mp4v2将H264+AAC合成mp4
相关资源
- 树莓派GPU手册 videocore IV
- Linux命令大全 完整版
- Linux系统 用户界面与进程的创建
- Linux嵌入式驱动开发视频(麦可网)
- 集美大学《基于unix_linux的C系统编程》
- 2440 linux2.6下I2C驱动,编译通过
- linux下C 网络编程
- Linux从入门到精通第2版光盘视频.txt
- 2018阿里云linux一键安装web环境最新版
- 马哥Linux2016视频就业班和架构班
- linux下QT程序读写配置文件小程序
- uClinux-dist-20070130-nios2-02.diff.gz
- linux c程序获取cpu使用率及内存使用情
- 马哥linux2016初中高级,网盘提取码
- lpc3250试验手册
- Centos 6.9 64位镜像文件
- CDLINUX 启动盘制作必备的 splash.xpm.gz
- cuDNN v7.0.5-linux绝对可以用的
- linux串口编程直接make编译
- 编写程序mycat.c,实现文件内容的显示
- Qt实现Linux任务管理器SysMonitor.zip
- linux下的rs232测试程序
- linux下串口编程简单
- 基于Linux的wps的必要字体
- linux下操做OV5640摄像头
- Linux取证技术PPT演示稿
- 仿照linux的buddy+slub内存管理算法
- rtlinux用法详解
- Linux系统tftp安装包
- 张浩Linux操作系统巡检报告
评论
共有 条评论