<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          Ping程序的C語言編程

          作者: 時(shí)間:2011-02-16 來源:網(wǎng)絡(luò) 收藏

            大部分人用ping命令只是作為查看另一個(gè)系統(tǒng)的網(wǎng)絡(luò)連接是否正常的一種簡單方法。在這篇文章中,作者將介紹如何用C語言編寫一個(gè)模擬ping命令功能的程序。

            ping命令是用來查看網(wǎng)絡(luò)上另一個(gè)主機(jī)系統(tǒng)的網(wǎng)絡(luò)連接是否正常的一個(gè)工具。ping命令的工作原理是:向網(wǎng)絡(luò)上的另一個(gè)主機(jī)系統(tǒng)發(fā)送ICMP報(bào)文,如果指定系統(tǒng)得到了報(bào)文,它將把報(bào)文一模一樣地傳回給發(fā)送者,這有點(diǎn)象潛水艇聲納系統(tǒng)中使用的發(fā)聲裝置。

            例如,在Linux終端上執(zhí)行ping localhost命令將會看到以下結(jié)果:

            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

            由上面的執(zhí)行結(jié)果可以看到,ping命令執(zhí)行后顯示出被測試系統(tǒng)主機(jī)名和相應(yīng)IP地址、返回給當(dāng)前主機(jī)的ICMP報(bào)文順序號、ttl生存時(shí)間和往返時(shí)間rtt(單位是毫秒,即千分之一秒)。要寫一個(gè)模擬ping命令,這些信息有啟示作用。

            要真正了解ping命令實(shí)現(xiàn)原理,就要了解ping命令所使用到的TCP/IP協(xié)議。

            ICMP(Internet Control Message,網(wǎng)際控制報(bào)文協(xié)議)是為網(wǎng)關(guān)和目標(biāo)主機(jī)而提供的一種差錯(cuò)控制機(jī)制,使它們在遇到差錯(cuò)時(shí)能把錯(cuò)誤報(bào)告給報(bào)文源發(fā)方。ICMP協(xié)議是IP層的一個(gè)協(xié)議,但是由于差錯(cuò)報(bào)告在發(fā)送給報(bào)文源發(fā)方時(shí)可能也要經(jīng)過若干子網(wǎng),因此牽涉到路由選擇等問題,所以ICMP報(bào)文需通過IP協(xié)議來發(fā)送。ICMP數(shù)據(jù)報(bào)的數(shù)據(jù)發(fā)送前需要兩級封裝:首先添加ICMP報(bào)頭形成ICMP報(bào)文,再添加IP報(bào)頭形成IP數(shù)據(jù)報(bào)。如下圖所示

            IP報(bào)頭

            ICMP報(bào)頭

            ICMP數(shù)據(jù)報(bào)

            IP報(bào)頭格式

            由于IP層協(xié)議是一種點(diǎn)對點(diǎn)的協(xié)議,而非端對端的協(xié)議,它提供無連接的數(shù)據(jù)報(bào)服務(wù),沒有端口的概念,因此很少使用bind()和connect()函數(shù),若有使用也只是用于設(shè)置IP地址。發(fā)送數(shù)據(jù)使用sendto()函數(shù),接收數(shù)據(jù)使用recvfrom()函數(shù)。IP報(bào)頭格式如下圖:

            在Linux中,IP報(bào)頭格式數(shù)據(jù)結(jié)構(gòu)()定義如下:

            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程序只使用以下數(shù)據(jù):

            IP報(bào)頭長度IHL(Internet Header Length)?D?D以4字節(jié)為一個(gè)單位來記錄IP報(bào)頭的長度,是上述IP數(shù)據(jù)結(jié)構(gòu)的ip_hl變量。

            生存時(shí)間TTL(Time To Live)?D?D以秒為單位,指出IP數(shù)據(jù)報(bào)能在網(wǎng)絡(luò)上停留的最長時(shí)間,其值由發(fā)送方設(shè)定,并在經(jīng)過路由的每一個(gè)節(jié)點(diǎn)時(shí)減一,當(dāng)該值為0時(shí),數(shù)據(jù)報(bào)將被丟棄,是上述IP數(shù)據(jù)結(jié)構(gòu)的ip_ttl變量。

            ICMP報(bào)頭格式

            ICMP報(bào)文分為兩種,一是錯(cuò)誤報(bào)告報(bào)文,二是查詢報(bào)文。每個(gè)ICMP報(bào)頭均包含類型、編碼和校驗(yàn)和這三項(xiàng)內(nèi)容,長度為8位,8位和16位,其余選項(xiàng)則隨ICMP的功能不同而不同。

            Ping命令只使用眾多ICMP報(bào)文中的兩種:"請求回送'(ICMP_ECHO)和"請求回應(yīng)'(ICMP_ECHOREPLY)。在Linux中定義如下:

            #define ICMP_ECHO  0

            #define ICMP_ECHOREPLY 8

            這兩種ICMP類型報(bào)頭格式如下:

            在Linux中ICMP數(shù)據(jù)結(jié)構(gòu)()定義如下:

            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語言相關(guān)文章:c語言教程


          tcp/ip相關(guān)文章:tcp/ip是什么




          評論


          相關(guān)推薦

          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();