IP addresses print CIDR of /? if it is not a real CIDR approved subnet mask. For example, a subnet mask of .250 gives a /?, but .252 gives /30. While this is not real-life (real life does not tell us when we use a foolish subnet), it helps considerably when testing things.
This commit is contained in:
parent
a1a7d10a56
commit
9c764c4f90
@ -6,7 +6,7 @@ using System.Threading.Tasks;
|
|||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using System.Xml;
|
using System.Xml;
|
||||||
using System.Windows.Forms;
|
using System.Windows.Forms;
|
||||||
|
using System.Text.RegularExpressions;
|
||||||
|
|
||||||
namespace EduNetworkBuilder
|
namespace EduNetworkBuilder
|
||||||
{
|
{
|
||||||
@ -141,18 +141,23 @@ namespace EduNetworkBuilder
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
UInt32 tInt = 0;
|
_mask = MaskNumFromCIDRString(mySplitVal[1]);
|
||||||
int cdr;
|
|
||||||
int.TryParse(mySplitVal[1], out cdr);
|
|
||||||
for (int loop = 0; loop < 32; loop++)
|
|
||||||
{
|
|
||||||
tInt = (tInt << 1);
|
|
||||||
if (loop < cdr) tInt++;
|
|
||||||
}
|
|
||||||
_mask = tInt;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static UInt32 MaskNumFromCIDRString(string cidr)
|
||||||
|
{
|
||||||
|
UInt32 tInt = 0;
|
||||||
|
int cdr;
|
||||||
|
int.TryParse(cidr, out cdr);
|
||||||
|
for (int loop = 0; loop < 32; loop++)
|
||||||
|
{
|
||||||
|
tInt = (tInt << 1);
|
||||||
|
if (loop < cdr) tInt++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tInt;
|
||||||
|
}
|
||||||
|
|
||||||
public bool Equals(UInt32 IP)
|
public bool Equals(UInt32 IP)
|
||||||
{
|
{
|
||||||
@ -228,6 +233,31 @@ namespace EduNetworkBuilder
|
|||||||
PadIt(_ip.ToIpString()), PadIt(_mask.ToIpString()), PadIt(gw));
|
PadIt(_ip.ToIpString()), PadIt(_mask.ToIpString()), PadIt(gw));
|
||||||
return tstring;
|
return tstring;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return the CIDR number for this address. This is the number of 1s before the first 0
|
||||||
|
/// </summary>
|
||||||
|
/// <returns></returns>
|
||||||
|
public int CIDRNumber()
|
||||||
|
{
|
||||||
|
string mask = GetMask.ToBitString();
|
||||||
|
mask = Regex.Replace(mask, "0", "");
|
||||||
|
return mask.Length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Return true if the subnet mask is really a true CIDR string /8, /16, etc.
|
||||||
|
/// It is false if the subnet mask creates a mask that does not map to CIDR. For
|
||||||
|
/// example, 255.255.255.250 is not a real subnet mask. 252 is.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>True if there are no 1s after the first 0</returns>
|
||||||
|
public bool ValidCIDR()
|
||||||
|
{
|
||||||
|
int cidr = CIDRNumber();
|
||||||
|
UInt32 tMask = MaskNumFromCIDRString(cidr.ToString());
|
||||||
|
if (tMask == _mask) return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class IpHelpers
|
public static class IpHelpers
|
||||||
|
@ -151,10 +151,10 @@ namespace EduNetworkBuilder
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
string mask = myIP.GetMask.ToBitString();
|
int cidr = myIP.CIDRNumber();
|
||||||
mask = Regex.Replace(mask, "0", "");
|
string answer = cidr.ToString();
|
||||||
int count = mask.Length;
|
if (!myIP.ValidCIDR()) answer = "?";
|
||||||
tstring += "/" + count.ToString();
|
tstring += "/" + answer;
|
||||||
}
|
}
|
||||||
return tstring;
|
return tstring;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user