www.pudn.com > SnifferPro.rar > ICMPGram.cpp


// ICMPGram.cpp: implementation of the ICMPGram class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "snifferpro.h" 
#include "ICMPGram.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
ICMPGram::ICMPGram() 
{ 
 
} 
 
ICMPGram::ICMPGram(const unsigned char *buf,int buflen) 
{ 
	unsigned char *pos; 
	//int buflen; 
 
	//buf=new unsigned char[bufferlen]; 
	//memcpy((void *)buf,buffer,bufferlen); 
	//buflen=bufferlen; 
 
	pos=(unsigned char *)buf; 
	type=*pos; 
 
	pos++; 
	code=*pos; 
 
	pos++; 
	checksum=(*pos)*0x100+(*pos); 
 
	datalen=buflen-4; 
	if(datalen>0){ 
		data=new unsigned char[datalen]; 
		memcpy((void *)data,buf+4,datalen); 
 
		switch(type){ 
		case 4://Source Quench 
			break; 
		case 5://Redirect 
			pos=data; 
			gatewayaddr=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			break; 
		case 8://Echo Request 
			pos=data;  
			identification=(*pos)*0x100+(*(++pos)); 
			pos++; 
			sequence=(*pos)*0x100+(*(++pos)); 
			break; 
		case 0://Echo Reply 
			pos=data;  
			identification=(*pos)*0x100+(*(++pos)); 
			pos++; 
			sequence=(*pos)*0x100+(*(++pos)); 
			break; 
		case 13://Time Stamp Request 
			pos=data;  
			identification=(*pos)*0x100+(*(++pos)); 
			pos++; 
			sequence=(*pos)*0x100+(*(++pos)); 
			pos++; 
			inittime=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			pos++; 
			receivetime=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			pos++; 
			sendtime=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			break; 
		case 14://Tiem Stamp Reply 
			pos=data;  
			identification=(*pos)*0x100+(*(++pos)); 
			pos++; 
			sequence=(*pos)*0x100+(*(++pos)); 
			pos++; 
			inittime=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			pos++; 
			receivetime=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			pos++; 
			sendtime=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			break; 
		case 17://Mase Request 
			pos=data;  
			identification=(*pos)*0x100+(*(++pos)); 
			pos++; 
			sequence=(*pos)*0x100+(*(++pos)); 
			pos++; 
			addrmask=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			break; 
		case 18://Mase Reply 
			pos=data;  
			identification=(*pos)*0x100+(*(++pos)); 
			pos++; 
			sequence=(*pos)*0x100+(*(++pos)); 
			pos++; 
			addrmask=(*pos)*0x1000000+(*(++pos))*0x10000+(*(++pos))*0x100+(*(++pos)); 
			break; 
		default:; 
		} 
	} 
	else{ 
		data=NULL; 
		datalen=0; 
	} 
} 
 
ICMPGram::~ICMPGram() 
{ 
	if(data!=NULL) 
		delete[] data; 
} 
 
void ICMPGram::GetType(CString &str1,CString &str2) 
{ 
	str2.Format(""); 
	switch(this->type){ 
	case 3: 
		str1.Format("Destination Unreachable"); 
		switch(this->code){ 
		case 0: 
			str2.Format("Net Unreachable"); 
			break; 
		case 1: 
			str2.Format("Host Unreachable"); 
			break; 
		case 2: 
			str2.Format("Protocol Unreachable"); 
			break; 
		case 3: 
			str2.Format("Port Unreachable"); 
			break; 
		case 4: 
			str2.Format("Fragmentation Needed and DF Set"); 
			break; 
		case 5: 
			str2.Format("Source Route Failed"); 
			break; 
		default : 
			str2.Format("Unknown"); 
		} 
		break; 
	case 11: 
		str1.Format("Time Exceeded"); 
		switch(this->code){ 
		case 0: 
			str2.Format("Time to Live Exceeded in Transit"); 
			break; 
		case 1: 
			str2.Format("Fragment Reassembly Time Exceeded"); 
			break; 
		default: 
			str2.Format("Unknown"); 
		} 
		break; 
	case 12: 
		str1.Format("Parameter Problem"); 
		if(this->code==0) 
			str2.Format("Pointer Indicates the Error"); 
		else 
			str2.Format("Unknown"); 
		break; 
	case 4: 
		str1.Format("Source Quench"); 
		break; 
	case 5: 
		str1.Format("Redirect"); 
		switch(this->code){ 
		case 0: 
			str2.Format("Redirect Datagrams for the Network"); 
			break; 
		case 1: 
			str2.Format("Redirect Datagrams for the Host"); 
			break; 
		case 2: 
			str2.Format("Redirect Datagrams for the Type of Service and Network"); 
			break; 
		case 3: 
			str2.Format("Redirect Datagrams for the Type of Service and Hos"); 
			break; 
		default: 
			str2.Format("Unknown"); 
		} 
		break; 
	case 8: 
		str1.Format("Echo"); 
		break; 
	case 0: 
		str1.Format("Echo Reply"); 
		break; 
	case 13: 
		str1.Format("Timestamp"); 
		break; 
	case 14: 
		str1.Format("Timestamp Reply"); 
		break; 
	case 15: 
		str1.Format("Information Request"); 
		break; 
	case 16: 
		str1.Format("Information Reply"); 
		break; 
	case 17: 
		str1.Format("Mask Request"); 
		break; 
	case 18: 
		str1.Format("Mask Reply"); 
		break; 
	default: 
		str1.Format("Unknown"); 
	} 
} 
 
CString ICMPGram::GetGateWay() 
{ 
	CString str; 
	unsigned char *pos; 
 
	pos=(unsigned char *)&gatewayaddr; 
	if(this->type==5) 
		str.Format("%d.%d.%d.%d",*(pos+3),*(pos+2),*(pos+1),*pos); 
	else 
		str.Format(""); 
	return str; 
} 
 
CString ICMPGram::GetAddrMask() 
{ 
	CString str; 
	unsigned char *pos; 
 
	pos=(unsigned char *)&addrmask; 
	if(this->type==17 || this->type==18) 
		str.Format("%d.%d.%d.%d",*(pos+3),*(pos+2),*(pos+1),*pos); 
	else 
		str.Format(""); 
	return str; 
}