用C语言实现Ping程序功能

               

本文转载自:http://www.ibm.com/developerworks/cn/linux/network/ping/

ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具。ping命令的工作原理是:向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文一模一样地传回给发送者,这有点象潜水艇声纳系统中使用的发声装置。

例如,在Linux终端上执行ping localhost命令将会看到以下结果:

PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data.64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=112 usec64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=79 usec64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=2 ttl=255 time=78 usec64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=3 ttl=255 time=82 usec--- localhost.localdomain ping statistics ---4 packets transmitted, 4 packets received, 0% packet lossround-trip min/avg/max/mdev = 0.078/0.087/0.112/0.018 ms

由上面的执行结果可以看到,ping命令执行后显示出被测试系统主机名和相应IP地址、返回给当前主机的ICMP报文顺序号、ttl生存时间和往返时间rtt(单位是毫秒,即千分之一秒)。要写一个模拟ping命令,这些信息有启示作用。

要真正了解ping命令实现原理,就要了解ping命令所使用到的TCP/IP协议。

ICMP(Internet Control Message,网际控制报文协议)是为网关和目标主机而提供的一种差错控制机制,使它们在遇到差错时能把错误报告给报文源发方。ICMP协议是IP层的一个协议,但是由于差错报告在发送给报文源发方时可能也要经过若干子网,因此牵涉到路由选择等问题,所以ICMP报文需通过IP协议来发送。ICMP数据报的数据发送前需要两级封装:首先添加ICMP报头形成ICMP报文,再添加IP报头形成IP数据报。如下图所示

IP报头
ICMP报头
ICMP数据报

IP报头格式

由于IP层协议是一种点对点的协议,而非端对端的协议,它提供无连接的数据报服务,没有端口的概念,因此很少使用bind()和connect()函数,若有使用也只是用于设置IP地址。发送数据使用sendto()函数,接收数据使用recvfrom()函数。IP报头格式如下图:


 

在Linux中,IP报头格式数据结构(<netinet/ip.h>)定义如下:

struct ip  {
    #if __BYTE_ORDER == __LITTLE_ENDIAN    unsigned int ip_hl:4;       /* header length */    unsigned int ip_v:4;        /* version */#endif#if __BYTE_ORDER == __BIG_ENDIAN    unsigned int ip_v:4;        /* version */    unsigned int ip_hl:4;       /* header length */#endif    u_int8_t ip_tos;            /* type of service */    u_short ip_len;         /* total length */    u_short ip_id;          /* identification */    u_short ip_off;         /* fragment offset field */#define IP_RF 0x8000            /* reserved fragment flag */#define IP_DF 0x4000            /* dont fragment flag */#define IP_MF 0x2000            /* more fragments flag */#define IP_OFFMASK 0x1fff       /* mask for fragmenting bits */    u_int8_t ip_ttl;            /* time to live */    u_int8_t ip_p;          /* protocol */    u_short ip_sum;         /* checksum */    struct in_addr ip_src, ip_dst;  /* source and dest address */  };

其中ping程序只使用以下数据:

  • IP报头长度IHL(Internet Header Length)以4字节为一个单位来记录IP报头的长度,是上述IP数据结构的ip_hl变量。
  • 生存时间TTL(Time To Live)以秒为单位,指出IP数据报能在网络上停留的最长时间,其值由发送方设定,并在经过路由的每一个节点时减一,当该值为0时,数据报将被丢弃,是上述IP数据结构的ip_ttl变量。

ICMP报头格式

ICMP报文分为两种,一是错误报告报文,二是查询报文。每个ICMP报头均包含类型、编码和校验和这三项内容,长度为8位,8位和16位,其余选项则随ICMP的功能不同而不同。

Ping命令只使用众多ICMP报文中的两种:"请求回送'(ICMP_ECHO)和"请求回应'(ICMP_ECHOREPLY)。在Linux中定义如下:

#define ICMP_ECHO   0#define ICMP_ECHOREPLY  8

这两种ICMP类型报头格式如下:


 

在Linux中ICMP数据结构(<netinet/ip_icmp.h>)定义如下:

struct icmp{
       u_int8_t  icmp_type;  /* type of message, see below */  u_int8_t  icmp_code;  /* type sub code */  u_int16_t icmp_cksum; /* ones complement checksum of struct */  union  {    u_char ih_pptr;     /* ICMP_PARAMPROB */    struct in_addr ih_gwaddr;   /* gateway address */    struct ih_idseq     /* echo datagram */    {
           u_int16_t icd_id;      u_int16_t icd_seq;    } ih_idseq;    u_int32_t ih_void;    /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */    struct ih_pmtu    {
           u_int16_t ipm_void;      u_int16_t ipm_nextmtu;    } ih_pmtu;    struct ih_rtradv    {
           u_int8_t irt_num_addrs;      u_int8_t irt_wpa;      u_int16_t irt_lifetime;    } ih_rtradv;  } icmp_hun;#define icmp_pptr   icmp_hun.ih_pptr#define icmp_gwaddr icmp_hun.ih_gwaddr#define icmp_id     icmp_hun.ih_idseq.icd_id#define icmp_seq        icmp_hun.ih_idseq.icd_seq#define icmp_void   icmp_hun.ih_void#define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void#define icmp_nextmtu    icmp_hun.ih_pmtu.ipm_nextmtu#define icmp_num_addrs  icmp_hun.ih_rtradv.irt_num_addrs#define icmp_wpa    icmp_hun.ih_rtradv.irt_wpa#define icmp_lifetime   icmp_hun.ih_rtradv.irt_lifetime  union  {    struct    {
           u_int32_t its_otime;      u_int32_t its_rtime;      
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值