手机版
你好,游客 登录 注册
背景:
阅读新闻

一步一步实现Linux下Ping命令

[日期:2012-03-14] 来源:Linux社区  作者:chenlong12580 [字体:大 中 小]

再来看看icmp的数据结构:

  1. /* 
  2.  * Structure of an icmp header. 
  3.  */  
  4. struct icmp {  
  5.     u_char  icmp_type;      /* type of message, see below */  
  6.     u_char  icmp_code;      /* type sub code */  
  7.     u_short icmp_cksum;     /* ones complement cksum of struct */  
  8.     union {  
  9.         u_char ih_pptr;         /* ICMP_PARAMPROB */  
  10.         struct in_addr ih_gwaddr;   /* ICMP_REDIRECT */  
  11.         struct ih_idseq {  
  12.             n_short icd_id;  
  13.             n_short icd_seq;  
  14.         } ih_idseq;  
  15.         int ih_void;  
  16.   
  17.         /* ICMP_UNREACH_NEEDFRAG -- Path MTU Discovery (RFC1191) */  
  18.         struct ih_pmtu {  
  19.             n_short ipm_void;  
  20.             n_short ipm_nextmtu;  
  21.         } ih_pmtu;  
  22.   
  23.         struct ih_rtradv {  
  24.             u_char irt_num_addrs;  
  25.             u_char irt_wpa;  
  26.             u_int16_t irt_lifetime;  
  27.         } ih_rtradv;  
  28.     } icmp_hun;  
  29. #define icmp_pptr   icmp_hun.ih_pptr   
  30. #define icmp_gwaddr icmp_hun.ih_gwaddr   
  31. #define icmp_id     icmp_hun.ih_idseq.icd_id   
  32. #define icmp_seq    icmp_hun.ih_idseq.icd_seq   
  33. #define icmp_void   icmp_hun.ih_void   
  34. #define icmp_pmvoid icmp_hun.ih_pmtu.ipm_void   
  35. #define icmp_nextmtu    icmp_hun.ih_pmtu.ipm_nextmtu   
  36. #define icmp_num_addrs  icmp_hun.ih_rtradv.irt_num_addrs   
  37. #define icmp_wpa    icmp_hun.ih_rtradv.irt_wpa   
  38. #define icmp_lifetime   icmp_hun.ih_rtradv.irt_lifetime   
  39.     union {  
  40.         struct id_ts {  
  41.             n_time its_otime;  
  42.             n_time its_rtime;  
  43.             n_time its_ttime;  
  44.         } id_ts;  
  45.         struct id_ip  {  
  46.             struct ip idi_ip;  
  47.             /* options and then 64 bits of data */  
  48.         } id_ip;  
  49.         struct icmp_ra_addr id_radv;  
  50.         u_int32_t id_mask;  
  51.         char    id_data[1];  
  52.     } icmp_dun;  
  53. #define icmp_otime  icmp_dun.id_ts.its_otime   
  54. #define icmp_rtime  icmp_dun.id_ts.its_rtime   
  55. #define icmp_ttime  icmp_dun.id_ts.its_ttime   
  56. #define icmp_ip     icmp_dun.id_ip.idi_ip   
  57. #define icmp_radv   icmp_dun.id_radv   
  58. #define icmp_mask   icmp_dun.id_mask   
  59. #define icmp_data   icmp_dun.id_data   
  60. };    

我们可以比较容易的看到类型、代码和校验和这些字段。接下来是一个联合的数据类型,里面的数据项比较多,可以看到序号之类的字段,这样看起来比较晕,型号内核为我们定义了下面的一些宏定义,使得我们只需要关注自己感兴趣的部分,例如:
#define  icmp_id       icmp_hun.ih_idseq.icd_id      则是ICMP数据报首部的标识符;
#define icmp_seq     icmp_hun.ih_idseq.icd_seq   则是我们前面提到的序号字段;
#define icmp_data    icmp_dun.id_data                  则是数据字段,可设置为发送数据报的时间。
    总结:我们仅仅需要设置这几个字段就好。
       icmp->icmp_type
  icmp->icmp_code
    icmp->icmp_cksum
    icmp->icmp_id
       icmp->icmp_seq
    icmp ->icmp_data;

linux
相关资讯       Linux编程 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款