//头文件
- #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文件
- #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;
- }
-
- if ((m_nSocketfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
- {
- perror("socket error");
- exit(1);
- }
-
- setuid(getuid());
-
- 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;
-
- 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
- memcpy((char *) &m_dest_addr, (char *) &inaddr, host->h_length);
-
- 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);
- recv_packet();
- }
- 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;
- }
- 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;
- }
- 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);
- if (sendto(m_nSocketfd, m_sendpacket, packetsize, 0,
- (struct sockaddr *) &m_dest_addr, sizeof (m_dest_addr)) < 0)
- {
- perror("sendto error");
- continue;
- }
- sleep(1);
- }
- }
- void CPing::recv_packet()
- {
- int n,fromlen;
-
- 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++;
- }
- }
- 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;
- icmp = (struct icmp *) (buf + iphdrlen);
- len -= iphdrlen;
- if (len < 8)
- {
- printf("ICMP packets/‘s length is less than 8/n");
- return -1;
- }
-
- 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;
- 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;
- }
调用
- #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
-
- #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);
-
- m_nTimeOut = m_nSend - m_nRecv;
- m_dAvgTime = m_dTotalResponseTimes/m_nRecv;
-
- }
-
- 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;
- }
-
-
- if ((m_nSocketfd = socket(AF_INET, SOCK_RAW, protocol->p_proto)) < 0)
- {
- printf("CreateSocket: create socket failed:%d\n",errno);
-
-
- return false;
- }
-
-
- setuid(getuid());
-
-
- 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;
-
-
- 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
- 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;
- }
-
-
- 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();
- recv_packet();
-
- 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;
- }
-
- 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;
- }
-
- void CPing::send_packet()
- {
- int packetsize;
- while (m_nSend < m_nMaxTestpkg)
- {
- m_nSend++;
- packetsize = pack(m_nSend);
- 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);
- }
- }
-
- 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++;
- }
-
- }
- 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;
- icmp = (struct icmp *) (buf + iphdrlen);
- len -= iphdrlen;
- if (len < 8)
- {
- printf( "ICMP packets\‘s length is less than 8");
-
- return -1;
- }
-
- 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;
- 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
原文:http://www.cnblogs.com/daochong/p/6873543.html