您的位置:首页 > 理论基础 > 计算机网络

【计算机网络】网络诊断工具ping的模拟实现之搭建框架

2017-07-23 10:37 507 查看
昨天整理了整个ICMP的知识,项目的知识储备已经完成了一半,今天需要看看这个报文在我们网络编程中该怎么发送。



上面是一个IP数据报,我们在使用ping的时候关注的是:被测试系统主机名和相应 IP 地址、返回给当前主机的 ICMP 报文顺序号、ttl 生存时间和往返时间 rtt(单位是毫秒,即千分之一秒)。



今天主要是搭建一个框架,整理好每部分该怎么去分配安排。本来打算自己创建一个结构体将ICMP的报文信息保存,但在Linux库中发现了ICMP的数据结构。

#include<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;
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
};


既然库中提供了当然不用我们自己去封装,然后我就顺便搬来了IP报文的数据结构。

#include<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的ttl(生存时间)、ip_ hl(报头长度)。

IP 报头长度IHL(Internet Header Length)以4字节为一个单位来记录IP报头的长度,是IP数据结构的ip_ hl变量。

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

既然ping是一个网络诊断工具,那么它首先是可以向外发送报文的,既然可以发送,那么对方回送的报文我们也要能接收。在上篇博客中,我们都知道ICMP报文是封装在IP数据报中发送出去,那么对应的就要有封装报文和分离报文。框架图如下。



写到这里对整个项目的框架是比较清楚的了。下篇博客我们就开始实现每个模块的功能。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: