日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

Ping原理Linux,ping实现原理

發布時間:2023/12/8 linux 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Ping原理Linux,ping实现原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

ping命令是用來查看網絡上另一個主機系統的網絡連接是否正常的一個工具。ping命令的工作原理是:向網絡上的另一個主機系統發送ICMP報文,如果指定系統得到了報文,它將把報文一模一樣地傳回給發送者,這有點象潛水艇聲納系統中使用的發聲裝置。 例如,在Linux終端上執行ping localhost命令將會看到以下結果: 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生存時間和往返時間rtt(單位是毫秒,即千分之一秒)。要寫一個模擬ping命令,這些信息有啟示作用。要真正了解ping命令實現原理,就要了解ping命令所使用到的TCP/IP協議。ICMP(Internet Control Message,網際控制報文協議)是為網關和目標主機而提供的一種差錯控制機制,使它們在遇到差錯時能把錯誤報告給報文源發方。ICMP協議是IP層的一個協議,但是由于差錯報告在發送給報文源發方時可能也要經過若干子網,因此牽涉到路由選擇等問題,所以ICMP報文需通過IP協議來發送。ICMP數據報的數據發送前需要兩級封裝:首先添加ICMP報頭形成ICMP報文,再添加IP報頭形成IP數據報。由于IP層協議是一種點對點的協議,而非端對端的協議,它提供無連接的數據報服務,沒有端口的概念,因此很少使用bind()和connect()函數,若有使用也只是用于設置IP地址。發送數據使用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報頭長度IHL(Internet Header Length)――以4字節為一個單位來記錄IP報頭的長度,是上述IP數據結構的ip_hl變量。 生存時間TTL(Time To Live)――以秒為單位,指出IP數據報能在網絡上停留的最長時間,其值由發送方設定,并在經過路由的每一個節點時減一,當該值為0時,數據報將被丟棄,是上述IP數據結構的ip_ttl變量。

ICMP報頭格式ICMP報文分為兩種,一是錯誤報告報文,二是查詢報文。每個ICMP報頭均包含類型、編碼和校驗和這三項內容,長度為8位,8位和16位,其余選項則隨ICMP的功能不同而不同。Ping命令只使用眾多ICMP報文中的兩種:"請求回送'(ICMP_ECHO)和"請求回應'(ICMP_ECHOREPLY)。

在Linux中定義如下:

#define ICMP_ECHO 0

#define ICMP_ECHOREPLY 8

這兩種ICMP類型報頭格式如下:

在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 */

} 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

};

使用宏定義令表達更簡潔,其中ICMP報頭為8字節,數據報長度最大為64K字節。校驗和算法――這一算法稱為網際校驗和算法,把被校驗的數據16位進行累加,然后取反碼,若數據字節長度為奇數,則數據尾部補一個字節的0以湊成偶數。此算法適用于IPv4、ICMPv4、IGMPV4、ICMPv6、UDP和TCP校驗和,更詳細的信息請參考RFC1071,校驗和字段為上述ICMP數據結構的icmp_cksum變量。 標識符――用于唯一標識ICMP報文,為上述ICMP數據結構的icmp_id宏所指的變量。 順序號――ping命令的icmp_seq便由這里讀出,代表ICMP報文的發送順序,為上述ICMP數據結構的icmp_seq宏所指的變量。ICMP數據報Ping命令中需要顯示的信息,包括icmp_seq和ttl都已有實現的辦法,但還缺rtt往返時間。為了實現這一功能,可利用ICMP數據報攜帶一個時間戳。使用以下函數生成時間戳:

#include int gettimeofday (struct timeval *tp, void *tzp)

其中timeval結構如下:

struct timeval

{

long tv_sec;

long tv_usec;

};

其中tv_sec為秒數,tv_usec微秒數。在發送和接收報文時由gettimeofday分別生成兩個timeval結構,兩者之差即為往返時間,即ICMP報文發送與接收的時間差,而timeval結構由ICMP數據報攜帶,tzp指針表示時區,一般都不使用,賦NULL值。 數據統計系統自帶的ping命令當它接送完所有ICMP報文后,會對所有發送和所有接收的ICMP報文進行統計,從而計算ICMP報文丟失的比率。為達此目的,定義兩個全局變量:接收計數器和發送計數器,用于記錄ICMP報文接受和發送數目。丟失數目=發送總數-接收總數,丟失比率=丟失數目/發送總數。

在實際的Ping命令實現中,主要用到的ICMP報頭的成員如下:

typedef struct _icmphdr //定義ICMP報頭(回送與或回送響應)

{unsigned char i_type;//8位類型unsigned char i_code; //8位代碼unsigned short i_cksum; //16位校驗和unsigned short i_id; //識別號(一般用進程號作為識別號)unsigned short i_seq; //報文序列號int timestamp;//時間戳} ICMP_HEADER;

目前,IPv4的報頭結構為常用的ICMP報文結構,包括ECHO-REQUEST(響應請求消息)、ECHO-REPLY(響應應答消息)、Destination Unreachable(目標不可到達消息)、Time Exceeded(超時消息)、Parameter problems(參數錯誤消息)、Source Quenchs(源抑制消息)、redirects(重定向消息)、Timestamps(時間戳消息)、Timestamp Replies(時間戳響應消息)、Address Masks(地址掩碼請求消息)、Address Mask Replies(地址掩碼響應消息)等,是Internet上十分重要的消息。

以上是我個人的一些總結,歡迎廣大網友們指正!

總結

以上是生活随笔為你收集整理的Ping原理Linux,ping实现原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。