Beispiel #1
0
void TestMutex(ScopedLogMessage& msg, const std::string& name)
{
	msg.Message(Poco::format(" --- %s ---", name));

	Poco::Stopwatch stopwatch;
	stopwatch.start();

	T lock;
	int counter = 0;

	MutexRunnable<T>* pRunnable[kNumRunnable];
	Poco::Thread thread[kNumRunnable];

	for(int i=0; i<kNumRunnable; ++i)
	{
		pRunnable[i] = new MutexRunnable<T>(lock, counter);
		thread[i].start(*pRunnable[i]);
	}

	for(int i=0; i<kNumRunnable; ++i)
	{
		thread[i].join();
		if(!pRunnable[i]->ok())
		{
			msg.Message(Poco::format(" MutexRunnable[%i] failed!", i));
		}
		delete pRunnable[i];
	}

	stopwatch.stop();

	msg.Message(Poco::format("  counter = %i (expected: %i)", counter, kNumLoops*kNumRunnable));
	msg.Message(Poco::format("  Elapsed time = %.3fmSec", (1000.0 * stopwatch.elapsed()) / stopwatch.resolution()));
}
Beispiel #2
0
bool CStudentMgr::SearchStudent(std::string strTable, std::string strKey, int nType, STUDENT_LIST& lResult)
{
	bool bResult = false;
	if (_session && !_session->isConnected())
		return false;

	try
	{
		Poco::Stopwatch sw;
		sw.start();
//		std::string strTable = "student";
		std::string strSql;
		if (nType == 1)
			strSql = Poco::format("select * from %s where name like '%%%s%%';", strTable, CMyCodeConvert::Gb2312ToUtf8(strKey));
		else
			strSql = Poco::format("select * from %s where zkzh like '%%%s%%';", strTable, CMyCodeConvert::Gb2312ToUtf8(strKey));
		Poco::Data::Statement stmt((*_session << strSql, into(lResult)));
//		Poco::Data::Statement stmt((*_mem << strSql, into(lResult)));
		stmt.execute();
		sw.stop();
		bResult = true;
		std::string strLog = Poco::format("查询报名库数据完成[%.3fms]", (double)sw.elapsed()/1000);
		g_pLogger->information(strLog);
	}
	catch (Poco::Exception& e)
	{
		std::string strErr = "查询报名库失败(" + e.displayText() + ")";
		g_pLogger->information(strErr);
		bResult = false;
	}
	return bResult;
}
Beispiel #3
0
void testPoco()
{    
    Poco::Stopwatch s;
    s.start();
    std::this_thread::sleep_for(std::chrono::milliseconds(1));
    std::cout << "Poco::Stopwatch: elapsed " << s.elapsed() << " microseconds" << std::endl;
}
Beispiel #4
0
	int main(const std::vector<std::string>& args)
	{
		if (!_helpRequested)
		{
			Poco::DateTime now;
			config().setString("PocoDoc.date", Poco::DateTimeFormatter::format(now, "%Y-%m-%d"));
			config().setString("PocoDoc.year", Poco::DateTimeFormatter::format(now, "%Y"));
			config().setString("PocoDoc.googleAnalyticsCode", generateGoogleAnalyticsCode());
			Poco::Stopwatch sw;
			int errors = 0;
			try
			{
				sw.start();
				errors = parseAll();
				fixup();
				writeDoc();
				copyResources();
				sw.stop();
			}
			catch (Exception& exc)
			{
				std::cerr << exc.displayText() << std::endl;
			}
			logger().information(NumberFormatter::format(errors) + " errors.");
			logger().information(std::string("Time: ") + Poco::DateTimeFormatter::format(Poco::Timespan(sw.elapsed())));
		}
		return Application::EXIT_OK;
	}
	void ProfiledBoundObject::Set(const char *name, KValueRef value)
	{
		std::string type = this->GetSubType(name);
		KValueRef result = ProfiledBoundObject::Wrap(value, type);

		Poco::Stopwatch sw;
		sw.start();
		delegate->Set(name, result);
		sw.stop();

		this->Log("set", type, sw.elapsed());
	}
	KValueRef ProfiledBoundObject::Get(const char *name)
	{
		std::string type = this->GetSubType(name);

		Poco::Stopwatch sw;
		sw.start();
		KValueRef value = delegate->Get(name);
		sw.stop();

		this->Log("get", type, sw.elapsed());
		return ProfiledBoundObject::Wrap(value, type);
	}
Beispiel #7
0
void TskReportPipeline::run()
{
    Poco::Stopwatch stopWatch;
    for (size_t i = 0; i < m_modules.size(); i++)
    {
        stopWatch.restart();
        TskModule::Status status = m_modules[i]->report();
        stopWatch.stop();
        updateModuleExecutionTime(m_modules[i]->getModuleId(), stopWatch.elapsed());

        TskServices::Instance().getImgDB().setModuleStatus(0, m_modules[i]->getModuleId(), (int)status);

        // The reporting pipeline continues to run on module failure. Only shutdown the pipeline if a module signals STOP.
        if (status == TskModule::STOP)
        {
            break;
        }
    }
}
	void HTTPClientBinding::Send(const ValueList& args, SharedValue result)
	{
		if (this->Get("connected")->ToBool())
		{
			throw ValueException::FromString("already connected");
		}
		if (args.size()==1)
		{
			// can be a string of data or a file
			SharedValue v = args.at(0);
			if (v->IsObject())
			{
				// probably a file
				SharedKObject obj = v->ToObject();
				this->datastream = obj->Get("toString")->ToMethod()->Call()->ToString();
			}
			else if (v->IsString())
			{
				this->datastream = v->ToString();
			}
			else if (v->IsNull() || v->IsUndefined())
			{
				this->datastream = "";
			}
			else
			{
				throw ValueException::FromString("unknown data type");
			}
		}
		this->thread = new Poco::Thread();
		this->thread->start(&HTTPClientBinding::Run,(void*)this);
		if (!this->async)
		{
			PRINTD("Waiting on HTTP Client thread to finish (sync)");
			Poco::Stopwatch sw;
			sw.start();
			while (!this->shutdown && sw.elapsedSeconds() * 1000 < this->timeout)
			{
				this->thread->tryJoin(100);
			}
			PRINTD("HTTP Client thread finished (sync)");
		}
	}
Beispiel #9
0
void Benchmark(Mtx& mtx, std::string const& label)
{
	Poco::Stopwatch sw;
	sw.start();

	const int LOOP_COUNT = 1000000000;

	mtx.lock();

	for (int i = 0 ; i < LOOP_COUNT ; ++i)
	{
		mtx.unlock();
		mtx.lock();
	}

	mtx.unlock();

	sw.stop();

	std::cout << label << ' ' << sw.elapsed() << " [us]" << std::endl;
}
Beispiel #10
0
bool CStudentMgr::InsertData(STUDENT_LIST& lData, std::string strTable)
{
	bool bResult = false;
	if (_session && !_session->isConnected())
		return false;

	try
	{
		Poco::Stopwatch sw;
		sw.start();

	#if 1
		_mem = new Poco::Data::Session(Poco::Data::SQLite::Connector::KEY, ":memory:");
		Poco::Data::SQLite::Utility::fileToMemory(*_mem, _strDbPath);

		std::string strSql = Poco::format("INSERT INTO %s VALUES(:ln, :fn, :cn, :sn, :status)", strTable);
		Poco::Data::Statement stmt((*_mem << strSql, use(lData)));
		stmt.execute();

		Poco::Data::SQLite::Utility::memoryToFile(_strDbPath, *_mem);
	#else
		std::string strSql = Poco::format("INSERT INTO %s VALUES(:ln, :fn, :cn, :sn, :status)", strTable);
		Poco::Data::Statement stmt((*_session << strSql, use(lData)));
		stmt.execute();
	#endif
		bResult = true;
		sw.stop();
		std::string strLog = Poco::format("插入报名库数据完成[%.6fs]", (double)sw.elapsed()/1000000);
		g_pLogger->information(strLog);
	}
	catch (Poco::Exception& e)
	{
		std::string strErr = "报名库InsertData失败(" + e.displayText() + ")";
		g_pLogger->information(strErr);
		bResult = false;
	}
	return bResult;
}
//----------------------------------------
//	main
//----------------------------------------
int main(int /*argc*/, char** /*argv*/)
{
	PrepareConsoleLogger logger(Poco::Logger::ROOT, Poco::Message::PRIO_INFORMATION);

	ScopedLogMessage msg("SynchronizedObjectTest ", "start", "end");

	Poco::Stopwatch		stopwatch;
	Poco::Thread		thread[kNumThreads];
	MyRunnable			runnable[kNumThreads];
	MySyncObject		syncObjects[kNumSyncObjects];
	const std::string	SyncObjNameTable[kNumSyncObjects] =
							{"AB", "AC", "BA", "BC", "CA", "CB"};

	for(std::size_t i=0; i<kNumThreads; ++i)
	{
		runnable[i].setIndex(&msg, &stopwatch, i, syncObjects);
	}
	for(std::size_t i=0; i<kNumSyncObjects; ++i)
	{
		syncObjects[i].setName(SyncObjNameTable[i]);
	}

	stopwatch.start();

	for(std::size_t i=0; i<kNumThreads; ++i)
	{
		thread[i].start(runnable[i]);
	}

	for(std::size_t i=0; i<kNumThreads; ++i)
	{
		thread[i].join();
	}

	return 0;
}
Beispiel #12
0
void AcceptThread::run() {
	Poco::Net::StreamSocket StrmSock;

	#ifndef _DEBUG
	string sNewConnectionIP;
	vector<std::pair<string,Poco::Timestamp::TimeDiff>> vConnections,vBannedIPs;
	vector<	std::pair<
						string,  /* IP */
						std::pair<
								Timestamp::TimeDiff, /* Ban time */
								Timestamp::TimeDiff /* Entry reset */
								 >
					>
	       > vBanLenght;

	Poco::Stopwatch Timer;
	Poco::Timestamp::TimeDiff lowest,actual;
	
	int x,i;
	short iCount;
	bool fExit;
	
	Timer.start();
	#endif

	_iThreadStatus = FC_THREADSTATUS_RUNNING;
	while(_iThreadStatus==FC_THREADSTATUS_RUNNING) {
		try {
			_ServerSock.listen();
			StrmSock = _ServerSock.acceptConnection(); 
			
			#ifndef _DEBUG
			cutOffPort(StrmSock.peerAddress().toString(),sNewConnectionIP);
			/* Check banlist */
			if (!vBannedIPs.empty()) {
				actual = (Timer.elapsed()/1000);
				fExit = false;

				for (x=vBannedIPs.size()-1;x>=0;x--){
					if (vBannedIPs[x].first.compare(sNewConnectionIP) != 0) {continue;}
					i = search(vBanLenght,sNewConnectionIP);

					if (actual - vBannedIPs[x].second >= vBanLenght[i].second.first) { /* Ban timed out */			
						/* Remove from connection list */
						if (!vConnections.empty()){
							for (i=vConnections.size()-1;i>=0;i--) { 
								if (vConnections[i].first.compare(vBannedIPs[x].first) == 0){vConnections.erase(vConnections.begin()+i);}
							}
						}

						vBannedIPs.erase(vBannedIPs.begin()+x); /* Remove from banlist */
						continue;
					}
					if (vBannedIPs[x].first.compare(sNewConnectionIP) == 0) {
						StrmSock.close();
						fExit = true;
						break;
					}
				}
				if (fExit) {continue;}
			}

			/* Remove old banlenght entries */
			if (!vBanLenght.empty()) {
				for (x = vBanLenght.size()-1;x>=0;x--) {
					if (vBanLenght[x].second.second < Timer.elapsed()/1000) { /* last ban was for 5 minutes -> remove it*/
						vBanLenght.erase( vBanLenght.begin()+x);
					}
				}
			}

			/* Manage connection list + ban if needed */
			vConnections.push_back(std::make_pair(sNewConnectionIP,Timer.elapsed()/1000L)); /* Add connection to list */
			iCount = 0;
			lowest = 30000;
			fExit = false;
			for (x=vConnections.size()-1;x>=0;x--) {
				if ((Timer.elapsed()/1000) - vConnections[x].second >= 30000){ /* Entry is old, remove it */
					vConnections.erase(vConnections.begin()+x);
					continue;
				}

				/* Timespan between actual time and connection time of this entry */
				actual = (Timer.elapsed()/1000) - vConnections[x].second;

				if (vConnections[x].first.compare(sNewConnectionIP) == 0) { /* This IP connected more than one time */
					iCount++;
					if (actual < lowest) {lowest = actual;}
				}

				/* More than four connections in a timespan of 10 seconds -> ban IP */
				if (iCount > 4 && lowest <= 10000) {
					StrmSock.close();
					vBannedIPs.push_back(std::make_pair(sNewConnectionIP,Timer.elapsed()/1000));

					/* Set banlenght */
					if ((x = search(vBanLenght,sNewConnectionIP)) == -1) {
						vBanLenght.push_back(std::make_pair(sNewConnectionIP,std::make_pair(10000,Timer.elapsed()/1000 + 5*60*1000)));
						/*x = vBanLenght.size()-1;*/
					}else{
						vBanLenght[x].second.first *= 2;
						vBanLenght[x].second.second = Timer.elapsed()/1000 + (5*60*1000) + vBanLenght[x].second.first;
					}
					/*cout<<vBanLenght[x].first<<" banned for:"<< (vBanLenght[x].second.first/1000)<<" seconds\n";*/

					fExit = true;
					break;
				}
			}
			if (fExit) {continue;}
			#endif

			if (!_pMinecraftServer->getPlayerPool()->isAnySlotFree()) { //There is no free slot
				StrmSock.sendBytes(_preparedServerFullMsg.c_str(),_preparedServerFullMsg.length());
				StrmSock.close();
				continue;
			}

			_pMinecraftServer->getPlayerPool()->Assign(StrmSock);
		}catch(...) {       /* Only happen if socket become closed */
			_iThreadStatus = FC_THREADSTATUS_DEAD;
			return;
		}
	}
}
void TskFileAnalysisPipeline::run(TskFile* file)
{
    const std::string MSG_PREFIX = "TskFileAnalysisPipeline::run : ";

    if (m_modules.size() == 0)
        return;

    if (file == NULL)
    {
        LOGERROR(MSG_PREFIX + "passed NULL file pointer");
        throw TskNullPointerException();
    }

    TskImgDB& imgDB = TskServices::Instance().getImgDB();

    try
    {
        // If this is an excluded file or the file is not ready for analysis
        // we return without processing.
        if (excludeFile(file))
        {
            std::stringstream msg;
            msg << MSG_PREFIX << "skipping file (excluded) "  << file->getName() << "(" << file->getId() << ")";
            LOGINFO(msg.str());
            file->setStatus(TskImgDB::IMGDB_FILES_STATUS_ANALYSIS_SKIPPED);
            return;
        }

        if (file->getStatus() != TskImgDB::IMGDB_FILES_STATUS_READY_FOR_ANALYSIS) 
        {
            std::stringstream msg;
            msg << MSG_PREFIX << "skipping file (not ready) " << file->getName() << "(" << file->getId() << ")";
            LOGINFO(msg.str());
            return;
        }

        // Update status to indicate analysis is in progress.
        file->setStatus(TskImgDB::IMGDB_FILES_STATUS_ANALYSIS_IN_PROGRESS);
        std::stringstream msg;
        msg << MSG_PREFIX <<  "analyzing " << file->getName() << "(" << file->getId() << ")";
        LOGINFO(msg.str());

        // If there is an Executable module in the pipeline we must
        // ensure that the file exists on disk.
        if (m_hasExeModule && !file->exists())
        {
            TskFileManagerImpl::instance().saveFile(file);
        }

        bool bModuleFailed = false;

        Poco::Stopwatch stopWatch;
        for (int i = 0; i < m_modules.size(); i++)
        {
            // we have no way of knowing if the file was closed by a module,
            // so always make sure it is open
            file->open();

            // Reset the file offset to the beginning of the file.
            file->seek(0);

            stopWatch.restart();
            TskModule::Status status = m_modules[i]->run(file);
            stopWatch.stop();            
            updateModuleExecutionTime(m_modules[i]->getModuleId(), stopWatch.elapsed());
            
            imgDB.setModuleStatus(file->getId(), m_modules[i]->getModuleId(), (int)status);

            // If any module encounters a failure while processing a file
            // we will set the file status to failed once the pipeline is complete.
            if (status == TskModule::FAIL)
                bModuleFailed = true;

            // Stop processing the file when a module tells us to.
            else if (status == TskModule::STOP)
                break;
        }

        // Delete the file if it exists. The file may have been created by us
        // above or by a module that required it to exist on disk.
        // Carved and derived files should not be deleted since the content is
        // typically created by external tools.
        if (file->getTypeId() != TskImgDB::IMGDB_FILES_TYPE_CARVED &&
            file->getTypeId() != TskImgDB::IMGDB_FILES_TYPE_DERIVED &&
            file->exists())
        { 
            TskFileManagerImpl::instance().deleteFile(file);
        }

        // We allow modules to set status on the file so we only update it
        // if the modules haven't.
        if (file->getStatus() == TskImgDB::IMGDB_FILES_STATUS_ANALYSIS_IN_PROGRESS)
        {
            if (bModuleFailed)
            {
                file->setStatus(TskImgDB::IMGDB_FILES_STATUS_ANALYSIS_FAILED);
            }
            else
            {
                file->setStatus(TskImgDB::IMGDB_FILES_STATUS_ANALYSIS_COMPLETE);
            }
        }
    }
    catch (std::exception& ex)
    {
        std::stringstream msg;
        msg << MSG_PREFIX << "error while processing file id (" << file->getId() << ") : " << ex.what();
        LOGERROR(msg.str());
        imgDB.updateFileStatus(file->getId(), TskImgDB::IMGDB_FILES_STATUS_ANALYSIS_FAILED);

        // Rethrow the exception
        throw;
    }
}
Beispiel #14
0
World::World(string Name,char iDimension,MinecraftServer* pServer) :
	_WorldName(Name),
	_iDimension(iDimension)
{
	_pMinecraftServer	= pServer;
	_pWorldFileHandler	= NULL;
	_pSettings			= NULL;
	_pLevelDataFile		= NULL;

	/* Init settings */
	_fHardcore		= false;
	_iMapFeatures	= 1;
	_fRaining		= false;
	_fThundering	= false;
	_iGameType		= 0;
	_iRainTime		= 0;
	_iSpawnX		= 0;
	_iSpawnY		= 0;
	_iSpawnZ		= 0;
	_iThunderTime	= 0;
	_iVersion		= 19133;
	_iLastPlayed	= 0L;
	_iSeed			= 0L;
	_iSizeOnDisk	= 0L;
	_iTime			= 0L;
	_sGeneratorName.assign("default");
	_sLevelName.assign("world");

	/* Prepare path */	
	Poco::Path path(pServer->getServerDirectory());
	path.pushDirectory(_WorldName);

	try {
		/* Check existance of needed directories */
		IOFile::createDir(path);
		path.pushDirectory("data");
		IOFile::createDir(path);
		path.popDirectory();
		path.pushDirectory("players");
		IOFile::createDir(path);
		path.popDirectory();
		path.pushDirectory("region");
		IOFile::createDir(path);
		path.popDirectory();
		path.setFileName("level.dat");


		/* try to load level.dat */
		Poco::File filepath(path);
		NBTTagCompound* pCompData;
		char* pData = NULL;

		try {
			try { //Seperate exception catchnig for bad errors
				_pLevelDataFile = new IOFile(filepath);
			}catch(FCRuntimeException& ex) {
				throw std::exception(ex.what());
			}

			if (_pLevelDataFile->getSize() < 2)  {throw FCRuntimeException("Unable to read level.dat",false);}

			pData = new char[_pLevelDataFile->getSize()];
			_pLevelDataFile->read(pData,_pLevelDataFile->getSize());

			NBTBinaryParser parser;
			_pSettings = parser.parse(pData,FC_NBT_IO_GZIP,_pLevelDataFile->getSize());
			pCompData = dynamic_cast<NBTTagCompound*>(_pSettings->getElementByName("Data"));
			if (pCompData == NULL) {throw FCRuntimeException("No Data compound");}
			delete [] pData;

		}catch(FCRuntimeException) {
			/* Something failed, generate own one */
			if (pData) {delete [] pData;}
			if (_pSettings) {delete _pSettings;}

			_pSettings = new NBTTagCompound("");
			_pSettings->addSubElement(pCompData = new NBTTagCompound("Data"));
		}catch(std::exception){
			throw FCRuntimeException("Unable to open/create level.dat");
		}


		NBTHelper<NBTTagByte,char>::read(pCompData,"hardcore",_fHardcore);
		NBTHelper<NBTTagByte,char>::read(pCompData,"MapFeatures",_iMapFeatures);
		NBTHelper<NBTTagByte,char>::read(pCompData,"raining",_fRaining);
		NBTHelper<NBTTagByte,char>::read(pCompData,"thundering",_fThundering);
		NBTHelper<NBTTagInt,int>::read(pCompData,"GameType",_iGameType);
		NBTHelper<NBTTagInt,int>::read(pCompData,"generatorVersion",_iGeneratorVersion);
		NBTHelper<NBTTagInt,int>::read(pCompData,"rainTime",_iRainTime);
		NBTHelper<NBTTagInt,int>::read(pCompData,"SpawnX",_iSpawnX);
		NBTHelper<NBTTagInt,int>::read(pCompData,"SpawnY",_iSpawnY);
		NBTHelper<NBTTagInt,int>::read(pCompData,"SpawnZ",_iSpawnZ);
		NBTHelper<NBTTagInt,int>::read(pCompData,"thunderTime",_iThunderTime);
		NBTHelper<NBTTagInt,int>::read(pCompData,"version",_iVersion);
		NBTHelper<NBTTagInt64,Tick>::read(pCompData,"LastPlayed",_iLastPlayed);
		NBTHelper<NBTTagInt64,long long>::read(pCompData,"RandomSeed",_iSeed);
		NBTHelper<NBTTagInt64,long long>::read(pCompData,"SizeOnDisk",_iSizeOnDisk);
		NBTHelper<NBTTagInt64,Tick>::read(pCompData,"Time",_iTime);
		NBTHelper<NBTTagString,string>::read(pCompData,"generatorName",_sGeneratorName);
		NBTHelper<NBTTagString,string>::read(pCompData,"LevelName",_sLevelName);


		/* Open region files */
		path.setFileName("");
		_pWorldFileHandler = new WorldFileHandler(path);



		/* Load chunks around the spawn point */
		int iLoadedChunks = 0;
		int iChunkSpawnX = _iSpawnX/16;
		int iChunkSpawnZ = _iSpawnZ/16;
		Poco::Stopwatch nextOutput;
		Poco::Stopwatch loadingTime;
	
		nextOutput.start();
		loadingTime.start();
		for (int x = iChunkSpawnX - 12;x<=iChunkSpawnX + 12;x++) {
			for (int z = iChunkSpawnZ - 12;z<=iChunkSpawnZ + 12;z++) {
				getChunk(x,z);
				iLoadedChunks++;

				if (nextOutput.elapsed() >= 1000000) {
					cout<<"\tProgress "<<((iLoadedChunks*100)/625)<<"%\n";
					nextOutput.restart();
				}
			}
		}
		loadingTime.stop();
		cout<<"\tLoading done in "<<(loadingTime.elapsed()/1000000)<<"s\n";
	}catch(FCRuntimeException& ex) {
		ex.rethrow();
	}
}
Beispiel #15
0
int main(int argc, char **argv)
{
    Poco::Stopwatch sw;

    std::string dir = Poco::Environment::get("POCO_BASE") + "/JSON/samples/Benchmark/";
    Poco::Path  filePath(dir, "input.big.json");

    std::ostringstream ostr;

    if (filePath.isFile()) {
        Poco::File inputFile(filePath);

        if (inputFile.exists()) {
            sw.start();
            Poco::FileInputStream fis(filePath.toString());
            Poco::StreamCopier::copyStream(fis, ostr);
            sw.stop();
        } else {
            std::cout << filePath.toString() << " doesn't exist!" << std::endl;
            return 1;
        }
    }

    std::cout << "JSON Benchmark" << std::endl;
    std::cout << "==============" << std::endl;

    std::string jsonStr = ostr.str();
    std::cout << "Total of " << jsonStr.size() << " bytes," << std::endl << "loaded in " << sw.elapsed() << " [us]," << std::endl;

    std::cout << std::endl << "POCO JSON barebone parse" << std::endl;
    Poco::JSON::Parser sparser(0);
    sw.restart();
    sparser.parse(jsonStr);
    sw.stop();
    std::cout << "---------------------------------" << std::endl;
    std::cout << "[std::string] parsed in " << sw.elapsed() << " [us]" << std::endl;
    std::cout << "---------------------------------" << std::endl;

    Poco::JSON::Parser  iparser(0);
    std::istringstream  istr(jsonStr);
    sw.restart();
    iparser.parse(istr);
    sw.stop();
    std::cout << "----------------------------------------" << std::endl;
    std::cout << "[std::istringstream] parsed in " << sw.elapsed() << " [us]" << std::endl;
    std::cout << "----------------------------------------" << std::endl;

    std::cout << std::endl << "POCO JSON Handle/Stringify" << std::endl;
    try
    {
        Poco::JSON::Parser sparser;
        sw.restart();
        sparser.parse(jsonStr);
        Poco::DynamicAny result = sparser.result();
        sw.stop();
        std::cout << "-----------------------------------------" << std::endl;
        std::cout << "[std::string] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "-----------------------------------------" << std::endl;

        Poco::JSON::Parser  isparser;
        std::istringstream  istr(jsonStr);
        sw.restart();
        isparser.parse(istr);
        result = isparser.result();
        sw.stop();
        std::cout << "------------------------------------------------" << std::endl;
        std::cout << "[std::istringstream] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "------------------------------------------------" << std::endl;

        // Serialize to string
        Poco::JSON::Object::Ptr obj;

        if (result.type() == typeid(Poco::JSON::Object::Ptr)) {
            obj = result.extract <Poco::JSON::Object::Ptr> ();
        }

        std::ostringstream out;
        sw.restart();
        obj->stringify(out);
        sw.stop();
        std::cout << "-----------------------------------" << std::endl;
        std::cout << "stringified in " << sw.elapsed() << " [us]" << std::endl;
        std::cout << "-----------------------------------" << std::endl;
        std::cout << std::endl;
    }
    catch(Poco::JSON::JSONException jsone)
    {
        std::cout << jsone.message() << std::endl;
    }

    return 0;
}
Beispiel #16
0
void PlayerThread::run() {
	unsigned char iPacket;
	Poco::Stopwatch Timer;

	_fRunning=true;

	Timer.start();
	while (_fRunning) {
		if (!isAssigned()) {
			while(!isAssigned()) {
				if (_fRunning==false) { break; }
				Thread::sleep(50);
			}
			if (_fRunning==false) { continue; }
			Timer.reset();
		} 


		try {
			//Thread Ticks
			Timer.stop();
			_iThreadTicks += Timer.elapsed() / 1000;
			Timer.reset();
			Timer.start();


			if (!_fSpawned && _iThreadTicks >= FC_MAXLOGINTIME) {
				Disconnect("Login timed out");
				continue;
			}

			Interval_KeepAlive(); 
			Interval_Time();
			Interval_HandleMovement();
			Interval_Movement();
			Interval_CalculateSpeed();
			Interval_CheckPosition();
			Interval_CheckEating();


			iPacket = _NetworkInRoot.readByte();

			if (!_fSpawned &&  (iPacket!=0x01 && iPacket!=0x02 && iPacket!=0xFE)) {
				Disconnect("Login not done!");
				continue;
			}

			//cout<<"Package recovered:"<<std::hex<<int(iPacket)<<"\n";
			switch (iPacket) {
			case 0x0:
				Packet0_KeepAlive();
				break;
			case 0x1:
				if (isSpawned()) {
					Disconnect("Login already done!");
					continue;
				}
				Packet1_Login();
				break;
			case 0x2:
				if (isSpawned()) {
					Disconnect("Login already done!");
					continue;
				}
				Packet2_Handshake();
				break;
			case 0x3:
				Packet3_Chat();
				break;
			case 0xa: 
				Packet10_Player();
				break;
			case 0xb:
				Packet11_Position();
				break;
			case 0xc: 
				Packet12_Look();
				break;
			case 0xd: 
				Packet13_PosAndLook();
				break;
			case 0xe:
				Packet14_Digging();
				break;
			case 0xf: 
				Packet15_PlayerBlockPlacement();
				break;
			case 0x10: 
				Packet16_HoldingChange();
				break;
			case 0x12: 
				Packet18_Animation();
				break;
			case 0x13: 
				Packet19_EntityAction();
				break;
			case 0x65: 
				Packet101_CloseWindow();
				break;
			case 0x66:
				Packet102_WindowClick();
				break;
			case 0xFE: //Server List Ping
				Packet254_ServerListPing();
				break;
			case 0xFF: //Disconnect
				Packet255_Disconnect();
				break;
			default: 
				Disconnect("Unknown packet!");
				cout<<"Unknown packet received! 0x"<<std::hex<<int(iPacket)<<endl;
				break;
			}
		} catch (Poco::RuntimeException) {
			Disconnect(FC_LEAVE_OTHER);
		}
	}
	if(_fAssigned){Disconnect(FC_LEAVE_OTHER);}
	_fRunning=true;
}
Beispiel #17
0
void PktAnalyzer::analyzer(Packet &pkt)
{
	Poco::Stopwatch sw;
	_logger.debug("Got packet from queue");

	unsigned char *full_packet=pkt.get_payload();
	int id = pkt.get_id();
	uint32_t size = pkt.get_size();
	struct nfq_q_handle *qh=pkt.get_qh();
	struct ip *iph = (struct ip *)full_packet;
	struct ip6_hdr *iph6 = (struct ip6_hdr *)full_packet;

	// определяем версию протокола
	int ip_version=0;
	if(iph->ip_v == 6)
		ip_version = 6;
	else if (iph->ip_v == 4)
		ip_version = 4;
	if(!ip_version)
	{
		_logger.error("Unsupported IP protocol version %d for packet id %d",(int) iph->ip_v, id);
		nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
		dump_file(full_packet,size,id);
		return ;
	}

	unsigned char *pkt_data_ptr = NULL;
	struct tcphdr* tcph;

	pkt_data_ptr = full_packet + (ip_version == 4 ? sizeof(struct ip) : sizeof(struct ip6_hdr));

	tcph = (struct tcphdr *) pkt_data_ptr;

	// длина ip заголовка
	int iphlen = iphdr(full_packet)->ihl*4; // ipv4
	if(ip_version == 6)
		iphlen = sizeof(struct ip6_hdr);

	// длина tcp заголовка
	int tcphlen = tcphdr(full_packet+iphlen)->doff*4;

	// общая длина всех заголовков
	uint32_t hlen = iphlen + tcphlen;

	_parent->inc_total_bytes_packets(size);

	// пропускаем пакет без данных
	if(hlen == size)
	{
		nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
		return ;
	}

	int tcp_src_port=ntohs(tcph->source);
	int tcp_dst_port=ntohs(tcph->dest);
	std::unique_ptr<Poco::Net::IPAddress> src_ip;
	std::unique_ptr<Poco::Net::IPAddress> dst_ip;
	if(ip_version == 4)
	{
		src_ip.reset(new Poco::Net::IPAddress(&iph->ip_src,sizeof(in_addr)));
		dst_ip.reset(new Poco::Net::IPAddress(&iph->ip_dst,sizeof(in_addr)));
	} else {
		src_ip.reset(new Poco::Net::IPAddress(&iph6->ip6_src,sizeof(in6_addr)));
		dst_ip.reset(new Poco::Net::IPAddress(&iph6->ip6_dst,sizeof(in6_addr)));
	}

	uint8_t ip_protocol=(ip_version == 4 ? iph->ip_p : iph6->ip6_ctlun.ip6_un1.ip6_un1_nxt);


	{
		Poco::ScopedReadRWLock lock(nfqFilter::_ipportMapMutex);
		IPPortMap::iterator it_ip=nfqFilter::_ipportMap->find(*dst_ip.get());
		if(it_ip != nfqFilter::_ipportMap->end())
		{
			unsigned short port=tcp_dst_port;
			if (it_ip->second.size() == 0 || it_ip->second.find(port) != it_ip->second.end())
			{
				_parent->inc_matched_ip_port();
				if(_config.send_rst)
				{
					_logger.debug("HostList: Send RST to the client (%s) and server (%s) (packet no %d)",src_ip->toString(),dst_ip->toString(),id);
					std::string empty_str;
					SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),empty_str,true));
					_parent->inc_sended_rst();
					nfq_set_verdict(qh,id,NF_DROP,0,NULL);
				} else {
					_logger.debug("HostList: Set mark %d to packet no %d  port %hu",_config.mark_value,id,port);
					_parent->inc_marked_hosts();
					nfq_set_verdict2(qh,id,NF_ACCEPT,_config.mark_value,0,NULL);
				}
				return ;
			}
		}
	}


	// nDPI usage
	sw.reset();
	sw.start();
	nDPIWrapper nw;

	struct ndpi_flow_struct *flow=nw.get_flow();

	uint32_t current_tickt = 0;
	ndpi_protocol protocol = ndpi_detection_process_packet(nfqFilter::my_ndpi_struct, flow, full_packet, size, current_tickt, nw.get_src(), nw.get_dst());

	if(protocol.protocol == NDPI_PROTOCOL_UNKNOWN)
	{
		_logger.debug("Guessing protocol...");
		protocol = ndpi_guess_undetected_protocol(nfqFilter::my_ndpi_struct,
		   ip_protocol,
		   0,//ip
		   tcp_src_port, // sport
		   0,
		   tcp_dst_port); // dport
	}
	_logger.debug("Protocol is %hu/%hu ",protocol.master_protocol,protocol.protocol);
	sw.stop();
	_logger.debug("nDPI protocol detection occupied %ld us",sw.elapsed());
	if(protocol.master_protocol == NDPI_PROTOCOL_SSL || protocol.protocol == NDPI_PROTOCOL_SSL || protocol.protocol == NDPI_PROTOCOL_TOR)
	{
		if(flow->l4.tcp.ssl_seen_client_cert == 1)
		{
			std::string ssl_client;
			_logger.debug("Analysing SSL protocol");
			if(flow->protos.ssl.client_certificate[0] != '\0')
			{
				ssl_client=flow->protos.ssl.client_certificate;
				_logger.debug("SSL client is: %s",ssl_client);
			}
			if(!ssl_client.empty())
			{
				sw.reset();
				sw.start();
				if(_config.lower_host)
					std::transform(ssl_client.begin(), ssl_client.end(), ssl_client.begin(), ::tolower);
				AhoCorasickPlus::Match match;
				std::size_t host_len=ssl_client.length();
				bool found=false;
				{
					Poco::Mutex::ScopedLock lock(nfqFilter::_sslMutex);
					nfqFilter::atm_ssl->search(ssl_client,false);
					while(nfqFilter::atm_ssl->findNext(match) && !found)
					{
						if(match.pattern.length != host_len)
						{
							DomainsMatchType::Iterator it=nfqFilter::_SSLdomainsMatchType->find(match.id);
							bool exact_match=false;
							if(it != nfqFilter::_SSLdomainsMatchType->end())
								exact_match = it->second;
							if(exact_match)
								continue;
							if(ssl_client[host_len-match.pattern.length-1] != '.')
								continue;
						}
						found=true;
					}
				}
				sw.stop();
				_logger.debug("SSL Host seek occupied %ld us, host: %s",sw.elapsed(),ssl_client);
				if(found)
				{
					_parent->inc_matched_ssl();
					if(_config.send_rst)
					{
						_logger.debug("SSLHostList: Send RST to the client (%s) and server (%s) (packet no %d)",src_ip->toString(),dst_ip->toString(),id);
						std::string empty_str;
						SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),empty_str,true));
						_parent->inc_sended_rst();
						nfq_set_verdict(qh,id,NF_DROP,0,NULL);
					} else {
						_logger.debug("SSLHostList: Set mark %d to packet no %d, ssl host name: %s",_config.mark_value,id,ssl_client);
						_parent->inc_marked_ssl();
						nfq_set_verdict2(qh,id,NF_ACCEPT,_config.mark_value,0,NULL);
					}
					return ;
				} else {
					nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
					return ;
				}
			} else {
				if(_config.block_undetected_ssl)
				{
					Poco::ScopedReadRWLock lock(nfqFilter::_sslIpsSetMutex);
					if(nfqFilter::_sslIps->try_search_exact_ip(*dst_ip.get()))
					{
						_parent->inc_matched_ssl_ip();
						_logger.debug("Blocking/Marking SSL client hello packet from %s:%d to %s:%d", src_ip->toString(),tcp_src_port,dst_ip->toString(),tcp_dst_port);
						if(_config.send_rst)
						{
							_logger.debug("SSLClientHello: Send RST to the client (%s) and server (%s) (packet no %d)",src_ip->toString(),dst_ip->toString(),id);
							std::string empty_str;
							SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),empty_str,true));
							_parent->inc_sended_rst();
							nfq_set_verdict(qh,id,NF_DROP,0,NULL);
						} else {
							_logger.debug("SSLClientHello: Set mark %d to packet no %d",_config.mark_value,id);
							_parent->inc_marked_ssl();
							nfq_set_verdict2(qh,id,NF_ACCEPT,_config.mark_value,0,NULL);
						}
						return ;
					}
				}
				_logger.debug("No ssl client certificate found! Accept packet from %s:%d to %s:%d.",src_ip->toString(),tcp_src_port,dst_ip->toString(),tcp_dst_port);
			}
		}
		nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
		return ;
	}
	if(protocol.master_protocol != NDPI_PROTOCOL_HTTP && protocol.protocol != NDPI_PROTOCOL_HTTP && protocol.protocol != NDPI_PROTOCOL_DIRECT_DOWNLOAD_LINK)
	{
		_logger.debug("Not http protocol. Protocol is %hu/%hu from %s:%d to %s:%d",protocol.master_protocol,protocol.protocol,src_ip->toString(),tcp_src_port,dst_ip->toString(),tcp_dst_port);
		nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
		return ;
	}

	_logger.debug("Got HTTP protocol");

	std::string host((char *)&flow->host_server_name[0]);
	if((flow->http.method == HTTP_METHOD_GET || flow->http.method == HTTP_METHOD_POST || flow->http.method == HTTP_METHOD_HEAD) && !host.empty())
	{
		int dot_del=0;
		if(host[host.length()-1] == '.')
		{
			dot_del=host.length()-1;
			host.erase(dot_del,1);
		}
		if(_config.lower_host)
			std::transform(host.begin(), host.end(), host.begin(), ::tolower);
		sw.reset();
		sw.start();

		AhoCorasickPlus::Match match;
		bool found=false;
		{
			Poco::Mutex::ScopedLock lock(nfqFilter::_domainMapMutex);
			nfqFilter::atm_domains->search(host,false);
			std::size_t host_len=host.length();
			while(nfqFilter::atm_domains->findNext(match) && !found)
			{
				if(match.pattern.length != host_len)
				{
					DomainsMatchType::Iterator it=nfqFilter::_domainsMatchType->find(match.id);
					bool exact_match=false;
					if(it != nfqFilter::_domainsMatchType->end())
						exact_match = it->second;
					if(exact_match)
						continue;
					if(host[host_len-match.pattern.length-1] != '.')
						continue;
				}
				found=true;
			}
		}
		sw.stop();
		_logger.debug("Host seek occupied %ld us",sw.elapsed());
		if(found)
		{
			_logger.debug("Host %s present in domain (file line %d) list from ip %s", host, match.id, src_ip->toString());
			std::string add_param;
			switch (_config.add_p_type)
			{
				case A_TYPE_ID: add_param="id="+std::to_string(match.id);
						break;
				case A_TYPE_URL: add_param="url="+host;
						break;
				default: break;
			}
			SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port, src_ip.get(), dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),add_param));
			_parent->inc_redirected_domains();
			nfq_set_verdict(qh,id,NF_DROP,0,NULL);
			return ;
		}
		sw.reset();
		sw.start();
		found=false;
		std::string uri_o(flow->http.url ? flow->http.url : "");
		if(flow->http.url)
		{
			std::string uri;
			if(dot_del)
				uri_o.erase(dot_del+7,1);
			try
			{
				Poco::URI uri_p(uri_o);
				uri_p.normalize();
				uri.assign(uri_p.toString());
				if(_config.url_decode)
				{
#ifdef __USE_POCO_URI_DECODE
					Poco::URI::decode(uri_p.toString(),uri);
#else
					uri=url_decode(uri);
#endif
				}
			} catch (Poco::SyntaxException &ex)
			{
				_logger.debug("An SyntaxException occured: '%s' on URI: '%s'",ex.displayText(), uri_o);
				uri.assign(flow->http.url);
			}

			{
				Poco::Mutex::ScopedLock lock(nfqFilter::_urlMapMutex);
				nfqFilter::atm->search(uri,false);
				while(nfqFilter::atm->findNext(match) && !found)
				{
					if(_config.match_url_exactly && uri.length() != match.pattern.length)
						continue;
					found=true;
				}
			}
			sw.stop();
			_logger.debug("URL seek occupied %ld us for uri %s",sw.elapsed(),uri);
			if(found)
			{
				_logger.debug("URL %s present in url (file pos %u) list from ip %s",uri,match.id,src_ip->toString());
				std::string add_param;
				switch (_config.add_p_type)
				{
					case A_TYPE_ID: add_param="id="+std::to_string(match.id);
							break;
					case A_TYPE_URL: add_param="url="+uri;
							break;
						default: break;
				}
				SenderTask::queue.enqueueNotification(new RedirectNotification(tcp_src_port, tcp_dst_port,src_ip.get(),dst_ip.get(),/*acknum*/ tcph->ack_seq, /*seqnum*/ tcph->seq,/* flag psh */ (tcph->psh ? 1 : 0 ),add_param));
				_parent->inc_redirected_urls();
				nfq_set_verdict(qh,id,NF_DROP,0,NULL);
				return ;
			}
		}
	}
	nfq_set_verdict(qh,id,NF_ACCEPT,0,NULL);
}