From ea480fe7d7f0cf37775b6d9b0690c17452003260 Mon Sep 17 00:00:00 2001 From: Tim Young Date: Sat, 19 Sep 2015 11:09:51 -0400 Subject: [PATCH] Fixed numerous small glitches with switches, dhcp, and ping --- EduNetworkBuilder/NetworkCard.cs | 22 +++++++++++++++++----- EduNetworkBuilder/NetworkDevice.cs | 27 +++++++++++++++++++++++---- EduNetworkBuilder/Packet.cs | 1 + 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/EduNetworkBuilder/NetworkCard.cs b/EduNetworkBuilder/NetworkCard.cs index 59d2be5..33a5fdf 100644 --- a/EduNetworkBuilder/NetworkCard.cs +++ b/EduNetworkBuilder/NetworkCard.cs @@ -367,6 +367,12 @@ namespace EduNetworkBuilder } return null; } + public NetworkInterface PrimaryInterface() + { + if (interfaces.Count == 1) + return interfaces[0]; + return null; + } /*************************************** * @@ -392,8 +398,6 @@ namespace EduNetworkBuilder case NicType.eth: case NicType.wlan: //see if it the packet dest is local to this nic - if (WhereFrom.hostname == "router0") - Console.WriteLine("wearehere"); foreach (NetworkInterface nf in interfaces.ToList()) { if (tPacket.InboundNic != null && tPacket.InboundNic == this && @@ -407,6 +411,7 @@ namespace EduNetworkBuilder if ((nf != null && nf.myIP.GetIPString != NB.ZeroIPString) || 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) @@ -527,7 +532,8 @@ namespace EduNetworkBuilder if((tPacket.InboundNic != null && tPacket.InboundNic.GetNicType == NicType.wan) || tPacket.InboundNic == null || tPacket.destMAC == "") { //We need to find destination MAC and set source MAC - nPacket.sourceMAC = MAC; + if(nPacket.sourceMAC == null || nPacket.sourceMAC == "" || nPacket.isFresh) + nPacket.sourceMAC = MAC; //Update the MAC string getMAC=""; if (tPacket.OutboundIP != null) @@ -562,10 +568,11 @@ 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 && !tPacket.isFresh)) + if((nPacket.sourceIP == null || nPacket.sourceIP.GetIPString == NB.ZeroIPString) && !(nPacket.MyType == PacketType.dhcp_request && tPacket.isFresh)) { //set it to be the ip of management interface nPacket.sourceIP = WhereFrom.HubManagementIP(); + nPacket.sourceMAC = MAC; } if (nPacket.destMAC == null || nPacket.destMAC == "" && tPacket.OutboundIP != null) { @@ -616,7 +623,12 @@ namespace EduNetworkBuilder { HostNicID otherid = nd.NicIDFromArp(tPacket.destMAC); if (otherid.HostID != -1) - nd.StoreArp(tPacket.destMAC, tPacket.payloadIP.GetIP.ToIpString(), otherid); + { + string ipstring = NB.ZeroIPString; + if (tPacket.payloadIP != null) + ipstring = tPacket.payloadIP.GetIPString; + nd.StoreArp(tPacket.destMAC, ipstring, otherid); + } } return; } diff --git a/EduNetworkBuilder/NetworkDevice.cs b/EduNetworkBuilder/NetworkDevice.cs index 0924308..a5cd3d3 100644 --- a/EduNetworkBuilder/NetworkDevice.cs +++ b/EduNetworkBuilder/NetworkDevice.cs @@ -1682,10 +1682,21 @@ namespace EduNetworkBuilder nPacket.Tracking = new PacketMessage(); myNet.addPacket(nPacket); //We need to find a dhcp IP for this. - IPAddress dst = RequestDHCPLease(tPacket.sourceMAC, tPacket.OutboundIP); + IPAddress dst; + if (tPacket.InboundInterface == null) + { + dst = RequestDHCPLease(tPacket.sourceMAC, tPacket.OutboundIP); + } + else + { + dst = RequestDHCPLease(tPacket.sourceMAC, tPacket.InboundInterface.myIP); + } string tmac = nPacket.sourceMAC; nPacket.sourceMAC = tPacket.OutboundMAC; - nPacket.sourceIP = tPacket.OutboundIP; + if (tPacket.InboundInterface != null) + nPacket.sourceIP = tPacket.InboundInterface.myIP; + else + nPacket.sourceIP = tPacket.OutboundIP; nPacket.destMAC = tmac; nPacket.OutboundDestMAC = tmac; nPacket.payloadIP = dst; @@ -1713,7 +1724,7 @@ namespace EduNetworkBuilder if (response == ResponseToPacket.accept) { NetworkCard tnic = tPacket.OutboundNic; - if (tnic == null) + if (tnic == null || tnic.GetNicType == NicType.port || tnic.GetNicType == NicType.wport) tnic = HubManagementCard(); if (tnic != null) { @@ -2145,6 +2156,10 @@ namespace EduNetworkBuilder { //We are entering into the computer NetworkCard nc = NicFromID(arrivalNic); + //Say tell the packet that we are no longer on the link + tPacket.WhereAmI = this; + tPacket.MyStatus = PacketStatus.processing; + // tPacket.PacketDump(hostname, DebugPausePoint.packet_in); if(nc == null) { @@ -2166,7 +2181,11 @@ namespace EduNetworkBuilder { nf = nc.LocalInterface(tPacket.TsourceIP, tPacket.Tracking); } - if(nf == null && tPacket.MyType == PacketType.dhcp_request && isDHCPServer) + if(nf == null) + { + nf = nc.PrimaryInterface(); + } + if (nf == null && tPacket.MyType == PacketType.dhcp_request && isDHCPServer) { List addresses = nc.IPAddressList(); if (addresses.Count > 0) diff --git a/EduNetworkBuilder/Packet.cs b/EduNetworkBuilder/Packet.cs index 3d71161..8b415f7 100644 --- a/EduNetworkBuilder/Packet.cs +++ b/EduNetworkBuilder/Packet.cs @@ -340,6 +340,7 @@ namespace EduNetworkBuilder myDirection = nb_direction.to_dst; else myDirection = nb_direction.to_src; + Console.WriteLine("Starting on link. SMAC: " + sourceMAC + " DMAC: " + destMAC); AddMessage(DebugLevel.debug, " Starting on link"); }