当前位置:首页>Linux>Linux环境下C语言实现ping命令

Linux环境下C语言实现ping命令

  • 2026-10-11 06:13:49
Linux环境下C语言实现ping命令

Linux环境下C语言实现ping命令

涉及的知识点

ICMP的报文格式 可以看到ICMP位于网络层,是网络层协议。ICMP作为IP层数据报的数据,加上数据报的首部,组成IP数据报发送出去。 ICMP报文包含在IP数据报中,IP报头在ICMP报文的最前面。一个ICMP报文包括IP报头(至少20字节)、ICMP报头(至少八字节)和ICMP报文(属于ICMP报文的数据部分)。当IP报头中的协议字段值为1时,就说明这是一个ICMP报文。 类型:占1字节,标识ICMP报文的类型,从类型值来看ICMP报文可以分为两大类。第一类是取值为1~127的差错报文,第2类是取值128以上的信息报文 代码:占1字节,标识对应ICMP报文的代码。它与类型字段一起共同标识了ICMP报文的详细类型 校验和:占2字节,这是对包括ICMP报文数据部分在内的整个ICMP数据报的校验和,以检验报文在传输过程中是否出现了差错 内容:占8字节。 IP数据包格式 首部至少需要20个字节(4X5)最后得到ip头+ICMP头=28字节 setsockopt函数的使用

开启IP_HDRINCL特性,我们完全自己手动构造IP报文int on = 1;`:定义了一个整型变量 `on`,并将其初始化为 1。这个变量将用于设置套接字选项,以控制是否包含 IP 头部。setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));`:调用了 `setsockopt` 函数,将套接字选项 `IP_HDRINCL` 设置为 `on`。这个选项的作用是控制发送的数据包中是否包含 IP 头部。当 `on` 的值为 1 时,表示发送的数据包中包含 IP 头部;当 `on` 的值为其他非零值时,表示发送的数据包中不包含 IP 头部,系统将根据目的地址自动生成 IP 头部。

Linux信号量的使用 SIGALRM信号是操作系统中的其中一个信号。他的作用是设置进程隔多久后会收到一个SIGALRM信号

#include<unistd.h>#include<stdio.h>#include<stdlib.h>#include<signal.h>voidhandle_alarm(){     exit(0); } intmain(int argc, char *argv[]){     signal(SIGALRM, handle_alarm);     alarm(10);     while(1) {} } 

进程在10秒或10秒之后触发SIGALRM信号,然后执行信号处理函数,最后退出。

setitimer函数的使用 setitimer函数用于设置定时器,并指定定时器到期后所产生的信号行为。其函数原型如下:

#include<sys/time.h>//int setitimer(int which, const struct itimerval *new_value, struct itimerval *old_value);/*该函数接受三个参数:which:指定定时器类型,可以是以下两个值之一:ITIMER_REAL:真实时间定时器,使用系统实时时间,到期后将发送SIGALRM信号。ITIMER_VIRTUAL:虚拟时间定时器,使用进程运行时间,到期后将发送SIGVTALRM信号。ITIMER_PROF:以两者之和作为时间基准,到期后将发送SIGPROF信号。new_value:一个指向struct itimerval结构体的指针,用于指定新的定时器值(启动时间和间隔时间)。it_value字段表示定时器首次到期的时间间隔。it_interval字段表示定时器循环触发的时间间隔。old_value:一个指向struct itimerval结构体的指针,用于获取旧的定时器值,即之前设置的定时器值。如果不需要获取旧的定时器值,则可以传入NULL。调用setitimer函数后,会根据new_value所指定的定时器值来启动或修改定时器。到期后,系统会发送相应的信号,并根据信号处理机制执行相应的操作。可以使用signal函数或sigaction函数来捕获并处理定时器到期产生的信号。以下是一个示例代码,演示如何使用setitimer函数设置一个定时器:*/#include<stdio.h>#include<stdlib.h>#include<sys/time.h>#include<signal.h>voidtimer_handler(int signum){    printf("Timer expired!\n");}intmain(){    struct itimerval timer;    // 设置首次触发定时器为1秒后,定时器到期后每10秒触发一次    timer.it_interval.tv_sec = 10;    timer.it_interval.tv_usec = 0;    timer.it_value.tv_sec = 1;    timer.it_value.tv_usec = 0;    // 注册信号处理函数    signal(SIGALRM, timer_handler);    // 启动定时器    setitimer(ITIMER_REAL, &timer, NULL);    // 死循环,等待定时器到期    while (1);    return 0;}/*上述代码中,我们设置了一个定时器,其初次到期时间为1秒后,之后每10秒触发一次。当定时器到期时,会触发SIGALRM信号,并通过注册的信号处理函数timer_handler进行处理。在示例代码中,我们只是简单地打印一条信息并没有做其他处理。*/

gettimeofday:函数用于获取当前时间,包括秒数和微秒数。它的函数原型如下:

#include<sys/time.h>intgettimeofday(struct timeval *tv, struct timezone *tz);- `tv`参数是一个指向`timeval`结构体的指针,用于存储获取的时间信息。- `tz`参数是一个指向`timezone`结构体的指针,用于存储时区信息。在现代Linux系统中,通常可以将其设置为NULL,不使用时区信息`timeval`结构体定义如下:struct timeval {    time_t      tv_sec;     // 秒    suseconds_t tv_usec;    // 微秒};
#include<stdio.h>#include<sys/time.h>intmain(){    struct timeval tv;    /* 获取当前时间 */    if (gettimeofday(&tv, NULL) == 0) {        printf("Current time: %ld seconds, %ld microseconds\n", tv.tv_sec, tv.tv_usec);    } else {        perror("gettimeofday");        return 1;    }    return 0;}

在这个示例中,我们首先定义了一个timeval结构体变量tv,然后调用gettimeofday函数获取当前时间,并将结果打印出来。

checksum校验怎么算出来的 如上面,我们得到IP帧头部实际数据(十六进制):

45 00 00 91 10 1b 00 00 40 11 a9 98 c0 a8 00 01 ff ff ff ff

我们看到Wireshark分析出来的Header Checksum是0xa998,下面我们计算验证一下。 1.根据IPv4头部格式,我们知道第11和12个字节是要填写的Checksum,先把这两个字节都设置为0,得到

45 00 00 91 10 1b 00 00 40 11 00 00 c0 a8 00 01 ff ff ff ff

2.每两个字节组成一个数字,然后累加

4500 0091 101b 0000  4011  0000 c0a8  0001  ffff  ffff 4500 + 0091 + 101b + 0000 +  4011 + 0000  + c0a8 + 0001 +  ffff  + ffff = 0x35664

3.把后面两个字节组成的数字5664 和 进位3相加,上面的 3 5664,分开两个数相加就是

3 + 5664 = 5667,一般是写成 5664 + 3 = 0x5667

4.取反,C语言用~符号

~(0x5667) = 0xa998

思路已经很清晰了,下面就是编码实现

#include<stdio.h>typedef unsigned short u_int16_t;typedef unsigned char u_int8_t;u_int16_tchecksum(u_int16_t *packet, int packlen){    /* 定义sum大小为8字节,定义小了,会累计的时候会出现位数不够 ,丢失高位的数据 */unsigned long sum = 0;//求和while (packlen > 1) {		sum = sum  + *(packet++);		packlen = packlen - 2;}/* 因为两个字节合并unsigned short整型做计算,所以如果报文大小为奇数字节则单独加上最后一个字节的数据 */    if  (packlen > 0)        sum = sum + *(unsigned char *)packet;/* TODO: this depends on byte order 不理解这里写这个干嘛 *//* 如果存在溢出则截断高16位数据与低16位数据求和,如sum=0x386B6 则sum=0x86B6 + 0x3 */while (sum >> 16)		sum = (sum & 0xffff) + (sum >> 16);/* sum取反码,转为2个字节,不然会把高位的ffff这些都输出了 */return (u_int16_t)~sum;}intmain(int argc, char *argv[]){    int i = 0;    int j = 0;    unsigned short checksumvalue[20];    unsigned char bytes[] = {        0xa4, 0xae, 0x11, 0x17, 0x49, 0xe7, 0xf4, 0xde,         0xaf, 0xed, 0xd0, 0x15, 0x08, 0x00, 0x45, 0x00,         0x00, 0x91, 0x10, 0x1b, 0x00, 0x00, 0x40, 0x11,         0xa9, 0x98, 0xc0, 0xa8, 0x00, 0x01, 0xff, 0xff,        0xff, 0xff, 0x01, 0xbb, 0xe3, 0xa6, 0x6a, 0xd5,         0x1e, 0x6b, 0x56, 0xc1, 0x3f, 0xc3, 0x80, 0x12,         0xfa, 0xf0, 0x0c, 0xae, 0x00, 0x00, 0x02, 0x04,         0x05, 0xac, 0x01, 0x03, 0x03, 0x08, 0x01, 0x01,         0x04, 0x02    };    printf("合并后的值:\n");    /* 从0x45到0xff每两个字节合并为一个十六进制数 */    for (i = 14; i < 33; i += 2) {        checksumvalue[j] = (bytes[i] << 8) | bytes[i+1];        printf("0x%04x\n", checksumvalue[j]);        j++;    }    /* 把第11,12字节合并后的数据0xa998, 即数组里面的checksumvalue[5] 置为0 */    checksumvalue[5] = 0;     u_int16_t ip_checksum = checksum(checksumvalue, 20);printf("ip checksum: 0x%04x\n", ip_checksum);    return 0;}

运行结果如下:

完整代码如下:

#define ICMP_ECHOREPLY 0 /* Echo应答*/#define ICMP_ECHO   /*Echo请求*/#define BUFSIZE 1500    /*发送缓存最大值*/#define DEFAULT_LEN 56  /**ping消息数据默认大小/  /*数据类型别名*/  typedef unsigned char u8;  typedef unsigned short u16;  typedef unsigned int u32;  /*ICMP消息头部*/  struct icmphdr {      u8 type;     /*定义消息类型*/      u8 code;    /*定义消息代码*/      u16 checksum;   /*定义校验*/      union{          struct{          u16 id;          u16 sequence;      }echo;      u32 gateway;      struct{          u16 unsed;          u16 mtu;      }frag; /*pmtu实现*/      }un;    /*ICMP数据占位符*/      u8 data[0];  #define icmp_id un.echo.id  #define icmp_seq un.echo.sequence  };  #define ICMP_HSIZE sizeof(struct icmphdr)  /*定义一个IP消息头部结构体*/  struct iphdr {      u8 hlen:4, ver:4;   /*定义4位首部长度,和IP版本号为IPV4*/      u8 tos;/*8位服务类型TOS*/      u16 tot_len;/*16位总长度*/      u16 id;/*16位标志位*/      u16 frag_off;/*3位标志位*/      u8 ttl;/*8位生存周期*/      u8 protocol;/*8位协议*/      u16 check;/*16位IP首部校验和*/      u32 saddr;/*32位源IP地址*/      u32 daddr;/*32位目的IP地址*/  };  char *hostname;/*被ping的主机名*/  int datalen = DEFAULT_LEN;  /*ICMP消息携带的数据长度*/  char sendbuf[BUFSIZE];      /*发送字符串数组*/   char recvbuf[BUFSIZE];      /*接收字符串数组*/  int nsent;/*发送的ICMP消息序号*/  int nrecv;/*接收的ICMP消息序号*/  pid_t pid;/*ping程序的进程PID*/  struct timeval recvtime;    /*收到ICMP应答的时间戳*/  int sockfd;/*发送和接收原始套接字*/  struct sockaddr_in dest;    /*被ping的主机IP*/  struct sockaddr_in from;    /*发送ping应答消息的主机IP*/  struct sigaction act_alarm;  struct sigaction act_int;  /*函数原型*/  voidalarm_handler(int);/*SIGALRM处理程序*/  voidint_handler(int);/*SIGINT处理程序*/  voidset_sighandler();/*设置信号处理程序*/  voidsend_ping();/*发送ping消息*/  voidrecv_reply();/*接收ping应答*/  u16 checksum(u8 *buf, int len); /*计算校验和*/  inthandle_pkt();/*ICMP应答消息处理*/  voidget_statistics(int, int);  /*统计ping命令的检测结果*/  voidbail(constchar *);/*错误报告*/
#include<stdio.h>#include<stdlib.h>#include<sys/time.h>/*是Linux系统的日期时间头文件*/#include<unistd.h>/* 是POSIX标准定义的unix类系统定义符号常量的头文件,包含了许多UNIX系统服务的函数原型,例如read函数、write函数和getpid函数*/#include<string.h>#include<sys/socket.h>/*对与引用socket函数必须*/#include<sys/types.h>#include<netdb.h>/*定义了与网络有关的结构,变量类型,宏,函数。函数gethostbyname()用*/#include<errno.h>/*sys/types.h中文名称为基本系统数据类型*/#include<arpa/inet.h>/*inet_ntoa()和inet_addr()这两个函数,包含在 arpa/inet.h*/#include<signal.h>/*进程对信号进行处理*/#include<netinet/in.h>/*互联网地址族*/#include"test.h"#define IP_HSIZE sizeof(struct iphdr)   /*定义IP_HSIZE为ip头部长度*/#define IPVERSION  4   /*定义IPVERSION为4,指出用ipv4*//*设置的时间是一个结构体,倒计时设置,重复倒时,超时值设为1秒*/  struct itimerval val_alarm = {.it_interval.tv_sec = 1,      .it_interval.tv_usec = 0,  .it_value.tv_sec = 0,  .it_value.tv_usec = 1  };  /*argc表示隐形程序命令行中参数的数目,argv是一个指向字符串数组指针,其中每一个字符对应一个参数*/intmain(int argc,char **argv){  struct hostent*host; /*该结构体属于include<netdb.h>*/       int					on = 1;      if( argc < 2)/*判断是否输入了地址*/ {       printf("Usage: %s hostname\n",argv[0]);  exit(1);      }  /*gethostbyname()返回对应于给定主机名的包含主机名字和地址信息的结构指针,*/     //if((host = getaddrinfo(argv[1])) == NULL)    if((host = gethostbyname(argv[1])) == NULL){     printf("usage:%s hostname/IP address\n", argv[0]);exit(1);      }      hostname = argv[1];/*取出地址名*/  memset(&dest,0,sizeof dest);/*将dest中前sizeof(dest)个字节替换为0并返回s,此处为初始化,给最大内存清零*/  	dest.sin_family=PF_INET;/*PF_INET为IPV4,internet协议,在<netinet/in.h>中,地址族*/   	dest.sin_port=ntohs(0);/*端口号,ntohs()返回一个以主机字节顺序表达的数。*/  	dest.sin_addr=*(struct in_addr *)host->h_addr_list[0];/*host->h_addr_list[0]是地址的指针.返回IP地址,初始化*/  /*PF_INEI套接字协议族,SOCK_RAW套接字类型,IPPROTO_ICMP使用协议,	调用socket函数来创建一个能够进行网络通信的套接字。这里判断是否创建成功*/ if((sockfd = socket(PF_INET, SOCK_RAW, IPPROTO_ICMP)) < 0){  perror("RAW socket created error");  exit(1);      }  /*设置当前套接字选项特定属性值,sockfd套接字,IPPROTO_IP协议层为IP层,	IP_HDRINCL套接字选项条目,套接字接收缓冲区指针,sizeof(on)缓冲区长度的长度*/     setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &on, sizeof(on));   /*getuid()函数返回一个调用程序的真实用户ID,setuid()是让普通用户	可以以root用户的角色运行只有root帐号才能运行的程序或命令。*/ //setuid(getuid()); //pid = getpid(); /*getpid函数用来取得目前进程的进程识别码*/  set_sighandler();/*对信号处理*/  printf("Ping %s(%s): %d bytes data in ICMP packets.\n", argv[1], inet_ntoa(dest.sin_addr), datalen);      //alarm(1);if((setitimer(ITIMER_REAL, &val_alarm, NULL)) == -1) /*定时函数*/  {        bail("setitimer fails.");  }    recv_reply(); /*接收ping应答*/  return 0;  }  /*发送ping消息*/  voidsend_ping(void){      struct iphdr*ip_hdr;   /*iphdr为IP头部结构体*/      struct icmphdr*icmp_hdr;   /*icmphdr为ICMP头部结构体*/      int					len;      int					len1;  /*ip头部结构体变量初始化*/      ip_hdr=(struct iphdr *)sendbuf; /*字符串指针*/         ip_hdr->hlen=sizeof(struct iphdr)>>2;  /*头部长度*/      ip_hdr->ver=IPVERSION;   /*版本*/      ip_hdr->tos=0;   /*服务类型*/      ip_hdr->tot_len=IP_HSIZE+ICMP_HSIZE+datalen; /*报文头部加数据的总长度*/      ip_hdr->id=0;    /*初始化报文标识*/      ip_hdr->frag_off=0;  /*设置flag标记为0*/      ip_hdr->protocol=IPPROTO_ICMP;/*运用的协议为ICMP协议*/      ip_hdr->ttl=255; /*一个封包在网络上可以存活的时间*/      ip_hdr->daddr=dest.sin_addr.s_addr;  /*目的地址*/      len1=ip_hdr->hlen<<2;  /*ip数据长度*/      /*ICMP头部结构体变量初始化*/      icmp_hdr=(struct icmphdr *)(sendbuf+len1);  /*字符串指针*/      icmp_hdr->type=8;    /*初始化ICMP消息类型type*/      icmp_hdr->code=0;    /*初始化消息代码code*/      icmp_hdr->icmp_id=pid;   /*把进程标识码初始给icmp_id*/      icmp_hdr->icmp_seq=nsent++;  /*发送的ICMP消息序号赋值给icmp序号*/          memset(icmp_hdr->data,0xff,datalen);  /*将datalen中前datalen个字节替换为0xff并返回icmp_hdr-dat*/        gettimeofday((struct timeval *)icmp_hdr->data,NULL); /* 获取当前时间*/      len=ip_hdr->tot_len; /*报文总长度赋值给len变量*/      icmp_hdr->checksum=0;    /*初始化*/      icmp_hdr->checksum=checksum((u8 *)icmp_hdr,len);  /*计算校验和*/      sendto(sockfd,sendbuf,len,0,(struct sockaddr *)&dest,sizeof (dest)); /*经socket传送数据*/  }  /*接收程序发出的ping命令的应答*/  voidrecv_reply(){  int			n;  int			len;      int			errno;      n = 0;	nrecv = 0;      len = sizeof(from);   /*发送ping应答消息的主机IP*/      while(nrecv < 6){  /*经socket接收数据,如果正确接收返回接收到的字节数,失败返回0.*/if((n=recvfrom(sockfd, recvbuf, sizeof(recvbuf), 0, (struct sockaddr *)&from, &len))<0){   if(errno==EINTR)  /*EINTR表示信号中断*/  continue;              bail("recvfrom error");          }  gettimeofday(&recvtime, NULL);   /*记录收到应答的时间*/  if(handle_pkt())    /*接收到错误的ICMP应答信息*/  continue;  		nrecv++;      }      //get_statistics(nsent, nrecv);     /*统计ping命令的检测结果*/  }   /*计算校验和*/  u16 checksum(u8 *buf,int len){      u32 sum	= 0;      u16 *cbuf;      cbuf = (u16 *)buf;      while(len > 1){  		sum += *cbuf++;  		len -= 2;      }      if(len){        sum += *(u8 *)cbuf;  }	sum = (sum >> 16) + (sum & 0xffff);  	sum += (sum >> 16);  return ~sum;  }  /*ICMP应答消息处理*/  inthandle_pkt(){  struct iphdr*ip;      struct icmphdr*icmp;      int					ip_hlen;      u16					ip_datalen; /*ip数据长度*/      double				rtt; /* 往返时间*/      struct timeval*sendtime;      ip = (struct iphdr *)recvbuf;      ip_hlen = ip->hlen << 2;      ip_datalen = ntohs(ip->tot_len) - ip_hlen;      icmp = (struct icmphdr *)(recvbuf + ip_hlen);      if(checksum((u8 *)icmp, ip_datalen)) /*计算校验和*/         return -1;  if(icmp->icmp_id != pid)  return -1;  	sendtime = (struct timeval *)icmp->data; /*发送时间*/  	rtt = ((&recvtime)->tv_sec - sendtime->tv_sec) * 1000 + ((&recvtime)->tv_usec - sendtime->tv_usec)/1000.0; /* 往返时间*/  /*打印结果*/  printf("%d bytes from %s:icmp_seq=%u ttl=%d rtt=%.3f ms\n",  \			ip_datalen,/*IP数据长度*/  inet_ntoa(from.sin_addr),   /*目的ip地址*/  			icmp->icmp_seq,/*icmp报文序列号*/  			ip->ttl,/*生存时间*/  			rtt);/*往返时间*/  return 0;  }  /*设置信号处理程序*/  voidset_sighandler(){  	act_alarm.sa_handler = alarm_handler;  /*sigaction()会依参数signum指定的信号编号来设置该信号的处理函数。参数signum指所要捕获信号或忽略的信号,	&act代表新设置的信号共用体,NULL代表之前设置的信号处理结构体。这里判断对信号的处理是否成功。*/    if(sigaction(SIGALRM, &act_alarm, NULL) == -1)    {bail("SIGALRM handler setting fails.");  }	act_int.sa_handler = int_handler;      if(sigaction(SIGINT, &act_int, NULL) == -1)  {bail("SIGALRM handler setting fails.");  }}   /*统计ping命令的检测结果*/  voidget_statistics(int nsent,int nrecv){      printf("--- %s ping statistics ---\n",inet_ntoa(dest.sin_addr)); /*将网络地址转换成“.”点隔的字符串格式。*/      printf("%d packets transmitted, %d received, %0.0f%% ""packet loss\n",  \		nsent,nrecv,1.0*(nsent-nrecv)/nsent*100);  }  /*错误报告*/  voidbail(constchar * on_what){  /*:向指定的文件写入一个字符串(不写入字符串结束标记符‘\0’)。成功写入一个字符串后,	文件的位置指针会自动后移,函数返回值为0;否则返回EOR(符号常量,其值为-1)。*/     fputs(strerror(errno),stderr);       fputs(":",stderr);      fputs(on_what,stderr);      fputc('\n',stderr); /*送一个字符到一个流中*/      exit(1);  }   /*SIGINT(中断信号)处理程序*/  voidint_handler(int sig){  printf("Timer123 expired!\n");    get_statistics(nsent,nrecv);    /*统计ping命令的检测结果*/      close(sockfd);  /*关闭网络套接字*/      exit(1);  }   /*SIGALRM(终止进程)处理程序*/  voidalarm_handler(int signo){      send_ping();    /*发送ping消息*/  }

运行结果:

最新文章

随机文章