mirror of
https://github.com/xcp-ng/xenadmin.git
synced 2024-11-23 12:30:50 +01:00
CA-116005: validation of the SN mask doesn't appear to be working correctly on the last two values of the mask.
-New IPv4 subnet mask validator added. Signed-off-by: Gabor Apati-Nagy <gabor.apati-nagy@citrix.com>
This commit is contained in:
parent
9825ffc41f
commit
205ffe16d9
@ -151,7 +151,7 @@ namespace XenAdmin.Dialogs
|
||||
NetworkComboBox.SelectedIndex != -1 &&
|
||||
(DHCPIPRadioButton.Checked ||
|
||||
((StringUtility.IsIPAddress(IPAddressTextBox.Text)) &&
|
||||
StringUtility.IsIPAddress(SubnetTextBox.Text) && IsOptionalIPAddress(GatewayTextBox.Text))) &&
|
||||
StringUtility.IsValidNetmask(SubnetTextBox.Text) && IsOptionalIPAddress(GatewayTextBox.Text))) &&
|
||||
(type == Type.SECONDARY || ((IsOptionalIPAddress(PreferredDNSTextBox.Text)
|
||||
&& IsOptionalIPAddress(AlternateDNS1TextBox.Text)
|
||||
&& IsOptionalIPAddress(AlternateDNS2TextBox.Text))));
|
||||
|
@ -33,6 +33,8 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Linq;
|
||||
using System.Collections;
|
||||
|
||||
namespace XenAdmin.Core
|
||||
{
|
||||
@ -150,6 +152,59 @@ namespace XenAdmin.Core
|
||||
private static readonly Regex IPRegex = new Regex(@"^([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$");
|
||||
private static readonly Regex IPRegex0 = new Regex(@"^([0]{1,3})\.([0]{1,3})\.([0]{1,3})\.([0]{1,3})$");
|
||||
|
||||
/// <summary>
|
||||
/// Validates IPv4 subnet mask string (strict)
|
||||
/// Eg. 255.255.240.0 is valid
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Logically valid values with trailing zeros will not pass validation
|
||||
/// Eg. 255.255.240.00 will return NOT valid
|
||||
/// </remarks>
|
||||
/// <param name="netmask"></param>
|
||||
/// <returns></returns>
|
||||
public static bool IsValidNetmask(string netmask)
|
||||
{
|
||||
if (netmask == null)
|
||||
return false;
|
||||
|
||||
var netmaskBytes = new List<byte>();
|
||||
var parts = netmask.Split('.').ToList();
|
||||
|
||||
if (parts.Count != 4 || parts.Any(p => p.Length > 3))
|
||||
return false;
|
||||
|
||||
foreach (var part in parts)
|
||||
{
|
||||
byte byteValue;
|
||||
|
||||
//Converting value to byte if possible. The second check is to make sure that valid, but server side not supported values not get through (eg. 000, 00)
|
||||
if (!byte.TryParse(part, System.Globalization.NumberStyles.None, null, out byteValue) || byteValue.ToString() != part)
|
||||
return false;
|
||||
|
||||
netmaskBytes.Add(byteValue);
|
||||
}
|
||||
|
||||
var bits = new BitArray(netmaskBytes.ToArray());
|
||||
if (bits.Count != 32)
|
||||
return false;
|
||||
|
||||
bool wasZero = false;
|
||||
for (int octetNo = 0; octetNo < 4; octetNo ++)
|
||||
for (int relPos = 7; relPos >=0 ; relPos --) //less significant bit is on the left
|
||||
{
|
||||
bool val = bits[octetNo * 8 + relPos];
|
||||
|
||||
//if there is 1 again and there has been any 0 before, netmask is invalid. All other cases (if we get here) are valid.
|
||||
if (wasZero && val)
|
||||
return false;
|
||||
|
||||
if (!val)
|
||||
wasZero = true;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static bool IsIPAddress(string s)
|
||||
{
|
||||
if (string.IsNullOrEmpty(s))
|
||||
|
Loading…
Reference in New Issue
Block a user