Add functions to the IP address. We need to use them and then test, test, test...

This commit is contained in:
Tim Young 2020-12-07 12:00:10 -07:00
parent fdc3877e4f
commit 2f63aacecd

View File

@ -24,11 +24,13 @@ namespace EduNetworkBuilder
private IPAddress theNetmask; private IPAddress theNetmask;
private IPAddress theGateway; private IPAddress theGateway;
private bool Unset = true;
public NB_IPAddress() { } //used for reflection public NB_IPAddress() { } //used for reflection
/// <summary> /// <summary>
/// Duplicate an IP address structure /// Duplicate an IP address structure
/// </summary> /// </summary>
/// <param name="Orig"></param> /// <param name="Orig">The IP to clone from</param>
public NB_IPAddress(NB_IPAddress Orig) public NB_IPAddress(NB_IPAddress Orig)
{ {
_ip = Orig._ip; _ip = Orig._ip;
@ -38,22 +40,42 @@ namespace EduNetworkBuilder
theIP = Orig.theIP; theIP = Orig.theIP;
theNetmask = Orig.theNetmask; theNetmask = Orig.theNetmask;
theGateway = Orig.theGateway; theGateway = Orig.theGateway;
Unset = false;
} }
/// <summary>
/// Make a simple IP address with netmask
/// </summary>
/// <param name="ip">The new IP</param>
/// <param name="mask">The new netmask</param>
/// <param name="WhatType"></param>
public NB_IPAddress(string ip, string mask, IPAddressType WhatType) public NB_IPAddress(string ip, string mask, IPAddressType WhatType)
{ {
myType = WhatType; myType = WhatType;
_ip = ip.ParseIp(); _ip = ip.ParseIp();
_mask = mask.ParseIp(); _mask = mask.ParseIp();
Unset = false;
} }
/// <summary>
/// Make a new IP address, netmask, with gateway.
/// </summary>
/// <param name="ip">The IP address</param>
/// <param name="mask">The netmask</param>
/// <param name="gw">The gateway to use</param>
public NB_IPAddress(string ip, string mask, string gw) public NB_IPAddress(string ip, string mask, string gw)
{ {
myType = IPAddressType.route; myType = IPAddressType.route;
_ip = ip.ParseIp(); _ip = ip.ParseIp();
_mask = mask.ParseIp(); _mask = mask.ParseIp();
_gw = gw.ParseIp(); _gw = gw.ParseIp();
Unset = false;
} }
/// <summary>
/// Load an IP address from the XML file
/// </summary>
/// <param name="theNode">The XmlNode to load from</param>
public NB_IPAddress(XmlNode theNode) public NB_IPAddress(XmlNode theNode)
{ {
foreach (XmlNode Individual in theNode.ChildNodes) foreach (XmlNode Individual in theNode.ChildNodes)
@ -65,12 +87,15 @@ namespace EduNetworkBuilder
{ {
case "ip": case "ip":
_ip = Individual.InnerText.ParseIp(); _ip = Individual.InnerText.ParseIp();
Unset = false;
break; break;
case "mask": case "mask":
_mask = Individual.InnerText.ParseIp(); _mask = Individual.InnerText.ParseIp();
Unset = false;
break; break;
case "gateway": case "gateway":
_gw = Individual.InnerText.ParseIp(); _gw = Individual.InnerText.ParseIp();
Unset = false;
break; break;
case "type": case "type":
myType = NB.ParseEnum<IPAddressType>(Individual.InnerText); myType = NB.ParseEnum<IPAddressType>(Individual.InnerText);
@ -82,7 +107,8 @@ namespace EduNetworkBuilder
public void SetIP(UInt32 newIP) public void SetIP(UInt32 newIP)
{ {
_ip = newIP; _ip = newIP;
Unset = false;
} }
public bool Equals(NB_IPAddress CompareWith) public bool Equals(NB_IPAddress CompareWith)
@ -109,11 +135,13 @@ namespace EduNetworkBuilder
_ip = ip.ParseIp(); _ip = ip.ParseIp();
_mask = mask.ParseIp(); _mask = mask.ParseIp();
_gw = gw.ParseIp(); _gw = gw.ParseIp();
Unset = false;
} }
public void Reparse(string ip, string mask) public void Reparse(string ip, string mask)
{ {
_ip = ip.ParseIp(); _ip = ip.ParseIp();
_mask = mask.ParseIp(); _mask = mask.ParseIp();
Unset = false;
} }
public IPAddressType GetAddressType public IPAddressType GetAddressType
@ -298,6 +326,53 @@ namespace EduNetworkBuilder
return false; return false;
} }
#region IP Status Funcs
/// <summary>
/// If the IP address has not yet been assigned
/// </summary>
public bool IsUnassigned() { return Unset; }
/// <summary>
/// If the IP address is the same as the broadcast address
/// </summary>
public bool IsBroadcast()
{
uint BA = BroadcastAddress;
if (_ip == BA) return true;
return false;
}
/// <summary>
/// If the IP address is the same as the network address
/// </summary>
public bool IsNetwork()
{
uint NA = NetworkAddress;
if (_ip == NA) return true;
return false;
}
/// <summary>
/// If the IP address is the loopback address. Different for IPv4 than IPv6
/// </summary>
public bool IsLoopback()
{
if (GetIPString == "127.0.0.1") return true;
return false;
}
/// <summary>
/// If the IP address is 0.0.0.0 or the equavelent
/// </summary>
/// <returns>true if it is zeroes could be unset or anywhere</returns>
public bool IsZeroString() {
if (_ip == 0) return true;
return false;
}
#endregion
#region Break an IP #region Break an IP
public string GenRandomIPOrMask() public string GenRandomIPOrMask()