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

📄 packet.cpp

📁 使用VC++开发的很棒的上网拨号工具的程序的源码,可以选择PING,UDP,TCP/IP等等方式,功能比较全面.Using Visual C + + development of the great
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			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&& bCode==REJ)
				else if((bCode==REQ) || (bCode==ACK) || (bCode==NAK) && (!option[k].reject))
				{
					//switch(bCode){
					//case REQ:
					//	bState=IPCPOPEN;
					//	break;
					//case ACK:
					//	bState=IPCPACK;
					//	break;
					//}
					if (bCode==REQ  && bState==IPCPOPEN) 
					{
						bPacketTx[i++]=3;
						bPacketTx[i++]=6;
						for(j=0;j<option[k].bLength-2;j++)
							bPacketTx[i++]=0;
					}else if (bCode==NAK)
					{
						bPacketTx[i++]=3;
						bPacketTx[i++]=6;
						for(j=0;j<option[k].bLength-2;j++)
							bPacketTx[i++]=bIpaddress[j];
					}
					else 
					{
						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&& bCode==ACK || bCode==REQ))
			}//end for
			break;//end IPCP
/*
		case IP:
			if(compress==0)
			{
				bPacketTx[i++]=0xff;
				bPacketTx[i++]=0x03;
			}
			if (PF_compress!=1)
				bPacketTx[i++]=0x00;
			bPacketTx[i++]=0x21;
			switch(bCode)
			{
				case PING:
					iHeadpos=i;
					i=ipheader(i);
					for(j=0;j<4;j++)
					{
						bPacketTx[i++]=bIpaddress[j];//source ip address
					}
					for(j=0;j<4;j++)
					{
						bPacketTx[i++]=pPacketRx[j];//destination ip address
					}
					bPacketTx[i++]=0x08;//ICMP type 请求回显
					bPacketTx[i++]=bCode;//ICMP code
					iIcmpcheckpos=i;				
					bPacketTx[i++]=0x00;//icmp checksum set 0
					bPacketTx[i++]=0x00;//icmp checksum set 0
					bPacketTx[i++]=0x00;
					bPacketTx[i++]=0x01;
					bPacketTx[i++]=0x00;
					bPacketTx[i++]=bID;
					wlow=0;
					whigh=0;
					for(j=iIcmpcheckpos-2;j<i;j+=2)
					{
						whigh+=bPacketTx[j];
					}
					for(j=iIcmpcheckpos-1;j<i;j+=2)
					{
						wlow+=bPacketTx[j];
					}
					wlow+=whigh/256;
					whigh=(whigh & 255)+wlow/256;
					wlow=(wlow & 255)+whigh/256;	
					whigh=~whigh;
					wlow=~wlow;
					bPacketTx[iIcmpcheckpos++]=(whigh & 0x00ff);
					bPacketTx[iIcmpcheckpos]=(wlow & 0x00ff);
					break;//end case ping
				default:
					memset(bPacketTx,0x00,256);
					break;
			}
			break;//end IP
*/
		case CCP:
			break;//end CCP
		default:
			break;
	}//end switch type
	if (wType==IP)
	{
		wLen=i-5+compress+PF_compress;
		bPacketTx[iLenpos++]=wLen/256;
		bPacketTx[iLenpos]=wLen%256;
		wlow=0;
		whigh=0;
		for(j=iHeadpos;j<20+iHeadpos;j+=2)
		{
			whigh+=bPacketTx[j];
		}
		for(j=iHeadpos+1;j<=20+iHeadpos;j+=2)
		{
			wlow+=bPacketTx[j];
		}
		wlow+=whigh/256;
		whigh=(whigh & 255)+wlow/256;
		wlow=(wlow & 255)+whigh/256;
		whigh=~whigh;
		wlow=~wlow;
		bPacketTx[iHeadcheckpos++]=(whigh & 0x00ff);
		bPacketTx[iHeadcheckpos]=(wlow & 0x00ff);
	}
	else 
	{
		wLen=i-5+compress;
		bPacketTx[iLenpos++]=wLen/256;
		bPacketTx[iLenpos]=wLen%256;

	}
	wChecksum=m_crc.CountCRC(bPacketTx+1,i-1);//get checksum and copy to szPacketTx[crcpos]
	wChecksum=~wChecksum;
	bPacketTx[i++]=(wChecksum & 0x00ff);
	bPacketTx[i++]=((wChecksum>>8) & 0x00ff);



	int ii;
	for (ii=0; ii<i; ii++)
		bPacketTx1[ii]=bPacketTx[ii];

	for (ii=i; ii<256; ii++) bPacketTx1[ii]=0xcc;
	bPacketTx1[i]=0x7e;

	i=charactermap(i);
	bPacketTx[i]=0x7e;//and framing end 0x7e
	TxLen=i+1;
	bID++;
	reject=FALSE;
}

int CPacket::charactermap(int iLen)
{
	BYTE temp,endtemp;
	int i,j;

	for(i=1;i<iLen;i++)
	{
		if(bPacketTx[i]>=0x00 && bPacketTx[i]<0x20)
		{
			endtemp=bPacketTx[iLen++];
			for(j=iLen-1;j>i;j--)
			{
				temp=bPacketTx[j];
				bPacketTx[j+1]=temp;
			}
			temp=bPacketTx[i]+0x20;
			bPacketTx[i++]=0x7d;
			bPacketTx[i]=temp;
			bPacketTx[iLen]=endtemp;
		}//end if 0x00-0x20
		else if(bPacketTx[i]==0x7d)
		{
			endtemp=bPacketTx[iLen++];
			for(j=iLen-1;j>i;j--)
			{
				temp=bPacketTx[j];
				bPacketTx[j+1]=temp;
			}
			temp=0x5d;
			bPacketTx[i++]=0x7d;
			bPacketTx[i]=temp;
			bPacketTx[iLen]=endtemp;
		}//end if 0x7d
		else if(bPacketTx[i]==0x7e)
		{
			endtemp=bPacketTx[iLen++];
			for(j=iLen-1;j>i;j--)
			{
				temp=bPacketTx[j];
				bPacketTx[j+1]=temp;
			}
			temp=0x5e;
			bPacketTx[i++]=0x7d;
			bPacketTx[i]=temp;
			bPacketTx[iLen]=endtemp;
		}//end if 0x7e
	}//end main for
	return(iLen);
}
int CPacket::ipheader(int i)
{
	bPacketTx[i++]=0x45;//version and header length
	bPacketTx[i++]=0x00;//service
	iLenpos=i;			//total length position
	i+=2;
	bPacketTx[i++]=0x88;//identification-MSB
	bPacketTx[i++]=0x10;//identification-LSB
	bPacketTx[i++]=0x40;//flags
	bPacketTx[i++]=0x00;//fragment offset
	bPacketTx[i++]=0x7f;//TTL
	bPacketTx[i++]=0x01;//protocol
	iHeadcheckpos=i;
	bPacketTx[i++]=0x00;//header checksum
	bPacketTx[i++]=0x00;//header checksum
	return i;
}

void CPacket::InitPacket()
{


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

void CPacket::makeIPpacket(BYTE protocal, BYTE type, BYTE *temp)
{
	// this function need optimization of the process when not support for protcal or type

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

	i=0;
	bPacketTx[i++]=0x7e;//frame header

	if(compress==0)
	{
		bPacketTx[i++]=0xff;
		bPacketTx[i++]=0x03;
	}
	if (PF_compress!=1)
		bPacketTx[i++]=0x00;
	bPacketTx[i++]=0x21;
	iHeadpos=i;

	bPacketTx[i++]=0x45;//version and header length
	bPacketTx[i++]=0x00;//service
	iLenpos=i;			//total length position
	i+=2;
	bPacketTx[i++]=IP_headerTx.Identification >> 8;		//identification-MSB
	bPacketTx[i++]=IP_headerTx.Identification & 0xff;	//identification-LSB
	IP_headerTx.Identification++;
	bPacketTx[i++]=0x00;//0x40;//flags
	bPacketTx[i++]=0x00;//fragment offset
	TTLpos=i;
	bPacketTx[i++]=0x7f;//TTL
	bPacketTx[i++]=protocal;//protocol
	iHeadcheckpos=i;
	bPacketTx[i++]=0x00;//header checksum
	bPacketTx[i++]=0x00;//header checksum
	for(j=0;j<4;j++)
	{
		bPacketTx[i++]=bIpaddress[j];//source ip address
	}

	switch(protocal)
	{
	case IP_ICMP:
		switch(type)
		{
		case ICMP_PING:
			//format of temp for ping
				//destination address 4bytes
				//ping data length 2bytes, msb lsb
				//ping data 

			for(j=0;j<4;j++)
			{
				bPacketTx[i++]=temp[j];//destination ip address
			}
			bPacketTx[i++]=type;//ICMP type 请求回显
			bPacketTx[i++]=00;//ICMP code
			iIcmpcheckpos=i;				
			bPacketTx[i++]=0x00;//icmp checksum set 0
			bPacketTx[i++]=0x00;//icmp checksum set 0
			bPacketTx[i++]=0x00;
			bPacketTx[i++]=0x01;
			bPacketTx[i++]=ICMP_header.sequence & 0x00ff;	//sequence number lsb
			bPacketTx[i++]=ICMP_header.sequence >> 8;	//sequence number msb
			ICMP_header.sequence++;
			k=temp[4]*256+temp[5];

			for (j=0;j<k;j++)
			{
				bPacketTx[i++]=temp[6+j];
			}

			break;
		case ICMP_PINGREPLY:
			//format of temp for ping
				//recieved data start at the data 0x45....
			bPacketTx[TTLpos]=temp[8]-1;
			for(j=0;j<4;j++)
			{
				bPacketTx[i++]=temp[j+12];//destination ip address
			}
			bPacketTx[i++]=type;	//ICMP type 回显应答
			bPacketTx[i++]=00;		//ICMP code
			iIcmpcheckpos=i;				
			bPacketTx[i++]=0x00;	//icmp checksum set 0
			bPacketTx[i++]=0x00;	//icmp checksum set 0
			bPacketTx[i++]=temp[24];
			bPacketTx[i++]=temp[25];
			bPacketTx[i++]=temp[26];	//reply the same sequence number
			bPacketTx[i++]=temp[27];	//reply the same sequence number

			int jj,kk;
			kk=0;
			kk+=temp[2]*256+temp[3];
			jj=28;
			for (;jj<kk;) 
				bPacketTx[i++]=temp[jj++];	//copy the last data
			break;
		default:
			break;
		}
		//count the icmp checksum
		wlow=0;
		whigh=0;
		for(j=iIcmpcheckpos-2;j<i;j+=2)
		{
			whigh+=bPacketTx[j];
		}
		for(j=iIcmpcheckpos-1;j<i;j+=2)
		{
			wlow+=bPacketTx[j];
		}

		//write the sub header checksum
		wlow+=whigh/256;
		whigh=(whigh & 255)+wlow/256;
		wlow=(wlow & 255)+whigh/256;	
		whigh=~whigh;
		wlow=~wlow;
		bPacketTx[iIcmpcheckpos++]=(whigh & 0x00ff);
		bPacketTx[iIcmpcheckpos]=(wlow & 0x00ff);

		break;
	case IP_UDP:
		//format of temp for udp:
			//destination address 4bytes
			
			//source port 2bytes msb+lsb
			//destination port  2bytes msb+lsb
			//udp data length 2bytes msb +lsb temp[8]:temp[9]
			//udp check sum 2bytes msb +lsb (from source port to the end of the udp_data)
			//udp data temp[12]
		for(j=0;j<10;j++)
		{
			bPacketTx[i++]=temp[j];//destination ip address
		}
		iIcmpcheckpos=i;
		bPacketTx[i++]=0;
		bPacketTx[i++]=0;

		k=temp[8]*256+temp[9]+4;
		for(j=12;j<k;j++)
		{
			bPacketTx[i++]=temp[j];//destination ip address
		}

		//attention
		//the check sum of udp is include:
		//pseudo header,UDP header,UDP data
		//pseudo header include :12bytes
		//source ip address(4b)
		//destination ip address(4b)
		//0(1b), ip protocol(1b), and udp length(2b)

		wlow=0;
		whigh=0;
		for(j=iIcmpcheckpos-14;j<i;j+=2)
		{
			whigh+=bPacketTx[j];
		}
		for(j=iIcmpcheckpos-13;j<i;j+=2)
		{
			wlow+=bPacketTx[j];
		}
		whigh+=temp[8];
		wlow+=protocal;
		wlow+=temp[9];


		//write the sub header checksum
		wlow+=whigh/256;
		whigh=(whigh & 255)+wlow/256;
		wlow=(wlow & 255)+whigh/256;	
		whigh=~whigh;
		wlow=~wlow;
		bPacketTx[iIcmpcheckpos++]=(whigh & 0x00ff);
		bPacketTx[iIcmpcheckpos]=(wlow & 0x00ff);


	default:
		break;
	}
	
	k=16-compress-PF_compress+1;
	IP_header.SrcIP[0]=bPacketTx[k++];
	IP_header.SrcIP[1]=bPacketTx[k++];
	IP_header.SrcIP[2]=bPacketTx[k++];
	IP_header.SrcIP[3]=bPacketTx[k++];
	IP_header.DesIP[0]=bPacketTx[k++];
	IP_header.DesIP[1]=bPacketTx[k++];
	IP_header.DesIP[2]=bPacketTx[k++];
	IP_header.DesIP[3]=bPacketTx[k++];

	wLen=i-5+compress+PF_compress;
	bPacketTx[iLenpos++]=wLen/256;
	bPacketTx[iLenpos]=wLen%256;

	
	//count the ip header's checksum
	wlow=0;
	whigh=0;
	for(j=iHeadpos;j<20+iHeadpos;j+=2)
	{
		whigh+=bPacketTx[j];
	}
	for(j=iHeadpos+1;j<=20+iHeadpos;j+=2)
	{
		wlow+=bPacketTx[j];
	}
	wlow+=whigh/256;
	whigh=(whigh & 255)+wlow/256;
	wlow=(wlow & 255)+whigh/256;
	whigh=~whigh;
	wlow=~wlow;
	bPacketTx[iHeadcheckpos++]=(whigh & 0x00ff);
	bPacketTx[iHeadcheckpos]=(wlow & 0x00ff);

	wChecksum=m_crc.CountCRC(bPacketTx+1,i-1);//get checksum and copy to szPacketTx[crcpos]
	wChecksum=~wChecksum;
	bPacketTx[i++]=(wChecksum & 0x00ff);
	bPacketTx[i++]=((wChecksum>>8) & 0x00ff);



	int ii;
	for (ii=0; ii<i; ii++)
		bPacketTx1[ii]=bPacketTx[ii];

	for (ii=i; ii<256; ii++) bPacketTx1[ii]=0xcc;
	bPacketTx1[i]=0x7e;

	i=charactermap(i);
	bPacketTx[i]=0x7e;//and framing end 0x7e
	TxLen=i+1;
	bID++;
	reject=FALSE;

}

⌨️ 快捷键说明

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