Outnat.cpp
上传用户:heseme
上传日期:2009-12-23
资源大小:228k
文件大小:24k
开发平台:

Visual C++

  1. #include "stdafx.h"
  2. #include "outnat.h"
  3. #include "Struct.h"
  4. extern struct  NAT_Cfg pnatcfg;
  5. extern in_addr_t OutGlobalAddr;
  6. extern  BYTE INNO;
  7. extern  BYTE OUTNO;
  8. extern  BYTE DMNO;
  9. #define   ETH_HEAD    14  //以太帧头大小
  10. #define   IP_HEAD     20  //
  11. //#define   DYN_LOW_PORT    5000    //用于转换的最低端口
  12. unsigned short   DYN_LOW_PORT ;  //用于转换的最低端口
  13. //#define   DYN_HIGH_PORT    10000  //用于转换的最高端口
  14. unsigned short   DYN_HIGH_PORT;     //用于转换的最高端口
  15. //#define   MAX_NAT_CONNECT  500    //可支持的最大TCP连接数
  16. unsigned short   MAX_NAT_CONNECT;  //可支持的最大TCP连接数
  17. //#define   MAX_NAT_CONNECTUDP  100 //可支持的最大UDP连接数
  18. //#define   ITEM_LENG        25     
  19. //#define   OTHERITEM_LENG   25
  20. //#define   TCPTIMEOUT       5       //每个TCP转换表项的超时值
  21. unsigned short   TCPTIMEOUT;       //每个TCP转换表项的超时值
  22. //#define   UDPTIMEOUT       5       //每个UDP转换表项的超时值
  23. //#define   MAX_STATICITEM   6
  24. NATTABLE_ITEM*     nattable;          //转换表  
  25. HANDLE             access_event;      //访问控制
  26. BYTE               StaticTCPcount;    //设置的TCP静态转换个数
  27. BYTE               StaticUDPcount;     //设置的UDP静态转换个数
  28. STATICTCPTABLE     StaticTCPtable[100] ;  //静态转换表
  29. STATICUDPTABLE     StaticUDPtable[100] ;  //静态转换表
  30. ULONG        uc_GlobalIPAddress;       //对外的全局地址
  31. //ULONG        ul_InterIPAddress;
  32. NATTABLE_ITEM*   otheritem;           //转换表项
  33. USHORT*  pChooseportBufferheader;     //可选择端口栈的头指针
  34. USHORT*  pChooseportBufferbottom;     //可选择端口栈的尾指针
  35. USHORT         InHashIDTable[1000];   //转换表的入站索引
  36. USHORT         OutHashIDTable[1000];  //转换表的出站索引
  37. //input eth and calculate checksum
  38. BOOL GetTCPCksum(BYTE* buffer,int leng)
  39. {
  40.     //BYTE* tempbuffer;
  41. USHORT  tcpleng;
  42. USHORT  tcpchecksum;
  43. USHORT*  tempushort;
  44. USHORT  IPleng;
  45. BYTE*   tempbuffer;
  46. buffer[34+16] = 0;
  47. buffer[34+17] = 0;
  48.     //getIPleng(total)
  49.     ((BYTE*)(&IPleng))[0] = buffer[17];
  50.     ((BYTE*)(&IPleng))[1] = buffer[16];
  51. //get tcpleng(total)
  52. tcpleng = IPleng - 20;
  53.     
  54. //get pseudo head
  55. tempbuffer = new BYTE[tcpleng+12];
  56. memcpy(tempbuffer+12,buffer+34,tcpleng);
  57. tempbuffer[0] = buffer[26];
  58. tempbuffer[1] = buffer[27];
  59. tempbuffer[2] = buffer[28];
  60. tempbuffer[3] = buffer[29];
  61. tempbuffer[4] = buffer[30];
  62. tempbuffer[5] = buffer[31];
  63. tempbuffer[6] = buffer[32];
  64. tempbuffer[7] = buffer[33];
  65.         tempbuffer[8] = 0;
  66. tempbuffer[9] = 6;
  67. tempbuffer[10] = ((BYTE*)(&tcpleng))[1];
  68. tempbuffer[11] = ((BYTE*)(&tcpleng))[0];
  69.         
  70. tempushort = (USHORT*) tempbuffer;
  71. tcpchecksum = GetCksum(tempushort, tcpleng+12);
  72. buffer[34+16] = ((BYTE*)(&tcpchecksum))[0];
  73. buffer[34+17] = ((BYTE*)(&tcpchecksum))[1];
  74. delete [] tempbuffer;
  75. return TRUE;
  76. }
  77. //input eth and calculate checksum
  78. BOOL GetUDPCksum(BYTE* buffer,int leng)
  79. {
  80. USHORT  udpleng;
  81. USHORT  udpchecksum;
  82. USHORT*  tempushort;
  83. BYTE*   tempbuffer;
  84. //set checksum=0;
  85. buffer[34+6] = 0;
  86. buffer[34+7] = 0;
  87.     //getudpleng(total)
  88.     ((BYTE*)(&udpleng))[0] = buffer[34+5];
  89.     ((BYTE*)(&udpleng))[1] = buffer[34+4];
  90.     //get pseudo head
  91. tempbuffer = new BYTE[udpleng+12];
  92. memcpy(tempbuffer+12,buffer+34,udpleng);
  93. tempbuffer[0] = buffer[26];
  94. tempbuffer[1] = buffer[27];
  95. tempbuffer[2] = buffer[28];
  96. tempbuffer[3] = buffer[29];
  97. tempbuffer[4] = buffer[30];
  98. tempbuffer[5] = buffer[31];
  99. tempbuffer[6] = buffer[32];
  100. tempbuffer[7] = buffer[33];
  101.         tempbuffer[8] = 0;
  102. tempbuffer[9] = 17;
  103. tempbuffer[10] = buffer[34+4];
  104. tempbuffer[11] = buffer[34+5];
  105.         
  106. tempushort = (USHORT*) tempbuffer;
  107. udpchecksum = GetCksum(tempushort, udpleng+12);
  108. buffer[34+6] = ((BYTE*)(&udpchecksum))[0];
  109. buffer[34+7] = ((BYTE*)(&udpchecksum))[1];
  110. delete [] tempbuffer;
  111. return TRUE;
  112. }
  113. //input eth and calculate checksum
  114. BOOL GetIPCksum(BYTE* buffer,int leng)
  115. {
  116. BYTE* tempbufferttt;
  117. USHORT* tempushortbuffer;
  118. USHORT  checksum;
  119. tempbufferttt = &buffer[14];
  120. tempushortbuffer = (USHORT* )tempbufferttt;
  121. tempushortbuffer[5] = 0;
  122.     checksum = GetCksum(tempushortbuffer,20);
  123. tempushortbuffer[5] = checksum;
  124.     return TRUE;
  125. }
  126. //input buffer  and calculate checksum
  127. USHORT  GetCksum(USHORT* buffer,int size)
  128. {
  129. DWORD cksum;
  130. USHORT OddByte;
  131. cksum=0;
  132. while(size>1)
  133. {
  134. cksum+=*buffer++;
  135. size-=2;
  136. }
  137. if(size==1)
  138. {
  139.         OddByte = 0;
  140. *((unsigned char*)&OddByte)= *(unsigned char*)buffer;
  141. cksum+=OddByte;
  142. }
  143. cksum=(cksum>>16)+(cksum&0xffff);
  144. cksum+=(cksum>>16);
  145. cksum=~cksum;
  146.     return (unsigned short)cksum;
  147. }
  148. /*for test*/
  149. BOOL NatForOutIcmpPacket(BYTE* EthBuffer,ULONG packetlength,USHORT Adapternumber)
  150. {
  151.     BYTE* tempbuffer;
  152. BYTE* tempbufferttt;
  153. USHORT*  tempushortbuffer;
  154. USHORT   checksum;
  155.     int   EheadLength;
  156. EheadLength=14;
  157.     tempbuffer=EthBuffer;
  158. if(tempbuffer[12]==0x08&&tempbuffer[13]==0x00)
  159. {
  160. if(tempbuffer[23]!=0x01)
  161. {
  162. return FALSE;
  163. }
  164. //begin modify
  165. tempbuffer[26] = 0xc0;
  166.         tempbuffer[27] = 0xa8;
  167. tempbuffer[28] = 0x81;
  168. tempbuffer[29] = 0xc1;
  169. //modify ip checksum
  170.         tempbufferttt = &tempbuffer[14];
  171. tempushortbuffer = (USHORT* )tempbufferttt;
  172. tempushortbuffer[5] = 0;
  173.         checksum = GetCksum(tempushortbuffer,20);
  174. tempushortbuffer[5] = checksum;
  175. }
  176. else
  177. {
  178. TRACE(_T("Found a not IP packetn"));
  179. }
  180. return TRUE;
  181. }
  182. /*for test*/
  183. BOOL NatForInIcmpPacket(BYTE* EthBuffer,ULONG packetlength,USHORT Adapternumber)
  184. {
  185.     BYTE* tempbuffer;
  186. USHORT*  tempushortbuffer;
  187. USHORT   checksum;
  188.     int   EheadLength;
  189. EheadLength=14;
  190.     tempbuffer=EthBuffer;
  191. if(tempbuffer[12]==0x08&&tempbuffer[13]==0x00)
  192. {
  193. if(tempbuffer[23]!=0x01)
  194. {
  195. TRACE(_T("It's  not a IPandICMP packetn"));
  196. return FALSE;
  197. }
  198. tempbuffer[30] = 0xc0;
  199.         tempbuffer[31] = 0xa8;
  200. tempbuffer[32] = 0x0;
  201. tempbuffer[33] = 0x0b;
  202. //modify ip checksum
  203.         tempbuffer = &tempbuffer[14];
  204. tempushortbuffer = (USHORT* )tempbuffer;
  205. tempushortbuffer[5] = 0;
  206.         checksum  = GetCksum(tempushortbuffer,20);
  207. tempushortbuffer[5] = checksum;
  208. }
  209. else
  210. {
  211. TRACE(_T("Found a not IP packetn"));
  212. }
  213. return TRUE;
  214. }
  215. //add item to nattable
  216. NATTABLE_ITEM*  NatTableAddItem(BYTE* EthBuffer,BOOL bDirection, USHORT type)
  217. {
  218.    _ETHER_PACKET*   pEtherPacket;
  219.    _IP_HEADER*      pIPheader;
  220.    _TCP_HEADER*     pTCPheader;
  221.    _UDP_HEADER*     pUDPheader;
  222.        NATTABLE_ITEM*   pResult;
  223.    NATTABLE_ITEM*   ptempResult;  
  224.    USHORT           uSearchInterPort;
  225.    USHORT           uSearchOutPort;
  226.    USHORT             pos;
  227.    USHORT             index;
  228.    USHORT           tempSuffix;
  229.        pEtherPacket = (_ETHER_PACKET*)EthBuffer;
  230.    pIPheader    = (_IP_HEADER*)(EthBuffer+14);
  231.    //pTCPheader   = (_TCP_HEADER*)(EthBuffer+34);      
  232.        
  233.    switch (type) {
  234.    case 1: //it a tcp
  235.        TRACE(_T("ADD TCP ITEMn"));
  236.        pTCPheader   = (_TCP_HEADER*)(EthBuffer+34);
  237.                uSearchInterPort = pTCPheader->source;    
  238.    //modify InHashIDTable;
  239.    //modify OutHashIDTable;
  240.    //modify nattable
  241.        //get out port    
  242.    if(pChooseportBufferheader == pChooseportBufferbottom)
  243.    return 0;     
  244.        
  245.    
  246.    uSearchOutPort = *pChooseportBufferheader;    
  247.    pChooseportBufferheader--;
  248.    
  249.        //modify nattable
  250.    //USHORT  iii = uSearchOutPort - DYN_LOW_PORT;
  251.    
  252.    pResult= &nattable[swaps(uSearchOutPort) - DYN_LOW_PORT];
  253.        memset(pResult,0,sizeof(NATTABLE_ITEM));
  254.    pResult->ul_GlobalIPAddress = uc_GlobalIPAddress;
  255.    pResult ->ul_InterIPAddress = pIPheader->saddr;
  256.    pResult ->ul_OutIPAddress   = pIPheader ->daddr;
  257.    pResult ->us_InterPort      = uSearchInterPort;
  258.    pResult ->us_OutPort        = uSearchOutPort;
  259.    pResult ->us_TimeOut        = TCPTIMEOUT;   
  260.        pResult-> bUse              = 1;
  261.        //get for InhashIDTable
  262.    pos = uSearchOutPort% 997 +1;
  263.    tempSuffix = InHashIDTable[pos];
  264.    // modify InHashIDTable
  265.     if(InHashIDTable[pos] == 0)
  266.     InHashIDTable[pos] = swaps(uSearchOutPort) - DYN_LOW_PORT;
  267. else
  268.  {
  269. ptempResult = &nattable[(InHashIDTable[pos])];
  270. tempSuffix = InHashIDTable[pos];
  271. while(1) 
  272. {
  273.              if(ptempResult->uInSuffixNext !=0)
  274.  {
  275.  tempSuffix  = ptempResult->uInSuffixNext;
  276.  ptempResult = &nattable[ptempResult->uInSuffixNext];            
  277.  continue;
  278.  }
  279.  else
  280.  {
  281.  ptempResult->uInSuffixNext = swaps(uSearchOutPort) - DYN_LOW_PORT;
  282.                  pResult->uInSuffixParent   = tempSuffix;
  283.  break;
  284.  }
  285. }
  286.  }
  287.    
  288.    //modify uOutHashIDTable
  289.    //get outHashID
  290.        index = NATGetIndexOther(pIPheader->saddr,pIPheader->daddr,pTCPheader->source);
  291.       
  292.    if(OutHashIDTable[index] == 0)
  293.     OutHashIDTable[index] = swaps(uSearchOutPort) - DYN_LOW_PORT;
  294. else
  295.  {
  296. ptempResult = &nattable[(OutHashIDTable[index])];
  297. tempSuffix = OutHashIDTable[index];
  298. while(1) 
  299. {
  300.              if(ptempResult->uOutSuffixNext !=0)
  301.  {
  302.  tempSuffix  = ptempResult->uOutSuffixNext;
  303.  ptempResult = &nattable[ptempResult->uOutSuffixNext];            
  304.  continue;
  305.  }
  306.  else
  307.  {
  308.                  ptempResult->uOutSuffixNext = swaps(uSearchOutPort) - DYN_LOW_PORT;
  309.                  pResult->uOutSuffixParent = tempSuffix;
  310.  break;
  311.  }
  312. }
  313.  }
  314.           
  315.         break;
  316.    case 2: //it a udp
  317.    break;
  318.    default:
  319.    return 0;
  320.    }
  321.    
  322.    return pResult;           
  323. }
  324. NATTABLE_ITEM* NatSearch(BYTE* EthBuffer,BOOL bDirection, USHORT type)
  325. {
  326.    _ETHER_PACKET*   pEtherPacket;
  327.    _IP_HEADER*      pIPheader;
  328.    _TCP_HEADER*     pTCPheader;
  329.    _UDP_HEADER*     pUDPheader;
  330.        NATTABLE_ITEM*   pResult;
  331.    USHORT           pos;
  332.        USHORT           otherindex;
  333.        NATTABLE_ITEM*  pOtherResult;
  334.        pEtherPacket = (_ETHER_PACKET*)EthBuffer;
  335.    pIPheader    = (_IP_HEADER*)(EthBuffer+14);
  336.    
  337.    switch (type) {
  338.    case 1: //it is a tcp
  339.        WaitForSingleObject(access_event,INFINITE);
  340.            //ResetEvent(access_event);
  341. //    TRACE(_T("begin search item n"));
  342.        pTCPheader   = (_TCP_HEADER*)(EthBuffer+34);
  343.                if(bDirection) //out is true, in is false
  344.    {
  345.    //search for out packet
  346.    //get index in othertable
  347. //    TRACE(_T("OUT SEARCH n"));
  348.    otherindex = NATGetIndexOther(pIPheader->saddr,pIPheader->daddr,pTCPheader->source);
  349.    if(OutHashIDTable[otherindex]<1)
  350.            return 0;
  351.                     pOtherResult = &nattable[OutHashIDTable[otherindex]];
  352.                    while(1)
  353.    {
  354.   
  355.               if(pOtherResult->us_TimeOut != 0 && pOtherResult->ul_InterIPAddress == pIPheader->saddr&&
  356.  pTCPheader->source == pOtherResult->us_InterPort)
  357.   {
  358.    pOtherResult->us_TimeOut = TCPTIMEOUT;
  359.    //SetEvent(access_event);
  360.                return pOtherResult;
  361.   }
  362.               if(pOtherResult->uOutSuffixNext != 0 )
  363.   {
  364.                    pOtherResult = &nattable[pOtherResult->uOutSuffixNext];
  365.    TRACE(_T("found  next for out packetn")); 
  366.                     continue;
  367.   }
  368.               else
  369.   {
  370.                pOtherResult = 0;
  371.                     break;
  372.   }
  373.   
  374.    }
  375.        //SetEvent(access_event);
  376.                    return  pOtherResult;
  377.    }
  378.    else
  379.    {
  380.    //search for in packet
  381. //    TRACE(_T("IN SEARCH n"));
  382.    pos = pTCPheader->dest % 997+1;
  383.    if(InHashIDTable[pos]>0)
  384.       pResult = &nattable[(InHashIDTable[pos])] ;  
  385.                    else
  386.    return 0;
  387.                    while(1)
  388.    {
  389.   
  390.               if(pResult->us_TimeOut != 0 && pResult->us_OutPort == pTCPheader->dest )
  391.   {
  392.   pResult->us_TimeOut = TCPTIMEOUT;
  393.   //SetEvent(access_event);
  394.                    return pResult;
  395.   }
  396.               if(pResult->uInSuffixNext != 0)
  397.   {
  398.                    pResult = &nattable[pResult->uInSuffixNext];
  399.    TRACE(_T("found  next for in packetn")); 
  400.                     continue;
  401.   }
  402.               else
  403.   {
  404.                pResult = 0;
  405.    //TRACE(_T(""));
  406.                    break;
  407.   }
  408.   
  409.    }
  410.    //SetEvent(access_event);
  411.                    return  pResult;
  412.    }
  413.    break;
  414.    case 2: //it a udp
  415.        break;
  416.    default:
  417.    return 0;
  418.    }    
  419.    return 0;
  420. }
  421. //nat packet include in and out
  422. BOOL NatPacket(BYTE* total_EthBuffer)
  423. {
  424. _ETHER_PACKET*   pEtherPacket;
  425. _IP_HEADER*      pIPheader;
  426. _TCP_HEADER*     pTCPheader;
  427. _UDP_HEADER*     pUDPheader;
  428.     NATTABLE_ITEM*   pResult;
  429.     BYTE*  EthBuffer;
  430.     int Direction;
  431.     EthBuffer = total_EthBuffer+2;
  432. pResult = 0;
  433.     pEtherPacket = (_ETHER_PACKET*)EthBuffer;
  434. pIPheader    = (_IP_HEADER*)(EthBuffer+14);
  435. pTCPheader   = (_TCP_HEADER*)(EthBuffer+34);
  436. pUDPheader   = (_UDP_HEADER*)(EthBuffer+34);
  437.     //if the packet is not a ip
  438. if(pEtherPacket->ETH.h_proto != 0x0008)
  439. {
  440. return FALSE;
  441. }
  442.    
  443. //the packet is from devinstance = 3 ?
  444. //need modify
  445.   if(total_EthBuffer[1]!=(OUTNO-0x30))
  446. return FALSE;
  447.     //set direction by out or in
  448. if(total_EthBuffer[0] == 1)
  449.         Direction = 1;
  450. else
  451. Direction = 0;
  452. if(Direction)//out is true, in is false
  453. {
  454. switch  (pIPheader->protocol) {
  455. case 6:
  456. //out
  457. //the packet by this computer not nat
  458. if(pIPheader ->saddr == uc_GlobalIPAddress)
  459.     return TRUE;
  460.         
  461. //first find in statictable
  462.         if(!NATSearchStatic(EthBuffer,1,1))
  463. {
  464. pResult = NatSearch(EthBuffer,1,1);
  465. // TRACE(_T("search endn"));
  466. //Note: no judge the result of additem
  467. if(!pResult)
  468. {
  469. WaitForSingleObject(access_event,INFINITE);
  470.         ResetEvent(access_event);
  471. pResult = NatTableAddItem(EthBuffer,1,1);
  472. SetEvent(access_event);
  473. }
  474.         if(!pResult)
  475. {
  476. //if go to here ,something is wrong
  477. TRACE(_T("addite erron"));
  478. return FALSE;
  479. }
  480. //modify src port
  481. pTCPheader ->source = pResult->us_OutPort;
  482. }
  483.         //modify ipaddress
  484. pIPheader->saddr = uc_GlobalIPAddress;
  485. //TRACE(_T("sport =%x", pTCPheader ->source));
  486. //modify ip checksum
  487. GetIPCksum(EthBuffer,1000); 
  488. //modify tcp checksum
  489.     GetTCPCksum(EthBuffer,1000);                                                                                                                                                                                                                                                                                                                                                                                    GetTCPCksum(EthBuffer,1000);
  490. break;
  491. case 17:
  492. //Note: no add for udp packet
  493. //the packet by this computer not nat
  494. if(pIPheader ->saddr == uc_GlobalIPAddress)
  495.     return TRUE;
  496. pIPheader->saddr = uc_GlobalIPAddress;
  497. //TRACE(_T("sport =%x", pTCPheader ->source));
  498. //modify ip checksum
  499. GetIPCksum(EthBuffer,1000);
  500.         //modify tcp checksum
  501.     GetUDPCksum(EthBuffer,1000);
  502. break;
  503. default:
  504. return FALSE;
  505. }
  506. }
  507. else
  508. {
  509. switch (pIPheader->protocol){
  510. case 6:
  511. //in
  512. //TRACE(_T("source port = %d n"),swaps(pTCPheader->source));
  513. //first find in statictable
  514. if(!NATSearchStatic(EthBuffer,0,1))
  515. {
  516. pResult = NatSearch(EthBuffer,0,1);
  517.         if(pResult == 0)
  518. {
  519. TRACE(_T("IN PACKET NOT FOUND ITEM n"));
  520. return FALSE;
  521. }
  522. //modify dst ipaddress "192.168.0.11"
  523. //modify dst port
  524. pIPheader->daddr = pResult->ul_InterIPAddress;
  525. pTCPheader ->dest = pResult->us_InterPort;
  526. }
  527. //modify ip checksum
  528. // TRACE(_T("IN PACKET begin nat n"));
  529. GetIPCksum(EthBuffer,1000);
  530.         //modify tcp checksum
  531. GetTCPCksum(EthBuffer,1000);
  532. break;
  533.  case 17:
  534.         //if in staticudptable, modify dst ipaddress
  535.  //first find in statictable
  536. if(NATSearchStatic(EthBuffer,0,2))
  537. {
  538.     GetIPCksum(EthBuffer,1000);
  539.             GetUDPCksum(EthBuffer,1000);
  540. }
  541. break;
  542. default:
  543. return FALSE;
  544. }
  545. }
  546. return TRUE;
  547. }
  548. void NatCfg()
  549. {
  550.    DYN_LOW_PORT=pnatcfg.us_LowPort;  
  551.    DYN_HIGH_PORT=pnatcfg.us_HighPort;
  552.    MAX_NAT_CONNECT=pnatcfg.us_MaxNatNumber;
  553.    TCPTIMEOUT=pnatcfg.nTimeOut_TCP; 
  554.  //Set up nattable and inhashIDtable,outhashIDtable ,three table
  555.  //BYTE* temp = new BYTE[1024*ITEM_LENG];
  556.  //memset(temp,0,1024*ITEM_LENG);
  557.  //tempaddd for test
  558. //need modify
  559.  STATICTABLE      statictable;
  560. //  statictable.count = 3;
  561.      statictable.count = pnatcfg.StaticTable.count;
  562. //  STATICTABLEITEM*  statictableitem = new STATICTABLEITEM[pnatcfg->StaticTable.count];
  563.      
  564. //     statictable.item = statictableitem;
  565.  statictable.item=pnatcfg.StaticTable.item;
  566. /*
  567.  statictableitem[0].type = 1;
  568.  statictableitem[0].ulIpadress = 0x0b00a8c0;
  569.  statictableitem[0].usPort =  swaps(20);//16415;
  570.  statictableitem[1].type = 2;
  571.  statictableitem[1].ulIpadress = 0x0b00a8c0;
  572.  statictableitem[1].usPort =  0xba08;
  573.  statictableitem[2].type = 1;
  574.  statictableitem[2].ulIpadress = 0x0b00a8c0;
  575.  statictableitem[2].usPort =  300;
  576. */
  577.  //for test end
  578.    StaticTCPcount = 0;
  579.  StaticUDPcount = 0;
  580.  STATICTABLEITEM*  temp;
  581.  temp = statictable.item;
  582.      
  583.  //set static data from statictable
  584.  for(int j=0;j<statictable.count;j++) 
  585.  {
  586.  switch  (temp->type){
  587.  case 1:
  588.  //tcp
  589.  StaticTCPtable[StaticTCPcount].ulIpadress = temp->ulIpadress;
  590.  StaticTCPtable[StaticTCPcount].usPort     = temp->usPort;
  591.  StaticTCPcount++;
  592.  break;
  593.  case 2:
  594.  //udp
  595.  StaticUDPtable[StaticUDPcount].ulIpadress = temp->ulIpadress;
  596.  StaticUDPtable[StaticUDPcount].usPort     = temp->usPort;
  597.  StaticUDPcount++;
  598.  break;
  599.  default:
  600.  break;
  601.  }
  602.  temp++;
  603.  }
  604. //     delete[] statictableitem;
  605.  nattable = new NATTABLE_ITEM[MAX_NAT_CONNECT];
  606.  memset(nattable,0,sizeof(NATTABLE_ITEM)*MAX_NAT_CONNECT);
  607.      
  608.  memset(InHashIDTable,0,1000);
  609.  memset(OutHashIDTable,0,1000);
  610.      USHORT    lowport;
  611.  lowport = DYN_LOW_PORT;
  612.  //set GlobalIPAddress
  613.  //need modifyOutGlobalAddr
  614.  ((BYTE*)(&uc_GlobalIPAddress))[0] = OutGlobalAddr.s_b[0];
  615.  ((BYTE*)(&uc_GlobalIPAddress))[1] = OutGlobalAddr.s_b[1];
  616.  ((BYTE*)(&uc_GlobalIPAddress))[2] = OutGlobalAddr.s_b[2];
  617.  ((BYTE*)(&uc_GlobalIPAddress))[3] = OutGlobalAddr.s_b[3];   
  618.  
  619.    
  620.  //set up a buffer for choose outport;
  621.  pChooseportBufferbottom = new USHORT[MAX_NAT_CONNECT];
  622.  pChooseportBufferheader = pChooseportBufferbottom;
  623.      for(USHORT i=0;i<MAX_NAT_CONNECT;i++)
  624.  {
  625.  //Note: not add different port use for other side
  626.  pChooseportBufferbottom[i] = swaps(lowport +i);
  627.  pChooseportBufferheader   =  &pChooseportBufferbottom[i];
  628.  }
  629.  
  630.      
  631.  access_event = CreateEvent(0,1,1,0);
  632.  DWORD     dwThreadId;
  633.  //for timeout inspec
  634.  ::CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)NatBeginThread,NULL,0,&dwThreadId);
  635.  
  636.      return ;
  637. }
  638. BOOL  NATableTimeInspec()
  639. {
  640. //modify TimeOut
  641. //NATTABLE_ITEM* pTempItem     = 0;
  642. //NATTABLE_ITEM* pTempItemlast ;
  643. USHORT    temp;
  644.     
  645. for(int i=1;i<MAX_NAT_CONNECT;i++)
  646. {
  647.        
  648.  if(nattable[i].bUse == 0)
  649.  continue;
  650.  if(nattable[i].us_TimeOut == 0)
  651.  {
  652.  //modif nattable item and inhashidtable and outhashidtable
  653.  if(nattable[i].uInSuffixParent >0 )
  654.    nattable[nattable[i].uInSuffixParent].uInSuffixNext = nattable[i].uInSuffixNext;
  655.  else
  656.  {
  657.  temp = nattable[i].us_OutPort % 997 +1;
  658.  if(nattable[i].uInSuffixNext == 0)
  659.  InHashIDTable[temp] = 0;
  660.  else
  661.  {
  662.  InHashIDTable[temp] = nattable[i].uInSuffixNext;
  663.  //nattable[nattable[i].uInSuffixNext].uInSuffixParent = 0;
  664.  }
  665.  }
  666.  if(nattable[i].uInSuffixNext >0)
  667.  nattable[nattable[i].uInSuffixNext].uInSuffixParent = nattable[i].uInSuffixParent;  
  668.  if(nattable[i].uOutSuffixParent >0 )
  669.    nattable[nattable[i].uOutSuffixParent].uOutSuffixNext = nattable[i].uOutSuffixNext;
  670.  else
  671.  {
  672.  temp = NATGetIndexOther(nattable[i].ul_InterIPAddress,
  673.     nattable[i].ul_OutIPAddress,nattable[i].us_InterPort);
  674.  if(nattable[i].uOutSuffixNext == 0)
  675.  OutHashIDTable[temp] = 0;
  676.  else
  677.  {
  678.  OutHashIDTable[temp] = nattable[i].uOutSuffixNext;
  679.  //nattable[nattable[i].uOutSuffixNext].uOutSuffixParent = 0;
  680.  }
  681.  }
  682.  if(nattable[i].uOutSuffixNext >0)
  683.  nattable[nattable[i].uOutSuffixNext].uOutSuffixParent = nattable[i].uOutSuffixParent;  
  684.  //
  685.  pChooseportBufferheader++;
  686.  *pChooseportBufferheader = nattable[i].us_OutPort;
  687.  if(nattable[i].us_OutPort >2100)
  688.                       TRACE(_T("port ERRn"));
  689.              memset(&nattable[i],0,sizeof(NATTABLE_ITEM));  
  690.  TRACE(_T("del a item :%dn"), i);
  691.  }
  692.  else
  693.              nattable[i].us_TimeOut--;
  694. }
  695.     
  696. return TRUE;
  697. }
  698. void ClearNatUseResoure()
  699. {
  700. delete[]  nattable;
  701. //delete[]  nattableUDP;
  702. delete[]  pChooseportBufferbottom;
  703. // delete[]  statictableitem;
  704. //delete[]  pChooseportBufferbottomUDP;
  705. CloseHandle(access_event);
  706. //CloseHandle(access_event2);
  707. return;
  708. }
  709. void NatBeginThread(PVOID temp)
  710. {
  711.      while(1)
  712.  {
  713.  Sleep(60000);
  714.  WaitForSingleObject(access_event,INFINITE);
  715.      ResetEvent(access_event);  
  716.  NATableTimeInspec();
  717.          SetEvent(access_event);
  718.  //TRACE(_T("over timeinspec n"));
  719. // WaitForSingleObject(access_event2,INFINITE);
  720.     // ResetEvent(access_event2);  
  721. // NATableTimeInspecUDP();
  722.         // SetEvent(access_event2);
  723.  }
  724. }
  725. USHORT  NATGetIndexOther(ULONG ipaddress1,ULONG ipaddress2,USHORT sport)
  726. {
  727.       USHORT  tempindex;
  728.   tempindex = 0;
  729.      
  730.   tempindex = ((USHORT* )&ipaddress1)[0]+((USHORT* )&ipaddress1)[1]
  731.   +((USHORT* )&ipaddress2)[0]+((USHORT* )&ipaddress2)[1]
  732.   +sport;
  733. //  TRACE(_T("1:  %d n"),((USHORT* )&ipaddress1)[0]);
  734. //  TRACE(_T("2:  %d n"),((USHORT* )&ipaddress1)[1]);
  735. //  TRACE(_T("3:  %d n"),((USHORT* )&ipaddress2)[0]);
  736. //  TRACE(_T("4:  %d n"),((USHORT* )&ipaddress2)[1]);
  737. //   TRACE(_T("PROT:  %d n"),sport);
  738. //      TRACE(_T("INDEX:  %d n"),tempindex);
  739.   return tempindex % 997;   
  740. }
  741. USHORT swaps(USHORT port)
  742. {
  743. USHORT lo,hi;
  744. USHORT host;
  745. lo=port&0xff;
  746. hi=port&0xff00;
  747. lo=lo<<8;
  748. hi=hi>>8;
  749. host=hi|lo;
  750. return host;
  751. }
  752. BOOL NATSearchStatic(BYTE* EthBuffer,BOOL Direction,USHORT type)
  753. {
  754. _IP_HEADER*      pIPheader;
  755. _TCP_HEADER*     pTCPheader;
  756. _UDP_HEADER*     pUDPheader;
  757.     
  758. pIPheader    = (_IP_HEADER*)(EthBuffer+14);
  759. pTCPheader   = (_TCP_HEADER*)(EthBuffer+34);
  760. pUDPheader   = (_UDP_HEADER*)(EthBuffer+34);
  761.     
  762. switch (type) {
  763. /*
  764. =1:  TCP;
  765. =2:  UDP;
  766. */
  767. case 1:
  768. if(Direction)
  769. /*
  770. Direction =TRUE;OUT
  771. Direction =FALSE;IN
  772. */
  773. {
  774. for(int i=0;i<StaticTCPcount;i++)
  775. {
  776. if(StaticTCPtable[i].usPort == pTCPheader->source)
  777. {
  778.                // pIPheader->saddr = uc_GlobalIPAddress;
  779. TRACE(_T("STATIC NAT OUT TCPn"));
  780. return TRUE;
  781. }
  782. }
  783. return FALSE;
  784. }
  785. else
  786. {
  787. if(pTCPheader->dest>DYN_LOW_PORT&&pTCPheader->dest<DYN_HIGH_PORT)
  788.           return FALSE;
  789. for(int j=0;j<StaticTCPcount;j++)
  790. {
  791. if(StaticTCPtable[j].usPort == pTCPheader->dest)
  792. {
  793.                 pIPheader->daddr = StaticTCPtable[j].ulIpadress;
  794. TRACE(_T("STATIC NAT IN TCPn"));
  795. return TRUE;
  796. }
  797. }
  798. return FALSE;
  799. }
  800. break;
  801. case 2:
  802. if(!Direction)
  803. /*
  804. Direction =TRUE;OUT
  805. Direction =FALSE;IN
  806. */
  807. {
  808.    for(int f=0;f<StaticUDPcount;f++)
  809. {
  810. if(StaticUDPtable[f].usPort == pUDPheader->dest)
  811. {
  812.                 pIPheader->daddr = StaticUDPtable[f].ulIpadress;
  813. TRACE(_T("STATIC NAT IN UDPn"));
  814. return TRUE;
  815. }
  816. }
  817. return FALSE;
  818. }
  819. break;
  820. default:
  821. return FALSE;
  822. }
  823. return FALSE;
  824. }