From 5bd41d219816cf2f4c0e9f3aeb32e7ffd8176988 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Wed, 26 Aug 2015 16:14:34 -0700 Subject: [PATCH] Allow devices to receive multiple DHCP responses and choose between them. --- EduNetworkBuilder/NetworkCard.cs | 22 +++++++++++++++++++++- EduNetworkBuilder/NetworkDevice.cs | 30 ++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 7 deletions(-) diff --git a/EduNetworkBuilder/NetworkCard.cs b/EduNetworkBuilder/NetworkCard.cs index 174218b..956cdbf 100644 --- a/EduNetworkBuilder/NetworkCard.cs +++ b/EduNetworkBuilder/NetworkCard.cs @@ -235,6 +235,8 @@ namespace EduNetworkBuilder { if (iface.myIP.BroadcastAddress == dest.GetIP) return true;//If they are pinging the broadcast IP + if (iface.myIP.GetIPString == NB.BroadcastIPString) + return true; } return false; } @@ -312,8 +314,26 @@ namespace EduNetworkBuilder { if (UsesDHCP && CanUseDHCP) { - if(interfaces.Count > 0) + bool doIt = true; + if (interfaces.Count > 0 && interfaces[0].myIP.GetIPString == NB.ZeroIPString) + { + doIt = true; + } + else + { + 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; + } } } diff --git a/EduNetworkBuilder/NetworkDevice.cs b/EduNetworkBuilder/NetworkDevice.cs index 489e10b..faa30e6 100644 --- a/EduNetworkBuilder/NetworkDevice.cs +++ b/EduNetworkBuilder/NetworkDevice.cs @@ -975,13 +975,21 @@ namespace EduNetworkBuilder 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) { - foreach (NetworkCard nic in NICs) - { - if (nic.HasBroadcastAddresses(dest)) - return true; - } + if (dest.BroadcastAddress == dest.GetIP) + return true; + if (dest.GetIPString == NB.BroadcastIPString) + return true; return false; } @@ -1132,12 +1140,22 @@ namespace EduNetworkBuilder if (tPacket.sourceIP == null) return ResponseToPacket.reject; 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.What == PacketType.arp_request && tPacket.MyType == PacketType.arp_answer) typesMatch = true; if (ipc.What == PacketType.dhcp_request && tPacket.MyType == PacketType.dhcp_answer) + { + Console.WriteLine(" " + hostname + ": True"); typesMatch = true; + } if (ipc.What == PacketType.ping_request && tPacket.MyType == PacketType.ping_answer) typesMatch = true; if(typesMatch) @@ -1632,7 +1650,7 @@ namespace EduNetworkBuilder 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."); string sIP = "?.?.?.?";