1. 程式人生 > >c++ 實現ping

c++ 實現ping

n) time_t ply setuid details struct copyto 新版本 -o

//頭文件

[cpp] view plain copy
  1. /*
  2. * File: CPing.h
  3. * Author: jaylong35
  4. *
  5. * Created on 2011年1月26日, 下午3:12
  6. */
  7. #ifndef CPING_H
  8. #define CPING_H
  9. #include <string>
  10. #include <signal.h>
  11. #include <arpa/inet.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <unistd.h>
  15. #include <netinet/in.h>
  16. #include <netinet/ip.h>
  17. #include <netinet/ip_icmp.h>
  18. #include <netdb.h>
  19. #include <setjmp.h>
  20. #include <errno.h>
  21. #include <sys/time.h>
  22. using namespace std;
  23. #define PACKET_SIZE 4096
  24. #define SEND_DATA_LEN 56
  25. #define ERROR -1
  26. #define SUCCESS 1
  27. #define MAX_WAIT_TIME 5
  28. #define MAX_NO_PACKETS 100
  29. class CPing
  30. {
  31. public:
  32. CPing(const char * ip, int timeout);
  33. CPing(const CPing& orig);
  34. virtual ~CPing();
  35. private:
  36. std::string m_strIp;
  37. int m_nTimeOut;
  38. int m_nPkgLen;
  39. double m_dAvgTime;
  40. double m_dFasterResponseTime;
  41. double m_dLowerResponseTime;
  42. double m_dTotalResponseTimes;
  43. int m_nSend;
  44. int m_nRecv;
  45. int m_nSocketfd;
  46. pid_t m_Pid;
  47. struct sockaddr_in m_dest_addr;
  48. struct sockaddr_in m_from;
  49. char m_sendpacket[PACKET_SIZE];
  50. char m_recvpacket[PACKET_SIZE];
  51. struct timeval m_tvrecv;
  52. public:
  53. enum
  54. {
  55. PING_FAILED,
  56. PING_SUCCEED
  57. };
  58. std::string GetIp() { return m_strIp; }
  59. int GetTimeOut() { return m_nTimeOut; }
  60. int GetPkgLen() { return m_nPkgLen; }
  61. void SetIp(const char * ip) { m_strIp = ip; }
  62. void SetTimeOut(int timeout) { m_nTimeOut = timeout; }
  63. void SetPkgLen(int pkglen) { m_nPkgLen = pkglen; }
  64. double GetAvgResponseTime() { return m_dAvgTime; }
  65. double GetFasterResponseTime() { return m_dFasterResponseTime; }
  66. double GetLowerResponseTime() { return m_dLowerResponseTime; }
  67. unsigned int GetPingStatus();
  68. static unsigned short cal_chksum(unsigned short *addr, int len);
  69. void statistics();
  70. int pack(int pack_no);
  71. void send_packet(int num);
  72. void recv_packet(void);
  73. int unpack(char *buf, int len);
  74. void tv_sub(struct timeval *out, struct timeval *in);
  75. bool ping(int times);
  76. };
  77. #endif /* CPING_H

cpp文件

[c-sharp] view plain copy
  1. /*
  2. * File: CPing.cpp
  3. * Author: jaylong35
  4. *
  5. * Created on 2011年1月26日, 下午3:12
  6. */
  7. #include "CPing.h"
  8. CPing::CPing(const char * ip, int timeout)
  9. {
  10. m_strIp = ip;
  11. m_nTimeOut = timeout;
  12. m_nSend = 0;
  13. m_nRecv = 0;
  14. m_nSocketfd = 0;
  15. }
  16. CPing::CPing(const CPing& orig)
  17. {
  18. }
  19. CPing::~CPing()
  20. {
  21. }
  22. bool CPing::ping(int times)
  23. {
  24. struct hostent *host;
  25. struct protoent *protocol;
  26. unsigned long inaddr = 0l;
  27. int size = 50 * 1024;
  28. if ((protocol = getprotobyname("icmp")) == NULL)
  29. {
  30. perror("getprotobyname");
  31. return false;
  32. }
  33. /*生成使用ICMP的原始套接字,這種套接字只有root才能生成*/
  34. if ((m_nSocketfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
  35. {
  36. perror("socket error");
  37. exit(1);
  38. }
  39. /* 回收root權限,設置當前用戶權限*/
  40. setuid(getuid());
  41. /*擴大套接字接收緩沖區到50K這樣做主要為了減小接收緩沖區溢出的
  42. 的可能性,若無意中ping一個廣播地址或多播地址,將會引來大量應答*/
  43. setsockopt(m_nSocketfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
  44. bzero(&m_dest_addr, sizeof (m_dest_addr));
  45. m_dest_addr.sin_family = AF_INET;
  46. /*判斷是主機名還是ip地址*/
  47. if (inaddr = inet_addr(m_strIp.c_str()) == INADDR_NONE)
  48. {
  49. if ((host = gethostbyname(m_strIp.c_str())) == NULL) /*是主機名*/
  50. {
  51. perror("gethostbyname error");
  52. exit(1);
  53. }
  54. memcpy((char *) &m_dest_addr.sin_addr, host->h_addr, host->h_length);
  55. }
  56. else /*是ip地址*/
  57. memcpy((char *) &m_dest_addr, (char *) &inaddr, host->h_length);
  58. /*獲取main的進程id,用於設置ICMP的標誌符*/
  59. m_Pid = getpid();
  60. printf("PING %s(%s): %d bytes data in ICMP packets./n", m_strIp.c_str(),
  61. inet_ntoa(m_dest_addr.sin_addr), SEND_DATA_LEN);
  62. int i = 0;
  63. while(i < times)
  64. {
  65. i++;
  66. send_packet(1); /*發送所有ICMP報文*/
  67. recv_packet(); /*接收所有ICMP報文*/
  68. }
  69. statistics(); /*進行統計*/
  70. }
  71. unsigned short CPing::cal_chksum(unsigned short *addr, int len)
  72. {
  73. int nleft=len;
  74. int sum=0;
  75. unsigned short *w=addr;
  76. unsigned short answer=0;
  77. while(nleft > 1)
  78. {
  79. sum += *w++;
  80. nleft -= 2;
  81. }
  82. if( nleft == 1)
  83. {
  84. *(unsigned char *)(&answer) = *(unsigned char *)w;
  85. sum += answer;
  86. }
  87. sum = (sum >> 16) + (sum & 0xffff);
  88. sum += (sum >> 16);
  89. answer = ~sum;
  90. return answer;
  91. }
  92. void CPing::tv_sub(struct timeval *out,struct timeval *in)
  93. {
  94. if( (out->tv_usec-=in->tv_usec)<0)
  95. {
  96. --out->tv_sec;
  97. out->tv_usec+=1000000;
  98. }
  99. out->tv_sec-=in->tv_sec;
  100. }
  101. void CPing::statistics()
  102. {
  103. printf("/n--------------------PING statistics-------------------/n");
  104. printf("%d packets transmitted, %d received , %%%d lost/n", m_nSend, m_nRecv,
  105. (m_nSend - m_nRecv) / m_nSend * 100);
  106. close(m_nSocketfd);
  107. m_nTimeOut = m_nSend - m_nRecv;
  108. m_dAvgTime = m_dTotalResponseTimes/m_nRecv;
  109. return;
  110. }
  111. /*設置ICMP報頭*/
  112. int CPing::pack(int pack_no)
  113. {
  114. int packsize;
  115. struct icmp *icmp;
  116. struct timeval *tval;
  117. icmp = (struct icmp*) m_sendpacket;
  118. icmp->icmp_type = ICMP_ECHO;
  119. icmp->icmp_code = 0;
  120. icmp->icmp_cksum = 0;
  121. icmp->icmp_seq = pack_no;
  122. icmp->icmp_id = m_Pid;
  123. packsize = 8 + SEND_DATA_LEN;
  124. tval = (struct timeval *) icmp->icmp_data;
  125. gettimeofday(tval, NULL); /*記錄發送時間*/
  126. icmp->icmp_cksum = cal_chksum((unsigned short *) icmp, packsize); /*校驗算法*/
  127. return packsize;
  128. }
  129. /*發送三個ICMP報文*/
  130. void CPing::send_packet(int num)
  131. {
  132. if(num > MAX_NO_PACKETS)
  133. num = MAX_NO_PACKETS;
  134. int packetsize;
  135. int i = 0;
  136. while (i < num)
  137. {
  138. i++;
  139. m_nSend++;
  140. packetsize = pack(m_nSend); /*設置ICMP報頭*/
  141. if (sendto(m_nSocketfd, m_sendpacket, packetsize, 0,
  142. (struct sockaddr *) &m_dest_addr, sizeof (m_dest_addr)) < 0)
  143. {
  144. perror("sendto error");
  145. continue;
  146. }
  147. sleep(1); /*每隔一秒發送一個ICMP報文*/
  148. }
  149. }
  150. /*接收所有ICMP報文*/
  151. void CPing::recv_packet()
  152. {
  153. int n,fromlen;
  154. //signal(SIGALRM, statistics);
  155. fromlen = sizeof (m_from);
  156. while (m_nRecv < m_nSend)
  157. {
  158. alarm(MAX_WAIT_TIME);
  159. if ((n = recvfrom(m_nSocketfd, m_recvpacket, sizeof (m_recvpacket), 0,
  160. (struct sockaddr *) &m_from, (socklen_t *)&fromlen)) < 0)
  161. {
  162. if (errno == EINTR)continue;
  163. perror("recvfrom error");
  164. continue;
  165. }
  166. gettimeofday(&m_tvrecv, NULL); /*記錄接收時間*/
  167. if (unpack(m_recvpacket, n) == -1)
  168. continue;
  169. m_nRecv++;
  170. }
  171. }
  172. /*剝去ICMP報頭*/
  173. int CPing::unpack(char *buf,int len)
  174. {
  175. int i,iphdrlen;
  176. struct ip *ip;
  177. struct icmp *icmp;
  178. struct timeval *tvsend;
  179. double rtt;
  180. ip = (struct ip *) buf;
  181. iphdrlen = ip->ip_hl << 2; /*求ip報頭長度,即ip報頭的長度標誌乘4*/
  182. icmp = (struct icmp *) (buf + iphdrlen); /*越過ip報頭,指向ICMP報頭*/
  183. len -= iphdrlen; /*ICMP報頭及ICMP數據報的總長度*/
  184. if (len < 8) /*小於ICMP報頭長度則不合理*/
  185. {
  186. printf("ICMP packets/‘s length is less than 8/n");
  187. return -1;
  188. }
  189. /*確保所接收的是我所發的的ICMP的回應*/
  190. if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == m_Pid))
  191. {
  192. tvsend = (struct timeval *) icmp->icmp_data;
  193. tv_sub(&m_tvrecv, tvsend); /*接收和發送的時間差*/
  194. rtt = m_tvrecv.tv_sec * 1000 + m_tvrecv.tv_usec / 1000; /*以毫秒為單位計算rtt*/
  195. m_dTotalResponseTimes += rtt;
  196. if(m_dFasterResponseTime == -1)
  197. {
  198. m_dFasterResponseTime = rtt;
  199. }
  200. else if(m_dFasterResponseTime > rtt)
  201. {
  202. m_dFasterResponseTime = rtt;
  203. }
  204. if(m_dLowerResponseTime == -1)
  205. {
  206. m_dLowerResponseTime = rtt;
  207. }
  208. else if(m_dLowerResponseTime < rtt)
  209. {
  210. m_dLowerResponseTime = rtt;
  211. }
  212. /*顯示相關信息*/
  213. printf("%d/tbyte from %s/t: icmp_seq=%u/tttl=%d/trtt=%.3f/tms/n",
  214. len,
  215. inet_ntoa(m_from.sin_addr),
  216. icmp->icmp_seq,
  217. ip->ip_ttl,
  218. rtt);
  219. }
  220. else return -1;
  221. }

調用

[c-sharp] view plain copy
  1. #include "CPing.h"
  2. int main()
  3. {
  4. CPing ping("192.168.10.48",100);
  5. ping.ping(20);
  6. }


運行結果

PING 192.168.10.48(0.0.0.0): 56 bytes data in ICMP packets.表明
64 byte from 127.0.0.1 : icmp_seq=1 ttl=64 rtt=1000.000 ms
64 byte from 127.0.0.1 : icmp_seq=2 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=3 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=4 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=5 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=6 ttl=64 rtt=1002.000 ms
64 byte from 127.0.0.1 : icmp_seq=7 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=8 ttl=64 rtt=1002.000 ms
64 byte from 127.0.0.1 : icmp_seq=9 ttl=64 rtt=1002.000 ms
64 byte from 127.0.0.1 : icmp_seq=10 ttl=64 rtt=1000.000 ms
64 byte from 127.0.0.1 : icmp_seq=11 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=12 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=13 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=14 ttl=64 rtt=1000.000 ms
64 byte from 127.0.0.1 : icmp_seq=15 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=16 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=17 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=18 ttl=64 rtt=1002.000 ms
64 byte from 127.0.0.1 : icmp_seq=19 ttl=64 rtt=1001.000 ms
64 byte from 127.0.0.1 : icmp_seq=20 ttl=64 rtt=1001.000 ms

--------------------PING statistics-------------------
20 packets transmitted, 20 received , %0 lost
按 [Enter] 鍵關閉終端...

更新最新可用版本

因為很多朋友都要一個可用版本,所以今天在這裏更新一下上面的程序

新版本的程序如下

.h

[cpp] view plain copy
  1. /*
  2. * File: CPing.h
  3. * Author: scotte.ye
  4. *
  5. * Created on 2011年1月26日, 下午3:12
  6. */
  7. #ifndef CPING_H
  8. #define CPING_H
  9. #include <string>
  10. #include <signal.h>
  11. #include <arpa/inet.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <unistd.h>
  15. #include <netinet/in.h>
  16. #include <netinet/ip.h>
  17. #include <netinet/ip_icmp.h>
  18. #include <netdb.h>
  19. #include <setjmp.h>
  20. #include <errno.h>
  21. #include <sys/time.h>
  22. using namespace std;
  23. #define PACKET_SIZE 4096
  24. #define SEND_DATA_LEN 56
  25. #define ERROR -1
  26. #define SUCCESS 1
  27. #define MAX_WAIT_TIME 20
  28. #define MAX_NO_PACKETS 4
  29. class CPing
  30. {
  31. public:
  32. CPing(const char * ip, int timeout);
  33. CPing(const CPing& orig);
  34. virtual ~CPing();
  35. private:
  36. std::string m_strIp;
  37. std::string m_Ip;
  38. int m_nTimeOut;
  39. int m_nPkgLen;
  40. double m_dAvgTime;
  41. double m_dFasterResponseTime;
  42. double m_dLowerResponseTime;
  43. double m_dTotalResponseTimes;
  44. int m_nSend;
  45. int m_nRecv;
  46. int m_nSocketfd;
  47. pid_t m_Pid;
  48. struct sockaddr_in m_dest_addr;
  49. struct sockaddr_in m_from;
  50. char m_sendpacket[PACKET_SIZE];
  51. char m_recvpacket[PACKET_SIZE];
  52. struct timeval m_tvrecv;
  53. bool m_bTimeOut;
  54. //add by scotte.ye 2011-07-27
  55. int m_nMaxTimeWait;
  56. int m_nMaxTestpkg;
  57. public:
  58. enum
  59. {
  60. PING_FAILED,
  61. PING_SUCCEED
  62. };
  63. void SetMaxTimeWait(int nMaxTimeWait) { m_nMaxTimeWait = nMaxTimeWait; }
  64. void SetMaxTestpkg(int nMaxTestpkg) { m_nMaxTestpkg = nMaxTestpkg; }
  65. void SetPingManager(CPingManager * pPingManager) { m_pPingManager = pPingManager; }
  66. int GetSuccessTimes() { return m_nRecv; }
  67. std::string GetIp() { return m_Ip; }
  68. int GetTimeOut() { return m_nTimeOut; }
  69. int GetPkgLen() { return m_nPkgLen; }
  70. void SetIp(const char * ip) { m_strIp = ip; }
  71. void SetTimeOut(int timeout) { m_nTimeOut = timeout; }
  72. void SetPkgLen(int pkglen) { m_nPkgLen = pkglen; }
  73. double GetAvgResponseTime() { return m_dAvgTime; }
  74. double GetFasterResponseTime() { return m_dFasterResponseTime; }
  75. double GetLowerResponseTime() { return m_dLowerResponseTime; }
  76. unsigned int GetPingStatus();
  77. static unsigned short cal_chksum(unsigned short *addr, int len);
  78. //void statistics(int sig);
  79. int pack(int pack_no);
  80. void send_packet(void);
  81. void recv_packet(void);
  82. int unpack(char *buf, int len);
  83. void tv_sub(struct timeval *out, struct timeval *in);
  84. bool ping(int times);
  85. void statistics(int sig);
  86. bool CreateSocket();
  87. bool CloseSocket();
  88. //double ping_m();
  89. };
  90. #endif /* CPING_H */


cpp

[cpp] view plain copy
    1. /*
    2. * File: CPing.cpp
    3. * Author: scotte.ye
    4. *
    5. * Created on 2011年1月26日, 下午3:12
    6. */
    7. #include "CPing.h"
    8. #include "CPingManager.h"
    9. #include "include/Log/CLog.h"
    10. #include <sys/types.h>
    11. #include <fcntl.h>
    12. #define gettid() syscall(224)
    13. void CPing::statistics(int sig)
    14. {
    15. printf("------statistics------\n");
    16. printf("%d packets transmitted, %d received , %%%d lost\n", m_nSend, m_nRecv,
    17. (m_nSend - m_nRecv) / m_nSend * 100);
    18. //close(m_nSocketfd);
    19. m_nTimeOut = m_nSend - m_nRecv;
    20. m_dAvgTime = m_dTotalResponseTimes/m_nRecv;
    21. // m_bTimeOut = true;
    22. }
    23. CPing::CPing(const char * ip, int timeout)
    24. {
    25. m_strIp = ip;
    26. m_Ip = ip;
    27. m_nTimeOut = 0;
    28. m_nSend = 0;
    29. m_nRecv = 0;
    30. m_nSocketfd = 0;
    31. m_dFasterResponseTime = -1;
    32. m_dLowerResponseTime = -1;
    33. m_dAvgTime = -1;
    34. m_dTotalResponseTimes = 0;
    35. m_pPingManager = NULL;
    36. if(timeout > MAX_WAIT_TIME)
    37. m_nMaxTimeWait = MAX_WAIT_TIME;
    38. else
    39. m_nMaxTimeWait = timeout;
    40. m_nMaxTestpkg = MAX_NO_PACKETS;
    41. }
    42. CPing::CPing(const CPing& orig)
    43. {
    44. }
    45. CPing::~CPing()
    46. {
    47. CloseSocket();
    48. }
    49. bool CPing::CreateSocket()
    50. {
    51. CloseSocket();
    52. struct hostent hostinfo,*host;
    53. char buf[2048];
    54. struct protoent *protocol;
    55. unsigned long inaddr = 0l;
    56. int size = 50 * 1024;
    57. if ((protocol = getprotobyname("icmp")) == NULL)
    58. {
    59. printf("CreateSocket: getprotobyname failed:%d\n",errno);
    60. return false;
    61. }
    62. /*生成使用ICMP的原始套接字,這種套接字只有root才能生成*/
    63. if ((m_nSocketfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
    64. {
    65. printf("CreateSocket: create socket failed:%d\n",errno);
    66. return false;
    67. }
    68. /* 回收root權限,設置當前用戶權限*/
    69. setuid(getuid());
    70. /*擴大套接字接收緩沖區到50K這樣做主要為了減小接收緩沖區溢出的
    71. 的可能性,若無意中ping一個廣播地址或多播地址,將會引來大量應答*/
    72. int nRet = setsockopt(m_nSocketfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
    73. if(nRet != 0)
    74. {
    75. printf("CreateSocket: set socket receive buf failed:%d\n",errno);
    76. return false;
    77. }
    78. bzero(&m_dest_addr, sizeof (m_dest_addr));
    79. m_dest_addr.sin_family = AF_INET;
    80. /*判斷是主機名還是ip地址*/
    81. if ((inaddr = inet_addr(m_strIp.c_str())) == INADDR_NONE)
    82. {
    83. int nret;
    84. gethostbyname_r(m_strIp.c_str(), &hostinfo, buf, sizeof(buf), &host, &nret);
    85. if (nret != 0) /*是主機名*/
    86. {
    87. printf("CreateSocket: gethostbyname error %s failed:%d\n",m_strIp.c_str(),errno);
    88. return false;
    89. }
    90. memcpy((char *) &m_dest_addr.sin_addr, host->h_addr, host->h_length);
    91. }
    92. else /*是ip地址*/
    93. memcpy((char *) &m_dest_addr.sin_addr, (char *) &inaddr, sizeof(inaddr));
    94. m_Ip = inet_ntoa(m_dest_addr.sin_addr);
    95. return true;
    96. }
    97. bool CPing::CloseSocket()
    98. {
    99. if(m_nSocketfd !=0)
    100. close(m_nSocketfd);
    101. m_nSocketfd = 0;
    102. return true;
    103. }
    104. bool CPing::ping(int times)
    105. {
    106. int i = 0;
    107. while(i < times)
    108. {
    109. bool bRet = CreateSocket();
    110. if(!bRet)
    111. {
    112. printf("ping: create socket falied!\n");
    113. return false;
    114. }
    115. /*獲取main的進程id,用於設置ICMP的標誌符*/
    116. int nh = gettid();
    117. nh = nh<<8;
    118. time_t t;
    119. time(&t);
    120. int nt = t;
    121. nh = nh&0xff00;
    122. nt = nt&0xff;
    123. m_Pid = nt|nh;
    124. printf("PING %s(%s): %d bytes data in ICMP packets.\n", m_strIp.c_str(),
    125. m_Ip.c_str(), SEND_DATA_LEN);
    126. i++;
    127. m_nSend = 0;
    128. m_nRecv = 0;
    129. send_packet(); /*發送所有ICMP報文*/
    130. recv_packet(); /*接收所有ICMP報文*/
    131. //statistics(SIGALRM); /*進行統計*/
    132. if(m_nRecv > 0)
    133. break;
    134. }
    135. m_bTimeOut = false;
    136. if(m_nRecv > 0)
    137. {
    138. m_nTimeOut = m_nSend - m_nRecv;
    139. m_dAvgTime = m_dTotalResponseTimes/m_nRecv;
    140. }
    141. else
    142. {
    143. m_nTimeOut = m_nSend;
    144. m_dAvgTime = -1;
    145. return false;
    146. }
    147. return true;
    148. }
    149. unsigned short CPing::cal_chksum(unsigned short *addr, int len)
    150. {
    151. int nleft=len;
    152. int sum=0;
    153. unsigned short *w=addr;
    154. unsigned short answer=0;
    155. while(nleft > 1)
    156. {
    157. sum += *w++;
    158. nleft -= 2;
    159. }
    160. if( nleft == 1)
    161. {
    162. *(unsigned char *)(&answer) = *(unsigned char *)w;
    163. sum += answer;
    164. }
    165. sum = (sum >> 16) + (sum & 0xffff);
    166. sum += (sum >> 16);
    167. answer = ~sum;
    168. return answer;
    169. }
    170. void CPing::tv_sub(struct timeval *out,struct timeval *in)
    171. {
    172. if( (out->tv_usec-=in->tv_usec)<0)
    173. {
    174. --out->tv_sec;
    175. out->tv_usec+=1000000;
    176. }
    177. out->tv_sec-=in->tv_sec;
    178. }
    179. /*設置ICMP報頭*/
    180. int CPing::pack(int pack_no)
    181. {
    182. int packsize;
    183. struct icmp *icmp;
    184. struct timeval *tval;
    185. icmp = (struct icmp*) m_sendpacket;
    186. icmp->icmp_type = ICMP_ECHO;
    187. icmp->icmp_code = 0;
    188. icmp->icmp_cksum = 0;
    189. icmp->icmp_seq = pack_no;
    190. icmp->icmp_id = m_Pid;
    191. packsize = 8 + SEND_DATA_LEN;
    192. tval = (struct timeval *) icmp->icmp_data;
    193. gettimeofday(tval, NULL); /*記錄發送時間*/
    194. icmp->icmp_cksum = cal_chksum((unsigned short *) icmp, packsize); /*校驗算法*/
    195. return packsize;
    196. }
    197. /*發送三個ICMP報文*/
    198. void CPing::send_packet()
    199. {
    200. int packetsize;
    201. while (m_nSend < m_nMaxTestpkg)
    202. {
    203. m_nSend++;
    204. packetsize = pack(m_nSend); /*設置ICMP報頭*/
    205. if (sendto(m_nSocketfd, m_sendpacket, packetsize, 0,
    206. (struct sockaddr *) &m_dest_addr, sizeof (m_dest_addr)) < 0)
    207. {
    208. printf("send_packet: send error :%d\n",errno);
    209. continue;
    210. }
    211. usleep(2); /*每隔一秒發送一個ICMP報文*/
    212. }
    213. }
    214. /*接收所有ICMP報文*/
    215. void CPing::recv_packet()
    216. {
    217. int n,fromlen;
    218. while(m_nRecv < m_nSend)
    219. {
    220. struct timeval timeo;
    221. fd_set readfds;
    222. FD_ZERO(&readfds);
    223. FD_SET(m_nSocketfd,&readfds);
    224. int maxfds = m_nSocketfd +1;
    225. timeo.tv_sec = m_nMaxTimeWait;
    226. timeo.tv_usec = 0;
    227. n = select(maxfds,&readfds,NULL,NULL,&timeo);
    228. if(n == 0)
    229. {
    230. printf("recv_packet: select time out :%d",errno);
    231. return ;
    232. }
    233. else if(n < 0)
    234. {
    235. printf("recv_packet: select error :%d",errno);
    236. if(errno == EINTR)
    237. {
    238. printf("recv_packet: select error :%d",errno);
    239. continue;
    240. }
    241. else
    242. {
    243. printf("recv_packet: select error :%d",errno);
    244. return ;
    245. }
    246. }
    247. if ((n = recvfrom(m_nSocketfd, m_recvpacket, sizeof (m_recvpacket), 0,
    248. (struct sockaddr *) &m_from, (socklen_t *)&fromlen)) <= 0)
    249. {
    250. printf("recv_packet: recv error :%d",errno);
    251. return;
    252. }
    253. gettimeofday(&m_tvrecv, NULL); /*記錄接收時間*/
    254. if (unpack(m_recvpacket, n) == -1)
    255. {
    256. continue;
    257. }
    258. m_nRecv++;
    259. }
    260. //return ;
    261. }
    262. /*剝去ICMP報頭*/
    263. int CPing::unpack(char *buf,int len)
    264. {
    265. int i,iphdrlen;
    266. struct ip *ip;
    267. struct icmp *icmp;
    268. struct timeval *tvsend;
    269. double rtt;
    270. ip = (struct ip *) buf;
    271. iphdrlen = ip->ip_hl << 2; /*求ip報頭長度,即ip報頭的長度標誌乘4*/
    272. icmp = (struct icmp *) (buf + iphdrlen); /*越過ip報頭,指向ICMP報頭*/
    273. len -= iphdrlen; /*ICMP報頭及ICMP數據報的總長度*/
    274. if (len < 8) /*小於ICMP報頭長度則不合理*/
    275. {
    276. printf( "ICMP packets\‘s length is less than 8");
    277. return -1;
    278. }
    279. /*確保所接收的是我所發的的ICMP的回應*/
    280. if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == m_Pid)&&(m_Ip == inet_ntoa(m_from.sin_addr)))
    281. {
    282. tvsend = (struct timeval *) icmp->icmp_data;
    283. tv_sub(&m_tvrecv, tvsend); /*接收和發送的時間差*/
    284. rtt = m_tvrecv.tv_sec * 1000 + m_tvrecv.tv_usec / 1000; /*以毫秒為單位計算rtt*/
    285. m_dTotalResponseTimes += rtt;
    286. if(m_dFasterResponseTime == -1)
    287. {
    288. m_dFasterResponseTime = rtt;
    289. }
    290. else if(m_dFasterResponseTime > rtt)
    291. {
    292. m_dFasterResponseTime = rtt;
    293. }
    294. if(m_dLowerResponseTime == -1)
    295. {
    296. m_dLowerResponseTime = rtt;
    297. }
    298. else if(m_dLowerResponseTime < rtt)
    299. {
    300. m_dLowerResponseTime = rtt;
    301. }
    302. /*顯示相關信息*/
    303. printf("%d\tbyte from %s\t: icmp_seq=%u\tttl=%d\trtt=%.3f\tms\n",
    304. len,
    305. inet_ntoa(m_from.sin_addr),
    306. icmp->icmp_seq,
    307. ip->ip_ttl,
    308. rtt);
    309. }
    310. else
    311. {
    312. printf("throw away the old package %d\tbyte from %s\t: icmp_seq=%u\tttl=%d\trtt=%.3f\tms",
    313. len,
    314. inet_ntoa(m_from.sin_addr),
    315. icmp->icmp_seq,
    316. ip->ip_ttl,
    317. rtt);
    318. return -1;
    319. }
    320. }

c++ 實現ping