Allow devices to receive multiple DHCP responses and choose between them.

This commit is contained in:
Tim Young 2015-08-26 16:14:34 -07:00
parent 01ef8b2fea
commit 5bd41d2198
2 changed files with 45 additions and 7 deletions

View File

@ -235,6 +235,8 @@ namespace EduNetworkBuilder
{ {
if (iface.myIP.BroadcastAddress == dest.GetIP) if (iface.myIP.BroadcastAddress == dest.GetIP)
return true;//If they are pinging the broadcast IP return true;//If they are pinging the broadcast IP
if (iface.myIP.GetIPString == NB.BroadcastIPString)
return true;
} }
return false; return false;
} }
@ -311,11 +313,29 @@ namespace EduNetworkBuilder
public void SetIPForDHCP(IPAddress newIP) public void SetIPForDHCP(IPAddress newIP)
{ {
if (UsesDHCP && CanUseDHCP) if (UsesDHCP && CanUseDHCP)
{
bool doIt = true;
if (interfaces.Count > 0 && interfaces[0].myIP.GetIPString == NB.ZeroIPString)
{
doIt = true;
}
else
{ {
if(interfaces.Count > 0) if(interfaces.Count > 0)
{
//The IP address is not zero. This means it already has an IP.
Random rnd = NB.GetRandom();
int chance = rnd.Next(6);
if (chance > 3)
doIt = false;
}
}
if (doIt && interfaces.Count > 0)
{
interfaces[0].myIP = newIP; interfaces[0].myIP = newIP;
} }
} }
}
public bool HasLocalInterface(IPAddress theIP) public bool HasLocalInterface(IPAddress theIP)
{ {

View File

@ -975,13 +975,21 @@ namespace EduNetworkBuilder
return HasRouteMatching(dest); return HasRouteMatching(dest);
} }
//public bool HasBroadcastAddress(IPAddress dest)
//{
// foreach (NetworkCard nic in NICs)
// {
// if (nic.HasBroadcastAddresses(dest))
// return true;
// }
// return false;
//}
public bool HasBroadcastAddress(IPAddress dest) public bool HasBroadcastAddress(IPAddress dest)
{ {
foreach (NetworkCard nic in NICs) if (dest.BroadcastAddress == dest.GetIP)
{ return true;
if (nic.HasBroadcastAddresses(dest)) if (dest.GetIPString == NB.BroadcastIPString)
return true; return true;
}
return false; return false;
} }
@ -1132,12 +1140,22 @@ namespace EduNetworkBuilder
if (tPacket.sourceIP == null) return ResponseToPacket.reject; if (tPacket.sourceIP == null) return ResponseToPacket.reject;
foreach(IPConnectionEntry ipc in IPConnections) foreach(IPConnectionEntry ipc in IPConnections)
{ {
if(ipc.What == PacketType.dhcp_request)
{
Console.WriteLine(hostname + ": packet=" + tPacket.MyType.ToString());
Console.WriteLine(" " + hostname + ": d:" + ipc.destIP.GetIPString + " s:" + tPacket.sourceIP.GetIPString);
if (HasBroadcastAddress(ipc.destIP))
Console.WriteLine(" Broadcast");
}
if(ipc.destIP.GetIP == tPacket.sourceIP.GetIP || HasBroadcastAddress(ipc.destIP)) if(ipc.destIP.GetIP == tPacket.sourceIP.GetIP || HasBroadcastAddress(ipc.destIP))
{ {
if (ipc.What == PacketType.arp_request && tPacket.MyType == PacketType.arp_answer) if (ipc.What == PacketType.arp_request && tPacket.MyType == PacketType.arp_answer)
typesMatch = true; typesMatch = true;
if (ipc.What == PacketType.dhcp_request && tPacket.MyType == PacketType.dhcp_answer) if (ipc.What == PacketType.dhcp_request && tPacket.MyType == PacketType.dhcp_answer)
{
Console.WriteLine(" " + hostname + ": True");
typesMatch = true; typesMatch = true;
}
if (ipc.What == PacketType.ping_request && tPacket.MyType == PacketType.ping_answer) if (ipc.What == PacketType.ping_request && tPacket.MyType == PacketType.ping_answer)
typesMatch = true; typesMatch = true;
if(typesMatch) if(typesMatch)
@ -1632,7 +1650,7 @@ namespace EduNetworkBuilder
return; return;
} }
} }
else if(!tPacket.isFresh) else if(!tPacket.isFresh && !ForwardsPackets())
{ {
tPacket.AddMessage(DebugLevel.info, "ERROR! A packet returned to a machine that we were not expecting it from."); tPacket.AddMessage(DebugLevel.info, "ERROR! A packet returned to a machine that we were not expecting it from.");
string sIP = "?.?.?.?"; string sIP = "?.?.?.?";