用C语言实现Ping程序功能

上传人:枫** 文档编号:512738238 上传时间:2024-02-20 格式:DOCX 页数:10 大小:26.17KB
返回 下载 相关 举报
用C语言实现Ping程序功能_第1页
第1页 / 共10页
用C语言实现Ping程序功能_第2页
第2页 / 共10页
用C语言实现Ping程序功能_第3页
第3页 / 共10页
用C语言实现Ping程序功能_第4页
第4页 / 共10页
用C语言实现Ping程序功能_第5页
第5页 / 共10页
点击查看更多>>
资源描述

《用C语言实现Ping程序功能》由会员分享,可在线阅读,更多相关《用C语言实现Ping程序功能(10页珍藏版)》请在金锄头文库上搜索。

1、用C语言实现Ping程序功能大部分人用ping命令只是作为查看另一个系统的网络连接是否正常的一种简单方法。在这篇文章中, 作者将介绍如何用C语言编写一个模拟ping命令功能的程序。ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具ping命令的工作原理是: 向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文一模一样地传回给 发送者,这有点象潜水艇声纳系统中使用的发声装置。例如,在Linux终端上执行ping localhost命令将会看到以下结果:PING localhost.localdomain (127.0.0.1) from 127.0.0

2、.1 : 56(84) bytes of data.64 bytes from localhos t.l ocaldomain (127.0.0.1): icmp_seq=0 ttl=255 time=112 usec64 bytes from localhos t.l ocaldomain (127.0.0.1): icmp_seq=1 ttl=255 time=79 usec64 bytes from localhos t.l ocaldomain (127.0.0.1): icmp_seq=2 ttl=255 time=78 usec64 bytes from localhos t.l

3、ocaldomain (127.0.0.1): icmp_seq=3 ttl=255 time=82 useclocalhost.localdomain ping statistics 4 packets transmitted, 4 packets received, 0% packet loss round -t rip min/avg/max/mdev = 0.078/0.087/0.112/0.018 ms 由上面的执行结果可以看到,ping命令执行后显示出被测试系统主机名和相应IP地址、返回给当前主 机的ICMP报文顺序号、ttl生存时间和往返时间rtt (单位是毫秒,即千分之一秒)

4、。要写一个模拟 ping命令,这些信息有启示作用。要真正了解ping命令实现原理,就要了解ping命令所使用到的TCP/IP协议。ICMP(Internet Control Message,网际控制报文协议)是为网关和目标主机而提供的一种差错控制机 制,使它们在遇到差错时能把错误报告给报文源发方。ICMP协议是IP层的一个协议,但是由于差错报 告在发送给报文源发方时可能也要经过若干子网,因此牵涉到路由选择等问题,所以ICMP报文需通过 IP协议来发送。ICMP数据报的数据发送前需要两级封装:首先添加ICMP报头形成ICMP报文,再添加 IP报头形成IP数据报。如下图所示IP报头ICMP报头IC

5、MP数据报IP报头格式由于IP层协议是一种点对点的协议,而非端对端的协议,它提供无连接的数据报服务,没有端口的槪 念,因此很少使用bind ()和conneet()函数,若有使用也只是用于设置IP地址。发送数据使用sendto() 函数,接收数据使用reevfrom()函数。IP报头格式如下图:版本号VERIP报头长度IHL服务类型TOS数据报长度TL报文标志ID报文标志F分段偏移量F0生存时间TTL协议号PORT报头校验和源地址 目标地址任选项和填充位在Linux中,IP报头格式数据结构()定义如下: struet ip#if _BYTE_ORDER = _LITTLE_ENDIANunsi

6、gned int ip_hl:4;/* header length */version */unsigned int ip_v:4;/*unsigned int ip_v:4; unsigned int ip_hl:4;/* version */ /* header length */#endifu_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 */#defi

7、ne 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 bitsu_int8_t ip_ttl;/* time to live */u_int8_t ip_p;/* protocol */u_short ip_sum;/* checksum */*/struet in_addr ip_src,

8、 ;ip_dst; /* source and dest address */#endif#if BYTE ORDER = BIG ENDIAN其中ping程序只使用以下数据:IP报头长度IHL (In terne t Header Leng th) ?D?D以4字节为一个单位来记录IP报头的长度, 是上述IP数据结构的ip_hl变量。生存时间TTL(Time To Live)?D?D以秒为单位,指出IP数据报能在网络上停留的最长时间, 其值由发送方设定,并在经过路由的每一个节点时减一,当该值为0时,数据报将被丢弃,是上述 IP数据结构的ip_ttl变量。ICMP报头格式ICMP报文分为两种,

9、一是错误报告报文,二是查询报文。每个ICMP报头均包含类型、编码和校验和这 三项内容,长度为8位,8位和16位,其余选项则随ICMP的功能不同而不同。Ping命令只使用众多ICMP报文中的两种:请求回送(ICMP_ECH0)和请求回应(ICMP_ECHOREPLY)。 在Linux中定义如下:#define ICMP_ECHO 0#define ICMP ECHOREPLY 8 这两种ICMP类型报头格式如下:类型 TYPE(80)编码CODE (没有使用)校验和CHECKSUM标志符 Identifier顺序号 Sequence NO在 Linux 中 ICMP 数据结构()定义如下: st

10、ruct icmpu_int8_t icmp_type; /* type of message, see below */ u_int8_t icmp_code; /* type sub code */u_intl6_t icmp_cksum; /* ones complement checksum of struct */ unionu_char ih_pptr;/* ICMP_PARAMPROB */struct in_addr ih_gwaddr; /* gateway address */struct ih_idseq /* echo datagram */ u_intl6_t icd

11、_id;u_intl6_t icd_seq; ih_idseq;u_int32_t ih_void;/* ICMP UNREACH NEEDFRAG Path MTU Discovery (RFC1191) */struet ih_pmtuu_intl6_t ipm_void;u_intl6_t ipm_nextmtu; ih_pmtu;struet ih_rtradv u_int8_t irt_num_addrs;u_int8_t irt_wpa;u_intl6_t irt_lifetime; ih_rtradv; iemp_hun;#define iemp_pptricmp_hun.ih_

12、pptr#define iemp_gwaddricmp_hun.ih_gwaddr#define iemp_idicmp_hun.ih_idseq.icd_id#define iemp_seqicmp_hun.ih_idseq.icd_seq#define iemp_voidicmp_hun.ih_void#define iemp_pmvoid icmp_hun.ih_pmtu .ipm_void#define iemp_nextmtu icmp_hun.ih_pmtu .ipm_nextmtu#define iemp_num_addrs icmp_hun.ih_rtradv.irt_num_

13、addrs#define iemp_wpa icmp_hun.ih_rtradv.irt_wpa#define iemp_lifetime icmp_hun.ih_rtradv.irt_lifetime unionstruetu_int32_t its_otime;u_int32_t its_rtime;u_int32_t its_ttime; id_ts;struetstruet ip idi_ip;/* options and then 64 bits of data */ id_ip;struet icmp_ra_addr id_radv;u_int32_t id_mask;u_int8

14、_tid_datal; iemp_dun;#define iemp_otime iemp_dun.id_ts.its_otime#define iemp_rtime iemp_dun.id_ts.its_rtime#define iemp_ttime iemp_dun.id_ts.its_ttime#define iemp ip iemp dun.id ip.idi ip#define #define #define ;icmp_radv icmp_mask icmp_dataicmp_dun.id_radv icmp_dun.id_mask icmp_dun.id_data使用宏定义令表达更

15、简洁,其中ICMP报头为8字节,数据报长度最大为64K字节。.校验和算法?D?D这一算法称为网际校验和算法,把被校验的数据16位进行累加,然后取反码,若数据字节长度为奇数,则数据尾部补一个字节的0以凑成偶数。此算法适用于IPv4、ICMPv4、 IGMPV4、ICMPv6、UDP和TCP校验和,更详细的信息请参考RFC1071,校验和字段为上述ICMP数据 结构的icmp_cksum变量。.标识符?D?D用于唯一标识ICMP报文,为上述ICMP数据结构的icmp_id宏所指的变量。.顺序号?D?Dping命令的icmp_seq便由这里读出,代表ICMP报文的发送顺序,为上述ICMP数据结构的icmp_seq宏所指的变量。ICMP数据报Ping命令中需要显示的信息,包括icmp_seq和ttl都已有实现的办法,但还缺rtt往返时间。为了实 现这一功能,可利用ICMP数据报携带一个时间戳。使用以下函数生成时间戳:#include

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号