Packet.cpp
上传用户:karykuang
上传日期:2010-02-26
资源大小:103k
文件大小:21k
源码类别:

TCP/IP协议栈

开发平台:

Visual C++

  1. // Packet.cpp: implementation of the CPacket class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include "demo.h"
  6. #include "Packet.h"
  7. #ifdef _DEBUG
  8. #undef THIS_FILE
  9. static char THIS_FILE[]=__FILE__;
  10. #define new DEBUG_NEW
  11. #endif
  12. //////////////////////////////////////////////////////////////////////
  13. // Construction/Destruction
  14. //////////////////////////////////////////////////////////////////////
  15. CPacket::CPacket()
  16. {
  17. TxLen=0;
  18. reject=FALSE;
  19. map=FALSE;
  20. recPacketS=0;
  21. IPCP_s=0;
  22. bID=1;
  23. bState=INITIAL;
  24. compress=0;
  25. PF_compress=0;
  26. for(int i=0;i<8;i++)
  27. {
  28. option[i].reject=TRUE;
  29. memset(option[i].bData,0x0,64);
  30. }
  31. memset(bPacketTx,0x0,256);
  32. memset(bIpaddress,0x00,4);
  33. iHeadcheckpos=0;
  34. iLenpos=0;
  35. }
  36. CPacket::~CPacket()
  37. {
  38. }
  39. bool CPacket::checkpacket(BYTE* pPacketstr)
  40. {
  41. BYTE bCode;
  42. CCRC m_crc;
  43. memset(bPacketTx,0x00,256);
  44. memset(bPacketTx1,0x00,256);
  45. TxLen=0;
  46. if ( (pPacketstr[3-compress]&1) && (pPacketstr[3-compress]!=0xff) )
  47. {
  48. wType=pPacketstr[3-compress];
  49. }
  50. else wType=pPacketstr[3-compress]*256+pPacketstr[4-compress];
  51. switch(wType){
  52. case LCP:
  53. {
  54. bCode=pPacketstr[5-compress];
  55. bID=pPacketstr[6-compress];
  56. switch(bCode){
  57. case REQ:
  58. /*if (bState==LCPOPEN) 
  59. {
  60. bState=INITIAL;
  61. reject=FALSE;
  62. testoption(wType,bCode,pPacketstr);
  63. makepacket(wType,bCode,pPacketstr);
  64. }
  65. else
  66. */
  67. {
  68. testoption(wType,bCode,pPacketstr);
  69. makepacket(wType,bCode,pPacketstr);
  70. }
  71. break;//end REQ
  72. case ACK:
  73. //if (pPacketstr[6]!=bID) break;
  74. testoption(wType,bCode,pPacketstr);
  75. memset(bPacketTx,0x00,256);
  76. bState=PAPOPEN;
  77. makepacket(PAP,REQ,bPacketTx);
  78. break;//end ACK
  79. case NAK:
  80. break;//end NAK
  81. case REJ:
  82. break;//end REJ
  83. case ECHO_REQ:
  84. testoption(wType,bCode,pPacketstr);
  85. memset(bPacketTx,0x00,256);
  86. makepacket(wType,bCode,bPacketTx);
  87. break;//end TERM
  88. default:
  89. break;
  90. }//end code
  91. break;
  92. }//end LCP
  93. case PAP:
  94. bCode=pPacketstr[5-compress];
  95. bID=pPacketstr[6-compress];
  96. switch(bCode){
  97. case REQ:
  98. break;
  99. case ACK:
  100. bState=PAPPASS;
  101. memset(bPacketTx,0x00,256);
  102. makepacket(IPCP,REQ,bPacketTx);
  103. break;
  104. case NAK:
  105. break;
  106. default:
  107. break;
  108. }//end switch
  109. break;//end PAP
  110. case IPCP:
  111. bCode=pPacketstr[5-compress];
  112. bID=pPacketstr[6-compress];
  113. switch(bCode){
  114. case REQ:
  115. testoption(wType,bCode,pPacketstr);
  116. if (bState<IPCPOPEN && reject==false) bState=IPCPOPEN;
  117. if (bState==IPCPOK)
  118. {
  119. IPCP_s=IPCPREOPEN;
  120. //makepacket(wType,NAK,pPacketstr);
  121. }
  122. //else 
  123. makepacket(wType,ACK,pPacketstr);
  124. break;
  125. case ACK:
  126. if (bState<IPCPOK) bState=IPCPOK;
  127. break;
  128. case NAK:
  129. testoption(wType,bCode,pPacketstr);
  130. if (bState<IPCPNAK && reject==false) bState=IPCPNAK;
  131. makepacket(wType,REQ,pPacketstr);
  132. break;
  133. default:
  134. break;
  135. }//end switch
  136. break;//end IPCP
  137. case IP:
  138. IP_header.protocol=pPacketstr[14-compress-PF_compress];
  139. int k;
  140. k=16-compress-PF_compress+1;
  141. IP_header.SrcIP[0]=pPacketstr[k++];
  142. IP_header.SrcIP[1]=pPacketstr[k++];
  143. IP_header.SrcIP[2]=pPacketstr[k++];
  144. IP_header.SrcIP[3]=pPacketstr[k++];
  145. IP_header.DesIP[0]=pPacketstr[k++];
  146. IP_header.DesIP[1]=pPacketstr[k++];
  147. IP_header.DesIP[2]=pPacketstr[k++];
  148. IP_header.DesIP[3]=pPacketstr[k++];
  149. switch(IP_header.protocol)
  150. {
  151. case IP_ICMP:
  152. ICMP_header.type=pPacketstr[25-compress-PF_compress];
  153. switch (ICMP_header.type)
  154. {
  155. case ICMP_PING:
  156. makeIPpacket(IP_ICMP,ICMP_PINGREPLY,pPacketstr-compress-PF_compress+5);
  157. break;
  158. case ICMP_PINGREPLY:
  159. recPacketS=1;
  160. break;
  161. default:
  162. break;
  163. }
  164. //bCode=PING;
  165. //testoption(wType,bCode,pPacketstr);
  166. //makepacket(wType,bCode,pPacketstr);
  167. break;//end IP
  168. case IP_UDP:
  169. //format of temp for udp:
  170. //destination address
  171. //source port
  172. //destination port
  173. //udp data length
  174. //udp check sum 2bytes msb +lsb
  175. //udp data
  176. //m_packet.makeIPpacket(IP_UDP,NULL,temp);
  177. break;
  178. default:
  179. break;
  180. }
  181. break;
  182. case CCP:
  183. break;//end CCP
  184. default:
  185. break;
  186. }//end case
  187. return true;
  188. }
  189. void CPacket::testoption(WORD wType,BYTE bCode,BYTE* pOptionstr)
  190. {
  191. WORD wSize,wStart;
  192. int i=0,j=0,k;
  193. DWORD dwMagic;
  194. reject=FALSE;
  195. srand((unsigned)time(NULL));
  196. dwMagic=rand();
  197. wStart=9-compress;
  198. wSize=pOptionstr[7-compress]*256+pOptionstr[8-compress]+8-compress;//length+framebegin(3)+protocol(2)+checksum(2)+frameend(1)
  199. if (wSize>MAXRX-8) wSize=MAXRX-8-compress; //truncate packet if larger than buffer
  200. for(k=0;k<8;k++)
  201. {
  202. option[k].reject=TRUE;
  203. memset(option[k].bData,0x0,64);
  204. }
  205. while(wStart<wSize-3)
  206. {
  207. option[j].bType=pOptionstr[wStart++];
  208. option[j].bLength=pOptionstr[wStart++];
  209. for (i=0;i<option[j].bLength-2;i++)
  210. {
  211. option[j].bData[i]=pOptionstr[i+wStart];
  212. }//end for
  213. option[j].bData[i]='';
  214. wStart=i+wStart;
  215. j++;
  216. }//end while
  217. iOptionnum=j;
  218. switch(wType)
  219. {
  220. case LCP:
  221. {
  222. for(i=0;i<j;i++)
  223. {
  224. if (option[i].bType>0 && option[i].bType<=29)
  225. {
  226. switch(option[i].bType)
  227. {
  228. case 1: //Maximum Receive Unit
  229. option[i].reject=FALSE;
  230. //option[i].bData[0]=0x00;
  231. //option[i].bData[1]=0xff;
  232. //option[i].bLength=0x04;
  233. break;
  234. case 2: //Async-Control-Character-Map
  235. option[i].reject=FALSE;
  236. map=TRUE;
  237. break;
  238. case 3: //Authentication-protocol
  239. option[i].reject=TRUE;
  240. reject=TRUE;
  241. break;
  242. case 5: //Magic-number
  243. option[i].reject=TRUE;
  244. reject=TRUE;
  245. //option[i].reject=FALSE;
  246. //option[i].bData[3]=dwMagic & 0x00ff;
  247. //option[i].bData[2]=(dwMagic>>8) & 0x00ff;
  248. //option[i].bData[1]=(dwMagic>>16) & 0x00ff;
  249. //option[i].bData[0]=(dwMagic>>24) & 0x00ff;;
  250. //option[i].bLength=0x06;
  251. break;
  252. case 7: //Protocol-Field-Compression
  253. option[i].reject=FALSE;
  254. PF_compress=1;
  255. break;
  256. case 8: //Address-and-Control-Field-Compression
  257. option[i].reject=FALSE;
  258. if(bCode==ACK)
  259. compress=2;
  260. break;
  261. /*
  262. case 0x11: //Numbered-Mode
  263. option[i].reject=FALSE;
  264. break;
  265. case 0x13: //Callback
  266. option[i].reject=FALSE;
  267. break;
  268. */
  269. default://reject the rest
  270. option[i].reject=TRUE;
  271. reject=TRUE;
  272. break;
  273. }//end switch
  274. }else
  275. {
  276. //wrong options
  277. }//end if ...option type
  278. }//end for
  279. break;
  280. }//end case LCP
  281. case IPCP:
  282. for(i=0;i<j;i++)
  283. {
  284. if ((option[i].bType>0 && option[i].bType<5)||(option[i].bType>=129 && option[i].bType<=132) )
  285. {
  286. switch(option[i].bType)
  287. {
  288. case 1: //ip addresses
  289. option[i].reject=TRUE;
  290. reject=TRUE;
  291. break;
  292. case 2: //ip compression protocol
  293. option[i].reject=TRUE;
  294. reject=TRUE;
  295. // option[i].reject=FALSE;
  296. break;
  297. case 3: //ip address
  298. option[i].reject=FALSE;
  299.      //if (bCode==ACK)
  300. if (bCode==NAK)
  301. {
  302. for (k=0;k<option[i].bLength-2;k++)
  303. {
  304. bIpaddress[k]=option[i].bData[k];
  305. }//end for
  306. }//end if
  307. break;
  308. default://reject the rest
  309. option[i].reject=TRUE;
  310. reject=TRUE;
  311. break;
  312. }//end switch
  313. }//end if 
  314. }//end for
  315. break;
  316. }
  317. }
  318. void CPacket::makepacket(WORD wType,BYTE bCode,BYTE* pPacketRx)
  319. {
  320. int i,j,k;
  321. WORD wLen,whigh=0,wlow=0;//header checksum;
  322. int iHeadpos;
  323. WORD wChecksum;
  324. memset(bPacketTx,0x00,256);
  325. memset(bPacketTx1,0x00,256);
  326. i=0;
  327. bPacketTx[i++]=0x7e;//frame header
  328. switch(wType)
  329. {
  330. case LCP:
  331. switch(bCode)
  332. {
  333. case REQ:
  334. for(i=1;i<5;i++)
  335. {
  336. bPacketTx[i]=pPacketRx[i];//frame head and protocol
  337. }
  338. if(reject)
  339. {
  340. bCode=REJ;
  341. }
  342. else//ack
  343. {
  344. bCode=ACK;
  345. }//end if(reject)
  346. bPacketTx[i++]=bCode;//code
  347. bPacketTx[i++]=bID;//id
  348. iLenpos=i;//get the length postion
  349. i+=2;
  350. for(k=0;k<iOptionnum;k++)
  351. {
  352. if(bCode==REJ && option[k].reject)
  353. {
  354. bPacketTx[i++]=option[k].bType;
  355. bPacketTx[i++]=option[k].bLength;
  356. for(j=0;j<option[k].bLength-2;j++)
  357. bPacketTx[i++]=option[k].bData[j];
  358. }//end if(option[i].reject&& szCode==REJ)
  359. if(bCode==ACK && !option[k].reject)
  360. {
  361. bState=LCPOPEN;//ZJ 0619
  362. bPacketTx[i++]=option[k].bType;
  363. bPacketTx[i++]=option[k].bLength;
  364. for(j=0;j<option[k].bLength-2;j++)
  365. bPacketTx[i++]=option[k].bData[j];
  366. }//end if(!option[i].reject&& szCode==ACK)
  367. }//end for
  368. break;//REQ
  369. case ACK:
  370. for(i=1;i<5;i++)
  371. {
  372. bPacketTx[i]=pPacketRx[i];//frame head and protocol
  373. }
  374. bPacketTx[i++]=REQ;//code
  375. bPacketTx[i++]=bID;//id
  376. iLenpos=i;//get the length postion
  377. i+=2;
  378. while(pPacketRx[i]!=0x7e)
  379. {
  380. bPacketTx[i]=pPacketRx[i];
  381. i++;
  382. }
  383. i-=2;
  384. bPacketTx[i]='';
  385. bPacketTx[i+1]='';
  386. break;//end ACK
  387. case ECHO_REQ:
  388. for(i=1;i<5;i++)
  389. {
  390. bPacketTx[i]=pPacketRx[i];//frame head and protocol
  391. }
  392. bPacketTx[i++]=ECHO_REP;//code
  393. bPacketTx[i++]=bID;//id
  394. iLenpos=i;//get the length postion
  395. i+=2;
  396. while(pPacketRx[i]!=0x7e)
  397. {
  398. bPacketTx[i]=pPacketRx[i];
  399. i++;
  400. }
  401. i-=2;
  402. bPacketTx[i]='';
  403. bPacketTx[i+1]='';
  404. break;//ECHO_REQ
  405. }//end switch code
  406. break;//end LCP
  407. case PAP:
  408. bPacketTx[i++]=0xff;
  409. bPacketTx[i++]=0x03;
  410. bPacketTx[i++]=0xc0;
  411. bPacketTx[i++]=0x23;
  412. bPacketTx[i++]=bCode;
  413. bPacketTx[i++]=bID;
  414. iLenpos=i;
  415. i+=2;
  416. bPacketTx[i++]=0x06;
  417. bPacketTx[i++]='1';
  418. bPacketTx[i++]='6';
  419. bPacketTx[i++]='3';
  420. bPacketTx[i++]=0x08;
  421. bPacketTx[i++]='1';
  422. bPacketTx[i++]='6';
  423. bPacketTx[i++]='3';
  424. break;//end PAP
  425. case IPCP:
  426. for(i=1;i<5-compress;i++)
  427. {
  428. bPacketTx[i]=pPacketRx[i];//frame head and protocol
  429. }
  430. if(reject) bCode=REJ;
  431. /*
  432. else
  433. {
  434. if( bState < IPCPOPEN ) //WCY
  435. bCode=REQ;
  436. else
  437. bCode=ACK;
  438. }
  439. */
  440. //end if(reject)
  441. bPacketTx[i++]=bCode;//code
  442. bPacketTx[i++]=bID;//id
  443. iLenpos=i;//get the length postion
  444. i+=2;
  445. for(k=0;k<iOptionnum;k++)
  446. {
  447. if(bCode==REJ && option[k].reject)
  448. {
  449. bPacketTx[i++]=option[k].bType;
  450. bPacketTx[i++]=option[k].bLength;
  451. for(j=0;j<option[k].bLength-2;j++)
  452. bPacketTx[i++]=option[k].bData[j];
  453. }//end if(option[i].reject&& bCode==REJ)
  454. else if((bCode==REQ) || (bCode==ACK) || (bCode==NAK) && (!option[k].reject))
  455. {
  456. //switch(bCode){
  457. //case REQ:
  458. // bState=IPCPOPEN;
  459. // break;
  460. //case ACK:
  461. // bState=IPCPACK;
  462. // break;
  463. //}
  464. if (bCode==REQ  && bState==IPCPOPEN) 
  465. {
  466. bPacketTx[i++]=3;
  467. bPacketTx[i++]=6;
  468. for(j=0;j<option[k].bLength-2;j++)
  469. bPacketTx[i++]=0;
  470. }else if (bCode==NAK)
  471. {
  472. bPacketTx[i++]=3;
  473. bPacketTx[i++]=6;
  474. for(j=0;j<option[k].bLength-2;j++)
  475. bPacketTx[i++]=bIpaddress[j];
  476. }
  477. else 
  478. {
  479. bPacketTx[i++]=option[k].bType;
  480. bPacketTx[i++]=option[k].bLength;
  481. for(j=0;j<option[k].bLength-2;j++)
  482. bPacketTx[i++]=option[k].bData[j];
  483. }
  484. }//end if(!option[i].reject&& bCode==ACK || bCode==REQ))
  485. }//end for
  486. break;//end IPCP
  487. /*
  488. case IP:
  489. if(compress==0)
  490. {
  491. bPacketTx[i++]=0xff;
  492. bPacketTx[i++]=0x03;
  493. }
  494. if (PF_compress!=1)
  495. bPacketTx[i++]=0x00;
  496. bPacketTx[i++]=0x21;
  497. switch(bCode)
  498. {
  499. case PING:
  500. iHeadpos=i;
  501. i=ipheader(i);
  502. for(j=0;j<4;j++)
  503. {
  504. bPacketTx[i++]=bIpaddress[j];//source ip address
  505. }
  506. for(j=0;j<4;j++)
  507. {
  508. bPacketTx[i++]=pPacketRx[j];//destination ip address
  509. }
  510. bPacketTx[i++]=0x08;//ICMP type 请求回显
  511. bPacketTx[i++]=bCode;//ICMP code
  512. iIcmpcheckpos=i;
  513. bPacketTx[i++]=0x00;//icmp checksum set 0
  514. bPacketTx[i++]=0x00;//icmp checksum set 0
  515. bPacketTx[i++]=0x00;
  516. bPacketTx[i++]=0x01;
  517. bPacketTx[i++]=0x00;
  518. bPacketTx[i++]=bID;
  519. wlow=0;
  520. whigh=0;
  521. for(j=iIcmpcheckpos-2;j<i;j+=2)
  522. {
  523. whigh+=bPacketTx[j];
  524. }
  525. for(j=iIcmpcheckpos-1;j<i;j+=2)
  526. {
  527. wlow+=bPacketTx[j];
  528. }
  529. wlow+=whigh/256;
  530. whigh=(whigh & 255)+wlow/256;
  531. wlow=(wlow & 255)+whigh/256;
  532. whigh=~whigh;
  533. wlow=~wlow;
  534. bPacketTx[iIcmpcheckpos++]=(whigh & 0x00ff);
  535. bPacketTx[iIcmpcheckpos]=(wlow & 0x00ff);
  536. break;//end case ping
  537. default:
  538. memset(bPacketTx,0x00,256);
  539. break;
  540. }
  541. break;//end IP
  542. */
  543. case CCP:
  544. break;//end CCP
  545. default:
  546. break;
  547. }//end switch type
  548. if (wType==IP)
  549. {
  550. wLen=i-5+compress+PF_compress;
  551. bPacketTx[iLenpos++]=wLen/256;
  552. bPacketTx[iLenpos]=wLen%256;
  553. wlow=0;
  554. whigh=0;
  555. for(j=iHeadpos;j<20+iHeadpos;j+=2)
  556. {
  557. whigh+=bPacketTx[j];
  558. }
  559. for(j=iHeadpos+1;j<=20+iHeadpos;j+=2)
  560. {
  561. wlow+=bPacketTx[j];
  562. }
  563. wlow+=whigh/256;
  564. whigh=(whigh & 255)+wlow/256;
  565. wlow=(wlow & 255)+whigh/256;
  566. whigh=~whigh;
  567. wlow=~wlow;
  568. bPacketTx[iHeadcheckpos++]=(whigh & 0x00ff);
  569. bPacketTx[iHeadcheckpos]=(wlow & 0x00ff);
  570. }
  571. else 
  572. {
  573. wLen=i-5+compress;
  574. bPacketTx[iLenpos++]=wLen/256;
  575. bPacketTx[iLenpos]=wLen%256;
  576. }
  577. wChecksum=m_crc.CountCRC(bPacketTx+1,i-1);//get checksum and copy to szPacketTx[crcpos]
  578. wChecksum=~wChecksum;
  579. bPacketTx[i++]=(wChecksum & 0x00ff);
  580. bPacketTx[i++]=((wChecksum>>8) & 0x00ff);
  581. int ii;
  582. for (ii=0; ii<i; ii++)
  583. bPacketTx1[ii]=bPacketTx[ii];
  584. for (ii=i; ii<256; ii++) bPacketTx1[ii]=0xcc;
  585. bPacketTx1[i]=0x7e;
  586. i=charactermap(i);
  587. bPacketTx[i]=0x7e;//and framing end 0x7e
  588. TxLen=i+1;
  589. bID++;
  590. reject=FALSE;
  591. }
  592. int CPacket::charactermap(int iLen)
  593. {
  594. BYTE temp,endtemp;
  595. int i,j;
  596. for(i=1;i<iLen;i++)
  597. {
  598. if(bPacketTx[i]>=0x00 && bPacketTx[i]<0x20)
  599. {
  600. endtemp=bPacketTx[iLen++];
  601. for(j=iLen-1;j>i;j--)
  602. {
  603. temp=bPacketTx[j];
  604. bPacketTx[j+1]=temp;
  605. }
  606. temp=bPacketTx[i]+0x20;
  607. bPacketTx[i++]=0x7d;
  608. bPacketTx[i]=temp;
  609. bPacketTx[iLen]=endtemp;
  610. }//end if 0x00-0x20
  611. else if(bPacketTx[i]==0x7d)
  612. {
  613. endtemp=bPacketTx[iLen++];
  614. for(j=iLen-1;j>i;j--)
  615. {
  616. temp=bPacketTx[j];
  617. bPacketTx[j+1]=temp;
  618. }
  619. temp=0x5d;
  620. bPacketTx[i++]=0x7d;
  621. bPacketTx[i]=temp;
  622. bPacketTx[iLen]=endtemp;
  623. }//end if 0x7d
  624. else if(bPacketTx[i]==0x7e)
  625. {
  626. endtemp=bPacketTx[iLen++];
  627. for(j=iLen-1;j>i;j--)
  628. {
  629. temp=bPacketTx[j];
  630. bPacketTx[j+1]=temp;
  631. }
  632. temp=0x5e;
  633. bPacketTx[i++]=0x7d;
  634. bPacketTx[i]=temp;
  635. bPacketTx[iLen]=endtemp;
  636. }//end if 0x7e
  637. }//end main for
  638. return(iLen);
  639. }
  640. int CPacket::ipheader(int i)
  641. {
  642. bPacketTx[i++]=0x45;//version and header length
  643. bPacketTx[i++]=0x00;//service
  644. iLenpos=i; //total length position
  645. i+=2;
  646. bPacketTx[i++]=0x88;//identification-MSB
  647. bPacketTx[i++]=0x10;//identification-LSB
  648. bPacketTx[i++]=0x40;//flags
  649. bPacketTx[i++]=0x00;//fragment offset
  650. bPacketTx[i++]=0x7f;//TTL
  651. bPacketTx[i++]=0x01;//protocol
  652. iHeadcheckpos=i;
  653. bPacketTx[i++]=0x00;//header checksum
  654. bPacketTx[i++]=0x00;//header checksum
  655. return i;
  656. }
  657. void CPacket::InitPacket()
  658. {
  659. TxLen=0;
  660. reject=FALSE;
  661. map=FALSE;
  662. recPacketS=0;
  663. IPCP_s=0;
  664. bID=1;
  665. bState=INITIAL;
  666. compress=0;
  667. PF_compress=0;
  668. for(int i=0;i<8;i++)
  669. {
  670. option[i].reject=TRUE;
  671. memset(option[i].bData,0x0,64);
  672. }
  673. memset(bPacketTx,0x0,256);
  674. memset(bIpaddress,0x00,4);
  675. iHeadcheckpos=0;
  676. iLenpos=0;
  677. }
  678. void CPacket::makeIPpacket(BYTE protocal, BYTE type, BYTE *temp)
  679. {
  680. // this function need optimization of the process when not support for protcal or type
  681. int i,j,k;
  682. WORD wLen,whigh=0,wlow=0;//header checksum;
  683. int iHeadpos,TTLpos;
  684. WORD wChecksum;
  685. memset(bPacketTx,0x00,256);
  686. memset(bPacketTx1,0x00,256);
  687. i=0;
  688. bPacketTx[i++]=0x7e;//frame header
  689. if(compress==0)
  690. {
  691. bPacketTx[i++]=0xff;
  692. bPacketTx[i++]=0x03;
  693. }
  694. if (PF_compress!=1)
  695. bPacketTx[i++]=0x00;
  696. bPacketTx[i++]=0x21;
  697. iHeadpos=i;
  698. bPacketTx[i++]=0x45;//version and header length
  699. bPacketTx[i++]=0x00;//service
  700. iLenpos=i; //total length position
  701. i+=2;
  702. bPacketTx[i++]=IP_headerTx.Identification >> 8; //identification-MSB
  703. bPacketTx[i++]=IP_headerTx.Identification & 0xff; //identification-LSB
  704. IP_headerTx.Identification++;
  705. bPacketTx[i++]=0x00;//0x40;//flags
  706. bPacketTx[i++]=0x00;//fragment offset
  707. TTLpos=i;
  708. bPacketTx[i++]=0x7f;//TTL
  709. bPacketTx[i++]=protocal;//protocol
  710. iHeadcheckpos=i;
  711. bPacketTx[i++]=0x00;//header checksum
  712. bPacketTx[i++]=0x00;//header checksum
  713. for(j=0;j<4;j++)
  714. {
  715. bPacketTx[i++]=bIpaddress[j];//source ip address
  716. }
  717. switch(protocal)
  718. {
  719. case IP_ICMP:
  720. switch(type)
  721. {
  722. case ICMP_PING:
  723. //format of temp for ping
  724. //destination address 4bytes
  725. //ping data length 2bytes, msb lsb
  726. //ping data 
  727. for(j=0;j<4;j++)
  728. {
  729. bPacketTx[i++]=temp[j];//destination ip address
  730. }
  731. bPacketTx[i++]=type;//ICMP type 请求回显
  732. bPacketTx[i++]=00;//ICMP code
  733. iIcmpcheckpos=i;
  734. bPacketTx[i++]=0x00;//icmp checksum set 0
  735. bPacketTx[i++]=0x00;//icmp checksum set 0
  736. bPacketTx[i++]=0x00;
  737. bPacketTx[i++]=0x01;
  738. bPacketTx[i++]=ICMP_header.sequence & 0x00ff; //sequence number lsb
  739. bPacketTx[i++]=ICMP_header.sequence >> 8; //sequence number msb
  740. ICMP_header.sequence++;
  741. k=temp[4]*256+temp[5];
  742. for (j=0;j<k;j++)
  743. {
  744. bPacketTx[i++]=temp[6+j];
  745. }
  746. break;
  747. case ICMP_PINGREPLY:
  748. //format of temp for ping
  749. //recieved data start at the data 0x45....
  750. bPacketTx[TTLpos]=temp[8]-1;
  751. for(j=0;j<4;j++)
  752. {
  753. bPacketTx[i++]=temp[j+12];//destination ip address
  754. }
  755. bPacketTx[i++]=type; //ICMP type 回显应答
  756. bPacketTx[i++]=00; //ICMP code
  757. iIcmpcheckpos=i;
  758. bPacketTx[i++]=0x00; //icmp checksum set 0
  759. bPacketTx[i++]=0x00; //icmp checksum set 0
  760. bPacketTx[i++]=temp[24];
  761. bPacketTx[i++]=temp[25];
  762. bPacketTx[i++]=temp[26]; //reply the same sequence number
  763. bPacketTx[i++]=temp[27]; //reply the same sequence number
  764. int jj,kk;
  765. kk=0;
  766. kk+=temp[2]*256+temp[3];
  767. jj=28;
  768. for (;jj<kk;) 
  769. bPacketTx[i++]=temp[jj++]; //copy the last data
  770. break;
  771. default:
  772. break;
  773. }
  774. //count the icmp checksum
  775. wlow=0;
  776. whigh=0;
  777. for(j=iIcmpcheckpos-2;j<i;j+=2)
  778. {
  779. whigh+=bPacketTx[j];
  780. }
  781. for(j=iIcmpcheckpos-1;j<i;j+=2)
  782. {
  783. wlow+=bPacketTx[j];
  784. }
  785. //write the sub header checksum
  786. wlow+=whigh/256;
  787. whigh=(whigh & 255)+wlow/256;
  788. wlow=(wlow & 255)+whigh/256;
  789. whigh=~whigh;
  790. wlow=~wlow;
  791. bPacketTx[iIcmpcheckpos++]=(whigh & 0x00ff);
  792. bPacketTx[iIcmpcheckpos]=(wlow & 0x00ff);
  793. break;
  794. case IP_UDP:
  795. //format of temp for udp:
  796. //destination address 4bytes
  797. //source port 2bytes msb+lsb
  798. //destination port  2bytes msb+lsb
  799. //udp data length 2bytes msb +lsb temp[8]:temp[9]
  800. //udp check sum 2bytes msb +lsb (from source port to the end of the udp_data)
  801. //udp data temp[12]
  802. for(j=0;j<10;j++)
  803. {
  804. bPacketTx[i++]=temp[j];//destination ip address
  805. }
  806. iIcmpcheckpos=i;
  807. bPacketTx[i++]=0;
  808. bPacketTx[i++]=0;
  809. k=temp[8]*256+temp[9]+4;
  810. for(j=12;j<k;j++)
  811. {
  812. bPacketTx[i++]=temp[j];//destination ip address
  813. }
  814. //attention
  815. //the check sum of udp is include:
  816. //pseudo header,UDP header,UDP data
  817. //pseudo header include :12bytes
  818. //source ip address(4b)
  819. //destination ip address(4b)
  820. //0(1b), ip protocol(1b), and udp length(2b)
  821. wlow=0;
  822. whigh=0;
  823. for(j=iIcmpcheckpos-14;j<i;j+=2)
  824. {
  825. whigh+=bPacketTx[j];
  826. }
  827. for(j=iIcmpcheckpos-13;j<i;j+=2)
  828. {
  829. wlow+=bPacketTx[j];
  830. }
  831. whigh+=temp[8];
  832. wlow+=protocal;
  833. wlow+=temp[9];
  834. //write the sub header checksum
  835. wlow+=whigh/256;
  836. whigh=(whigh & 255)+wlow/256;
  837. wlow=(wlow & 255)+whigh/256;
  838. whigh=~whigh;
  839. wlow=~wlow;
  840. bPacketTx[iIcmpcheckpos++]=(whigh & 0x00ff);
  841. bPacketTx[iIcmpcheckpos]=(wlow & 0x00ff);
  842. default:
  843. break;
  844. }
  845. k=16-compress-PF_compress+1;
  846. IP_header.SrcIP[0]=bPacketTx[k++];
  847. IP_header.SrcIP[1]=bPacketTx[k++];
  848. IP_header.SrcIP[2]=bPacketTx[k++];
  849. IP_header.SrcIP[3]=bPacketTx[k++];
  850. IP_header.DesIP[0]=bPacketTx[k++];
  851. IP_header.DesIP[1]=bPacketTx[k++];
  852. IP_header.DesIP[2]=bPacketTx[k++];
  853. IP_header.DesIP[3]=bPacketTx[k++];
  854. wLen=i-5+compress+PF_compress;
  855. bPacketTx[iLenpos++]=wLen/256;
  856. bPacketTx[iLenpos]=wLen%256;
  857. //count the ip header's checksum
  858. wlow=0;
  859. whigh=0;
  860. for(j=iHeadpos;j<20+iHeadpos;j+=2)
  861. {
  862. whigh+=bPacketTx[j];
  863. }
  864. for(j=iHeadpos+1;j<=20+iHeadpos;j+=2)
  865. {
  866. wlow+=bPacketTx[j];
  867. }
  868. wlow+=whigh/256;
  869. whigh=(whigh & 255)+wlow/256;
  870. wlow=(wlow & 255)+whigh/256;
  871. whigh=~whigh;
  872. wlow=~wlow;
  873. bPacketTx[iHeadcheckpos++]=(whigh & 0x00ff);
  874. bPacketTx[iHeadcheckpos]=(wlow & 0x00ff);
  875. wChecksum=m_crc.CountCRC(bPacketTx+1,i-1);//get checksum and copy to szPacketTx[crcpos]
  876. wChecksum=~wChecksum;
  877. bPacketTx[i++]=(wChecksum & 0x00ff);
  878. bPacketTx[i++]=((wChecksum>>8) & 0x00ff);
  879. int ii;
  880. for (ii=0; ii<i; ii++)
  881. bPacketTx1[ii]=bPacketTx[ii];
  882. for (ii=i; ii<256; ii++) bPacketTx1[ii]=0xcc;
  883. bPacketTx1[i]=0x7e;
  884. i=charactermap(i);
  885. bPacketTx[i]=0x7e;//and framing end 0x7e
  886. TxLen=i+1;
  887. bID++;
  888. reject=FALSE;
  889. }