bool CFirewallOpener::AddRule(const CICSRuleInfo& riPortRule, const INetSharingConfigurationPtr pNSC, const INetConnectionPropsPtr pNCP) { INetSharingPortMappingPtr pNSPM; HRESULT hr = pNSC->AddPortMapping(riPortRule.m_strRuleName.AllocSysString(), riPortRule.m_byProtocol, riPortRule.m_nPortNumber, riPortRule.m_nPortNumber, 0, CComBSTR(L"127.0.0.1"), ICSTT_IPADDRESS, &pNSPM); CComBSTR bstrName; pNCP->get_Name(&bstrName); if ( SUCCEEDED(hr) && SUCCEEDED(pNSPM->Enable())) { // ==> Improved ICS-Firewall support [MoNKi] - Max if(riPortRule.m_bRemoveOnExit || m_bClearMappings) { CICSRuleInfo ruleToAdd(riPortRule); AddToICFdat(ruleToAdd); } // <== Improved ICS-Firewall support [MoNKi] - Max theApp.QueueDebugLogLine(false, _T("Succeeded to add Rule '%s' for Port '%u' on Connection '%s'"),riPortRule.m_strRuleName, riPortRule.m_nPortNumber, CString(bstrName)); return true; } else { theApp.QueueDebugLogLine(false, _T("Failed to add Rule '%s' for Port '%u' on Connection '%s'"),riPortRule.m_strRuleName, riPortRule.m_nPortNumber, CString(bstrName)); return false; } }
bool CFirewallOpener::AddRule(const CICSRuleInfo& riPortRule, const INetSharingConfigurationPtr pNSC, const INetConnectionPropsPtr pNCP){ INetSharingPortMappingPtr pNSPM; HRESULT hr = pNSC->AddPortMapping(riPortRule.m_strRuleName.AllocSysString(), riPortRule.m_byProtocol, riPortRule.m_nPortNumber, riPortRule.m_nPortNumber, 0, L"127.0.0.1", ICSTT_IPADDRESS, &pNSPM); CComBSTR bstrName; pNCP->get_Name(&bstrName); if ( SUCCEEDED(hr) && SUCCEEDED(pNSPM->Enable())){ theApp.QueueDebugLogLine(false, _T("Succeeded to add Rule '%s' for Port '%u' on Connection '%s'"),riPortRule.m_strRuleName, riPortRule.m_nPortNumber, CString(bstrName)); return true; } else{ theApp.QueueDebugLogLine(false, _T("Failed to add Rule '%s' for Port '%u' on Connection '%s'"),riPortRule.m_strRuleName, riPortRule.m_nPortNumber, CString(bstrName)); return false; } }
bool CFirewallOpener::FindRule(const EFOCAction eAction, const CICSRuleInfo& riPortRule, const INetSharingConfigurationPtr pNSC, INetSharingPortMappingPropsPtr* outNSPMP){ INetSharingPortMappingCollectionPtr pNSPMC; RETURN_ON_FAIL(pNSC->get_EnumPortMappings (ICSSC_DEFAULT, &pNSPMC)); INetSharingPortMappingPtr pNSPM; IEnumVARIANTPtr varEnum; IUnknownPtr pUnk; RETURN_ON_FAIL(pNSPMC->get__NewEnum(&pUnk)); RETURN_ON_FAIL(pUnk->QueryInterface(__uuidof(IEnumVARIANT), (void**)&varEnum)); _variant_t var; while (S_OK == varEnum->Next(1, &var, NULL)) { INetSharingPortMappingPropsPtr pNSPMP; if (V_VT(&var) == VT_DISPATCH && SUCCEEDED(V_DISPATCH(&var)->QueryInterface(__uuidof(INetSharingPortMapping),(void**)&pNSPM)) && SUCCEEDED(pNSPM->get_Properties (&pNSPMP))) { UCHAR ucProt = 0; long uExternal = 0; CComBSTR bstrName; pNSPMP->get_IPProtocol (&ucProt); pNSPMP->get_ExternalPort (&uExternal); pNSPMP->get_Name(&bstrName); switch(eAction){ case FOC_FINDRULEBYPORT: if (riPortRule.m_nPortNumber == uExternal && riPortRule.m_byProtocol == ucProt){ if (outNSPMP != NULL) *outNSPMP = pNSPM; return true; } break; case FOC_FINDRULEBYNAME: if (riPortRule.m_strRuleName == CString(bstrName)){ if (outNSPMP != NULL) *outNSPMP = pNSPM; return true; } break; case FOC_DELETERULEEXCACT: if (riPortRule.m_strRuleName == CString(bstrName) && riPortRule.m_nPortNumber == uExternal && riPortRule.m_byProtocol == ucProt) { RETURN_ON_FAIL(pNSC->RemovePortMapping(pNSPM)); theApp.QueueDebugLogLine(false,_T("Rule removed")); } break; case FOC_DELETERULEBYNAME: if (riPortRule.m_strRuleName == CString(bstrName)){ RETURN_ON_FAIL(pNSC->RemovePortMapping(pNSPM)); theApp.QueueDebugLogLine(false,_T("Rule removed")); } break; default: ASSERT( false ); } } var.Clear(); } switch(eAction){ case FOC_DELETERULEBYNAME: case FOC_DELETERULEEXCACT: return true; case FOC_FINDRULEBYPORT: case FOC_FINDRULEBYNAME: default: return false; } }