/// <summary> /// Checks whether an IP address matches to an exception rule /// </summary> /// <param name="ipAddress">The IP address to check</param> /// <param name="exceptionRule">The exception rule to match it against</param> /// <returns>True if the IP address matches to the exception rule. False otherwise</returns> private bool IpAddressMatchesExceptionRule(IPAddress ipAddress, ExceptionRule exceptionRule) { if (String.IsNullOrEmpty(exceptionRule.Mask) && IPAddress.Parse(exceptionRule.IpAddress).Equals(ipAddress)) { return(true); } if (!String.IsNullOrEmpty(exceptionRule.Mask) && IPUtilities.IsInSameSubnet(ipAddress, exceptionRule.IpAddress, exceptionRule.Mask)) { return(true); } return(false); }
/// <summary> /// Checks for conflicts between the new rule and the existing exception rules /// </summary> /// <param name="newRule">The new rule to check</param> /// <returns>True if there is no conflict. False otherwise</returns> private bool CheckNewRule(ExceptionRule newRule) { foreach (ExceptionRule existingRule in this.existingRules) { if (String.IsNullOrEmpty(newRule.Mask)) { if (String.IsNullOrEmpty(existingRule.Mask)) { if (newRule.IpAddress.Equals(existingRule.IpAddress)) { if (newRule.AllowedMode == existingRule.AllowedMode) { MessageBox.Show(this, "This exception rule allready exists", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(false); } else { MessageBox.Show(this, "This exception rule conflicts with rule: " + existingRule.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(false); } } } else { if (IPUtilities.IsInSameSubnet(newRule.IpAddress, existingRule.IpAddress, existingRule.Mask)) { if (newRule.AllowedMode == existingRule.AllowedMode) { DialogResult result = MessageBox.Show(this, "This exception rule overlaps with rule: " + existingRule.ToString() + ". Add it anyway?", this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) { return(true); } else { return(false); } } else { MessageBox.Show(this, "This exception rule conflicts with rule: " + existingRule.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(false); } } } } else { if (String.IsNullOrEmpty(existingRule.Mask)) { if (IPUtilities.IsInSameSubnet(existingRule.IpAddress, newRule.IpAddress, newRule.Mask)) { if (newRule.AllowedMode == existingRule.AllowedMode) { DialogResult result = MessageBox.Show(this, "This exception rule overlaps with rule: " + existingRule.ToString() + ". Add it anyway?", this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) { return(true); } else { return(false); } } else { MessageBox.Show(this, "This exception rule conflicts with rule: " + existingRule.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(false); } } } else { if (IPUtilities.IsInSameSubnet(newRule.IpAddress, existingRule.IpAddress, existingRule.Mask) || IPUtilities.IsInSameSubnet(existingRule.IpAddress, newRule.IpAddress, newRule.Mask)) { if (newRule.AllowedMode == existingRule.AllowedMode) { DialogResult result = MessageBox.Show(this, "This exception rule overlaps with rule: " + existingRule.ToString() + ". Add it anyway?", this.Text, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1); if (result == DialogResult.Yes) { return(true); } else { return(false); } } else { MessageBox.Show(this, "This exception rule conflicts with rule: " + existingRule.ToString(), this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1); return(false); } } } } } return(true); }