您好,登錄后才能下訂單哦!
附源碼:ping.cpp ping.h 是類的實現。
實例代碼是從項目的應用中剝離出來的:
ping.cpp:
#include "ping.h" Ping::Ping() { m_maxPacketSize = 4; m_datalen = 56; m_nsend = 0; m_nreceived = 0; m_icmp_seq = 0; } /*校驗和算法*/ unsigned short Ping::getChksum(unsigned short *addr,int len) { int nleft=len; int sum=0; unsigned short *w=addr; unsigned short answer=0; /*把ICMP報頭二進制數據以2字節為單位累加起來*/ while(nleft>1) { sum+=*w++; nleft-=2; } /*若ICMP報頭為奇數個字節,會剩下最后一字節。把最后一個字節視為一個2字節數據的高字節,這個2字節數據的低字節為0,繼續累加*/ if( nleft==1) { *(unsigned char *)(&answer)=*(unsigned char *)w; sum+=answer; } sum=(sum>>16)+(sum&0xffff); sum+=(sum>>16); answer=~sum; return answer; } /*設置ICMP報頭*/ int Ping::packIcmp(int pack_no, struct icmp* icmp) { int i,packsize; struct icmp *picmp; struct timeval *tval; picmp = icmp; picmp->icmp_type=ICMP_ECHO; picmp->icmp_code=0; picmp->icmp_cksum=0; picmp->icmp_seq=pack_no; picmp->icmp_id= m_pid; packsize= 8 + m_datalen; tval= (struct timeval *)icmp->icmp_data; gettimeofday(tval,NULL); /*記錄發送時間*/ picmp->icmp_cksum=getChksum((unsigned short *)icmp,packsize); /*校驗算法*/ return packsize; } /*剝去ICMP報頭*/ bool Ping::unpackIcmp(char *buf,int len, struct IcmpEchoReply *icmpEchoReply) { int i,iphdrlen; struct ip *ip; struct icmp *icmp; struct timeval *tvsend, tvrecv, tvresult; 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 false; } /*確保所接收的是我所發的的ICMP的回應*/ if( (icmp->icmp_type==ICMP_ECHOREPLY) && (icmp->icmp_id == m_pid) ) { tvsend=(struct timeval *)icmp->icmp_data; gettimeofday(&tvrecv,NULL); /*記錄接收時間*/ tvresult = tvSub(tvrecv, *tvsend); /*接收和發送的時間差*/ rtt=tvresult.tv_sec*1000 + tvresult.tv_usec/1000; /*以毫秒為單位計算rtt*/ icmpEchoReply->rtt = rtt; icmpEchoReply->icmpSeq = icmp->icmp_seq; icmpEchoReply->ipTtl = ip->ip_ttl; icmpEchoReply->icmpLen = len; return true; } else { return false; } } /*兩個timeval結構相減*/ struct timeval Ping::tvSub(struct timeval timeval1,struct timeval timeval2) { struct timeval result; result = timeval1; if (result.tv_usec < timeval2.tv_usec < 0) { --result.tv_sec; result.tv_usec += 1000000; } result.tv_sec -= timeval2.tv_sec; return result; } /*發送三個ICMP報文*/ bool Ping::sendPacket() { size_t packetsize; while( m_nsend < m_maxPacketSize) { m_nsend++; m_icmp_seq++; packetsize = packIcmp(m_icmp_seq, (struct icmp*)m_sendpacket); /*設置ICMP報頭*/ if(sendto(m_sockfd,m_sendpacket, packetsize, 0, (struct sockaddr *) &m_dest_addr, sizeof(m_dest_addr)) < 0 ) { perror("sendto error"); continue; } } return true; } /*接收所有ICMP報文*/ bool Ping::recvPacket(PingResult &pingResult) { int len; extern int errno; struct IcmpEchoReply icmpEchoReply; int maxfds = m_sockfd + 1; int nfd = 0; fd_set rset; FD_ZERO(&rset); socklen_t fromlen = sizeof(m_from_addr); struct timeval timeout; timeout.tv_sec = 4; timeout.tv_usec = 0; for (int recvCount = 0; recvCount < m_maxPacketSize; recvCount++) { //printf("begin recv\n"); FD_SET(m_sockfd, &rset); if ((nfd = select(maxfds, &rset, NULL, NULL, &timeout)) == -1) { perror("select error"); continue; } if (nfd == 0) { //recv time out //printf("request timeout\n"); icmpEchoReply.isReply = false; pingResult.icmpEchoReplys.push_back(icmpEchoReply); continue; } if (FD_ISSET(m_sockfd, &rset)) { if( (len = recvfrom(m_sockfd, m_recvpacket,sizeof(m_recvpacket),0, (struct sockaddr *)&m_from_addr,&fromlen)) <0) { if(errno==EINTR) continue; perror("recvfrom error"); continue; } icmpEchoReply.fromAddr = inet_ntoa(m_from_addr.sin_addr) ; if (icmpEchoReply.fromAddr != pingResult.ip) { //printf("invalid address, discard\n"); //retry again recvCount--; continue; } } if(unpackIcmp(m_recvpacket, len, &icmpEchoReply)==-1) { //retry again recvCount--; continue; } icmpEchoReply.isReply = true; pingResult.icmpEchoReplys.push_back(icmpEchoReply); m_nreceived++; } return true; } bool Ping::getsockaddr(const char * hostOrIp, struct sockaddr_in* sockaddr) { struct hostent *host; struct sockaddr_in dest_addr; unsigned long inaddr=0l; bzero(&dest_addr,sizeof(dest_addr)); dest_addr.sin_family=AF_INET; /*判斷是主機名還是ip地址*/ if( inaddr=inet_addr(hostOrIp)==INADDR_NONE) { if((host=gethostbyname(hostOrIp))==NULL) /*是主機名*/ { //printf("gethostbyname error:%s\n", hostOrIp); return false; } memcpy( (char *)&dest_addr.sin_addr,host->h_addr,host->h_length); } /*是ip地址*/ else if (!inet_aton(hostOrIp, &dest_addr.sin_addr)){ //memcpy( (char *)&dest_addr,(char *)&inaddr,host->h_length); //fprintf(stderr, "unknow host:%s\n", hostOrIp); return false; } *sockaddr = dest_addr; return true; } bool Ping::ping(std::string host, PingResult& pingResult) { return ping(host, 1, pingResult); } bool Ping::ping(std::string host, int count, PingResult& pingResult) { struct protoent *protocol; int size = 50 * 1024; IcmpEchoReply icmpEchoReply; bool ret; m_nsend = 0; m_nreceived = 0; pingResult.icmpEchoReplys.clear(); m_maxPacketSize = count; m_pid = getpid(); pingResult.dataLen = m_datalen; // if( (protocol = getprotobyname("icmp") )==NULL) // { // perror("getprotobyname"); // return false; // } /*生成使用ICMP的原始套接字,這種套接字只有root才能生成*/ // if( (m_sockfd=socket(AF_INET,SOCK_RAW,protocol->p_proto) )<0) // { // //perror("socket error"); // extern int errno; // pingResult.error = strerror(errno); // return false; // } if ((m_sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP))<0) { perror("socket error"); } /*擴大套接字接收緩沖區到50K這樣做主要為了減小接收緩沖區溢出的 的可能性,若無意中ping一個廣播地址或多播地址,將會引來大量應答*/ setsockopt(m_sockfd,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size) ); /*獲取main的進程id,用于設置ICMP的標志符*/ if (!getsockaddr(host.c_str(), &m_dest_addr)) { pingResult.error = "unknow host " + host; return false; } pingResult.ip = inet_ntoa(m_dest_addr.sin_addr); sendPacket(); /*發送所有ICMP報文*/ recvPacket(pingResult); /*接收所有ICMP報文*/ pingResult.nsend = m_nsend; pingResult.nreceived = m_nreceived; close(m_sockfd); return true; }
ping.h
#ifndef PING_H #define PING_H #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signal.h> #include <sys/select.h> #include <arpa/inet.h> #include <sys/types.h> #include <sys/time.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 <string> #include <vector> #define PACKET_SIZE 4096 #define MAX_WAIT_TIME 5 #define MAX_NO_PACKETS 3 struct IcmpEchoReply { int icmpSeq; int icmpLen; int ipTtl; double rtt; std::string fromAddr; bool isReply; }; struct PingResult { int dataLen; int nsend; int nreceived; std::string ip; std::string error; std::vector<IcmpEchoReply> icmpEchoReplys; }; class Ping { public: Ping(); bool ping(std::string host, PingResult &pingResult); bool ping(std::string host, int count, PingResult& pingResult); private: unsigned short getChksum(unsigned short *addr,int len); int packIcmp(int pack_no, struct icmp* icmp); bool unpackIcmp(char *buf,int len, struct IcmpEchoReply *icmpEchoReply); struct timeval tvSub(struct timeval timeval1,struct timeval timval2); bool getsockaddr(const char * hostOrIp, sockaddr_in* sockaddr); bool sendPacket(); bool recvPacket(PingResult &pingResult); private: char m_sendpacket[PACKET_SIZE]; char m_recvpacket[PACKET_SIZE]; int m_maxPacketSize; int m_sockfd; int m_datalen; int m_nsend; int m_nreceived; int m_icmp_seq; struct sockaddr_in m_dest_addr; struct sockaddr_in m_from_addr; pid_t m_pid; }; #endif
實例代碼:
if ( eth == false ) { char * hostOrIp = "192.168.1.20"; int nsend = 0, nreceived = 0; bool ret; PingResult pingResult; Ping ping = Ping(); for (int count = 1; count <= 4; count++) { ret = ping.ping(hostOrIp, 1, pingResult); if (count == 1) { printf("PING %s(%s): %d bytes data in ICMP packets.\n", hostOrIp, pingResult.ip.c_str(), pingResult.dataLen); } if (!ret) { printf("%s\n", pingResult.error.c_str()); break; } showPingResult(pingResult); nsend += pingResult.nsend; nreceived += pingResult.nreceived; } if (ret) { printf("%d packets transmitted, %d received , %%%d lost\n", nsend, nreceived, (nsend - nreceived) / nsend * 100); eth = true; system("echo 0 > /sys/class/gpio/gpio230/value"); // 燈6 } else { printf("-----------------eth0 failed--------------\n"); eth = false; system("echo 1 > /sys/class/gpio/gpio230/value"); // 燈6 } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。