/// <summary> /// Checks to find whether the Windows XP/2003 Firewall is enabled on adapters and if so it opens ports. /// </summary> /// <param name="portMapName">Name of the Port Map to look for (must be the same as when it was added)</param> /// <param name="port">Port Number</param> /// <param name="protocolIsTcp">true if TCP, false if UDP</param> /// <remarks> /// WARNING: This method does not inform the user that the firewall punchthrough is being added. Applications /// should always inform the user when adding punchthroughs to the firewall, for security reasons. /// </remarks> public static void RemoveOldFirewallPort(string portMapName, ushort port, ProtocolType protocol) { ValidateForOldCompatibleFirewall(); ValidateAdministrator(); byte protocolAsByte = ConvertAndValidateProtocol(protocol); INetSharingManager mgr = new NetSharingManagerClass(); // Iterate through all of the available connections foreach (INetConnection iCon in mgr.EnumEveryConnection) { INetSharingConfiguration iShareConfig = mgr.get_INetSharingConfigurationForINetConnection(iCon); if (iShareConfig.InternetFirewallEnabled) // skip this connection if the firewall is disabled { foreach (INetSharingPortMapping portMap in iShareConfig.get_EnumPortMappings(tagSHARINGCONNECTION_ENUM_FLAGS.ICSSC_ENABLED)) { // Remove this port mapping only if the name & port match if ((ushort)(portMap.Properties.ExternalPort) == port && portMap.Properties.IPProtocol == protocolAsByte) { if (String.Compare(portMap.Properties.Name, portMapName) == 0) { iShareConfig.RemovePortMapping(portMap); } } } } } }
/// <summary> /// Checks to find whether the Windows XP/2003 Firewall is enabled on adapters and if so it opens ports. /// </summary> /// <param name="portMapName">Name of the Port Map to look for</param> /// <param name="port">Port Number</param> /// <param name="protocolIsTcp">true if TCP, false if UDP</param> /// <remarks> /// WARNING: This method does not inform the user that the firewall punchthrough is being added. Applications /// should always inform the user when adding punchthroughs to the firewall, for security reasons. /// </remarks> public static void AddOldFirewallPort(string portMapName, ushort port, ProtocolType protocol) { ValidateForOldCompatibleFirewall(); ValidateAdministrator(); // Get the protocolAsByte ICF constant byte protocolAsByte = ConvertAndValidateProtocol(protocol); INetSharingManager mgr = new NetSharingManagerClass(); // Iterate through all of the available connections foreach (INetConnection iCon in mgr.EnumEveryConnection) { INetSharingConfiguration iShareConfig = mgr.get_INetSharingConfigurationForINetConnection(iCon); if (iShareConfig.InternetFirewallEnabled) // skip this connection if the firewall is disabled { // Make sure that this firewall doesn't already have a port map for the same port bool portMapExists = false; foreach (INetSharingPortMapping portMap in iShareConfig.get_EnumPortMappings(tagSHARINGCONNECTION_ENUM_FLAGS.ICSSC_ENABLED)) { if ((ushort)(portMap.Properties.ExternalPort) == port && portMap.Properties.IPProtocol == protocolAsByte) { portMapExists = true; break; } } if (!portMapExists) { // Finally, add & enable the new port map INetSharingPortMapping newPortMap = iShareConfig.AddPortMapping(portMapName, protocolAsByte, port, port, 0, SystemInformation.ComputerName, tagICS_TARGETTYPE.ICSTT_NAME); newPortMap.Enable(); } } } }