Ping程序的C語(yǔ)言編程
大部分人用ping命令只是作為查看另一個(gè)系統的網(wǎng)絡(luò )連接是否正常的一種簡(jiǎn)單方法。在這篇文章中,作者將介紹如何用C語(yǔ)言編寫(xiě)一個(gè)模擬ping命令功能的程序。
ping命令是用來(lái)查看網(wǎng)絡(luò )上另一個(gè)主機系統的網(wǎng)絡(luò )連接是否正常的一個(gè)工具。ping命令的工作原理是:向網(wǎng)絡(luò )上的另一個(gè)主機系統發(fā)送ICMP報文,如果指定系統得到了報文,它將把報文一模一樣地傳回給發(fā)送者,這有點(diǎn)象潛水艇聲納系統中使用的發(fā)聲裝置。
例如,在Linux終端上執行ping localhost命令將會(huì )看到以下結果:
PING localhost.localdomain (127.0.0.1) from 127.0.0.1 : 56(84) bytes of data.
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=0 ttl=255 time=112 usec
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=1 ttl=255 time=79 usec
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=2 ttl=255 time=78 usec
64 bytes from localhost.localdomain (127.0.0.1): icmp_seq=3 ttl=255 time=82 usec
--- localhost.localdomain ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max/mdev = 0.078/0.087/0.112/0.018 ms
由上面的執行結果可以看到,ping命令執行后顯示出被測試系統主機名和相應IP地址、返回給當前主機的ICMP報文順序號、ttl生存時(shí)間和往返時(shí)間rtt(單位是毫秒,即千分之一秒)。要寫(xiě)一個(gè)模擬ping命令,這些信息有啟示作用。
要真正了解ping命令實(shí)現原理,就要了解ping命令所使用到的TCP/IP協(xié)議。
ICMP(Internet Control Message,網(wǎng)際控制報文協(xié)議)是為網(wǎng)關(guān)和目標主機而提供的一種差錯控制機制,使它們在遇到差錯時(shí)能把錯誤報告給報文源發(fā)方。ICMP協(xié)議是IP層的一個(gè)協(xié)議,但是由于差錯報告在發(fā)送給報文源發(fā)方時(shí)可能也要經(jīng)過(guò)若干子網(wǎng),因此牽涉到路由選擇等問(wèn)題,所以ICMP報文需通過(guò)IP協(xié)議來(lái)發(fā)送。ICMP數據報的數據發(fā)送前需要兩級封裝:首先添加ICMP報頭形成ICMP報文,再添加IP報頭形成IP數據報。如下圖所示
IP報頭
ICMP報頭
ICMP數據報
IP報頭格式
由于IP層協(xié)議是一種點(diǎn)對點(diǎn)的協(xié)議,而非端對端的協(xié)議,它提供無(wú)連接的數據報服務(wù),沒(méi)有端口的概念,因此很少使用bind()和connect()函數,若有使用也只是用于設置IP地址。發(fā)送數據使用sendto()函數,接收數據使用recvfrom()函數。IP報頭格式如下圖:
在Linux中,IP報頭格式數據結構(
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報頭長(cháng)度IHL(Internet Header Length)?D?D以4字節為一個(gè)單位來(lái)記錄IP報頭的長(cháng)度,是上述IP數據結構的ip_hl變量。
生存時(shí)間TTL(Time To Live)?D?D以秒為單位,指出IP數據報能在網(wǎng)絡(luò )上停留的最長(cháng)時(shí)間,其值由發(fā)送方設定,并在經(jīng)過(guò)路由的每一個(gè)節點(diǎn)時(shí)減一,當該值為0時(shí),數據報將被丟棄,是上述IP數據結構的ip_ttl變量。
ICMP報頭格式
ICMP報文分為兩種,一是錯誤報告報文,二是查詢(xún)報文。每個(gè)ICMP報頭均包含類(lèi)型、編碼和校驗和這三項內容,長(cháng)度為8位,8位和16位,其余選項則隨ICMP的功能不同而不同。
Ping命令只使用眾多ICMP報文中的兩種:"請求回送'(ICMP_ECHO)和"請求回應'(ICMP_ECHOREPLY)。在Linux中定義如下:
#define ICMP_ECHO 0
#define ICMP_ECHOREPLY 8
這兩種ICMP類(lèi)型報頭格式如下:
在Linux中ICMP數據結構(
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 */
c語(yǔ)言相關(guān)文章:c語(yǔ)言教程
tcp/ip相關(guān)文章:tcp/ip是什么
評論