⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 remoteadministrator.cpp

📁 <Visual C++ 网络程序设计实例详解>配套源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        strRemoteMachineIP.GetBuffer(0), 
        REMOTE_ADMIN_PROCESS_EXECUTE_PIPE
        );

    // Remote service communication pipe name
    ::sprintf(
        szRemoteAdminProcessKillPipeName, 
        _T("\\\\%s\\pipe\\%s"), 
        strRemoteMachineIP.GetBuffer(0), 
        REMOTE_ADMIN_PROCESS_KILL_PIPE
        );

    // Remote shutdown pipe
    ::sprintf(
        szRemoteAdminSysShutdownPipe,
        _T("\\\\%s\\pipe\\%s"), 
        strRemoteMachineIP.GetBuffer(0), 
        REMOTE_ADMIN_SYS_SHUTDOWN_PIPE
        );


    SECURITY_ATTRIBUTES SecAttrib = {0};
    SECURITY_DESCRIPTOR SecDesc;
    ::InitializeSecurityDescriptor(&SecDesc, SECURITY_DESCRIPTOR_REVISION);
    ::SetSecurityDescriptorDacl(&SecDesc, TRUE, NULL, TRUE);

    SecAttrib.nLength = sizeof(SECURITY_ATTRIBUTES);
    SecAttrib.lpSecurityDescriptor = &SecDesc;;
    SecAttrib.bInheritHandle = TRUE;

    // Connects to the remote service's communication pipe
    while(dwRetry--)
    {
        if (::WaitNamedPipe(szRemoteAdminPipeName, 5000))
        {
            hCommandPipe = ::CreateFile( 
                                 szRemoteAdminPipeName,
                                 GENERIC_WRITE | GENERIC_READ, 
                                 0,
                                 &SecAttrib, 
                                 OPEN_EXISTING, 
                                 FILE_ATTRIBUTE_NORMAL, 
                                 NULL
                                 );

             ::CloseHandle(hCommandPipe);  

             CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteMachineIP);
             //pMachineInfo->SetRemoteAdminPipe(hCommandPipe);

             ::Sleep(10000);
             if (::WaitNamedPipe(szRemoteAdminProcessInfoPipeName, 5000))
             {
                 hCommandPipe = ::CreateFile( 
                                 szRemoteAdminProcessInfoPipeName,
                                 GENERIC_WRITE | GENERIC_READ, 
                                 0,
                                 &SecAttrib, 
                                 OPEN_EXISTING, 
                                 FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, 
                                 NULL
                                 );

                 pMachineInfo->SetRemoteAdminProcessInfoPipe(hCommandPipe);
             }

             if (::WaitNamedPipe(szRemoteAdminProcessExecutePipeName, 5000))
             {
                 hCommandPipe = ::CreateFile( 
                                 szRemoteAdminProcessExecutePipeName,
                                 GENERIC_WRITE | GENERIC_READ, 
                                 0,
                                 &SecAttrib, 
                                 OPEN_EXISTING, 
                                 FILE_ATTRIBUTE_NORMAL, 
                                 NULL
                                 );

                 pMachineInfo->SetRemoteAdminProcessExecutePipe(hCommandPipe);
             }

             if (::WaitNamedPipe(szRemoteAdminProcessKillPipeName, 5000))
             {
                 hCommandPipe = ::CreateFile( 
                                 szRemoteAdminProcessKillPipeName,
                                 GENERIC_WRITE | GENERIC_READ, 
                                 0,
                                 &SecAttrib, 
                                 OPEN_EXISTING, 
                                 FILE_ATTRIBUTE_NORMAL, 
                                 NULL
                                 );

                 pMachineInfo->SetRemoteAdminProcessKillPipe(hCommandPipe);
             }

             if (::WaitNamedPipe(szRemoteAdminSysShutdownPipe, 5000))
             {
                 hCommandPipe = ::CreateFile( 
                                 szRemoteAdminSysShutdownPipe,
                                 GENERIC_WRITE | GENERIC_READ, 
                                 0,
                                 &SecAttrib, 
                                 OPEN_EXISTING, 
                                 FILE_ATTRIBUTE_NORMAL, 
                                 NULL
                                 );

                 pMachineInfo->SetRemoteAdminSysShutDownPipe(hCommandPipe);
             }

            
             break;
         }
         else
         {
             // Let's try it again
             ::Sleep(dwRetryTimeOut);
         }
    }

    return hCommandPipe != INVALID_HANDLE_VALUE;
}

BOOL CRemoteAdministrator::InstallAndStartRemoteService(CString strRemoteMachineIP)
{
    // Open remote Service Manager
    SC_HANDLE hSCM = ::OpenSCManager(
                         strRemoteMachineIP.GetBuffer(0), 
                         NULL, 
                         SC_MANAGER_ALL_ACCESS
                         );

    if (hSCM == NULL)
    {
        return FALSE;
    }
      
   
    // Maybe it's already there and installed, let's try to run
    SC_HANDLE hService =::OpenService(hSCM, SERVICENAME, SERVICE_ALL_ACCESS);

    // Creates service on remote machine, if it's not installed yet
    if (hService == NULL)
    {
        hService = ::CreateService(
                        hSCM, 
                        SERVICENAME, 
                        LONGSERVICENAME,
                        SERVICE_ALL_ACCESS, 
                        SERVICE_WIN32_OWN_PROCESS,
                        SERVICE_DEMAND_START, 
                        SERVICE_ERROR_NORMAL,
                        _T("%SystemRoot%\\system32\\")REMOTE_ADMIN_SERVICE_EXE,
                        NULL, 
                        NULL, 
                        NULL, 
                        NULL, 
                        NULL
                        );
    }
      
   
    if (hService == NULL)
    {
        ::CloseServiceHandle(hSCM);
        return FALSE;
    }

    // Start service
    if (!::StartService(hService, 0, NULL))
    {
        return FALSE;
    }

    ::CloseServiceHandle(hService);
    ::CloseServiceHandle(hSCM);

    return TRUE;
}

CProcessInfoList* CRemoteAdministrator::GetProcessInfoList(CString strRemoteMachineIP)
{
    CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteMachineIP);
    
    return pMachineInfo->GetProcessInfoList();
}

void CRemoteAdministrator::DeleteAndDisconnectAllMachines()
{
    CMachineInfo* pMachineInfo = NULL;

    POSITION pos = m_milConnectedMachines.GetHeadPosition();
    while (pos != (POSITION)0xcdcdcdcd && pos != NULL)
    {
        pMachineInfo = m_milConnectedMachines.GetNext(pos);

        // Tell server threads to end
        pMachineInfo->SendEndThreadMessage();
        
        // Close the pipe handles
        pMachineInfo->ClosePipeHandles();

        // End the connection
        EstablishAllConnections(pMachineInfo->GetIP(), pMachineInfo->GetPassword(), FALSE);

        // Free memory 
        delete pMachineInfo;
    }

    m_milConnectedMachines.RemoveAll();
}

void CRemoteAdministrator::DeleteAndDisconnectMachine(CString strRemoteAdminMachine)
{
    CMachineInfo* pMachineInfo = GetMachineInfo(strRemoteAdminMachine);

	if (pMachineInfo)
	{
		pMachineInfo->SendEndThreadMessage();
		pMachineInfo->ClosePipeHandles();
		BOOL bConnectionDeleted = EstablishAllConnections(pMachineInfo->GetIP(), pMachineInfo->GetPassword(), FALSE);
        DeleteMachine(*pMachineInfo);
	}
}


void CRemoteAdministrator::GetConnectedMachinesIP(CString** pstrConnctedMachinesIP/*out*/, int* piNumberOfConnectedMachines /*out*/)
{
	::EnterCriticalSection(&g_CriticalSection);
    // Get the number of machines
    *piNumberOfConnectedMachines = m_milConnectedMachines.GetCount();

    // Allocate array of CStrings to hold IP of these machines
    *pstrConnctedMachinesIP = new CString[*piNumberOfConnectedMachines];

    // Now loop throught the machine info list and upadte the IP array
    CMachineInfo* pMachineInfo = NULL;
    POSITION pos = m_milConnectedMachines.GetHeadPosition();
    int iIndex = 0;
    while (pos != (POSITION)0xcdcdcdcd && pos != NULL)
    {
        pMachineInfo = m_milConnectedMachines.GetNext(pos);
        (*pstrConnctedMachinesIP)[iIndex] = pMachineInfo->GetIP();
        ++iIndex;
    }
	::LeaveCriticalSection(&g_CriticalSection);
}

int CRemoteAdministrator::GetTotalMachinesMonitored()
{
    return m_milConnectedMachines.GetCount();
}

BOOL CRemoteAdministrator::AddToConnectionPendingList(CString strIP)
{
    CString* pstrPendinConnectionIP = new CString;
    if (pstrPendinConnectionIP != NULL)
    {
        *pstrPendinConnectionIP = strIP;
        m_cplPendingConnectionList.AddTail(pstrPendinConnectionIP);

        return TRUE;
    }

    return FALSE;
}

BOOL CRemoteAdministrator::RemoveFromConnecionPendingList(CString strIP)
{
    POSITION pos = m_cplPendingConnectionList.GetHeadPosition();

    while (pos != (POSITION)0xcdcdcdcd && pos != NULL)
    {
        CString* pstrIP = m_cplPendingConnectionList.GetAt(pos);

        if (*pstrIP == strIP)
        {
            m_cplPendingConnectionList.RemoveAt(pos);

            delete pstrIP;

            return TRUE;
        }

        m_cplPendingConnectionList.GetNext(pos);
    }

    return FALSE;
}

BOOL CRemoteAdministrator::IsConnectionPending(CString strIP)
{
    POSITION pos = m_cplPendingConnectionList.GetHeadPosition();

    while (pos != (POSITION)0xcdcdcdcd && pos != NULL)
    {
        CString* pstrIP = m_cplPendingConnectionList.GetNext(pos);

        if (*pstrIP == strIP)
        {
             return TRUE;
        }
    }

    return FALSE;
}

CString CRemoteAdministrator::GetPassword(CString strIP)
{
	CMachineInfo* pMachineInfo = GetMachineInfo(strIP);

	ASSERT(pMachineInfo != NULL);

	if (pMachineInfo != NULL)
	{
		return pMachineInfo->GetPassword();
	}
	else
	{
		return _T("Could not retrieve the password");
	}
}

UINT CRemoteAdministrator::GetNumberOfMachineConnectionsStillInProgress()
{
	return m_cplPendingConnectionList.GetCount();
}


CString CRemoteAdministrator::GetComputerNameFromIP(CString& strIP)
{
	unsigned long ulAddr = ::inet_addr(LPCTSTR(strIP));
	CString strHostName = _T("0");

	if (ulAddr != INADDR_NONE)  // Valid IP address passed, hence could resolve address
	{
		HOSTENT* pHostEnt    = ::gethostbyaddr((char*)&ulAddr, sizeof(ulAddr), AF_INET);
		if (pHostEnt != NULL)
		{
			strHostName = pHostEnt->h_addr;

			return strHostName;
		}
	}

	ASSERT(NULL); // Shouldn't reach here, if Winsock initialized properly
    return strHostName;	
}


CString CRemoteAdministrator::GetComputerIPFromName(CString& strComputerName)
{
	unsigned long ulAddr = ::inet_addr(LPCTSTR(strComputerName));
	CString strIP = _T("0");

	if (ulAddr == INADDR_NONE) // Not a valid IP in dotted format, so can be a string
	{
		HOSTENT* pHostEnt = ::gethostbyname((LPCTSTR)(strComputerName));
		if (pHostEnt != NULL)
		{
			in_addr* addr = (in_addr*)pHostEnt->h_addr_list[0]; 
			
			int ip1 = addr->S_un.S_un_b.s_b1;
            int ip2 = addr->S_un.S_un_b.s_b2;
            int ip3 = addr->S_un.S_un_b.s_b3;
            int ip4 = addr->S_un.S_un_b.s_b4;

			strIP.Format("%d.%d.%d.%d", ip1, ip2, ip3, ip4);
			return strIP;
		}
	}

	ASSERT(NULL); // Shouldn't reach here, if Winsock initialized properly
    return strIP;	
}

⌨️ 快捷键说明

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