Beispiel #1
0
void CListLoader::AddList(CSecureRule* pRule)
{
	ASSERT( pRule->m_nType == CSecureRule::srExternal );
	if ( ! pRule || pRule->m_nType != CSecureRule::srExternal || ! pRule->m_pContent )
		return;

	if ( ! m_pQueue.Find( pRule ) )
		m_pQueue.AddTail( pRule );

	if ( ! IsThreadEnabled() )
		BeginThread( "ListLoader" );
}
Beispiel #2
0
void CDownloadTask::RunCopy()
{
	m_nFileError = m_pDownload->MoveFile( m_sDestination, CopyProgressRoutine, this );

	m_bSuccess = ( m_nFileError == ERROR_SUCCESS );

	if ( ! IsThreadEnabled() ) return;	// Aborted

	if ( m_bSuccess )
		m_pDownload->OnMoved();
	else
		m_pDownload->SetFileError( GetFileError(), L"" );
}
Beispiel #3
0
void CDownloadTask::RunMerge()
{
	m_fProgress = 0;
	const float fIncrement = 100.0f / m_oMergeFiles.GetCount();

	for ( POSITION pos = m_oMergeFiles.GetHeadPosition(); pos && IsThreadEnabled(); )
	{
		if ( m_pDownload->IsCompleted() || m_pDownload->IsMoving() ) break;

		m_bSuccess |= m_pDownload->RunMergeFile( m_oMergeFiles.GetNext( pos ), m_bMergeValidation, m_oMergeGaps, this, fIncrement );
	}

	m_fProgress = 100.0f;
}
void CLibraryTipCtrl::OnRun()
{
	while ( IsThreadEnabled() )
	{
		Doze( 1000 );

		if ( ! IsThreadEnabled() )
			break;

		m_pSection.Lock();
		CString strPath = m_sPath;
		m_pSection.Unlock();

		if ( strPath.IsEmpty() )	// ToDo: Make preview requests by hash?
			continue;

		CImageFile pFile;
		BOOL bSuccess = CThumbCache::Cache( strPath, &pFile );

		m_pSection.Lock();

		if ( m_bmThumb.m_hObject ) m_bmThumb.DeleteObject();

		if ( m_sPath == strPath )
		{
			m_sPath.Empty();

			if ( bSuccess )
			{
				m_bmThumb.Attach( pFile.CreateBitmap() );
				Invalidate();
			}
		}

		m_pSection.Unlock();
	}
}
Beispiel #5
0
// The thread runs this method
// Accept incoming connections from remote computers and figure out what they want
void CHandshakes::OnRun()
{
	// Loop while the socket is valid
	while ( IsThreadEnabled() && IsValid() )
	{
		// Wait for a computer to call us, which fires the wakeup event
		Doze( 1000 );

		// Accept the connection from the remote computer, making a new CHandshake object for it in the list
		while ( AcceptConnection() );

		// Send and receive data with each remote computer in the list
		RunHandshakes();

		// If we've listened for and accepted at least one connection, update the discovery services
		RunStableUpdate();
	}
}
void CLibraryMetaPanel::OnRun()
{
	m_pSection.Lock();

	while ( IsThreadEnabled() )
	{	
		CString strPath = m_sPath;

		if ( strPath.IsEmpty() )
			break;

		m_pSection.Unlock();

		CImageFile pFile;
		BOOL bSuccess = CThumbCache::Cache( strPath, &pFile );

		m_pSection.Lock();

		// If nothing changes
		if ( m_sPath == strPath )
		{
			if ( bSuccess )
			{
				if ( m_bmThumb.m_hObject )
					m_bmThumb.DeleteObject();

				m_bmThumb.Attach( pFile.CreateBitmap() );
			}

			Invalidate();

			break;
		}
	}

	m_pSection.Unlock();
}
Beispiel #7
0
void CDownloadTask::RunPreviewRequest()
{
	m_bSuccess = m_pRequest->Execute( false );	// Without threading

	if ( ! IsThreadEnabled() || m_pDownload->m_sPath.IsEmpty() ) return;

	// Save downloaded preview as png-file
	const CString strPath = m_pDownload->m_sPath + L".png";
	CImageFile pImage;
	CBuffer* pBuffer = IsPreviewAnswerValid( m_pDownload->m_oSHA1 );
	if ( pBuffer && pBuffer->m_pBuffer && pBuffer->m_nLength &&
		 pImage.LoadFromMemory( L".jpg", pBuffer->m_pBuffer, pBuffer->m_nLength, FALSE, TRUE ) &&
		 pImage.SaveToFile( (LPCTSTR)strPath, 100 ) )
	{
		// Make it hidden, so the files won't be shared
		SetFileAttributes( (LPCTSTR)strPath, FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_SYSTEM );
	}
	else
	{
		// Failed
		m_pDownload->m_bWaitingPreview = FALSE;
		theApp.Message( MSG_ERROR, IDS_SEARCH_DETAILS_PREVIEW_FAILED, (LPCTSTR)GetRequest() );
	}
}
Beispiel #8
0
void CPlugins::OnRun()
{
	while ( IsThreadEnabled() )
	{
		Doze( 1000 );

		if ( ! IsThreadEnabled() )
			break;

		if ( m_inCLSID == CLSID_NULL )
		{
			m_pReady.PulseEvent();
			continue;
		}

		CQuickLock oLock( m_pSection );

		// Revoke interface
		CPluginPtr* pGITPlugin = NULL;
		if ( m_pCache.Lookup( m_inCLSID, pGITPlugin ) )
		{
			delete pGITPlugin;

			TRACE( "Dropped plugin %s\n", (LPCSTR)CT2A( Hashes::toGuid( m_inCLSID ) ) );
		}

		m_pCache.SetAt( m_inCLSID, NULL );

		HINSTANCE hRes = AfxGetResourceHandle();

		pGITPlugin = new CPluginPtr;
		if ( pGITPlugin )
		{
			//HRESULT hr;

			// Create plugin & Add plugin interface to GIT
			if ( SUCCEEDED( /*hr =*/ pGITPlugin->m_pIUnknown.CoCreateInstance( m_inCLSID ) ) &&
				 SUCCEEDED( /*hr =*/ pGITPlugin->m_pGIT.Attach( pGITPlugin->m_pIUnknown ) ) )
			{
				m_pCache.SetAt( m_inCLSID, pGITPlugin );

				TRACE( "Created plugin %s\n", (LPCSTR)CT2A( Hashes::toGuid( m_inCLSID ) ) );
			}
			else
				delete pGITPlugin;
		}

		AfxSetResourceHandle( hRes );

		m_inCLSID = CLSID_NULL;

		m_pReady.SetEvent();
	}

	CQuickLock oLock( m_pSection );

	// Revoke all interfaces
	for ( POSITION pos = m_pCache.GetStartPosition() ; pos ; )
	{
		CLSID pCLSID;
		CPluginPtr* pGITPlugin = NULL;
		m_pCache.GetNextAssoc( pos, pCLSID, pGITPlugin );
		delete pGITPlugin;
	}
	m_pCache.RemoveAll();
}
Beispiel #9
0
void CListLoader::OnRun()
{
	while ( IsThreadEnabled() && m_pQueue.GetCount() )
	{
		CSecureRule* pRule = m_pQueue.GetHead();

		if ( ! pRule || ! pRule->m_pContent || pRule->m_nType != CSecureRule::srExternal )
		{
			m_pQueue.RemoveHead();
			continue;
		}

		CString strPath = pRule->GetContentWords();
		if ( strPath.GetLength() < 6 )
		{
			m_pQueue.RemoveHead();
			continue;
		}

		CString strCommentBase = pRule->m_sComment;
		if ( strCommentBase.IsEmpty() )
			strCommentBase = _T("• %u");
		else if ( strCommentBase.ReverseFind( _T('•') ) >= 0 )
			strCommentBase = strCommentBase.Left( strCommentBase.ReverseFind( _T('•') ) + 1 ) + _T(" %u");
		else
			strCommentBase += _T("  • %u");

		if ( strPath[1] != _T(':') )
			strPath = Settings.General.DataPath + strPath;

		CFile pFile;
		if ( ! pFile.Open( (LPCTSTR)strPath.GetBuffer(), CFile::modeRead ) )
		{
			m_pQueue.RemoveHead();
			continue;
		}

		const BYTE nIndex = Security.SetRuleIndex( pRule );

		try
		{
			CBuffer pBuffer;
			const DWORD nLength = pFile.GetLength();
			pBuffer.EnsureBuffer( nLength );
			pBuffer.m_nLength = nLength;
			pFile.Read( pBuffer.m_pBuffer, nLength );
			pFile.Close();

			// Format: Delineated Lists

			CString strLine, strURN;
			DWORD nCount = 0;
			int nPos;

//TIMER_START
			while ( pBuffer.ReadLine( strLine ) && IsThreadEnabled() && pRule )
			{
				strLine.TrimRight();

				if ( strLine.GetLength() < 7 )
					continue;									// Blank/Invalid line

				if ( strLine[ 0 ] == '#' )
				{
					if ( strLine[ strLine.GetLength() - 1 ] == _T(':') && strLine.Find( _T("urn:") ) > 0 )
						strURN = strLine.Mid( strLine.Find( _T("urn:") ) );		// Default "# urn:type:"
					continue;									// Comment line
				}

				if ( strLine[ 0 ] < '0' || strLine[ 0 ] > 'z' )	// Whitespace/Chars
					continue;									// Invalid line

				if ( ++nCount % 10 == 0 )
				{
					if ( pRule->m_sComment.IsEmpty() )
						strCommentBase = _T("• %u");
					else if ( pRule->m_sComment.ReverseFind( _T('•') ) < 0 )
						strCommentBase = pRule->m_sComment + _T("  • %u");

					pRule->m_sComment.Format( strCommentBase, nCount );
					Sleep( 1 );		// Limit CPU
				}

				// Hashes:

				if ( ( ! strURN.IsEmpty() && strLine.Find( _T('.'), 5 ) < 0 ) || StartsWith( strLine, _PT("urn:") ) )
				{
					nPos = strLine.FindOneOf( _T(" \t") );
					if ( nPos > 0 )
						strLine.Truncate( nPos );				// Trim at whitespace (remove any trailing comments)
					if ( ! strURN.IsEmpty() && ! StartsWith( strLine, _PT("urn:") ) )
						strLine = strURN + strLine;				// Default "urn:type:" prepended
					if ( strLine.GetLength() > 35 )
						Security.SetHashMap( strLine, nIndex );
					else
						nCount--;
					continue;
				}

				// IPs:

				nPos = strLine.ReverseFind( _T(':') );
				if ( nPos > 0 )
					strLine = strLine.Mid( nPos + 1 );			// Remove leading comment for some formats

				nPos = strLine.FindOneOf( _T(" \t") );
				if ( nPos > 0 )
					strLine.Truncate( nPos );					// Trim at whitespace (remove any trailing comments)

				if ( strLine.GetLength() < 7 || strLine.Find( _T('.') ) < 1 )
				{
					nCount--;
					continue;
				}

				nPos = strLine.Find( _T('-') );					// Possible Range
				if ( nPos < 0 )									// Single IP
				{
					Security.SetAddressMap( IPStringToDWORD( strLine, TRUE ), nIndex );
					continue;
				}

				CString strFirst = strLine.Left( nPos );
				CString strLast  = strLine.Mid( nPos + 1 );

				if ( strFirst == strLast )
				{
					Security.SetAddressMap( IPStringToDWORD( strLine, TRUE ), nIndex );
					continue;
				}

				// inet_addr( CT2CA( (LPCTSTR)strLast )
				DWORD nFirst = IPStringToDWORD( strFirst, FALSE );
				DWORD nLast  = IPStringToDWORD( strLast, FALSE );

				if ( nFirst < 10 || nFirst >= 0xE0000000 )	// 0 or "0.0." or "224-255"
					continue;		// Redundant/Invalid

				//if ( Network.IsReserved( (IN_ADDR*)nFirst ) )		// Crash
				//if ( StartsWith( strFirst, _PT("0.0") ) ||
				//	 StartsWith( strFirst, _PT("6.0") ) ||
				//	 StartsWith( strFirst, _PT("7.0") ) ||
				//	 StartsWith( strFirst, _PT("11.0") ) ||
				//	 StartsWith( strFirst, _PT("55.0") ) ||
				//	 StartsWith( strFirst, _PT("127.0") ) )
				//	continue;		// Redundant

				for ( DWORD nRange = Settings.Security.ListRangeLimit ; nFirst <= nLast && nRange ; nFirst++, nRange-- )
				{
					Security.SetAddressMap( htonl( nFirst ), nIndex );	// Reverse host-byte order
				}
			}

			if ( pRule )
				pRule->m_sComment.Format( strCommentBase, nCount );		// Final update

			PostMainWndMessage( WM_SANITY_CHECK );
//TIMER_STOP
		}
		catch ( CException* pException )
		{
			if ( pFile.m_hFile != CFile::hFileNull )
				pFile.Close();	// File is still open so close it
			pException->Delete();
		}

		m_pQueue.RemoveHead();	// Done
	}

	Exit();
	Wakeup();

	Sleep( 5000 );

	// Recheck
	if ( ! m_pQueue.GetCount() )	//  && IsThreadEnabled()
	{
		CQuickLock oLock( Security.m_pSection );

		Security.m_Cache.clear();

		PostMainWndMessage( WM_SANITY_CHECK );
	}
}