c++ 實現ping
//頭文件
[cpp] view plain copy
- /*
- * File: CPing.h
- * Author: jaylong35
- *
- * Created on 2011年1月26日, 下午3:12
- */
- #ifndef CPING_H
- #define CPING_H
- #include <string>
- #include <signal.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/ip_icmp.h>
- #include <netdb.h>
- #include <setjmp.h>
- #include <errno.h>
- #include <sys/time.h>
- using namespace std;
- #define PACKET_SIZE 4096
- #define SEND_DATA_LEN 56
- #define ERROR -1
- #define SUCCESS 1
- #define MAX_WAIT_TIME 5
- #define MAX_NO_PACKETS 100
- class CPing
- {
- public:
- CPing(const char * ip, int timeout);
- CPing(const CPing& orig);
- virtual ~CPing();
- private:
- std::string m_strIp;
- int m_nTimeOut;
- int m_nPkgLen;
- double m_dAvgTime;
- double m_dFasterResponseTime;
- double m_dLowerResponseTime;
- double m_dTotalResponseTimes;
- int m_nSend;
- int m_nRecv;
- int m_nSocketfd;
- pid_t m_Pid;
- struct sockaddr_in m_dest_addr;
- struct sockaddr_in m_from;
- char m_sendpacket[PACKET_SIZE];
- char m_recvpacket[PACKET_SIZE];
- struct timeval m_tvrecv;
- public:
- enum
- {
- PING_FAILED,
- PING_SUCCEED
- };
- std::string GetIp() { return m_strIp; }
- int GetTimeOut() { return m_nTimeOut; }
- int GetPkgLen() { return m_nPkgLen; }
- void SetIp(const char * ip) { m_strIp = ip; }
- void SetTimeOut(int timeout) { m_nTimeOut = timeout; }
- void SetPkgLen(int pkglen) { m_nPkgLen = pkglen; }
- double GetAvgResponseTime() { return m_dAvgTime; }
- double GetFasterResponseTime() { return m_dFasterResponseTime; }
- double GetLowerResponseTime() { return m_dLowerResponseTime; }
- unsigned int GetPingStatus();
- static unsigned short cal_chksum(unsigned short *addr, int len);
- void statistics();
- int pack(int pack_no);
- void send_packet(int num);
- void recv_packet(void);
- int unpack(char *buf, int len);
- void tv_sub(struct timeval *out, struct timeval *in);
- bool ping(int times);
- };
- #endif /* CPING_H
cpp文件
[c-sharp] view plain copy
- /*
- * File: CPing.cpp
- * Author: jaylong35
- *
- * Created on 2011年1月26日, 下午3:12
- */
- #include "CPing.h"
- CPing::CPing(const char * ip, int timeout)
- {
- m_strIp = ip;
- m_nTimeOut = timeout;
- m_nSend = 0;
- m_nRecv = 0;
- m_nSocketfd = 0;
- }
- CPing::CPing(const CPing& orig)
- {
- }
- CPing::~CPing()
- {
- }
- bool CPing::ping(int times)
- {
- struct hostent *host;
- struct protoent *protocol;
- unsigned long inaddr = 0l;
- int size = 50 * 1024;
- if ((protocol = getprotobyname("icmp")) == NULL)
- {
- perror("getprotobyname");
- return false;
- }
- /*生成使用ICMP的原始套接字,這種套接字只有root才能生成*/
- if ((m_nSocketfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
- {
- perror("socket error");
- exit(1);
- }
- /* 回收root權限,設置當前用戶權限*/
- setuid(getuid());
- /*擴大套接字接收緩沖區到50K這樣做主要為了減小接收緩沖區溢出的
- 的可能性,若無意中ping一個廣播地址或多播地址,將會引來大量應答*/
- setsockopt(m_nSocketfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
- bzero(&m_dest_addr, sizeof (m_dest_addr));
- m_dest_addr.sin_family = AF_INET;
- /*判斷是主機名還是ip地址*/
- if (inaddr = inet_addr(m_strIp.c_str()) == INADDR_NONE)
- {
- if ((host = gethostbyname(m_strIp.c_str())) == NULL) /*是主機名*/
- {
- perror("gethostbyname error");
- exit(1);
- }
- memcpy((char *) &m_dest_addr.sin_addr, host->h_addr, host->h_length);
- }
- else /*是ip地址*/
- memcpy((char *) &m_dest_addr, (char *) &inaddr, host->h_length);
- /*獲取main的進程id,用於設置ICMP的標誌符*/
- m_Pid = getpid();
- printf("PING %s(%s): %d bytes data in ICMP packets./n", m_strIp.c_str(),
- inet_ntoa(m_dest_addr.sin_addr), SEND_DATA_LEN);
- int i = 0;
- while(i < times)
- {
- i++;
- send_packet(1); /*發送所有ICMP報文*/
- recv_packet(); /*接收所有ICMP報文*/
- }
- statistics(); /*進行統計*/
- }
- unsigned short CPing::cal_chksum(unsigned short *addr, int len)
- {
- int nleft=len;
- int sum=0;
- unsigned short *w=addr;
- unsigned short answer=0;
- while(nleft > 1)
- {
- sum += *w++;
- nleft -= 2;
- }
- if( nleft == 1)
- {
- *(unsigned char *)(&answer) = *(unsigned char *)w;
- sum += answer;
- }
- sum = (sum >> 16) + (sum & 0xffff);
- sum += (sum >> 16);
- answer = ~sum;
- return answer;
- }
- void CPing::tv_sub(struct timeval *out,struct timeval *in)
- {
- if( (out->tv_usec-=in->tv_usec)<0)
- {
- --out->tv_sec;
- out->tv_usec+=1000000;
- }
- out->tv_sec-=in->tv_sec;
- }
- void CPing::statistics()
- {
- printf("/n--------------------PING statistics-------------------/n");
- printf("%d packets transmitted, %d received , %%%d lost/n", m_nSend, m_nRecv,
- (m_nSend - m_nRecv) / m_nSend * 100);
- close(m_nSocketfd);
- m_nTimeOut = m_nSend - m_nRecv;
- m_dAvgTime = m_dTotalResponseTimes/m_nRecv;
- return;
- }
- /*設置ICMP報頭*/
- int CPing::pack(int pack_no)
- {
- int packsize;
- struct icmp *icmp;
- struct timeval *tval;
- icmp = (struct icmp*) m_sendpacket;
- icmp->icmp_type = ICMP_ECHO;
- icmp->icmp_code = 0;
- icmp->icmp_cksum = 0;
- icmp->icmp_seq = pack_no;
- icmp->icmp_id = m_Pid;
- packsize = 8 + SEND_DATA_LEN;
- tval = (struct timeval *) icmp->icmp_data;
- gettimeofday(tval, NULL); /*記錄發送時間*/
- icmp->icmp_cksum = cal_chksum((unsigned short *) icmp, packsize); /*校驗算法*/
- return packsize;
- }
- /*發送三個ICMP報文*/
- void CPing::send_packet(int num)
- {
- if(num > MAX_NO_PACKETS)
- num = MAX_NO_PACKETS;
- int packetsize;
- int i = 0;
- while (i < num)
- {
- i++;
- m_nSend++;
- packetsize = pack(m_nSend); /*設置ICMP報頭*/
- if (sendto(m_nSocketfd, m_sendpacket, packetsize, 0,
- (struct sockaddr *) &m_dest_addr, sizeof (m_dest_addr)) < 0)
- {
- perror("sendto error");
- continue;
- }
- sleep(1); /*每隔一秒發送一個ICMP報文*/
- }
- }
- /*接收所有ICMP報文*/
- void CPing::recv_packet()
- {
- int n,fromlen;
- //signal(SIGALRM, statistics);
- fromlen = sizeof (m_from);
- while (m_nRecv < m_nSend)
- {
- alarm(MAX_WAIT_TIME);
- if ((n = recvfrom(m_nSocketfd, m_recvpacket, sizeof (m_recvpacket), 0,
- (struct sockaddr *) &m_from, (socklen_t *)&fromlen)) < 0)
- {
- if (errno == EINTR)continue;
- perror("recvfrom error");
- continue;
- }
- gettimeofday(&m_tvrecv, NULL); /*記錄接收時間*/
- if (unpack(m_recvpacket, n) == -1)
- continue;
- m_nRecv++;
- }
- }
- /*剝去ICMP報頭*/
- int CPing::unpack(char *buf,int len)
- {
- int i,iphdrlen;
- struct ip *ip;
- struct icmp *icmp;
- struct timeval *tvsend;
- double rtt;
- ip = (struct ip *) buf;
- iphdrlen = ip->ip_hl << 2; /*求ip報頭長度,即ip報頭的長度標誌乘4*/
- icmp = (struct icmp *) (buf + iphdrlen); /*越過ip報頭,指向ICMP報頭*/
- len -= iphdrlen; /*ICMP報頭及ICMP數據報的總長度*/
- if (len < 8) /*小於ICMP報頭長度則不合理*/
- {
- printf("ICMP packets/‘s length is less than 8/n");
- return -1;
- }
- /*確保所接收的是我所發的的ICMP的回應*/
- if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == m_Pid))
- {
- tvsend = (struct timeval *) icmp->icmp_data;
- tv_sub(&m_tvrecv, tvsend); /*接收和發送的時間差*/
- rtt = m_tvrecv.tv_sec * 1000 + m_tvrecv.tv_usec / 1000; /*以毫秒為單位計算rtt*/
- m_dTotalResponseTimes += rtt;
- if(m_dFasterResponseTime == -1)
- {
- m_dFasterResponseTime = rtt;
- }
- else if(m_dFasterResponseTime > rtt)
- {
- m_dFasterResponseTime = rtt;
- }
- if(m_dLowerResponseTime == -1)
- {
- m_dLowerResponseTime = rtt;
- }
- else if(m_dLowerResponseTime < rtt)
- {
- m_dLowerResponseTime = rtt;
- }
- /*顯示相關信息*/
- printf("%d/tbyte from %s/t: icmp_seq=%u/tttl=%d/trtt=%.3f/tms/n",
- len,
- inet_ntoa(m_from.sin_addr),
- icmp->icmp_seq,
- ip->ip_ttl,
- rtt);
- }
- else return -1;
- }
調用
[c-sharp] view plain copy
- #include "CPing.h"
- int main()
- {
- CPing ping("192.168.10.48",100);
- ping.ping(20);
- }
運行結果
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
- /*
- * File: CPing.h
- * Author: scotte.ye
- *
- * Created on 2011年1月26日, 下午3:12
- */
- #ifndef CPING_H
- #define CPING_H
- #include <string>
- #include <signal.h>
- #include <arpa/inet.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <unistd.h>
- #include <netinet/in.h>
- #include <netinet/ip.h>
- #include <netinet/ip_icmp.h>
- #include <netdb.h>
- #include <setjmp.h>
- #include <errno.h>
- #include <sys/time.h>
- using namespace std;
- #define PACKET_SIZE 4096
- #define SEND_DATA_LEN 56
- #define ERROR -1
- #define SUCCESS 1
- #define MAX_WAIT_TIME 20
- #define MAX_NO_PACKETS 4
- class CPing
- {
- public:
- CPing(const char * ip, int timeout);
- CPing(const CPing& orig);
- virtual ~CPing();
- private:
- std::string m_strIp;
- std::string m_Ip;
- int m_nTimeOut;
- int m_nPkgLen;
- double m_dAvgTime;
- double m_dFasterResponseTime;
- double m_dLowerResponseTime;
- double m_dTotalResponseTimes;
- int m_nSend;
- int m_nRecv;
- int m_nSocketfd;
- pid_t m_Pid;
- struct sockaddr_in m_dest_addr;
- struct sockaddr_in m_from;
- char m_sendpacket[PACKET_SIZE];
- char m_recvpacket[PACKET_SIZE];
- struct timeval m_tvrecv;
- bool m_bTimeOut;
- //add by scotte.ye 2011-07-27
- int m_nMaxTimeWait;
- int m_nMaxTestpkg;
- public:
- enum
- {
- PING_FAILED,
- PING_SUCCEED
- };
- void SetMaxTimeWait(int nMaxTimeWait) { m_nMaxTimeWait = nMaxTimeWait; }
- void SetMaxTestpkg(int nMaxTestpkg) { m_nMaxTestpkg = nMaxTestpkg; }
- void SetPingManager(CPingManager * pPingManager) { m_pPingManager = pPingManager; }
- int GetSuccessTimes() { return m_nRecv; }
- std::string GetIp() { return m_Ip; }
- int GetTimeOut() { return m_nTimeOut; }
- int GetPkgLen() { return m_nPkgLen; }
- void SetIp(const char * ip) { m_strIp = ip; }
- void SetTimeOut(int timeout) { m_nTimeOut = timeout; }
- void SetPkgLen(int pkglen) { m_nPkgLen = pkglen; }
- double GetAvgResponseTime() { return m_dAvgTime; }
- double GetFasterResponseTime() { return m_dFasterResponseTime; }
- double GetLowerResponseTime() { return m_dLowerResponseTime; }
- unsigned int GetPingStatus();
- static unsigned short cal_chksum(unsigned short *addr, int len);
- //void statistics(int sig);
- int pack(int pack_no);
- void send_packet(void);
- void recv_packet(void);
- int unpack(char *buf, int len);
- void tv_sub(struct timeval *out, struct timeval *in);
- bool ping(int times);
- void statistics(int sig);
- bool CreateSocket();
- bool CloseSocket();
- //double ping_m();
- };
- #endif /* CPING_H */
cpp
[cpp] view plain copy
- /*
- * File: CPing.cpp
- * Author: scotte.ye
- *
- * Created on 2011年1月26日, 下午3:12
- */
- #include "CPing.h"
- #include "CPingManager.h"
- #include "include/Log/CLog.h"
- #include <sys/types.h>
- #include <fcntl.h>
- #define gettid() syscall(224)
- void CPing::statistics(int sig)
- {
- printf("------statistics------\n");
- printf("%d packets transmitted, %d received , %%%d lost\n", m_nSend, m_nRecv,
- (m_nSend - m_nRecv) / m_nSend * 100);
- //close(m_nSocketfd);
- m_nTimeOut = m_nSend - m_nRecv;
- m_dAvgTime = m_dTotalResponseTimes/m_nRecv;
- // m_bTimeOut = true;
- }
- CPing::CPing(const char * ip, int timeout)
- {
- m_strIp = ip;
- m_Ip = ip;
- m_nTimeOut = 0;
- m_nSend = 0;
- m_nRecv = 0;
- m_nSocketfd = 0;
- m_dFasterResponseTime = -1;
- m_dLowerResponseTime = -1;
- m_dAvgTime = -1;
- m_dTotalResponseTimes = 0;
- m_pPingManager = NULL;
- if(timeout > MAX_WAIT_TIME)
- m_nMaxTimeWait = MAX_WAIT_TIME;
- else
- m_nMaxTimeWait = timeout;
- m_nMaxTestpkg = MAX_NO_PACKETS;
- }
- CPing::CPing(const CPing& orig)
- {
- }
- CPing::~CPing()
- {
- CloseSocket();
- }
- bool CPing::CreateSocket()
- {
- CloseSocket();
- struct hostent hostinfo,*host;
- char buf[2048];
- struct protoent *protocol;
- unsigned long inaddr = 0l;
- int size = 50 * 1024;
- if ((protocol = getprotobyname("icmp")) == NULL)
- {
- printf("CreateSocket: getprotobyname failed:%d\n",errno);
- return false;
- }
- /*生成使用ICMP的原始套接字,這種套接字只有root才能生成*/
- if ((m_nSocketfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
- {
- printf("CreateSocket: create socket failed:%d\n",errno);
- return false;
- }
- /* 回收root權限,設置當前用戶權限*/
- setuid(getuid());
- /*擴大套接字接收緩沖區到50K這樣做主要為了減小接收緩沖區溢出的
- 的可能性,若無意中ping一個廣播地址或多播地址,將會引來大量應答*/
- int nRet = setsockopt(m_nSocketfd, SOL_SOCKET, SO_RCVBUF, &size, sizeof (size));
- if(nRet != 0)
- {
- printf("CreateSocket: set socket receive buf failed:%d\n",errno);
- return false;
- }
- bzero(&m_dest_addr, sizeof (m_dest_addr));
- m_dest_addr.sin_family = AF_INET;
- /*判斷是主機名還是ip地址*/
- if ((inaddr = inet_addr(m_strIp.c_str())) == INADDR_NONE)
- {
- int nret;
- gethostbyname_r(m_strIp.c_str(), &hostinfo, buf, sizeof(buf), &host, &nret);
- if (nret != 0) /*是主機名*/
- {
- printf("CreateSocket: gethostbyname error %s failed:%d\n",m_strIp.c_str(),errno);
- return false;
- }
- memcpy((char *) &m_dest_addr.sin_addr, host->h_addr, host->h_length);
- }
- else /*是ip地址*/
- memcpy((char *) &m_dest_addr.sin_addr, (char *) &inaddr, sizeof(inaddr));
- m_Ip = inet_ntoa(m_dest_addr.sin_addr);
- return true;
- }
- bool CPing::CloseSocket()
- {
- if(m_nSocketfd !=0)
- close(m_nSocketfd);
- m_nSocketfd = 0;
- return true;
- }
- bool CPing::ping(int times)
- {
- int i = 0;
- while(i < times)
- {
- bool bRet = CreateSocket();
- if(!bRet)
- {
- printf("ping: create socket falied!\n");
- return false;
- }
- /*獲取main的進程id,用於設置ICMP的標誌符*/
- int nh = gettid();
- nh = nh<<8;
- time_t t;
- time(&t);
- int nt = t;
- nh = nh&0xff00;
- nt = nt&0xff;
- m_Pid = nt|nh;
- printf("PING %s(%s): %d bytes data in ICMP packets.\n", m_strIp.c_str(),
- m_Ip.c_str(), SEND_DATA_LEN);
- i++;
- m_nSend = 0;
- m_nRecv = 0;
- send_packet(); /*發送所有ICMP報文*/
- recv_packet(); /*接收所有ICMP報文*/
- //statistics(SIGALRM); /*進行統計*/
- if(m_nRecv > 0)
- break;
- }
- m_bTimeOut = false;
- if(m_nRecv > 0)
- {
- m_nTimeOut = m_nSend - m_nRecv;
- m_dAvgTime = m_dTotalResponseTimes/m_nRecv;
- }
- else
- {
- m_nTimeOut = m_nSend;
- m_dAvgTime = -1;
- return false;
- }
- return true;
- }
- unsigned short CPing::cal_chksum(unsigned short *addr, int len)
- {
- int nleft=len;
- int sum=0;
- unsigned short *w=addr;
- unsigned short answer=0;
- while(nleft > 1)
- {
- sum += *w++;
- nleft -= 2;
- }
- if( nleft == 1)
- {
- *(unsigned char *)(&answer) = *(unsigned char *)w;
- sum += answer;
- }
- sum = (sum >> 16) + (sum & 0xffff);
- sum += (sum >> 16);
- answer = ~sum;
- return answer;
- }
- void CPing::tv_sub(struct timeval *out,struct timeval *in)
- {
- if( (out->tv_usec-=in->tv_usec)<0)
- {
- --out->tv_sec;
- out->tv_usec+=1000000;
- }
- out->tv_sec-=in->tv_sec;
- }
- /*設置ICMP報頭*/
- int CPing::pack(int pack_no)
- {
- int packsize;
- struct icmp *icmp;
- struct timeval *tval;
- icmp = (struct icmp*) m_sendpacket;
- icmp->icmp_type = ICMP_ECHO;
- icmp->icmp_code = 0;
- icmp->icmp_cksum = 0;
- icmp->icmp_seq = pack_no;
- icmp->icmp_id = m_Pid;
- packsize = 8 + SEND_DATA_LEN;
- tval = (struct timeval *) icmp->icmp_data;
- gettimeofday(tval, NULL); /*記錄發送時間*/
- icmp->icmp_cksum = cal_chksum((unsigned short *) icmp, packsize); /*校驗算法*/
- return packsize;
- }
- /*發送三個ICMP報文*/
- void CPing::send_packet()
- {
- int packetsize;
- while (m_nSend < m_nMaxTestpkg)
- {
- m_nSend++;
- packetsize = pack(m_nSend); /*設置ICMP報頭*/
- if (sendto(m_nSocketfd, m_sendpacket, packetsize, 0,
- (struct sockaddr *) &m_dest_addr, sizeof (m_dest_addr)) < 0)
- {
- printf("send_packet: send error :%d\n",errno);
- continue;
- }
- usleep(2); /*每隔一秒發送一個ICMP報文*/
- }
- }
- /*接收所有ICMP報文*/
- void CPing::recv_packet()
- {
- int n,fromlen;
- while(m_nRecv < m_nSend)
- {
- struct timeval timeo;
- fd_set readfds;
- FD_ZERO(&readfds);
- FD_SET(m_nSocketfd,&readfds);
- int maxfds = m_nSocketfd +1;
- timeo.tv_sec = m_nMaxTimeWait;
- timeo.tv_usec = 0;
- n = select(maxfds,&readfds,NULL,NULL,&timeo);
- if(n == 0)
- {
- printf("recv_packet: select time out :%d",errno);
- return ;
- }
- else if(n < 0)
- {
- printf("recv_packet: select error :%d",errno);
- if(errno == EINTR)
- {
- printf("recv_packet: select error :%d",errno);
- continue;
- }
- else
- {
- printf("recv_packet: select error :%d",errno);
- return ;
- }
- }
- if ((n = recvfrom(m_nSocketfd, m_recvpacket, sizeof (m_recvpacket), 0,
- (struct sockaddr *) &m_from, (socklen_t *)&fromlen)) <= 0)
- {
- printf("recv_packet: recv error :%d",errno);
- return;
- }
- gettimeofday(&m_tvrecv, NULL); /*記錄接收時間*/
- if (unpack(m_recvpacket, n) == -1)
- {
- continue;
- }
- m_nRecv++;
- }
- //return ;
- }
- /*剝去ICMP報頭*/
- int CPing::unpack(char *buf,int len)
- {
- int i,iphdrlen;
- struct ip *ip;
- struct icmp *icmp;
- struct timeval *tvsend;
- double rtt;
- ip = (struct ip *) buf;
- iphdrlen = ip->ip_hl << 2; /*求ip報頭長度,即ip報頭的長度標誌乘4*/
- icmp = (struct icmp *) (buf + iphdrlen); /*越過ip報頭,指向ICMP報頭*/
- len -= iphdrlen; /*ICMP報頭及ICMP數據報的總長度*/
- if (len < 8) /*小於ICMP報頭長度則不合理*/
- {
- printf( "ICMP packets\‘s length is less than 8");
- return -1;
- }
- /*確保所接收的是我所發的的ICMP的回應*/
- if ((icmp->icmp_type == ICMP_ECHOREPLY) && (icmp->icmp_id == m_Pid)&&(m_Ip == inet_ntoa(m_from.sin_addr)))
- {
- tvsend = (struct timeval *) icmp->icmp_data;
- tv_sub(&m_tvrecv, tvsend); /*接收和發送的時間差*/
- rtt = m_tvrecv.tv_sec * 1000 + m_tvrecv.tv_usec / 1000; /*以毫秒為單位計算rtt*/
- m_dTotalResponseTimes += rtt;
- if(m_dFasterResponseTime == -1)
- {
- m_dFasterResponseTime = rtt;
- }
- else if(m_dFasterResponseTime > rtt)
- {
- m_dFasterResponseTime = rtt;
- }
- if(m_dLowerResponseTime == -1)
- {
- m_dLowerResponseTime = rtt;
- }
- else if(m_dLowerResponseTime < rtt)
- {
- m_dLowerResponseTime = rtt;
- }
- /*顯示相關信息*/
- printf("%d\tbyte from %s\t: icmp_seq=%u\tttl=%d\trtt=%.3f\tms\n",
- len,
- inet_ntoa(m_from.sin_addr),
- icmp->icmp_seq,
- ip->ip_ttl,
- rtt);
- }
- else
- {
- printf("throw away the old package %d\tbyte from %s\t: icmp_seq=%u\tttl=%d\trtt=%.3f\tms",
- len,
- inet_ntoa(m_from.sin_addr),
- icmp->icmp_seq,
- ip->ip_ttl,
- rtt);
- return -1;
- }
- }
c++ 實現ping