用C语言实现Ping程序功能.doc
《用C语言实现Ping程序功能.doc》由会员分享,可在线阅读,更多相关《用C语言实现Ping程序功能.doc(20页珍藏版)》请在冰豆网上搜索。
用C语言实现Ping程序功能
2001年10月01日
大部分人用ping命令只是作为查看另一个系统的网络连接是否正常的一种简单方法。
在这篇文章中,作者将介绍如何用C语言编写一个模拟ping命令功能的程序。
ping命令是用来查看网络上另一个主机系统的网络连接是否正常的一个工具。
ping命令的工作原理是:
向网络上的另一个主机系统发送ICMP报文,如果指定系统得到了报文,它将把报文一模一样地传回给发送者,这有点象潜水艇声纳系统中使用的发声装置。
例如,在Linux终端上执行ping localhost命令将会看到以下结果:
PINGlocalhost.localdomain(127.0.0.1)from127.0.0.1:
56(84)bytesofdata.
64bytesfromlocalhost.localdomain(127.0.0.1):
icmp_seq=0ttl=255time=112usec
64bytesfromlocalhost.localdomain(127.0.0.1):
icmp_seq=1ttl=255time=79usec
64bytesfromlocalhost.localdomain(127.0.0.1):
icmp_seq=2ttl=255time=78usec
64bytesfromlocalhost.localdomain(127.0.0.1):
icmp_seq=3ttl=255time=82usec
---localhost.localdomainpingstatistics---
4packetstransmitted,4packetsreceived,0%packetloss
round-tripmin/avg/max/mdev=0.078/0.087/0.112/0.018ms
由上面的执行结果可以看到,ping命令执行后显示出被测试系统主机名和相应IP地址、返回给当前主机的ICMP报文顺序号、ttl生存时间和往返时间rtt(单位是毫秒,即千分之一秒)。
要写一个模拟ping命令,这些信息有启示作用。
要真正了解ping命令实现原理,就要了解ping命令所使用到的TCP/IP协议。
ICMP(InternetControlMessage,网际控制报文协议)是为网关和目标主机而提供的一种差错控制机制,使它们在遇到差错时能把错误报告给报文源发方。
ICMP协议是IP层的一个协议,但是由于差错报告在发送给报文源发方时可能也要经过若干子网,因此牵涉到路由选择等问题,所以ICMP报文需通过IP协议来发送。
ICMP数据报的数据发送前需要两级封装:
首先添加ICMP报头形成ICMP报文,再添加IP报头形成IP数据报。
如下图所示
IP报头
ICMP报头
ICMP数据报
IP报头格式
由于IP层协议是一种点对点的协议,而非端对端的协议,它提供无连接的数据报服务,没有端口的概念,因此很少使用bind()和connect()函数,若有使用也只是用于设置IP地址。
发送数据使用sendto()函数,接收数据使用recvfrom()函数。
IP报头格式如下图:
在Linux中,IP报头格式数据结构()定义如下:
structip
{
#if__BYTE_ORDER==__LITTLE_ENDIAN
unsignedintip_hl:
4;/*headerlength*/
unsignedintip_v:
4;/*version*/
#endif
#if__BYTE_ORDER==__BIG_ENDIAN
unsignedintip_v:
4;/*version*/
unsignedintip_hl:
4;/*headerlength*/
#endif
u_int8_tip_tos;/*typeofservice*/
u_shortip_len;/*totallength*/
u_shortip_id;/*identification*/
u_shortip_off;/*fragmentoffsetfield*/
#defineIP_RF0x8000/*reservedfragmentflag*/
#defineIP_DF0x4000/*dontfragmentflag*/
#defineIP_MF0x2000/*morefragmentsflag*/
#defineIP_OFFMASK0x1fff/*maskforfragmentingbits*/
u_int8_tip_ttl;/*timetolive*/
u_int8_tip_p;/*protocol*/
u_shortip_sum;/*checksum*/
structin_addrip_src,ip_dst;/*sourceanddestaddress*/
};
其中ping程序只使用以下数据:
IP报头长度IHL(InternetHeaderLength)�D�D以4字节为一个单位来记录IP报头的长度,是上述IP数据结构的ip_hl变量。
生存时间TTL(TimeToLive)�D�D以秒为单位,指出IP数据报能在网络上停留的最长时间,其值由发送方设定,并在经过路由的每一个节点时减一,当该值为0时,数据报将被丢弃,是上述IP数据结构的ip_ttl变量。
回页首
ICMP报头格式
ICMP报文分为两种,一是错误报告报文,二是查询报文。
每个ICMP报头均包含类型、编码和校验和这三项内容,长度为8位,8位和16位,其余选项则随ICMP的功能不同而不同。
Ping命令只使用众多ICMP报文中的两种:
"请求回送'(ICMP_ECHO)和"请求回应'(ICMP_ECHOREPLY)。
在Linux中定义如下:
#defineICMP_ECHO0
#defineICMP_ECHOREPLY8
这两种ICMP类型报头格式如下:
在Linux中ICMP数据结构()定义如下:
structicmp
{
u_int8_ticmp_type;/*typeofmessage,seebelow*/
u_int8_ticmp_code;/*typesubcode*/
u_int16_ticmp_cksum;/*onescomplementchecksumofstruct*/
union
{
u_charih_pptr;/*ICMP_PARAMPROB*/
structin_addrih_gwaddr;/*gatewayaddress*/
structih_idseq/*echodatagram*/
{
u_int16_ticd_id;
u_int16_ticd_seq;
}ih_idseq;
u_int32_tih_void;
/*ICMP_UNREACH_NEEDFRAG--PathMTUDiscovery(RFC1191)*/
structih_pmtu
{
u_int16_tipm_void;
u_int16_tipm_nextmtu;
}ih_pmtu;
structih_rtradv
{
u_int8_tirt_num_addrs;
u_int8_tirt_wpa;
u_int16_tirt_lifetime;
}ih_rtradv;
}icmp_hun;
#defineicmp_pptricmp_hun.ih_pptr
#defineicmp_gwaddricmp_hun.ih_gwaddr
#defineicmp_idicmp_hun.ih_idseq.icd_id
#defineicmp_seqicmp_hun.ih_idseq.icd_seq
#defineicmp_voidicmp_hun.ih_void
#defineicmp_pmvoidicmp_hun.ih_pmtu.ipm_void
#defineicmp_nextmtuicmp_hun.ih_pmtu.ipm_nextmtu
#defineicmp_num_addrsicmp_hun.ih_rtradv.irt_num_addrs
#defineicmp_wpaicmp_hun.ih_rtradv.irt_wpa
#defineicmp_lifetimeicmp_hun.ih_rtradv.irt_lifetime
union
{
struct
{
u_int32_tits_otime;
u_int32_tits_rtime;
u_int32_tits_ttime;
}id_ts;
struct
{
structipidi_ip;
/*optionsandthen64bitsofdata*/
}id_ip;
structicmp_ra_addrid_radv;
u_int32_tid_mask;
u_int8_tid_data[1];
}icmp_dun;
#defineicmp_otimeicmp_dun.id_ts.its_otime
#defineicmp_rtimeicmp_dun.id_ts.its_rtime
#defineicmp_ttimeicmp_dun.id_ts.its_ttime
#defineicmp_ipicmp_dun.id_ip.idi_ip
#defineicmp_radvicmp_dun.id_radv
#defineicmp_maskicmp_dun.id_mask
#defineicmp_dataicmp_dun.id_data
};
使用宏定义令表达更简洁,其中ICMP报头为8字节,数据报长度最大为64K字节。
校验和算法�D�D这一算法称为网际校验和算法,把被校验的数据16位进行累加,然后取反码,若数据字节长度为奇数,则数据尾部补一个字节的0以凑成偶数。
此算法适用于IPv4、ICMPv4、IGMPV4、ICMPv6、UDP和TCP校验和,更详细的信息请参考RFC1071,校验和字段为上述ICMP数据结