ICMP(Internet Control Message Protocol)协议[总]

ICMP协议总述,包含实现的icmp协议应用集合程序icmpy;在以后各篇将讲解每种协议及其应用程序的实现

 

  ICMP协议是一种发送各种消息报告状态的协态,与IP协议一样属于网络层

  报文格式:

        

  完整的Type表:

               

  1. ICMP Echo

      作用:探测主机地址是否存活

   回送消息

         

   回送响应消息

         

 

  2. 超时报文

  作用:通知源主机超时信息

      

  Code: 0 - 由网关发送;网关在处理数据报时发现生存周期为0,则此数据报必须抛弃,通过超时信息通知源主机

      1 - 由主机发送;主机在组装分段的数据报时因为丢失段未能在规定时间内组装数据,此数据报必须抛弃,发送超时信息通知源                     主机

 

  3. 目标主机不可达报文

  

  Code: 0 - 网络不可达;指定的网络不可达,由网关发送不可达信息

      1 - 主机不可达;网关决定目的主机不可达,由网关发送不可达信息

      2 - 协议不可用;在目的主机,因指定协议模块不可用,由目的主机发送不可达信息

      3 - 端口不可达;在目的主机,因进程端口不可用,由目的主机发送不可达信息

      4 - 需要段和DF设置;当数据报必须分段传送,但“不可分段”位设置,网关发送不可达信息

      5 - 源路由失败

 

  4. 重定向报文

  作用:当路由器的接口收到报文,又要从该接口转发出去的时候,则向主机发送ICMP重定向报文,通知该主机在主机路由表上加上一条主机路由

      

  Code: 0 - 网络重定向

      1 - 主机重定向

      2 - 服务类型和网络重定向

      3 - 服务类型和主机重定向

 

  5. 时间戳报文

  作用:允许系统向另一个系统查询当前的时间

   时间戳请求

   

    请求端填写发起时间戳

 

   时间戳应答

   

    应答系统收到请求报文时填写接收时间戳;发送应答时填写发送时间戳

 

   6. 路由器通告

   作用:路由器发现使用ICMP路由器通告及路由器请求信息,允许主机发现子网上运作的路由器地址

    路由器请求

        

 

    路由器通告

    

     Num Addrs - 信息中广告的路由器地址号。

 

      Addr Entry Size - 具有32位字信息的每个路由器地址号(本协议中是2)。

              Lifetime - 路由器有效时间最大值。

              Router Address[i] - 在发送信息的 i = 1..Num Addrs 接口发送路由器的 IP 地址。

              Preference Level[i] - 每个路由器地址[i] i = 1..Num Addrs 作为缺省路由器地址,与同一子网中的其它路由器相关。

 

 关于CheckSum

 

 

   首先要注意的起止位置,从Type字段开始,到数据部分结束

          

   每16bit为单位求和(此时CheckSum字段应设为全0),高低位相加[与RFC不一样???],然后取反得到

   经典的计算公式:

 

unsigned short chksum(addr,len)    
    unsigned short *addr;  /* 校验数据开始地址(注意是以2字节为单位) */    
    int len;                /* 校验数据的长度大小,以字节为单位 */
{   
    int sum = 0;        /* 校验和 */    
    int nleft = len;    /* 未累加的数据长度 */    
    unsigned short *p;  /* 走动的临时指针,2字节为单位 */    
    unsigned short tmp = 0; /* 奇数字节长度时用到 */    
   while( nleft > 1)    
   {        
       sum += *p++;    /* 累加 */        
       nleft -= 2;    
    }    
   if(nleft == 1)      /* 奇数字节长度 */    
   {        
       *(unsigned char *)&tmp = *(&(unsigned char *)p); /* 将最后字节压如2字节的高位 */       
       sum += tmp;    
   }   
    sum += (sum >> 16) + (sum & 0xffff);    /* 高位低位相加 */    
    sum += sum >> 16;                       /* 上一步溢出时,将溢出位也加到sum中 */    
    tmp = ~sum;     /* 注意类型转换,现在的校验和为16位 */     
    return tmp;
}

 

 数据结构

  主要用到的是IP和ICMP的数据结构,包含的头文件是<netinet/ip.h>和<netinet/ip_icmp.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 */
  };
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;
      u_int32_t its_ttime;
    } id_ts;
    struct
    {
      struct ip idi_ip;
      /* options and then 64 bits of data */
    } id_ip;
    struct icmp_ra_addr id_radv;
    u_int32_t   id_mask;
    u_int8_t    id_data[1];
  } icmp_dun;
#define icmp_otime  icmp_dun.id_ts.its_otime
#define icmp_rtime  icmp_dun.id_ts.its_rtime
#define icmp_ttime  icmp_dun.id_ts.its_ttime
#define icmp_ip     icmp_dun.id_ip.idi_ip
#define icmp_radv   icmp_dun.id_radv
#define icmp_mask   icmp_dun.id_mask
#define icmp_data   icmp_dun.id_data
};

转自 http://blog.csdn.net/qy532846454/article/details/5384086
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值