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

Visual C++

  1. #include "StdAfx.h"
  2. #include "Struct.h"
  3. #include "ip.h"
  4. #include "ipfilter.h"
  5. #include "winsock2.h"
  6. #include "config.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. extern  FilterConfig filtercfg;//ipfilter.cpp
  13. extern  LogManage TheLogManage;
  14. extern  DefaultSetData TheDefaultSetData;
  15. extern  BYTE INNO;
  16. extern  BYTE OUTNO;
  17. extern  BYTE DMNO;
  18. static unsigned int netmask(unsigned int);
  19. static int numzeros(ULONG a);
  20. static int
  21. numzeros(ULONG a)
  22. {
  23. register int i;
  24. for (i=0; (i < 32) && !(a & 1); i++, a >>= 1);
  25. return i;
  26. }       
  27. static unsigned int netmask(unsigned int bits)
  28. {
  29. return (0xFFFFFFFFUL << ((bits < 32) ? 32 - bits : 0));
  30. }
  31. VOID StartFilter()
  32. {
  33.    manageTablesReq *NewToActive=new manageTablesReq;
  34.    NewToActive->command=ACTIVATE_NEW;
  35.    NewToActive->val=0;//类的最大数;在此函数下无用
  36.    filter_command(DIOCMNETWORK,(char*)NewToActive);
  37.    filter_command(DIOCSTART,NULL);
  38.    delete NewToActive;
  39. }
  40. VOID SetFilterlogMask()
  41. {
  42.    return;
  43. }
  44. int SetDmIpTab(CPtrList &listRejIp,UCHAR type)
  45. {
  46.    int tt=sizeof(addrTreeNode);
  47.    rejaccTableReq  *pRej=new rejaccTableReq;
  48.    int count=listRejIp.GetCount();
  49.    pRej->ptr=new RejAccTableEntry[count];
  50.    struct IpaddrRange *iprang;
  51.    POSITION pos;
  52.    int i=0;
  53.    pos=listRejIp.GetHeadPosition();
  54.    while(pos!=NULL)
  55.    {
  56.    iprang=(struct IpaddrRange *)listRejIp.GetNext(pos);
  57.    pRej->ptr[i].network.ss_addr=ntohl(iprang->nIpAddr);
  58.    pRej->ptr[i].bits=iprang->nIpaddrNum;
  59.    pRej->ptr[i].flag=1;//恒定
  60.    i++;
  61.    }
  62.  
  63.    pRej->addr=0;//无用!!
  64.    pRej->bytes=count*(sizeof(RejAccTableEntry));//bytes 数
  65.    int ret;
  66.    switch(type)
  67.    {
  68.    case T_INDM:
  69.    ret= filter_command(DIOCS_INDM,(char*)pRej);
  70.    break;
  71.    case T_OUTDM:
  72.    ret= filter_command(DIOCS_OUTDM,(char*)pRej);
  73.    break;
  74.    }
  75.    delete pRej->ptr;
  76.    delete pRej;
  77.    return ret;
  78. }
  79. int SetFilterAcceptTab(CPtrList &listRejIp)
  80. {
  81.    int tt=sizeof(addrTreeNode);
  82.    rejaccTableReq  *pRej=new rejaccTableReq;
  83.    int count=listRejIp.GetCount();
  84.    pRej->ptr=new RejAccTableEntry[count];
  85.    struct IpaddrRange *iprang;
  86.    POSITION pos;
  87.    int i=0;
  88.    pos=listRejIp.GetHeadPosition();
  89.    while(pos!=NULL)
  90.    {
  91.    iprang=(struct IpaddrRange *)listRejIp.GetNext(pos);
  92.    pRej->ptr[i].network.ss_addr=ntohl(iprang->nIpAddr);
  93.    pRej->ptr[i].bits=iprang->nIpaddrNum;
  94.    pRej->ptr[i].flag=1;//恒定
  95.    i++;
  96.    }
  97.    pRej->bytes=count*(sizeof(RejAccTableEntry));//bytes 数
  98.    int ret= filter_command(DIOCSREJECT,(char*)pRej);
  99.    delete pRej->ptr;
  100.    delete pRej;
  101.    return ret;
  102. }
  103. int SetFilterRejTab(CPtrList &listRejIp)
  104. {
  105.    int tt=sizeof(addrTreeNode);
  106.    rejaccTableReq  *pRej=new rejaccTableReq;
  107.    int count=listRejIp.GetCount();
  108.    pRej->ptr=new RejAccTableEntry[count];
  109.    struct IpaddrRange *iprang;
  110.    POSITION pos;
  111.    int i=0;
  112.    pos=listRejIp.GetHeadPosition();
  113.    while(pos!=NULL)
  114.    {
  115.    iprang=(struct IpaddrRange *)listRejIp.GetNext(pos);
  116.    pRej->ptr[i].network.ss_addr=ntohl(iprang->nIpAddr);
  117.    pRej->ptr[i].bits=iprang->nIpaddrNum;
  118.    pRej->ptr[i].flag=1;//恒定
  119.    i++;
  120.    }
  121.    pRej->bytes=count*(sizeof(RejAccTableEntry));//bytes 数
  122.    int ret=filter_command(DIOCSACCEPT,(char*)pRej);
  123.    delete  []pRej->ptr;
  124.    delete pRej;
  125.    return ret;
  126. }
  127. //设置端口列表请求的同时设置地址表
  128. //通过该地址树查找该地址所对应的端口列表所在的位置
  129. VOID SetFilterAddrTree(struct IpaddrRange iprange,USHORT group)
  130. {
  131.    addrTreeReq *paddrTree=new addrTreeReq;
  132.    paddrTree->num=0;//节点的总数
  133.    paddrTree->addr[paddrTree->num].ss_addr=ntohl(iprange.nIpAddr);
  134.   
  135.    paddrTree->bits[paddrTree->num]=iprange.nIpaddrNum;
  136.    paddrTree->group[paddrTree->num]=group;//所属的主机类
  137.    paddrTree->num++;
  138.    filter_command(DIOCSNETWORK,(char*)paddrTree);
  139.    delete paddrTree;
  140. }
  141. //END????????????????????????????????????????????????
  142. //对于设置的每一台主机分别调用;
  143. VOID SetFilterPortList(struct  HostList* phostlist,UCHAR type,unsigned short group)
  144. {
  145.    
  146.    portListReq *  portlistreq=new  portListReq; 
  147.    portlistreq->group=group;
  148.    switch (type) 
  149.    {
  150. case TCPSRCOUT:  
  151. {
  152.    ULONG num=phostlist->sTcpPortSet.sAllowOutSrcPortList.GetCount();
  153.    struct PortList *portlist;
  154.        portlistreq->ptr=new PortListEntry[num];
  155.        portlistreq->id=T_SRC_OUT;
  156.    int i=0;
  157.    POSITION pos;
  158.    pos=phostlist->sTcpPortSet.sAllowOutSrcPortList.GetHeadPosition();
  159.    while(pos!=NULL)
  160.    {
  161.   portlist=(struct PortList *)phostlist->sTcpPortSet.sAllowOutSrcPortList.GetNext(pos);
  162.   if(portlist->bRange)
  163.   {
  164.   portlistreq->ptr[i].begin=portlist->sPortRange.nPortBegin;
  165.   portlistreq->ptr[i].end=portlist->sPortRange.nPortEnd;
  166.   }
  167.   else
  168.   {
  169.   if(portlist->nPort==0)
  170.   {
  171.   num--;
  172.    i--;
  173.   }
  174.   else
  175.   {
  176.   portlistreq->ptr[i].begin=portlist->nPort;
  177.   portlistreq->ptr[i].end=portlist->nPort;
  178.   }
  179.   }
  180.   i++;
  181.    }
  182.         portlistreq->bytes=num*sizeof(PortListEntry);
  183.        filter_command(DIOCSGROUP,(char*)portlistreq);
  184. delete []portlistreq->ptr;
  185.     delete portlistreq;
  186. }
  187.         break;
  188. case TCPDSTOUT:  
  189. {
  190.    ULONG num=phostlist->sTcpPortSet.sAllowOutDstPortList.GetCount();
  191.    struct PortList *portlist;
  192.        portlistreq->ptr=new PortListEntry[num];
  193.        portlistreq->id=T_DST_OUT;
  194.    int i=0;
  195.    POSITION pos;
  196.    pos=phostlist->sTcpPortSet.sAllowOutDstPortList.GetHeadPosition();
  197.    while(pos!=NULL)
  198.    {
  199.   portlist=(struct PortList *)phostlist->sTcpPortSet.sAllowOutDstPortList.GetNext(pos);
  200.   if(portlist->bRange)
  201.   {
  202.   portlistreq->ptr[i].begin=portlist->sPortRange.nPortBegin;
  203.   portlistreq->ptr[i].end=portlist->sPortRange.nPortEnd;
  204.   }
  205.   else
  206.   {
  207.   if(portlist->nPort==0)
  208.   {
  209.   num--;
  210.    i--;
  211.   }
  212.   else
  213.   {
  214.   portlistreq->ptr[i].begin=portlist->nPort;
  215.   portlistreq->ptr[i].end=portlist->nPort;
  216.   }
  217.   }
  218.   i++;
  219.    }
  220.        portlistreq->bytes=num*sizeof(PortListEntry);
  221.        filter_command(DIOCSGROUP,(char*)portlistreq);
  222. delete []portlistreq->ptr;
  223.     delete portlistreq;
  224. }
  225.         break;
  226. case UDPSRCOUT:  
  227. {
  228.    ULONG num=phostlist->sUdpPortSet.sAllowOutSrcPortList.GetCount();
  229.    struct PortList *portlist;
  230.        portlistreq->ptr=new PortListEntry[num];
  231.        portlistreq->id=U_SRC_OUT;
  232.    int i=0;
  233.    POSITION pos;
  234.    pos=phostlist->sUdpPortSet.sAllowOutSrcPortList.GetHeadPosition();
  235.    while(pos!=NULL)
  236.    {
  237.   portlist=(struct PortList *)phostlist->sUdpPortSet.sAllowOutSrcPortList.GetNext(pos);
  238.   if(portlist->bRange)
  239.   {
  240.   portlistreq->ptr[i].begin=portlist->sPortRange.nPortBegin;
  241.   portlistreq->ptr[i].end=portlist->sPortRange.nPortEnd;
  242.   }
  243.   else
  244.   {
  245.   if(portlist->nPort==0)
  246.   {
  247.   num--;
  248.   i--;
  249.   }
  250.   else
  251.   {
  252.   portlistreq->ptr[i].begin=portlist->nPort;
  253.   portlistreq->ptr[i].end=portlist->nPort;
  254.   }
  255.   }
  256.   i++;
  257.    }
  258.        portlistreq->bytes=num*sizeof(PortListEntry);
  259.        filter_command(DIOCSGROUP,(char*)portlistreq);
  260. delete []portlistreq->ptr;
  261.     delete portlistreq;
  262. }
  263.         break;
  264. case UDPDSTOUT:  
  265. {
  266.    ULONG num=phostlist->sUdpPortSet.sAllowOutDstPortList.GetCount();
  267.    struct PortList *portlist;
  268.        portlistreq->ptr=new PortListEntry[num];
  269.        portlistreq->id=U_DST_OUT;
  270.    int i=0;
  271.    POSITION pos;
  272.    pos=phostlist->sUdpPortSet.sAllowOutDstPortList.GetHeadPosition();
  273.    while(pos!=NULL)
  274.    {
  275.   portlist=(struct PortList *)phostlist->sUdpPortSet.sAllowOutDstPortList.GetNext(pos);
  276.   if(portlist->bRange)
  277.   {
  278.   portlistreq->ptr[i].begin=portlist->sPortRange.nPortBegin;
  279.   portlistreq->ptr[i].end=portlist->sPortRange.nPortEnd;
  280.   }
  281.   else
  282.   {
  283.   if(portlist->nPort==0)
  284.   {
  285.   num--;
  286.      i--;
  287.   }
  288.   else
  289.   {
  290.   portlistreq->ptr[i].begin=portlist->nPort;
  291.   portlistreq->ptr[i].end=portlist->nPort;
  292.   }
  293.   }
  294.   i++;
  295.    }
  296.        portlistreq->bytes=num*sizeof(PortListEntry);
  297.        filter_command(DIOCSGROUP,(char*)portlistreq);
  298. delete []portlistreq->ptr;
  299.     delete portlistreq;
  300. }
  301.         break;
  302. case ICMPTYPEIN:
  303. {
  304.    ULONG num=phostlist->sInIcmpSet.GetCount();
  305.    struct IcmpList *portlist;
  306.        portlistreq->ptr=new PortListEntry[num];
  307.        portlistreq->id=I_TYP_IN;
  308.    int i=0;
  309.    POSITION pos;
  310.    pos=phostlist->sInIcmpSet.GetHeadPosition();
  311.    while(pos!=NULL)
  312.    {
  313.   portlist=(struct IcmpList *)phostlist->sInIcmpSet.GetNext(pos);
  314.   if(portlist->bRange)
  315.   {
  316.   portlistreq->ptr[i].begin=portlist->sIcmpRange.nIcmpBegin;
  317.   portlistreq->ptr[i].end=portlist->sIcmpRange.nIcmpEnd;
  318.   }
  319.   else
  320.   {
  321.    if(portlist->nIcmpType==0)
  322.   {
  323.   num--;
  324.      i--;
  325.   }
  326.   else
  327.   {
  328.   //ICM类型规定从1开始!!!因此减一
  329.   portlistreq->ptr[i].begin=portlist->nIcmpType-1;
  330.   portlistreq->ptr[i].end=portlist->nIcmpType-1;
  331.   }
  332.   }
  333.   i++;
  334.    }
  335.        portlistreq->bytes=num*4;
  336.        filter_command(DIOCSGROUP,(char*)portlistreq);
  337. delete []portlistreq->ptr;
  338.     delete portlistreq;
  339. }
  340.         break;
  341. case ICMPTYPEOUT:
  342. {
  343.    ULONG num=phostlist->sOutIcmpSet.GetCount();
  344.    struct IcmpList *portlist;
  345.        portlistreq->ptr=new PortListEntry[num];
  346.        portlistreq->id=I_TYP_OUT;
  347.    int i=0;
  348.    POSITION pos;
  349.    pos=phostlist->sOutIcmpSet.GetHeadPosition();
  350.    while(pos!=NULL)
  351.    {
  352.   portlist=(struct IcmpList *)phostlist->sOutIcmpSet.GetNext(pos);
  353.   if(portlist->bRange)
  354.   {
  355.   portlistreq->ptr[i].begin=portlist->sIcmpRange.nIcmpBegin;
  356.   portlistreq->ptr[i].end=portlist->sIcmpRange.nIcmpEnd;
  357.   }
  358.   else
  359.   {
  360.   if(portlist->nIcmpType==0)
  361.   {
  362.   num--;
  363.      i--;
  364.   }
  365.   else   
  366.   {
  367.   //ICM类型规定从1开始!!!因此减一
  368.   portlistreq->ptr[i].begin=portlist->nIcmpType-1;
  369.   portlistreq->ptr[i].end=portlist->nIcmpType-1;
  370.   }
  371.   }
  372.   i++;
  373.    }
  374.           portlistreq->bytes=num*4;
  375.        filter_command(DIOCSGROUP,(char*)portlistreq);
  376. delete []portlistreq->ptr;
  377.     delete portlistreq;
  378. }
  379.         break;
  380. default:
  381.      delete portlistreq;
  382. return;
  383.    }
  384. }
  385. //首先必须调用次函数
  386. VOID SetFilterInitReq(struct FireWallConfig  mycfg)
  387. {
  388. // initReq * inireq=new initReq;
  389. filter_command(DIOCINIT,(char*)NULL);
  390. // delete inireq;
  391. }
  392. VOID SetFilterCfg(struct FireWallConfig  mycfg,struct GlobalFlags myflags)
  393. {
  394.    filtercfg.discardAttackICMP=myflags.bSusPectIcmp;
  395.    filtercfg.discardIcmp=myflags.bforbidIcmp;
  396.    filtercfg.discardFragmentedICMP=myflags.bFragmentIcmp;
  397.    filtercfg.discardMulticast=myflags.bMulticast;
  398.    filtercfg.discardNonIp=myflags.bNonIpPacket;
  399.    filtercfg.discardOtherIp=myflags.bOtherIpPacket;
  400. //   filtercfg.discardRouteIp=myflags.现无
  401.    filtercfg.discardSuspectOffset=myflags.bSusPectOffset;
  402.    filtercfg.dm_ip=ntohl(mycfg.nMiddleIp);
  403.    filtercfg.dm_mask=ntohl(mycfg.nMiddleMask);
  404.    filtercfg.dm_number=DMNO-0X30;//mycfg.MdNumber;//for test!!
  405.    filtercfg.in_number=INNO-0X30;//mycfg.InNumber;
  406.    filtercfg.in_ip=ntohl(mycfg.nIntraIp);
  407.    filtercfg.in_mask=ntohl(mycfg.nIntraMask);
  408.    filtercfg.logMask=TheLogManage.TheIpFilter.Options|0XFFFE0000;//根据日志管理器设置
  409.    filtercfg.media_type=IFT_ETHER;//现在硬编码在此:以太
  410.    filtercfg.out_ip=ntohl(mycfg.nOutsideIp);
  411.    filtercfg.out_mask=ntohl(mycfg.nOutsideMask);
  412.    filtercfg.out_number=OUTNO-0X30;//mycfg.OutNumber;
  413. }
  414. VOID ClearStatisticsInfo()
  415. {
  416. filter_command(DIOCCSTATS,(char*)NULL);
  417. }