用C语言实现Ping程序功能Word文件下载.doc
- 文档编号:13315087
- 上传时间:2022-10-09
- 格式:DOC
- 页数:20
- 大小:72.50KB
用C语言实现Ping程序功能Word文件下载.doc
《用C语言实现Ping程序功能Word文件下载.doc》由会员分享,可在线阅读,更多相关《用C语言实现Ping程序功能Word文件下载.doc(20页珍藏版)》请在冰豆网上搜索。
icmp_seq=2ttl=255time=78usec
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报头格式数据结构(<
netinet/ip.h>
)定义如下:
structip
{
#if__BYTE_ORDER==__LITTLE_ENDIAN
unsignedintip_hl:
4;
/*headerlength*/
unsignedintip_v:
/*version*/
#endif
#if__BYTE_ORDER==__BIG_ENDIAN
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数据结构(<
netinet/ip_icmp.h>
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
struct
u_int32_tits_otime;
u_int32_tits_rtime;
u_int32_tits_ttime;
}id_ts;
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数据结
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 实现 Ping 程序 功能