溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

arm linux c/c++實(shí)現(xiàn)ping網(wǎng)絡(luò)測試

發(fā)布時(shí)間:2020-09-07 10:55:47 來源:網(wǎng)絡(luò) 閱讀:3736 作者:小溢 欄目:編程語言

附源碼:ping.cpp ping.h    是類的實(shí)現(xiàn)。

實(shí)例代碼是從項(xiàng)目的應(yīng)用中剝離出來的:



ping.cpp:

#include "ping.h"

Ping::Ping() {
	m_maxPacketSize = 4;
	m_datalen = 56;
	m_nsend = 0;
	m_nreceived = 0;
	m_icmp_seq = 0;
}

/*校驗(yàn)和算法*/
unsigned short Ping::getChksum(unsigned short *addr,int len)
{   
	int nleft=len;
	int sum=0;
	unsigned short *w=addr;
	unsigned short answer=0;

	/*把ICMP報(bào)頭二進(jìn)制數(shù)據(jù)以2字節(jié)為單位累加起來*/
	while(nleft>1)
	{   
		sum+=*w++;
		nleft-=2;
	}
	/*若ICMP報(bào)頭為奇數(shù)個(gè)字節(jié),會(huì)剩下最后一字節(jié)。把最后一個(gè)字節(jié)視為一個(gè)2字節(jié)數(shù)據(jù)的高字節(jié),這個(gè)2字節(jié)數(shù)據(jù)的低字節(jié)為0,繼續(xù)累加*/
	if( nleft==1)
	{   
		*(unsigned char *)(&answer)=*(unsigned char *)w;
		sum+=answer;
	}
	sum=(sum>>16)+(sum&0xffff);
	sum+=(sum>>16);
	answer=~sum;
	return answer;
}

/*設(shè)置ICMP報(bào)頭*/
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);    /*記錄發(fā)送時(shí)間*/
	picmp->icmp_cksum=getChksum((unsigned short *)icmp,packsize); /*校驗(yàn)算法*/
	return packsize;
}

/*剝?nèi)CMP報(bào)頭*/
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報(bào)頭長度,即ip報(bào)頭的長度標(biāo)志乘4*/
	icmp = (struct icmp *)(buf + iphdrlen);  /*越過ip報(bào)頭,指向ICMP報(bào)頭*/
	len -= iphdrlen;            /*ICMP報(bào)頭及ICMP數(shù)據(jù)報(bào)的總長度*/
	if(len < 8)                /*小于ICMP報(bào)頭長度則不合理*/
	{   
		printf("ICMP packets\'s length is less than 8\n");
		return false;
	}
	/*確保所接收的是我所發(fā)的的ICMP的回應(yīng)*/
	if( (icmp->icmp_type==ICMP_ECHOREPLY) && (icmp->icmp_id == m_pid) )
	{   
		
		tvsend=(struct timeval *)icmp->icmp_data;
		gettimeofday(&tvrecv,NULL);  /*記錄接收時(shí)間*/
		tvresult = tvSub(tvrecv, *tvsend);  /*接收和發(fā)送的時(shí)間差*/
		rtt=tvresult.tv_sec*1000 + tvresult.tv_usec/1000;  /*以毫秒為單位計(jì)算rtt*/
		icmpEchoReply->rtt = rtt;
		icmpEchoReply->icmpSeq = icmp->icmp_seq;
		icmpEchoReply->ipTtl = ip->ip_ttl;
		icmpEchoReply->icmpLen = len;
		return true;
	}
	else {
		return false;
	}
}

/*兩個(gè)timeval結(jié)構(gòu)相減*/
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;
}

/*發(fā)送三個(gè)ICMP報(bào)文*/
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); /*設(shè)置ICMP報(bào)頭*/

		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報(bào)文*/
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;
	/*判斷是主機(jī)名還是ip地址*/
	if( inaddr=inet_addr(hostOrIp)==INADDR_NONE)
	{    
		if((host=gethostbyname(hostOrIp))==NULL) /*是主機(jī)名*/
		{   
			//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");
    }
	/*擴(kuò)大套接字接收緩沖區(qū)到50K這樣做主要為了減小接收緩沖區(qū)溢出的
	  的可能性,若無意中ping一個(gè)廣播地址或多播地址,將會(huì)引來大量應(yīng)答*/
	setsockopt(m_sockfd,SOL_SOCKET,SO_RCVBUF,&size,sizeof(size) );
	
	/*獲取main的進(jìn)程id,用于設(shè)置ICMP的標(biāo)志符*/
	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();  /*發(fā)送所有ICMP報(bào)文*/
	recvPacket(pingResult);  /*接收所有ICMP報(bào)文*/
	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

實(shí)例代碼:

        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
            }
        }


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI