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


// IPGram.cpp: implementation of the IPGram class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "snifferpro.h" 
#include "IPGram.h" 
 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
 
IPGram::IPGram() 
{ 
 
} 
 
IPGram::IPGram(const unsigned char *buf,int buflen) 
{ 
	//unsigned char *buf; 
	unsigned char *pos; 
	//int buflen; 
 
	//buf=new unsigned char[buffer_len]; 
	//buflen=buffer_len; 
	//memcpy(buf,buffer,buflen); 
	 
	pos=(unsigned char *)buf; 
	version=*pos/16; 
	IHL=*pos%16; 
 
	pos++; 
	servicetype=*pos; 
	precedence=(*pos & 16)*4+(*pos & 8)*2+(*pos & 4); 
	if(*pos & 16) 
		delay=true; 
	else  
		delay=false; 
	if(*pos & 8) 
		throughtput=true; 
	else 
		throughtput=false; 
	if(*pos & 4) 
		reliability=true; 
	else 
		reliability=false; 
 
	pos++; 
	totallen=(*pos)*256+(*(pos+1)); 
 
	pos+=2; 
	identification=(*pos)*256+(*(pos+1)); 
 
	pos+=2; 
	if(*pos & 64) 
		DF=true; 
	else 
		DF=false; 
	if(*pos & 32) 
		MF=true; 
	else 
		MF=false; 
	fragoffset=(*pos%32)*256+(*(pos+1)); 
 
	pos+=2; 
	TTL=*pos; 
 
	pos++; 
	protocol=*pos; 
 
	pos++; 
	checksum=(*pos)*256+(*(pos+1)); 
 
	pos+=2; 
	srcaddr=(*pos)*0x1000000+(*(pos+1))*0x10000+(*(pos+2))*0x100+(*(pos+3)); 
 
	pos+=4; 
	destaddr=(*pos)*0x1000000+(*(pos+1))*0x10000+(*(pos+2))*0x100+(*(pos+3)); 
 
 
	if(IHL>5){ 
		pos+=4; 
		optlen=(IHL-5)*4; 
		options=new unsigned char[optlen]; 
		memcpy(options,buf+20,optlen); 
	} 
	else{ 
		optlen=0; 
		options=NULL; 
	} 
	 
	pos=(unsigned char *)(buf+IHL*4); 
	datalen=totallen-IHL*4; 
	if(datalen>0){ 
		data=new unsigned char[datalen]; 
		memcpy(data,buf+IHL*4,datalen); 
	} 
	else 
		data=NULL; 
	//delete[] buf; 
} 
 
IPGram::~IPGram() 
{ 
	if(data!=NULL) 
		delete[] data; 
	if(options!=NULL) 
		delete[] options; 
} 
 
CString IPGram::GetService() 
{ 
	CString str; 
 
	switch(this->protocol){ 
	case 6: 
		str.Format("TCP");break; 
	case 17: 
		str.Format("UDP");break; 
	case 1: 
		str.Format("ICMP");break; 
	case 89: 
		str.Format("OSPF");break; 
	default: 
		str.Format("%d",protocol); 
	} 
 
	return str; 
} 
 
 
void IPGram::GetSrcAddr(char *str) 
{	 
	int temp=srcaddr; 
	unsigned char i0,i1,i2,i3; 
 
	i0=(temp & 0xff000000)/0x1000000; 
	i1=(temp & 0x00ff0000)/0x10000; 
	i2=(temp & 0x0000ff00)/0x100; 
	i3=temp & 0x000000ff; 
	sprintf(str,"%003u.%003u.%003u.%003u",i0,i1,i2,i3); 
} 
 
CString IPGram::GetSrcAddr() 
{ 
	CString str; 
	int temp=srcaddr; 
	unsigned char i0,i1,i2,i3; 
 
	i0=(temp & 0xff000000)/0x1000000; 
	i1=(temp & 0x00ff0000)/0x10000; 
	i2=(temp & 0x0000ff00)/0x100; 
	i3=temp & 0x000000ff; 
	str.Format("%003u.%003u.%003u.%003u",i0,i1,i2,i3); 
 
	return str; 
} 
 
void IPGram::GetDestAddr(char *str) 
{ 
	int temp=destaddr; 
	unsigned char i0,i1,i2,i3; 
 
	i0=(temp & 0xff000000)/0x1000000; 
	i1=(temp & 0x00ff0000)/0x10000; 
	i2=(temp & 0x0000ff00)/0x100; 
	i3=temp & 0x000000ff; 
	sprintf(str,"%003u.%003u.%003u.%003u",i0,i1,i2,i3); 
} 
 
CString IPGram::GetDestAddr() 
{ 
	CString str; 
	int temp=destaddr; 
	unsigned char i0,i1,i2,i3; 
 
	i0=(temp & 0xff000000)/0x1000000; 
	i1=(temp & 0x00ff0000)/0x10000; 
	i2=(temp & 0x0000ff00)/0x100; 
	i3=temp & 0x000000ff; 
	str.Format("%003u.%003u.%003u.%003u",i0,i1,i2,i3); 
 
	return str; 
} 
 
CString IPGram::GetID() 
{ 
	CString str; 
	str.Format("%u",this->identification); 
	return str; 
}