11 Commits

8 changed files with 132 additions and 48 deletions

View File

@ -24,11 +24,13 @@ namespace EduNetworkBuilder
private IPAddress theNetmask;
private IPAddress theGateway;
private bool Unset = true;
public NB_IPAddress() { } //used for reflection
/// <summary>
/// Duplicate an IP address structure
/// </summary>
/// <param name="Orig"></param>
/// <param name="Orig">The IP to clone from</param>
public NB_IPAddress(NB_IPAddress Orig)
{
_ip = Orig._ip;
@ -38,22 +40,42 @@ namespace EduNetworkBuilder
theIP = Orig.theIP;
theNetmask = Orig.theNetmask;
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)
{
myType = WhatType;
_ip = ip.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)
{
myType = IPAddressType.route;
_ip = ip.ParseIp();
_mask = mask.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)
{
foreach (XmlNode Individual in theNode.ChildNodes)
@ -65,12 +87,15 @@ namespace EduNetworkBuilder
{
case "ip":
_ip = Individual.InnerText.ParseIp();
Unset = false;
break;
case "mask":
_mask = Individual.InnerText.ParseIp();
Unset = false;
break;
case "gateway":
_gw = Individual.InnerText.ParseIp();
Unset = false;
break;
case "type":
myType = NB.ParseEnum<IPAddressType>(Individual.InnerText);
@ -82,7 +107,8 @@ namespace EduNetworkBuilder
public void SetIP(UInt32 newIP)
{
_ip = newIP;
_ip = newIP;
Unset = false;
}
public bool Equals(NB_IPAddress CompareWith)
@ -109,11 +135,13 @@ namespace EduNetworkBuilder
_ip = ip.ParseIp();
_mask = mask.ParseIp();
_gw = gw.ParseIp();
Unset = false;
}
public void Reparse(string ip, string mask)
{
_ip = ip.ParseIp();
_mask = mask.ParseIp();
Unset = false;
}
public IPAddressType GetAddressType
@ -298,6 +326,57 @@ namespace EduNetworkBuilder
return false;
}
#region IP Status Funcs
/// <summary>
/// If the IP address has not yet been assigned
/// </summary>
public bool IsUnassigned() {
if (GetIPString == NB.ZeroIPString) return true; //Sometimes we use 0.0.0.0 as unset.
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; //the actual broadcast for the network
if (GetIPString == NB.BroadcastIPString) return true; //The special broadcast 255.255.255.255
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 == NB.LoopbackIPString) 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
public string GenRandomIPOrMask()
@ -332,12 +411,12 @@ namespace EduNetworkBuilder
public NB_IPAddress BreakIPNetmaskZero()
{
NB_IPAddress one = new NB_IPAddress(GetIPString, "0.0.0.0", myType);
NB_IPAddress one = new NB_IPAddress(GetIPString, NB.ZeroIPString, myType); //0.0.0.0
return one;
}
public NB_IPAddress BreakIPNetmask32()
{
NB_IPAddress one = new NB_IPAddress(GetIPString, "255.255.255.255", myType);
NB_IPAddress one = new NB_IPAddress(GetIPString, NB.BroadcastIPString, myType); //255.255.255.0
return one;
}
public NB_IPAddress BreakIPNetmaskRandom()
@ -359,12 +438,12 @@ namespace EduNetworkBuilder
public NB_IPAddress BreakIPAddressZero()
{
NB_IPAddress one = new NB_IPAddress("0.0.0.0", GetIPString, myType);
NB_IPAddress one = new NB_IPAddress(NB.ZeroIPString, GetIPString, myType);
return one;
}
public NB_IPAddress BreakIPAddress32()
{
NB_IPAddress one = new NB_IPAddress("255.255.255.255", GetIPString, myType);
NB_IPAddress one = new NB_IPAddress(NB.BroadcastIPString, GetIPString, myType);
return one;
}
public NB_IPAddress BreakIPAddressRandom()

View File

@ -53,7 +53,7 @@ namespace EduNetworkBuilder
string hostname = "";
if (ToEdit != null)
hostname = ToEdit.hostname;
if (toEdit.GetIP.ToIpString() == NB.ZeroIPString)
if (toEdit.IsZeroString())
{
string lIP = lastIP.GetIP.ToIpString();
string lNM = lastIP.GetMask.ToIpString();

View File

@ -513,7 +513,7 @@ namespace EduNetworkBuilder
case NetTestType.NeedsRouteToNet:
if (Source == null) return false; //Unable to do it. Do not count it against them.
tAddr = theNet.DNSLookup(Source,dHost);
if (tAddr == null || tAddr.GetIPString == NB.ZeroIPString)
if (tAddr == null || tAddr.IsZeroString())
tAddr = new NB_IPAddress(dHost);
if (Source.HasRouteMatching(tAddr))
{
@ -531,9 +531,9 @@ namespace EduNetworkBuilder
if (Dest.HasIPAddress(tAddr)) return false; //They gave the same address to the source that the dest has.
if (!theNet.HasUniqueIP(tAddr, Source)) return false; //Verify we have not given the IP to someone else
if (tAddr != null &&
tAddr.GetIPString != NB.ZeroIPString)
!tAddr.IsZeroString())
{
if(dAddress != null & dAddress.GetMask == tAddr.GetMask)
if(dAddress != null && dAddress.GetMask == tAddr.GetMask)
return true;
}
return false; //Something is not set right.

View File

@ -2427,7 +2427,7 @@ namespace EduNetworkBuilder
src = GetDeviceFromName(NT.sHost);
if (src == null) continue;
destination = DNSLookup(src, NT.dHost);
if (destination == null || destination.GetIPString == NB.ZeroIPString)
if (destination == null || destination.IsZeroString())
destination = new NB_IPAddress(NT.dHost);
src.PingFromHere(destination);
break;
@ -2435,7 +2435,7 @@ namespace EduNetworkBuilder
src = GetDeviceFromName(NT.sHost);
if (src == null) continue;
destination = DNSLookup(src, NT.dHost);
if (destination == null || destination.GetIPString == NB.ZeroIPString)
if (destination == null || destination.IsZeroString())
destination = new NB_IPAddress(NT.dHost);
src.AskArpFromHere(destination);
break;
@ -2443,7 +2443,7 @@ namespace EduNetworkBuilder
src = GetDeviceFromName(NT.sHost);
if (src == null) continue;
destination = DNSLookup(src, NT.dHost);
if (destination == null || destination.GetIPString == NB.ZeroIPString)
if (destination == null || destination.IsZeroString())
destination = new NB_IPAddress(NT.dHost);
src.TracerouteFromHere(destination);
break;

View File

@ -1624,7 +1624,7 @@ namespace EduNetworkBuilder
string dest = (string)Pressed.Tag;
NB_IPAddress destination;
destination = myNetwork.DNSLookup(ItemClickedOn, dest);
if (destination == null || destination.GetIPString == NB.ZeroIPString)
if (destination == null || destination.IsZeroString())
destination = new NB_IPAddress(dest);
//ItemClickedOn.PingFromHere(destination);
NB.DoActionPingDevice(ItemClickedOn.GetUniqueIdentifier, destination);
@ -1640,7 +1640,7 @@ namespace EduNetworkBuilder
string dest = (string)Pressed.Tag;
NB_IPAddress destination;
destination = myNetwork.DNSLookup(ItemClickedOn, dest);
if (destination == null || destination.GetIPString == NB.ZeroIPString)
if (destination == null || destination.IsZeroString())
destination = new NB_IPAddress(dest);
//ItemClickedOn.TracerouteFromHere(destination);
NB.DoActionTracertDevice(ItemClickedOn.GetUniqueIdentifier, destination);
@ -1656,7 +1656,7 @@ namespace EduNetworkBuilder
string dest = (string)Pressed.Tag;
NB_IPAddress destination;
destination = myNetwork.DNSLookup(ItemClickedOn, dest);
if (destination == null || destination.GetIPString == NB.ZeroIPString)
if (destination == null || destination.IsZeroString())
destination = new NB_IPAddress(dest);
NB.DoActionArpDevice(ItemClickedOn.GetUniqueIdentifier, destination);
//ItemClickedOn.AskArpFromHere(destination);

View File

@ -45,7 +45,7 @@ namespace EduNetworkBuilder
myID = new HostNicID(HostID, UniqueIdentifier, hostname, _nic_name);
NetworkInterface nInterface = new NetworkInterface(NicName(), NB.ZeroIPString, NB.ZeroIPString, myID);
if(theType == NicType.lo)
nInterface = new NetworkInterface(NicName(), "127.0.0.1", "255.0.0.0", myID);
nInterface = new NetworkInterface(NicName(), NB.LoopbackIPString, "255.0.0.0", myID);
interfaces.Add(nInterface);
ApplyNicRules();
SetIPForDHCP();
@ -313,7 +313,7 @@ namespace EduNetworkBuilder
if (iface.myIP.BroadcastAddress == 0) continue; //a netmask of 0.0.0.0 causes grief
if (iface.myIP.BroadcastAddress == dest.GetIP)
return true;//If they are pinging the broadcast IP
if (iface.myIP.GetIPString == NB.BroadcastIPString)
if (iface.myIP.IsBroadcast())
return true;
}
return false;
@ -435,7 +435,7 @@ namespace EduNetworkBuilder
if (UsesDHCP && CanUseDHCP)
{
bool doIt = true;
if (interfaces.Count > 0 && interfaces[0].myIP.GetIPString == NB.ZeroIPString)
if (interfaces.Count > 0 && interfaces[0].myIP.IsZeroString())
{
doIt = true;
}
@ -568,15 +568,15 @@ namespace EduNetworkBuilder
nf.ProcessOutboundPacket(nPacket);
if (nPacket.MyStatus == PacketStatus.finished || nPacket.MyStatus == PacketStatus.finished_failed || nPacket.MyStatus == PacketStatus.finished_ok)
continue; //If the packet cannot be sent out (VLAN stuff)
if (tPacket.OutboundIP == null || (nf.isLocal(tPacket.OutboundIP) || (tPacket.OutboundIP.GetIPString == NB.BroadcastIPString && tPacket.isFresh)))
if (tPacket.OutboundIP == null || (nf.isLocal(tPacket.OutboundIP) || (tPacket.OutboundIP.IsBroadcast() && tPacket.isFresh)))
{
if ((nf != null && nf.myIP.GetIPString != NB.ZeroIPString) || nPacket.MyType == PacketType.dhcp_request)
if ((nf != null && !nf.myIP.IsZeroString()) || nPacket.MyType == PacketType.dhcp_request)
{
//this means we have a local interface to send it out of
//if(nPacket.sourceMAC == null || nPacket.sourceMAC == "" || nPacket.isFresh)
nPacket.sourceMAC = MAC;
//If the source IP is empty then it is a new packet. We set the source to be us
if (nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString)
if (nPacket.sourceIP == null || nPacket.sourceIP.IsZeroString())
nPacket.sourceIP = nf.myIP;
nPacket.TsourceIP = nf.myIP;
if (nPacket.destMAC == null || nPacket.destMAC == "")
@ -627,14 +627,14 @@ namespace EduNetworkBuilder
if (nPacket.MyStatus == PacketStatus.finished || nPacket.MyStatus == PacketStatus.finished_failed || nPacket.MyStatus == PacketStatus.finished_ok)
continue; //If the packet cannot be sent out (VLAN stuff)
if (tPacket.OutboundIP != null && (nf.isLocal(tPacket.OutboundIP) || tPacket.OutboundIP.GetIPString == NB.BroadcastIPString))
if (tPacket.OutboundIP != null && (nf.isLocal(tPacket.OutboundIP) || tPacket.OutboundIP.IsBroadcast()))
{
if ((nf != null && nf.myIP != null && nf.myIP.GetIPString != NB.ZeroIPString) || nPacket.MyType == PacketType.dhcp_request)
if ((nf != null && nf.myIP != null && !nf.myIP.IsZeroString()) || nPacket.MyType == PacketType.dhcp_request)
{
//this means we have a local interface to send it out of
nPacket.sourceMAC = MAC;
//If the source IP is empty then it originated from here. We set the source to be us
if (nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString)
if (nPacket.sourceIP == null || nPacket.sourceIP.IsZeroString())
{
nPacket.sourceIP = nf.myIP;
WhereFrom.StoreOutgoingPacketInfo(nPacket); //the packet is not masqueraded, just accepted
@ -701,10 +701,10 @@ namespace EduNetworkBuilder
}
}
}
if (nf != null && nf.myIP != null && nf.myIP.GetIPString != NB.ZeroIPString)
if (nf != null && nf.myIP != null && !nf.myIP.IsZeroString())
{
//If the source IP is empty then it originated from here. We set the source to be us
if (tPacket.sourceIP == null || tPacket.sourceIP.GetIPString == NB.ZeroIPString)
if (tPacket.sourceIP == null || tPacket.sourceIP.IsZeroString())
{
tPacket.sourceIP = nf.myIP;
WhereFrom.StoreOutgoingPacketInfo(tPacket); //the packet is not yet tunneled
@ -785,7 +785,7 @@ namespace EduNetworkBuilder
//Need to find the managament interface MAC
nPacket.sourceMAC = WhereFrom.HubManagementMAC();
}
if ((nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString) && nPacket.MyType != PacketType.dhcp_request)
if ((nPacket.sourceIP == null || nPacket.sourceIP.IsZeroString()) && nPacket.MyType != PacketType.dhcp_request)
{
//set it to be the ip of management interface
nPacket.sourceIP = WhereFrom.HubManagementIP();
@ -800,7 +800,7 @@ namespace EduNetworkBuilder
nl = myNet.GetLinkFromID(ConnectedLink);
if (nl == null)
break;
if ((nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString) && tPacket.MyType != PacketType.dhcp_request) return false; //We still have no IP. Do not send the packet out.
if ((nPacket.sourceIP == null || nPacket.sourceIP.IsZeroString()) && tPacket.MyType != PacketType.dhcp_request) return false; //We still have no IP. Do not send the packet out.
nPacket.StartOnLink(nl, WhereFrom); //This sends the packet down the link.
myNet.addPacket(nPacket);
if (tPacket.isFresh)

View File

@ -1672,7 +1672,7 @@ namespace EduNetworkBuilder
public bool HasRouteMatching(string destString)
{
NB_IPAddress dest = new NB_IPAddress(destString);
if (dest.GetIPString == NB.ZeroIPString) return false;
if (dest.IsZeroString()) return false;
return HasRouteMatching(dest);
}
@ -1692,7 +1692,7 @@ namespace EduNetworkBuilder
//This "fixes" a strange fringe case of when we configure a subnet such that
//a packet that is traversing the network suddenly is a broadcast packet on the
//target network. Gotta have odd subnetting for this to happen
if (NB.BroadcastIPString == dest.GetIPString) return true;
if (dest.IsBroadcast()) return true;
foreach (NetworkCard nic in NICs)
{
if (nic.HasBroadcastAddresses(dest))
@ -2119,7 +2119,7 @@ namespace EduNetworkBuilder
//It was destined for here, or it is starting from scratch. See if we need to route it.
if (tPacket.destIP == null) tPacket.destIP = new NB_IPAddress(NB.ZeroIPString);
NB_IPAddress dest = DestinationFromIP(tPacket.destIP); //Get the IP, or GW
if(dest.GetIPString == NB.ZeroIPString && tPacket.destIP.GetIPString != NB.BroadcastIPString)
if(dest.IsZeroString() && !tPacket.destIP.IsBroadcast())
{
//No gateway set and no route...
string errString = string.Format(NB.Translate("ND_NoRouteStr"), hostname, tPacket.destIP.GetIPString);
@ -2135,7 +2135,7 @@ namespace EduNetworkBuilder
tPacket.OutboundIP = tPacket.destIP;
tPacket.TsourceIP = null;
}
else if(tPacket.destIP.GetIPString != NB.BroadcastIPString)
else if(!tPacket.destIP.IsBroadcast())
{
//it needs to go to a gateway. Set the next destination is the GW
tPacket.OutboundIP = dest;
@ -2424,7 +2424,7 @@ namespace EduNetworkBuilder
nPacket.sourceIP = new NB_IPAddress(NB.ZeroIPString);
nPacket.TsourceIP = new NB_IPAddress(NB.ZeroIPString);
}
if (tip.GetIP.ToIpString() != NB.ZeroIPString)
if (!tip.IsZeroString())
nPacket.destIP = tip;
if (tPacket.isFresh)
{
@ -2709,7 +2709,7 @@ namespace EduNetworkBuilder
if (tNic.GetNicType == NicType.lo) continue; //Don't check loopback nics
if (tNic.GetNicType == NicType.port) continue; //Don't check ports
nif = tNic.LocalInterface(ip, null);
if(nif != null && nif.myIP.GetIPString != NB.ZeroIPString)
if(nif != null && !nif.myIP.IsUnassigned())
{
return nif.myIP;
}
@ -2834,7 +2834,7 @@ namespace EduNetworkBuilder
string DHCPGW = "";
//Do not ping the zero string. Should not go anywhere
if (dest != null && dest.GetIPString == NB.ZeroIPString)
if (dest != null && dest.IsZeroString())
return new NB_IPAddress(NB.ZeroIPString);
foreach ( NB_IPAddress ip in RouteTable)
@ -2850,7 +2850,7 @@ namespace EduNetworkBuilder
DHCPGW = nic.FirstIP().GetGateway.ToIpString();
}
nIF = nic.LocalInterface(dest,null);
if (nIF != null && nIF.myIP.GetIPString != NB.ZeroIPString)
if (nIF != null && !nIF.myIP.IsZeroString())
{
DestAddr = dest; //We found it. Send it to the destination
return DestAddr;
@ -2859,9 +2859,13 @@ namespace EduNetworkBuilder
if (oNic != null)
{
List<NB_IPAddress> tlist = oNic.IPAddressList();
if(tlist.Count > 0 && tlist[0].GetGateway.ToIpString() != NB.ZeroIPString)
if(tlist.Count > 0)
{
return new NB_IPAddress(tlist[0].GetGateway.ToIpString());
//Make sure the address is not the zerostring...
NB_IPAddress nAddress = new NB_IPAddress(tlist[0].GetGateway.ToIpString());
if(!nAddress.IsZeroString())
return new NB_IPAddress(tlist[0].GetGateway.ToIpString());
}
}
if (DHCPGW != "")
@ -2923,10 +2927,10 @@ namespace EduNetworkBuilder
tPacket.MyStatus = PacketStatus.finished; //we have replaced it with other ones
return false;
}
if (tPacket.OutboundIP == null || tPacket.OutboundIP.GetIP.ToIpString() == NB.ZeroIPString && tPacket.MyType != PacketType.dhcp_request)
if (tPacket.OutboundIP == null || tPacket.OutboundIP.IsZeroString() && tPacket.MyType != PacketType.dhcp_request)
{
tPacket.OutboundIP = DestinationFromIP(dest,tPacket.OutboundNic);
if (tPacket.OutboundIP.GetIP.ToIpString() == NB.ZeroIPString)
if (tPacket.OutboundIP.IsZeroString())
{
if (tPacket.MyType == PacketType.dhcp_answer)
{
@ -3013,7 +3017,7 @@ namespace EduNetworkBuilder
{
nIF = nic.LocalInterface(toFind, null);
if (nIF != null) {
if (nIF.myIP.GetIPString == NB.ZeroIPString)
if (nIF.myIP.IsZeroString())
continue;
return nic;
}
@ -3062,7 +3066,7 @@ namespace EduNetworkBuilder
{
foreach(NB_IPAddress ip in nic.IPAddressList())
{
if (ip.GetIPString != NB.ZeroIPString && ip.GetIPString != NB.LoopbackIPString)
if (!ip.IsZeroString() && !ip.IsLoopback())
return ip;
}
}
@ -3089,7 +3093,7 @@ namespace EduNetworkBuilder
if(Source != null)
{
answer = LocalDeviceIP(Source);
if (answer != null && answer.GetIPString != NB.ZeroIPString)
if (answer != null && !answer.IsZeroString())
return answer;
}
//If we have a WAN Nic, use that first
@ -3415,7 +3419,8 @@ namespace EduNetworkBuilder
{
ArpTable.Add(AE);
}
if(IPFromArp(mac) != ip && ip != NB.ZeroIPString)
NB_IPAddress tAddress = new NB_IPAddress(ip);
if(IPFromArp(mac) != ip && !tAddress.IsZeroString())
{
foreach (ArpEntry arp in ArpTable.ToList())
{
@ -3502,7 +3507,7 @@ namespace EduNetworkBuilder
List<string> theStrings = new List<string>();
foreach(NB_IPAddress ip in DHCPRanges)
{
if (ip.GetIP.ToIpString() != "127.0.0.1")
if (!ip.IsLoopback())
{
if (WithAdditionalInfo)
{
@ -3523,7 +3528,7 @@ namespace EduNetworkBuilder
if (InterfaceIP == null) return null;
foreach (NB_IPAddress ip in DHCPRanges)
{
if (ip.GetIP.ToIpString() != "127.0.0.1" && InterfaceIP.IsLocal(ip))
if (!ip.IsLoopback() && InterfaceIP.IsLocal(ip))
return ip;
}
return null;

View File

@ -281,7 +281,7 @@ namespace EduNetworkBuilder
NetworkCard nic = HD.NicFromID(AttachedToHostNic);
if(nic != null && nic.GetNicType != NicType.port && nic.GetNicType != NicType.wport)
tPacket.TsourceIP = myIP;
if(tPacket.TsourceIP == null || tPacket.TsourceIP.GetIPString == NB.ZeroIPString)
if(tPacket.TsourceIP == null || tPacket.TsourceIP.IsZeroString())
tPacket.TsourceIP = myIP;
if (What == VLANTagType.Forbidden)