policy.cpp

来自「特别好玩有启发的东西」· C++ 代码 · 共 1,712 行 · 第 1/3 页

CPP
1,712
字号
/******************************************************
funtion:	initialize the module's policy.
date   :	2007-12
corp   :   kinghi.
******************************************************/

#include <stdlib.h>

#include "policy.h"
#include "log.h"

/*****************************************************************************
name		:fltKeyWord
usage		:initialize the keyword filter struct
calls			:getModAndFuncType()
be called		:.......	
table visited:	:KeyWordFilter
table repaired	:none;
input		:CDB_Operator,vector<std::string>,KeyWordType,eModuleType,FucType
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int fltKeyWord(CDB_Operator*  dbop,vector<CkeyWord>& tKeyWrdVec,
				 KeyWordType eKeyWordType,PluginType ePlugin,FuncType eFucType)
{
	//initialize the keyword filter policy.
	//keyword table.
	
	std::string strSqlExec;//construct the select sentence.
	
	/*select sentence example:
	"select Keyword FilterRuleID from KeywordFilter k where k.KeywordTypeID = 1 and k.PTID = 
		(select ID from PluginAndType pl where pl.PluginID = 1 and pl.PluginTypeID = 1); "
	*/
	
	strSqlExec = "select Keyword FilterRuleID from KeywordFilter k where k.KeywordTypeID = ";
	
	char* szBufTmp = (char*)malloc(2048);
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eKeyWordType);
	strSqlExec +=  szBufTmp;
	
	strSqlExec += " ";
	
	strSqlExec += "and k.PTID =  (select ID from PluginAndType pl where pl.PluginID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",ePlugin);
	strSqlExec +=  szBufTmp;
	strSqlExec += " and pl.eFucType = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eFucType);
	strSqlExec += szBufTmp;

	strSqlExec += ")";
	
	memset(szBufTmp,0,2048);
	strcpy(szBufTmp,strSqlExec.c_str());
	if(! dbop->exec_sql(szBufTmp)){
		//relative query.
		//get all records from table keywordfilter.
		free(szBufTmp);
		return -1;
	}
	free(szBufTmp);

	int iTotalRcd = 0;
	iTotalRcd = dbop->get_line_value();
	if(0 == iTotalRcd)
		return -1;
	
	int i = 0;
	char* szkeyWrdFldName[2] ={"Keyword","FilterRuleID"};//"KeywordTypeID","FilterRuleID"};
	std::vector<std::string> vecKeyWrdfld(szkeyWrdFldName,szkeyWrdFldName+2);
	std::vector<std::string> vecKeyWrdval;
	CkeyWord stKeyWrdTmp;
	for(;i < iTotalRcd; ++i){
			dbop->MoveToLine(i);
			
			dbop->GetMutliFieldValueByName(vecKeyWrdfld,vecKeyWrdval);
			stKeyWrdTmp.strKeyWord = vecKeyWrdval.operator[](0);
			stKeyWrdTmp.eFiltRule = (FiltRule)atoi(vecKeyWrdval.operator[](1).c_str());
			
			tKeyWrdVec.push_back(stKeyWrdTmp);	
	}//end for

	return NS_SUCCEEDED;
}



/*****************************************************************************
name		:fltTime
usage		:initialize the time filter policy 
calls			:none
be called		:every plugin policy load();
table visited:	:"TimeFilter"
table repaired	:none;
input		:CDB_Operator,vector<std::string>,eModuleType,FucType
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int fltTime(CDB_Operator* dbop ,CStyTime& tTimeSeg, PluginType ePlugin,FuncType eFuncType)
{
	//configure the time segment
	//table : TimeFilter.
	//select example:
	/* "select Time from TimeFilter tm where tm.PTID = (select ID from PluginAndType pl where
		pl.PluginID = 1 and pl.PluginTypeID = 1)"
	*/
	
	char* szBufTmp = (char*)malloc(2048);
	std::string strSqlExec;	
	
	strSqlExec +=  "select Time from TimeFilter tm where tm.PTID = (select ID from PluginAndType\
			 pl where pl.PluginID =";
			 
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",ePlugin);
	strSqlExec  += szBufTmp;
	
	strSqlExec  += " ";
	
	strSqlExec  += "and pl.PluginTypeID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eFuncType);
	strSqlExec  += szBufTmp;

	strSqlExec += ")";
	
	memset(szBufTmp,0,2048);
	strcpy(szBufTmp,strSqlExec.c_str());
	
	if( true  !=  dbop->exec_sql(szBufTmp)){//do relative query
		free(szBufTmp);
		return -1;
	}
	free(szBufTmp);
	int iTotalRcd;	
	if( 1  !=  (iTotalRcd = dbop->get_line_value()))
		return -1;
	
	int i;
	std::string strTimeSeg;
	char* szFldName[1] = {"Time"};
	std::vector<std::string> strFldName(szFldName,szFldName+1);
	std::vector<std::string> strFldVal;
	bool bret = false;
	for(i = 0; i < iTotalRcd; ++i){
		bret = dbop->GetMutliFieldValueByName(strFldName, strFldVal);
		if(!bret)
			return -1;
		strTimeSeg = strFldVal.operator[](0);
		tTimeSeg.SetTimeSeg(strTimeSeg);
	}			
	return 0;
}



/*****************************************************************************
name		:fltUrl
usage		:initialize the Url filter vector. 
calls			:none
be called		:every plugin policy;
table visited:	:"URLFilter"
table repaired	:none;
input		:CDB_Operator,vector<std::string>,PluginType,FuncType;
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int fltUrl(CDB_Operator* dbop ,vector<CkeyWord>& tUrlVec,
		  PluginType ePlugin ,	FuncType eFuncType)
{
	/* database select example:
	    "select URL FlterRuleID from URLFilter uf where uf.PTID = (select ID from PluginAndType pl 
	    where pl.PluginID = 1 and pl.PluginTypeID =1)"
	*/
	
	char* szBufTmp = (char*)malloc(2048);//enough space ?	
	std::string strSqlExec;

	strSqlExec += "select URL FlterRuleID from URLFilter uf where uf.PTID =   \
		      (select ID from PluginAndType pl where pl.PluginID = ";
		      
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",ePlugin);
	strSqlExec += szBufTmp;
	
	strSqlExec += " and pl.PluginTypeID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eFuncType);
	strSqlExec += szBufTmp;

	strSqlExec += ")";
	
	memset(szBufTmp,0,2048);
	strcpy(szBufTmp,strSqlExec.c_str());

	if(true !=  dbop->exec_sql(szBufTmp)){
		free(szBufTmp);
		return -1;
	}
	free(szBufTmp);	
	int iTotalRcd;
	iTotalRcd = dbop->get_line_value();
	if(iTotalRcd < 1)
		return -1;

	int i;
	char *szFieldName[2] = {"URL","FlterRuleID"};
	vector<std::string> vecFieldName(szFieldName,szFieldName+2);
	vector<std::string> vecFieldVal;
	CkeyWord stKeyWrdTmp;
	for(i = 0; i < iTotalRcd; ++i){
			if(! dbop->MoveToLine(i))
				return -1;
			if(! dbop->GetMutliFieldValueByName(vecFieldName, vecFieldVal))
				return -1;
			stKeyWrdTmp.strKeyWord = vecFieldVal.operator[](0);
			stKeyWrdTmp.eFiltRule = (FiltRule)atoi(vecFieldVal.operator[](1).c_str());
			tUrlVec.push_back(stKeyWrdTmp);
	}
	return 0;	
}




/*****************************************************************************
name		:fltIP
usage		:initialize the Url filter vector. 
calls			:none
be called		:every plugin policy;
table visited:	:"IPFilter",     #server ip.
table repaired	:none;
input		:CDB_Operator,vector<std::string>,PluginAndType( = PluginType,FuncType);
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int fltIP(CDB_Operator* dbop ,vector<CkeyWord> &tIPvec,PluginType ePlugin,FuncType eFuncType)
{
	/* database select example:
	    "select IP FilterRuleID from IPFilter ip where ip.PTID = (select ID from PluginAndType pl 
	    where pl.PluginID = 1  and pl.PluginTypeID =1)"
	*/	
	char* szBufTmp = (char*)malloc(2048);//enough space ?	
	std::string strSqlExec;

	strSqlExec += "select IP FilterRuleID from IPFilter ip where ip.PTID =(select ID\
				from PluginAndType pl where pl.PluginID = ";
				
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",ePlugin);
	strSqlExec += szBufTmp;
	
	strSqlExec += " and pl.PluginTypeID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eFuncType);
	strSqlExec += szBufTmp;

	strSqlExec += ")";
	
	memset(szBufTmp,0,2048);
	strcpy(szBufTmp,strSqlExec.c_str());

	if(true !=  dbop->exec_sql(szBufTmp)){
		free(szBufTmp);
		return -1;
	}
	free(szBufTmp);

	int iTotalRcd;
	iTotalRcd = dbop->get_line_value();
	if(iTotalRcd < 1)
		return -1;

	int i;
	char *szFieldName[2] = {"IP","FilterRuleID"};
	vector<std::string> vecFieldName(szFieldName,szFieldName+2);
	vector<std::string> vecFieldVal;
	CkeyWord stKeyWrdTmp;
	for(i = 0; i < iTotalRcd; ++i){
			if(! dbop->MoveToLine(i))
				return -1;
			if(! dbop->GetMutliFieldValueByName(vecFieldName, vecFieldVal))
				return -1;
			stKeyWrdTmp.strKeyWord = vecFieldVal.operator[](0);
			stKeyWrdTmp.eFiltRule = (FiltRule)atoi(vecFieldVal.operator[](1).c_str());
			
			tIPvec.push_back(stKeyWrdTmp);
	}	
	return 0;
}



/*****************************************************************************
name		:fltPort
usage		:initialize the Url filter vector. 
calls			:none
be called		:every plugin policy;
table visited:	:"PortFilter"
table repaired	:none;
input		:CDB_Operator,vector<std::string>,PluginAndType( = PluginType,FuncType);
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int fltPort(CDB_Operator*  dbop,vector<CkeyWord>& tPortVec,PluginType ePlugin,FuncType eFuncType)
{
	/* database select example:
	    "select Port from PortFilter pt where pt.PTID = (select ID from PluginAndType pl where pl.PluginID = 1
	    	and pl.PluginTypeID =1)"
	*/	
	char* szBufTmp = (char*)malloc(2048);//enough space ?
	std::string strSqlExec;

	strSqlExec += "select Port  FilterRuleID from PortFilter pt where pt.PTID =\
		      (select ID from PluginAndType pl where pl.PluginID = ";
		      
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",ePlugin);
	strSqlExec += szBufTmp;
	
	strSqlExec += " and pl.PluginTypeID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eFuncType);
	strSqlExec += szBufTmp;

	strSqlExec += ")";
	
	memset(szBufTmp,0,2048);
	strcpy(szBufTmp,strSqlExec.c_str());

	if(! dbop->exec_sql(szBufTmp)){
		free(szBufTmp);
		return -1;
	}
	free(szBufTmp);

	int iTotalRcd;
	iTotalRcd = dbop->get_line_value();
	if(1 > iTotalRcd)
		return -1;

	int i;
	char *szFieldName[2] = {"Port","FilterRuleID"};
	vector<std::string> vecFieldName(szFieldName,szFieldName+2);
	vector<std::string> vecFieldVal;
	CkeyWord stKeyWrdTmp;
	for(i = 0; i < iTotalRcd; ++i){
			if(! dbop->MoveToLine(i))
				return -1;
			if(! dbop->GetMutliFieldValueByName(vecFieldName, vecFieldVal))
				return -1;
			stKeyWrdTmp.strKeyWord = vecFieldVal.operator[](0);
			stKeyWrdTmp.eFiltRule = (FiltRule)atoi(vecFieldVal.operator[](1).c_str());
			
			tPortVec.push_back(stKeyWrdTmp);
	}	
	return 0;
}



/*****************************************************************************
name		:fltAddress
usage		:initialize the Url filter vector. 
calls			:none
be called		:every plugin policy;
table visited:	:"AddressFilter"
table repaired	:none;
input		:CDB_Operator,vector<std::string>,PluginAndType( = PluginType,FuncType);
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int fltAddress(CDB_Operator*  dbop,vector<CkeyWord>& tAddrVec,EmlAddrType eEmlAddrType,
				PluginType ePlugin,FuncType eFuncType)
{
	/* database select example:
	    "select Address  FilterRuleID from AddressFilter ad where ad.AddressTypeID = 1 
	    and  pt.PTID = (select ID from PluginAndType pl where pl.PluginID = 1
 	    and pl.PluginTypeID =1)"
	*/	
	char* szBufTmp = (char*)malloc(2048);//enough space ?	
	std::string strSqlExec;

	strSqlExec += "select Address FilterRuleID  from AddressFilter ad where ad.AddressTypeID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eEmlAddrType);
	strSqlExec += szBufTmp;
	
	strSqlExec += " and pt.PTID = (select ID from PluginAndType pl where pl.PluginID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",ePlugin);
	strSqlExec += szBufTmp;
	
	strSqlExec += " and pl.PluginTypeID = ";
	
	memset(szBufTmp,0,2048);
	sprintf(szBufTmp,"%d",eFuncType);
	strSqlExec += szBufTmp;

	strSqlExec += ")";
	
	memset(szBufTmp,0,2048);
	strcpy(szBufTmp,strSqlExec.c_str());

	if(true !=  dbop->exec_sql(szBufTmp)){
		free(szBufTmp);
		return -1;
	}
	free(szBufTmp);

	int iTotalRcd;
	iTotalRcd = dbop->get_line_value();
	if(iTotalRcd < 1)
		return -1;

	int i;
	char *szFieldName[2] = {"Port","FilterRuleID"};
	vector<std::string> vecFieldName(szFieldName,szFieldName+1);
	vector<std::string> vecFieldVal;
	CkeyWord stKeyWrdTmp;
	for(i = 0; i < iTotalRcd; ++i){
			if(! dbop->MoveToLine(i))
				return -1;
			if(! dbop->GetMutliFieldValueByName(vecFieldName, vecFieldVal))
				return -1;
			stKeyWrdTmp.strKeyWord = vecFieldVal.operator[](0);
			stKeyWrdTmp.eFiltRule = (FiltRule)atoi(vecFieldVal.operator[](1).c_str());
			
			tAddrVec.push_back(stKeyWrdTmp);
	}	
	return 0;
}





/*****************************************************************************
name		:CHttpPubPolicy::Load
usage		:initialize the http proxy policy. 
calls			:none
be called		:netstrobe main frame;
table visited:	:none
table repaired	:none;
input		:CDB_Operator
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int CHttpPubPolicy::Load( CDB_Operator*  dbop, char **pResult)
{//intialize the http-pub policy
  //a-TimeOut
  //b-IsContent
  //c-IsAuthenticate
  //d-IsTimeFilter
  //e-IsKeyWordFilter
  //f-IsService
  
 //1-CHttpPubPolicy table
	if(!dbop->exec_sql("select * from HTTPPub"))
		return -1;
	if(dbop->IsEmpty())
		return -1;	

	if(1  !=  dbop->get_line_value())//how many records?
		return -1;

	//HTTPProxy policy initializing:9 bools
	int col[6] = {2,3,4,5,6,7};
		std::string strTmp;
	std::vector<int> vecColNo(col,col+6);
	std::vector<std::string> vecHttpPubValue;
	dbop->GetMultiFieldValue(vecColNo,vecHttpPubValue);	

	//TimeOut
	strTmp = vecHttpPubValue.operator[](2);
	if(NULL ==strTmp.c_str())
		return -1;
	else TimeOut = (short)atoi(strTmp.c_str());//should judge the validity of the string?
	
	//brecord:whether to record the content
	strTmp = vecHttpPubValue.operator[](3);
	if(NULL == strTmp.c_str())
		return -1;
	else bRecord = atoi(strTmp.c_str());
	
	//bAuth:should verify the user's identity?
	strTmp = vecHttpPubValue.operator[](4);
	if(NULL == strTmp.c_str())
		return -1;
	else bAuth = atoi(strTmp.c_str());
	
	//bTime
	strTmp = vecHttpPubValue.operator[](5);
	if(NULL == strTmp.c_str())
		return -1;
	else bTime = atoi(strTmp.c_str());
	
	//bKeyWordFlt
	strTmp = vecHttpPubValue.operator[](6);
	if(NULL == strTmp.c_str())
		return -1;
	else bKeyWordFlt= atoi(strTmp.c_str());

	//isSeviece(bUse)
	strTmp = vecHttpPubValue.operator[](7);
	if(NULL == strTmp.c_str())
		return -1;
	else bUse = atoi(strTmp.c_str());
//2-time segment
	if(bTime)
		if(0 != fltTime(dbop,tTimeSegt,HTTP,PUB))
			return -1;
//3-content key words filter
	if(bKeyWordFlt)
		if(0 !=   fltKeyWord(dbop,tKeyWordVec,CONTENT,HTTP,PUB))
			return -1;
	return 0;  	
}


void CHttpPubPolicy::Clear()
{// clear the vector?
	tKeyWordVec.clear();
}
	


/*****************************************************************************
name		:CHttpProxyPolicy::Load
usage		:initialize the http proxy policy. 
calls			:none
be called		:netstrobe main frame;
table visited:	:none
table repaired	:none;
input		:CDB_Operator
output		:none
return value	:0-runing succeed; 1-runing failed.
others		:none.
*****************************************************************************/
int CHttpProxyPolicy::Load(CDB_Operator*  dbop, char * * pResult)
{
//initialize the CHttpProxyPolicy alone.  param pResult is not used now.
 //a-time;
 //b-keyword:content;
 //c-Url
 //d-Ip
 //e-port
 //policy talbe-------total five.
 
 //1-CHttpProxyPolicy  table
	if(! dbop->exec_sql("select * from HTTPProxy"))
		return -1;
	if(dbop->IsEmpty())

⌨️ 快捷键说明

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