Exemplo n.º 1
0
STDMETHODIMP CXRecords::Sort(VARIANT key, VARIANT varAsc)
{
	if(!m_pFields)return SetErrorInfo(s_errInit);

	int pos, bAsc;

	int count = m_listRecords->GetCount();

	if(count == 0)
		return S_OK;

	pos = m_pFields->FindField(key);
	if(pos < 0 || pos >= (int)m_pFields->GetCount())
		return DISP_E_BADINDEX;

	bAsc = varGetNumbar(varAsc, 1);

	s_csSort.Enter();
	s_posSort = pos;
	s_bAscSort = bAsc ? 1 : -1;

	qsort(&m_listRecords->GetValue(0), count, sizeof(CXComPtr<CXRecord>), sortProc);

	s_csSort.Leave();

	return S_OK;
}
		bool __cdecl ExecuteSimulatedAnnealing(
			__int32 sessionId,
			IGenericStream* inStream,
			IGenericStream* outStream)
		{
			CS.Enter();
			CSessionDataCache::iterator i = sessionDataCache.find(sessionId);
			_ASSERTE(i != sessionDataCache.end());
			CS.Leave();

			TSessionData& sd = i->second;

			__int8 bDirectAccess = false;
			inStream->Read(&bDirectAccess, sizeof(bDirectAccess));


			//here we are not suppose to more than obne simulation in the same loc
			unsigned __int64 locNo = 0;
			inStream->Read(&locNo, sizeof(locNo));
			ASSERT(locNo < sd.m_modelVector.size());
			unsigned __int64 XSize = 0;
			inStream->Read(&XSize, sizeof(XSize));
			ASSERT(XSize < (long)100);

			vector<double> paramArray(XSize);
			for (size_t i = 0; i < XSize; i++)
				inStream->Read(&paramArray[i], sizeof(paramArray[i]));

			//LeaveCriticalSection(&CS);

			CStatisticXY stat;
			sd.m_modelVector[locNo]->GetFValue(paramArray, stat);

			//write outputStream
			outStream->Write(&sessionId, sizeof(sessionId));
			outStream->Write(&locNo, sizeof(locNo));
			outStream->Write(&stat, sizeof(stat));


			return true;
		}
Exemplo n.º 3
0
STDMETHODIMP SimpleKfwEvent::OnApplicationRequest_( APP_REQUEST_INFO  *pAppRequestInfo, PULONG pAllow )
{
	DWORD dwResult = 0;
	DWORD dwResponseCode = Pass;
	char szCmdLine[4096] = {0};
	PROCESS_INFORMATION ProcessInfo;
	STARTUPINFOA StartupInfo;
	string szRequestType;
	string szRequestProtocol;
	string sModulePath;
	wstring wsExePath;
	int iret = 0;
	const int DenyCode = 'Deny';
	const int AlwaysDenyCode = 'alDy';
	const int AlwaysPassCode = 'alPs';
	const int PassRecordCode = 'Psrc';
	const int AlwaysPassRecordCode = 'alPr';

	if ( pAppRequestInfo == NULL || pAllow == NULL )
		return E_FAIL;
	
	switch( pAppRequestInfo->nRequestProtocol )
	{
	case enumPT_TCP:
		szRequestProtocol = "TCP";
		break;
	case enumPT_UDP:
		szRequestProtocol = "UDP";
		break;
	case enumPT_RAWIP:
		szRequestProtocol = "RAWIP";
		break;
	case enumPT_LANMAN:
		szRequestProtocol = "LAN";
		break;
	case enumPT_HTTP:
		szRequestProtocol = "HTTP";
		break;
	case enumPT_FILE_MON:
		szRequestProtocol = "FILE_MON";
		break;
	}

	switch( pAppRequestInfo->nRequestType )
	{
	case ART_Connect:
		szRequestType = "ART_Connect";	
		break;
	case ART_Listen:
		szRequestType = "ART_Listen";	
		break;
	case ART_Accept:
		szRequestType = "ART_Accept";	
		break;
	case ART_Bind:
		szRequestType = "ART_Bind";	
		break;
	case ART_Close:
		szRequestType = "ART_Close";	
		break;
	case ART_Create:
		szRequestType = "ART_Create";	
		break;
	case ART_TrustUrl:
		szRequestType = "ART_TrustUrl";
		break;
	case ART_FileChanged:
		szRequestType = "ART_FileChanged";
		break;
	case ART_PacketRecord:
		szRequestType = "ART_PacketRecord";
		break;
	default:
		szRequestType = "Unknown Request";
	}
		

	if( pAppRequestInfo->nRequestType == ART_PacketRecord )
	{
		DWORD lip = pAppRequestInfo->Parameters.TDI.dwLocalAddress;
		SHORT lport = pAppRequestInfo->Parameters.TDI.wLocalPort;
		
		DWORD rip = pAppRequestInfo->Parameters.TDI.dwRemoteAddress;
		SHORT rport = pAppRequestInfo->Parameters.TDI.wRemotePort;
		g_csOutput.Enter();
		printf( "-------------------------------------\n" );
		printf( "Direction:%s\n", pAppRequestInfo->Parameters.TDI.uPacketDir == enumPD_Send ? "Send" : "Recv"  );
		printf( "ART_PacketRecord: PackSize:%d\n", pAppRequestInfo->Parameters.TDI.uDataLen );
		printf( "LocalAddr:%02d.%02d.%02d.%02d:%d\n", 
			*(((BYTE*)&lip)+0), *(((BYTE*)&lip)+1), *(((BYTE*)&lip)+2), *(((BYTE*)&lip)+3), lport );
		printf( "RemoteAddr:%02d.%02d.%02d.%02d:%d\n", 
			*(((BYTE*)&rip)+0), *(((BYTE*)&rip)+1), *(((BYTE*)&rip)+2), *(((BYTE*)&rip)+3), rport );
		
		
		g_pcapRecorder.Record( TEXT("c:\\dns.pcap"), 
				(PROTO_TYPE)pAppRequestInfo->nRequestProtocol,
				(PACKET_DIR)pAppRequestInfo->Parameters.TDI.uPacketDir,
				lip, lport,
				rip, rport,
				pAppRequestInfo->Parameters.TDI.bData,
				pAppRequestInfo->Parameters.TDI.uDataLen );
		
		//g_dnsRecorder.Record( 
		//	(PROTO_TYPE)pAppRequestInfo->nRequestProtocol,
		//	(PACKET_DIR)pAppRequestInfo->Parameters.TDI.uPacketDir,
		//	lip, lport,
		//	rip, rport,
		//	pAppRequestInfo->Parameters.TDI.bData,
		//	pAppRequestInfo->Parameters.TDI.uDataLen );

		g_csOutput.Leave();

		return S_OK;
	}

	//获取堆栈模块
	sModulePath = "Parents:\n";
	if( !GetStackModule( pAppRequestInfo->dwParentModules, NULL, sModulePath, wsExePath ) )
	{
		//sModulePath = "NoFoundModle";
	}
	
	sModulePath += "StackModules:\n";
	//获取堆栈模块
	if( !GetStackModule( pAppRequestInfo->dwStackModules, pAppRequestInfo->dwStackRetAddr, sModulePath, wsExePath ) )
	{
		sModulePath = "NoFoundModle";
	}

	if( pAppRequestInfo->nRequestProtocol == enumPT_LANMAN )
	{
		string sPath;
		
		ConvertAnsi( pAppRequestInfo->Parameters.LanMan.wsPath, sPath );
		sprintf_s(szCmdLine, "fakepop.exe --ptype %s --t %s --pid %d --lan %s --m \n%s",
			szRequestProtocol.c_str(),
			szRequestType.c_str(), 
			pAppRequestInfo->dwProcessId,
			sPath.c_str(),
			sModulePath.c_str()
			);
	}
	else if( pAppRequestInfo->nRequestProtocol == enumPT_HTTP ) 
	{
		if( pAppRequestInfo->nRequestType == ART_TrustUrl )
		{
			//sprintf_s(szCmdLine, "fakepop.exe --ptype %s --t %s --pid %d --host %s --url %s --m \n%s",
			//	szRequestProtocol.c_str(),
			//	szRequestType.c_str(), 
			//	pAppRequestInfo->dwProcessId,
			//	pAppRequestInfo->Parameters.TrustUrl.szHost,
			//	pAppRequestInfo->Parameters.TrustUrl.szUrl,
			//	sModulePath.c_str()
			//	);
			USES_CONVERSION;

			ModuleInfo info = {0};
			info.dwModuleID = pAppRequestInfo->dwStackModules[0];

			if (g_pFwProxy)
				g_pFwProxy->GetModuleInfo( &info, 1 );

			char* pOpString = "UnkownOp";
			if (pAppRequestInfo->Parameters.TrustUrl.nOp == TRUST_URL_OP_HTTP_GET)
				pOpString = "Get";
			else if (pAppRequestInfo->Parameters.TrustUrl.nOp == TRUST_URL_OP_HTTP_POST)
				pOpString = "Post";

			sprintf_s( szCmdLine, "%s %s%s", 
				pOpString,
				pAppRequestInfo->Parameters.TrustUrl.szHost, 
				pAppRequestInfo->Parameters.TrustUrl.szUrl );
			printf( "find http request: %s:\n\t%s\n", W2A(info.wsModulePath), szCmdLine );

			/*
			if (_stricmp(pAppRequestInfo->Parameters.TrustUrl.szHost, "www.baidu.com") == 0)
			{
				printf("deny baidu:\n");
				*pAllow = Deny;
			}*/

			return S_OK;
		}
	}
	else 
	{
		if( pAppRequestInfo->nRequestType == ART_Connect )
		{
			USHORT localPort = 0;
			for( list<IRedirect*>::iterator item = g_listRedirect.begin(); 
						item != g_listRedirect.end();
						item ++ )
			{
				localPort = (*item)->NeedRedirect( wsExePath.c_str(), 
					pAppRequestInfo->Parameters.TDI.dwLocalAddress, pAppRequestInfo->Parameters.TDI.wLocalPort,
					pAppRequestInfo->Parameters.TDI.dwRemoteAddress, pAppRequestInfo->Parameters.TDI.wRemotePort );

				if( localPort != 0 )
				{
					*pAllow = MAKELONG( KfwRedirect, localPort );
					return S_OK;
				}
			}
		}

		sprintf_s(szCmdLine, "fakepop.exe --ptype %s --t %s --pid %d --lip %d --lport %d --rip %d --rport %d --m %s",
			szRequestProtocol.c_str(),
			szRequestType.c_str(), 
			pAppRequestInfo->dwProcessId,
			pAppRequestInfo->Parameters.TDI.dwLocalAddress, pAppRequestInfo->Parameters.TDI.wLocalPort,
			pAppRequestInfo->Parameters.TDI.dwRemoteAddress, pAppRequestInfo->Parameters.TDI.wRemotePort,
			sModulePath.c_str() );
	}


	ZeroMemory(&StartupInfo, sizeof(StartupInfo));
	StartupInfo.cb = sizeof StartupInfo ;

	if(CreateProcessA(NULL,szCmdLine,
		NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,
		NULL,&StartupInfo,&ProcessInfo))
	{	
		//printf( "\r\r\r\r\rPopRequest:%s\n", szCmdLine );
		WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
		GetExitCodeProcess( ProcessInfo.hProcess, &dwResult );

		CloseHandle(ProcessInfo.hThread);
		CloseHandle(ProcessInfo.hProcess);
	}
	else
	{
		//printf( "\r\r\r\r\rPopRequest:%s Error:%d\n", szCmdLine, GetLastError() );
	}
	//printf("KFW>>");
	
	if ( dwResult == DenyCode )
		dwResponseCode = Deny;
	else if( dwResult == AlwaysDenyCode )
		dwResponseCode = AlwaysDeny;
	else if( dwResult == AlwaysPassCode )
		dwResponseCode = AlwaysPass;

	*pAllow = dwResponseCode;

	if( pAppRequestInfo->nRequestProtocol == enumPT_TCP 
		|| pAppRequestInfo->nRequestProtocol == enumPT_UDP
		|| pAppRequestInfo->nRequestProtocol == enumPT_RAWIP )
	{
		KAppLogAdapter appLog( 
			pAppRequestInfo->dwStackModules[0], 
			pAppRequestInfo->nRequestProtocol,
			pAppRequestInfo->nRequestType,
			pAppRequestInfo->Parameters.TDI.dwLocalAddress, pAppRequestInfo->Parameters.TDI.wLocalPort,
			pAppRequestInfo->Parameters.TDI.dwRemoteAddress, pAppRequestInfo->Parameters.TDI.wRemotePort,
			GUID_NULL, 0, dwResponseCode, 0, 0 );

		//appLog.Do( m_logMgr );
	}
	else if(  pAppRequestInfo->nRequestProtocol == enumPT_LANMAN )
	{
		KLanmanLogAdapter lanmanLog( 
			pAppRequestInfo->dwStackModules[0], 
			pAppRequestInfo->Parameters.LanMan.wsPath,
			0, 
			dwResponseCode, 
			0, 
			0 );

		//lanmanLog.Do( m_logMgr );
	}

	return S_OK;
}
	ERMsg BeginSession(IAgent* agent, __int32 sessionId, DWORD hxGridSessionId, IGenericStream* globalDataStream = NULL)
	{
		ERMsg msg;
		//EnterCriticalSection(&sessionDataCache.m_CS);
		CS.Enter();

		CSessionDataCache::iterator i = sessionDataCache.find(sessionId);

		if (i == sessionDataCache.end())
		{
			//----------- read global data -------------
			//CStdString tmp;
			//tmp.Format("Before init\nsessionId = %d\nhxGridSessionId = %d",sessionId, hxGridSessionId);
			//MessageBox(NULL, (LPCTSTR)tmp, "BeginSession", MB_OK);   
			if (globalDataStream == NULL)
			{
				ASSERT(agent);
				HRESULT rz = agent->GetData(hxGridSessionId, SIMULATED_ANNEALING_DATA_DESCRIPTOR, &globalDataStream);

				if (rz != S_OK)
				{
					CS.Leave();

					msg.ajoute(string("ERROR: agent->GetData from ") + SIMULATED_ANNEALING_DATA_DESCRIPTOR + " failed");
					return msg;
				}
			}

			//insert a new session
			sessionDataCache.insert(std::make_pair(sessionId, TSessionData()));
			i = sessionDataCache.find(sessionId);
			ASSERT(i != sessionDataCache.end());


			TSessionData& sd = i->second;

			istringstream iStream(string((char*)globalDataStream->GetBasePointer(), globalDataStream->GetLength()));
			msg = sd.m_modelVector.ReadStream(iStream);
			if (!msg)
				return msg;

			//free global stream. No longer need for this session
			if (agent)
			{
				//release global stream 
				globalDataStream->Release();
				agent->FreeCachedData(hxGridSessionId, SIMULATED_ANNEALING_DATA_DESCRIPTOR);

				//set agent on model
				for (CSimulatedAnnealingVector::iterator it = sd.m_modelVector.begin(); it != sd.m_modelVector.end(); it++)
				{
					(*it)->m_pAgent = agent;
					(*it)->m_hxGridSessionID = hxGridSessionId;
				}
			}
		}

		CS.Leave();

		return msg;
	}