Packet.cpp
资源名称:PPP0208.rar [点击查看]
上传用户:karykuang
上传日期:2010-02-26
资源大小:103k
文件大小:21k
源码类别:
TCP/IP协议栈
开发平台:
Visual C++
- // 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]=' ';
- 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]=' ';
- bPacketTx[i+1]=' ';
- 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]=' ';
- bPacketTx[i+1]=' ';
- 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
- 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;
- }