Linux C++实现ping指令

//#include ping.h#include iostream#include stdio.h#include string#include string.h#include netinet/ip_icmp.h#include netdb.h#include sys/socket.h#include sys/types.h#include...

//#include "ping.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <netinet/ip_icmp.h>
#include <netdb.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>

#define PACKET_SIZE 4096
#define SEND_DATA_LEN 56
#define ERROR    -1
#define SUCCESS   1
#define MAX_WAIT_TIME 3
#define MAX_NO_PACKETS 4
#define NULL 0

using namespace std;

class Cping
{
	public:
		Cping(const char * ip, int timeout);
		Cping(const Cping& org);
		virtual ~Cping();
	
	private:
		std::string m_strIp;
		std::string m_copy_Ip;
		
		int m_nSend;
		int m_nRecv;
		struct sockaddr_in m_dest_addr;
		struct sockaddr_in m_from_addr;
		
		char m_sendpacket[PACKET_SIZE];
		char m_recvpacket[PACKET_SIZE];
		
		struct timeval m_tvrecv;
		struct timeval m_begin_tvrecv;
		struct timeval m_end_tvrecv;
		double m_dTotalResponseTimes;
		int m_nSocketfd;
		
		int m_nMaxTimeWait;
   public:
		bool ping(int times);
		bool CreateSocket();
		bool CloseSocket();
		
		void send_packet(void);
		void recv_packet(void);
		
		int pack(int pack_no);
		int unpack(char *buf, int len);
		
		void tv_sub(struct timeval *out, struct timeval *in);
		void statistics(int sig);
		
		static unsigned short cal_chksum(unsigned short *addr, int len);
};

Cping::Cping(const char *ip, int timeout)
{
	m_strIp = ip;
	m_copy_Ip = ip;
	
	m_nSend = 0;
	m_nRecv = 0;
	
	m_dTotalResponseTimes = 0;
	
	if(timeout > MAX_WAIT_TIME)
	{
		m_nMaxTimeWait = MAX_WAIT_TIME;
	}
	else
	{
		m_nMaxTimeWait = timeout;
	}
}

Cping::~Cping()
{
	if(!CloseSocket())
	{
		cout << "CloseSocket failed!" << endl;
	}
}

bool Cping::ping(int times)
{
	if(!CreateSocket())
	{
		printf("CreateSocket failed!!!\n");
		return false;
	}
	printf("PING %s(%s): %d bytes data in ICMP packets.\n", m_strIp.c_str(),
					m_copy_Ip.c_str(), SEND_DATA_LEN);
	while(times--)
	{
		send_packet();
		recv_packet();
		sleep(1);
	}
	statistics(SIGINT);
	return true;
}

bool Cping::CreateSocket()
{
	char buf[2048];
	int errnop = 0;
	unsigned long inaddr;
	struct hostent hostinfo, *dest_phost;
	struct protoent *protocol = NULL;
	
	if((protocol = getprotobyname("icmp")) == NULL)
	{
		perror("getprotobyname()");
		printf("CreateSocket : getprotobyname failed:%d\n",errnop);
		return false;
	}
	if(-1 == (m_nSocketfd = socket(AF_INET,SOCK_RAW,protocol->p_proto)))
	{
		perror("socket");
		printf("CreateSocket: create socket failed:%d\n", m_nSocketfd);
		return false;
	}
	setuid(getuid());
	m_dest_addr.sin_family = AF_INET;
	
	bzero(&(m_dest_addr.sin_zero),8);
	
	if((inaddr = inet_addr(m_strIp.c_str())) == INADDR_NONE)
	{
		//if(getprotobyname_r(m_strIp.c_str(), &hostinfo, buf, sizeof(buf), &dest_phost, &errnop))
		//{
		//	printf("CreateSocket: getprotobyname error %s failed:%d\n", m_strIp.c_str(),errnop);
		//	return false;
		/

本文标题为:Linux C++实现ping指令

基础教程推荐