⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 packet.cpp

📁 使用VC++开发的很棒的上网拨号工具的程序的源码,可以选择PING,UDP,TCP/IP等等方式,功能比较全面.Using Visual C + + development of the great
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Packet.cpp: implementation of the CPacket class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "demo.h"
#include "Packet.h"


#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CPacket::CPacket()
{

	TxLen=0;
	reject=FALSE;
	map=FALSE;
	recPacketS=0;
	IPCP_s=0;
	bID=1;
	bState=INITIAL;
	compress=0;
	PF_compress=0;
	for(int i=0;i<8;i++)
	{
		option[i].reject=TRUE;
		memset(option[i].bData,0x0,64);
	}
	memset(bPacketTx,0x0,256);
	memset(bIpaddress,0x00,4);
	iHeadcheckpos=0;

	iLenpos=0;
}



CPacket::~CPacket()
{

}


bool CPacket::checkpacket(BYTE* pPacketstr)
{
	BYTE bCode;
	CCRC m_crc;
	memset(bPacketTx,0x00,256);
	memset(bPacketTx1,0x00,256);
	TxLen=0;

	if ( (pPacketstr[3-compress]&1) && (pPacketstr[3-compress]!=0xff) )
	{
		wType=pPacketstr[3-compress];

	}
	else wType=pPacketstr[3-compress]*256+pPacketstr[4-compress];
	
	switch(wType){
	case LCP:
		{
			bCode=pPacketstr[5-compress];
			bID=pPacketstr[6-compress];
			switch(bCode){
			case REQ:
				/*if (bState==LCPOPEN) 
				{
					bState=INITIAL;
					reject=FALSE;
					testoption(wType,bCode,pPacketstr);
					makepacket(wType,bCode,pPacketstr);
				}
				else
				*/
				{
					testoption(wType,bCode,pPacketstr);
					makepacket(wType,bCode,pPacketstr);
				}
				break;//end REQ
			case ACK:
				//if (pPacketstr[6]!=bID) break;

				testoption(wType,bCode,pPacketstr);
				memset(bPacketTx,0x00,256);
				bState=PAPOPEN;
				makepacket(PAP,REQ,bPacketTx);
				break;//end ACK
			case NAK:

				break;//end NAK
			case REJ:

				break;//end REJ
			case ECHO_REQ:
				testoption(wType,bCode,pPacketstr);
				memset(bPacketTx,0x00,256);
				makepacket(wType,bCode,bPacketTx);

				
				break;//end TERM
			default:
				break;
			}//end code
			
			break;
		}//end LCP
	case PAP:
		bCode=pPacketstr[5-compress];
		bID=pPacketstr[6-compress];
		switch(bCode){
		case REQ:
			break;
		case ACK:

			bState=PAPPASS;
			memset(bPacketTx,0x00,256);
			makepacket(IPCP,REQ,bPacketTx);
			break;
		case NAK:
			break;
		default:
			break;
		}//end switch
		break;//end PAP
	case IPCP:
		bCode=pPacketstr[5-compress];
		bID=pPacketstr[6-compress];
		switch(bCode){
		case REQ:
			testoption(wType,bCode,pPacketstr);
			if (bState<IPCPOPEN && reject==false)	bState=IPCPOPEN;
			if (bState==IPCPOK)
			{
				IPCP_s=IPCPREOPEN;
				//makepacket(wType,NAK,pPacketstr);
			}
			//else 
				makepacket(wType,ACK,pPacketstr);
			break;
		case ACK:
			if (bState<IPCPOK)	bState=IPCPOK;
			break;
		case NAK:
			testoption(wType,bCode,pPacketstr);
			if (bState<IPCPNAK && reject==false) bState=IPCPNAK;
			makepacket(wType,REQ,pPacketstr);
			break;
		default:
			break;
		}//end switch
		break;//end IPCP
	case IP:
		IP_header.protocol=pPacketstr[14-compress-PF_compress];
		int k;
		k=16-compress-PF_compress+1;
		IP_header.SrcIP[0]=pPacketstr[k++];
		IP_header.SrcIP[1]=pPacketstr[k++];
		IP_header.SrcIP[2]=pPacketstr[k++];
		IP_header.SrcIP[3]=pPacketstr[k++];
		IP_header.DesIP[0]=pPacketstr[k++];
		IP_header.DesIP[1]=pPacketstr[k++];
		IP_header.DesIP[2]=pPacketstr[k++];
		IP_header.DesIP[3]=pPacketstr[k++];

		switch(IP_header.protocol)
		{
		case IP_ICMP:
			ICMP_header.type=pPacketstr[25-compress-PF_compress];
			switch (ICMP_header.type)
			{
			case ICMP_PING:
				makeIPpacket(IP_ICMP,ICMP_PINGREPLY,pPacketstr-compress-PF_compress+5);
				break;
			case ICMP_PINGREPLY:
				recPacketS=1;
				break;
			default:
				break;
			}
			//bCode=PING;
			//testoption(wType,bCode,pPacketstr);
			//makepacket(wType,bCode,pPacketstr);
			break;//end IP
		case IP_UDP:
			//format of temp for udp:
				//destination address
				//source port
				//destination port
				//udp data length
				//udp check sum 2bytes msb +lsb
				//udp data

			//m_packet.makeIPpacket(IP_UDP,NULL,temp);

			break;
		default:
			break;
		}
		break;
	case CCP:
		break;//end CCP
	default:
		break;
	}//end case
	return true;
}

void CPacket::testoption(WORD wType,BYTE bCode,BYTE* pOptionstr)
{
	WORD wSize,wStart;
	int i=0,j=0,k;
	DWORD dwMagic;

	reject=FALSE;
	srand((unsigned)time(NULL));
	dwMagic=rand();
	wStart=9-compress;
	wSize=pOptionstr[7-compress]*256+pOptionstr[8-compress]+8-compress;//length+framebegin(3)+protocol(2)+checksum(2)+frameend(1)
	if (wSize>MAXRX-8) wSize=MAXRX-8-compress;		//truncate packet if larger than buffer

	for(k=0;k<8;k++)
	{
		option[k].reject=TRUE;
		memset(option[k].bData,0x0,64);
	}

	while(wStart<wSize-3)
	{
		option[j].bType=pOptionstr[wStart++];
		option[j].bLength=pOptionstr[wStart++];
		for (i=0;i<option[j].bLength-2;i++)
		{
			option[j].bData[i]=pOptionstr[i+wStart];
		}//end for
		option[j].bData[i]='\0';
		wStart=i+wStart;
		j++;
	}//end while
	iOptionnum=j;

	switch(wType)
	{
		case LCP:
		{
			for(i=0;i<j;i++)
			{
				if (option[i].bType>0 && option[i].bType<=29)
				{
					switch(option[i].bType)
					{
						case 1:		//Maximum Receive Unit
							option[i].reject=FALSE;
							//option[i].bData[0]=0x00;
							//option[i].bData[1]=0xff;
							//option[i].bLength=0x04;
							break;
			
						case 2:		//Async-Control-Character-Map
							option[i].reject=FALSE;
							map=TRUE;
							break;
			
						case 3:		//Authentication-protocol
							option[i].reject=TRUE;
							reject=TRUE;
							break;
						
						case 5:		//Magic-number
							option[i].reject=TRUE;
							reject=TRUE;
							//option[i].reject=FALSE;
							//option[i].bData[3]=dwMagic & 0x00ff;
							//option[i].bData[2]=(dwMagic>>8) & 0x00ff;
							//option[i].bData[1]=(dwMagic>>16) & 0x00ff;
							//option[i].bData[0]=(dwMagic>>24) & 0x00ff;;
							//option[i].bLength=0x06;
							break;
						
						case 7:		//Protocol-Field-Compression
							option[i].reject=FALSE;
							PF_compress=1;
							break;
			
						case 8:		//Address-and-Control-Field-Compression
							option[i].reject=FALSE;
							if(bCode==ACK)
								compress=2;
							break;
/*
						case 0x11:		//Numbered-Mode
							option[i].reject=FALSE;
							break;

						case 0x13:		//Callback
							option[i].reject=FALSE;
							break;
*/

						default://reject the rest
							option[i].reject=TRUE;
							reject=TRUE;
							break;
					}//end switch
				}else
				{
					//wrong options
				}//end if ...option type

			}//end for
			break;
		}//end case LCP
		
		case IPCP:
			for(i=0;i<j;i++)
			{
				if ((option[i].bType>0 && option[i].bType<5)||(option[i].bType>=129 && option[i].bType<=132) )
				{
					switch(option[i].bType)
					{
						case 1:		//ip addresses
							option[i].reject=TRUE;
							reject=TRUE;
							break;
						case 2:		//ip compression protocol
							option[i].reject=TRUE;
							reject=TRUE;
						//	option[i].reject=FALSE;
							break;
						case 3:		//ip address
							option[i].reject=FALSE;
			    			//if (bCode==ACK)
							if (bCode==NAK)
							{
								for (k=0;k<option[i].bLength-2;k++)
								{
									bIpaddress[k]=option[i].bData[k];
								}//end for
							}//end if
							break;
						default://reject the rest
							option[i].reject=TRUE;
							reject=TRUE;
							break;
					}//end switch
				}//end if 
			}//end for
			break;
	}
}

void CPacket::makepacket(WORD wType,BYTE bCode,BYTE* pPacketRx)
{

	int i,j,k;
	WORD wLen,whigh=0,wlow=0;//header checksum;
	int iHeadpos;
	WORD wChecksum;
	
	
	memset(bPacketTx,0x00,256);
	memset(bPacketTx1,0x00,256);

	i=0;
	bPacketTx[i++]=0x7e;//frame header
	switch(wType)
	{
		case LCP:
			switch(bCode)
			{
				case REQ:
					for(i=1;i<5;i++)
					{
						bPacketTx[i]=pPacketRx[i];//frame head and protocol
						
					}
					if(reject)
					{
						bCode=REJ;
					}
					else//ack
					{
						bCode=ACK;
					}//end if(reject)
					
					bPacketTx[i++]=bCode;//code
					bPacketTx[i++]=bID;//id
					iLenpos=i;//get the length postion
					i+=2;
					
					for(k=0;k<iOptionnum;k++)
					{
						if(bCode==REJ && option[k].reject)
						{
							bPacketTx[i++]=option[k].bType;
							bPacketTx[i++]=option[k].bLength;
							for(j=0;j<option[k].bLength-2;j++)
								bPacketTx[i++]=option[k].bData[j];
						}//end if(option[i].reject&& szCode==REJ)
						if(bCode==ACK && !option[k].reject)
						{
							
							bState=LCPOPEN;//ZJ 0619
							bPacketTx[i++]=option[k].bType;
							bPacketTx[i++]=option[k].bLength;
							for(j=0;j<option[k].bLength-2;j++)
								bPacketTx[i++]=option[k].bData[j];
						}//end if(!option[i].reject&& szCode==ACK)
					}//end for
					
					break;//REQ
				case ACK:
					for(i=1;i<5;i++)
					{
						bPacketTx[i]=pPacketRx[i];//frame head and protocol
					}
					bPacketTx[i++]=REQ;//code
					bPacketTx[i++]=bID;//id	
					iLenpos=i;//get the length postion
					i+=2;
					while(pPacketRx[i]!=0x7e)
					{
						bPacketTx[i]=pPacketRx[i];
						i++;
					}
					i-=2;
					bPacketTx[i]='\0';
					bPacketTx[i+1]='\0';
					break;//end ACK
				case ECHO_REQ:
					for(i=1;i<5;i++)
					{
						bPacketTx[i]=pPacketRx[i];//frame head and protocol
					}
					bPacketTx[i++]=ECHO_REP;//code
					bPacketTx[i++]=bID;//id	
					iLenpos=i;//get the length postion
					i+=2;
					while(pPacketRx[i]!=0x7e)
					{
						bPacketTx[i]=pPacketRx[i];
						i++;
					}
					i-=2;
					bPacketTx[i]='\0';
					bPacketTx[i+1]='\0';
					break;//ECHO_REQ

			}//end switch code
			break;//end LCP
		case PAP:
			bPacketTx[i++]=0xff;
			bPacketTx[i++]=0x03;
			bPacketTx[i++]=0xc0;
			bPacketTx[i++]=0x23;
			bPacketTx[i++]=bCode;
			bPacketTx[i++]=bID;
			iLenpos=i;
			i+=2;
			bPacketTx[i++]=0x06;
			bPacketTx[i++]='1';
			bPacketTx[i++]='6';
			bPacketTx[i++]='3';
			bPacketTx[i++]=0x08;
			bPacketTx[i++]='1';
			bPacketTx[i++]='6';
			bPacketTx[i++]='3';
			break;//end PAP
		case IPCP:
			for(i=1;i<5-compress;i++)
			{
				bPacketTx[i]=pPacketRx[i];//frame head and protocol
			}
			if(reject)	bCode=REJ;
			/*
			else
			{	
				if( bState < IPCPOPEN )	//WCY
					bCode=REQ;
				else
					bCode=ACK;
			}
			*/

			//end if(reject)
			bPacketTx[i++]=bCode;//code
			bPacketTx[i++]=bID;//id

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -