From: Richard Whitehouse Date: Tue, 25 Jan 2011 03:48:55 +0000 (+0000) Subject: ARP Mangling implemented, and bug fixes for the MOOSE bridge involving caching entries. X-Git-Url: https://git.richardwhiuk.com/?a=commitdiff_plain;h=9a24a2ff81f8d12b0130181c73d5d94ba09e2455;p=ns-moose.git ARP Mangling implemented, and bug fixes for the MOOSE bridge involving caching entries. --- diff --git a/code/src/devices/bridge/model/moose-bridge-net-device.cc b/code/src/devices/bridge/model/moose-bridge-net-device.cc index 48b47eb..ae81609 100644 --- a/code/src/devices/bridge/model/moose-bridge-net-device.cc +++ b/code/src/devices/bridge/model/moose-bridge-net-device.cc @@ -35,7 +35,12 @@ MooseBridgeNetDevice::GetTypeId (void) { static TypeId tid = TypeId ("ns3::MooseBridgeNetDevice") .SetParent () - .AddConstructor (); + .AddConstructor () + .AddAttribute ("MooseExpirationTime", + "Time it takes for learned MOOSE state entry to expire.", + TimeValue (Seconds (30)), + MakeTimeAccessor (&MooseBridgeNetDevice::m_expirationTime), + MakeTimeChecker ()); return tid; } @@ -82,11 +87,11 @@ MooseAddress MooseBridgeNetDevice::FromMoose(MooseAddress const& addr){ SuffixState* state = iter->second; if(state->expirationTime > now){ + return MooseAddress(iter->second->ethernet); + } else { m_ethernetState.erase(iter); m_suffixState.erase(iter->second->ethernet); return MooseAddress(Mac48Address::GetBroadcast()); - } else { - return MooseAddress(iter->second->ethernet); } } @@ -119,12 +124,17 @@ MooseAddress MooseBridgeNetDevice::ToMoose(MooseAddress const& addr){ state.suffix = moose.GetMooseSuffix(); state.expirationTime = now + m_expirationTime; + NS_LOG_LOGIC("Allocating New MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ") for " << addr48); + m_ethernetState[state.suffix] = &state; } else { SuffixState &state = iter->second; if(state.expirationTime > now){ moose = MooseAddress::Combine(m_mooseAddress.GetMoosePrefix(), state.suffix); + + NS_LOG_LOGIC("Using Allocated MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ")"); + state.expirationTime = now + m_expirationTime; } else { // Delete old and alloc new @@ -135,12 +145,14 @@ MooseAddress MooseBridgeNetDevice::ToMoose(MooseAddress const& addr){ // Allocate New SuffixState &state = m_suffixState[addr48]; - + moose = MooseAddress::Allocate(m_mooseAddress.GetMoosePrefix()); state.ethernet = addr48; state.suffix = moose.GetMooseSuffix(); state.expirationTime = now + m_expirationTime; + NS_LOG_LOGIC("Expired MOOSE Suffix: (" << moose.GetMoosePrefix().GetInt() << "," << moose.GetMooseSuffix().GetInt() << ")"); + m_ethernetState[state.suffix] = &state; } diff --git a/code/src/devices/bridge/model/moose-bridge-port-net-device.cc b/code/src/devices/bridge/model/moose-bridge-port-net-device.cc index e16bc0c..d2e5d24 100644 --- a/code/src/devices/bridge/model/moose-bridge-port-net-device.cc +++ b/code/src/devices/bridge/model/moose-bridge-port-net-device.cc @@ -20,6 +20,7 @@ #include "moose-bridge-net-device.h" #include "bridge-port-net-device.h" #include "moose-bridge-port-net-device.h" +#include "ns3/arp-header.h" #include "ns3/moose-address.h" #include "ns3/node.h" #include "ns3/channel.h" @@ -57,8 +58,50 @@ MooseBridgePortNetDevice::~MooseBridgePortNetDevice(){ void MooseBridgePortNetDevice::Send (Ptr packet, const Address& src, const Address& dest, uint16_t protocolNumber){ NS_LOG_FUNCTION_NOARGS(); NS_LOG_DEBUG("Send(device=" << m_device << ",packet=" << packet << ",src=" << src << ",dest=" << dest << ",port=" << this << ",protocol=" << protocolNumber); - - BridgePortNetDevice::Send(packet, src, dest, protocolNumber); + + Ptr bridge = m_bridge->GetObject(); + + NS_ASSERT(bridge != NULL); + + Ptr npacket = packet->Copy(); + + if(protocolNumber == ArpL3Protocol::PROT_NUMBER){ + // Need to do ARP rewriting. + + ArpHeader arpHeader; + npacket->RemoveHeader(arpHeader); + + Mac48Address arp_dst48 = Mac48Address::ConvertFrom(arpHeader.GetDestinationHardwareAddress()); + MooseAddress arp_mdst(arp_dst48); + + if(arp_mdst.GetMooseType() == MooseAddress::MOOSE){ + if(arpHeader.IsRequest()){ + arpHeader.SetRequest( + arpHeader.GetSourceHardwareAddress(), + arpHeader.GetSourceIpv4Address(), + bridge->FromMoose(arp_mdst).GetMacAddress(), + arpHeader.GetDestinationIpv4Address() + ); + } else if(arpHeader.IsReply()){ + arpHeader.SetReply( + arpHeader.GetSourceHardwareAddress(), + arpHeader.GetSourceIpv4Address(), + bridge->FromMoose(arp_mdst).GetMacAddress(), + arpHeader.GetDestinationIpv4Address() + ); + } else { + NS_ASSERT (arpHeader.IsReply() || arpHeader.IsRequest()); + } + + NS_LOG_LOGIC("Rewriting ARP Header Destination (from=" << arp_dst48 << ",to=" << bridge->FromMoose(arp_mdst).GetMacAddress() << ")"); + + } + + npacket->AddHeader(arpHeader); + + } + + BridgePortNetDevice::Send(npacket, src, dest, protocolNumber); } void MooseBridgePortNetDevice::Receive (Ptr device, Ptr packet, uint16_t protocol, Address const &src, Address const &dst, NetDevice::PacketType packetType){ @@ -90,15 +133,46 @@ void MooseBridgePortNetDevice::Receive (Ptr device, Ptr MooseAddress newsrc = bridge->ToMoose(msrc); - packet->Print(std::cout); std::cout << std::endl; + Ptr npacket = packet->Copy(); + if(protocol == ArpL3Protocol::PROT_NUMBER){ // Need to do ARP rewriting. + ArpHeader arpHeader; + npacket->RemoveHeader(arpHeader); + + Mac48Address arp_src48 = Mac48Address::ConvertFrom(arpHeader.GetSourceHardwareAddress()); + MooseAddress arp_msrc(arp_src48); + + if(arp_msrc.GetMooseType() == MooseAddress::HOST){ + if(arpHeader.IsRequest()){ + arpHeader.SetRequest( + bridge->ToMoose(arp_msrc).GetMacAddress(), + arpHeader.GetSourceIpv4Address(), + arpHeader.GetDestinationHardwareAddress(), + arpHeader.GetDestinationIpv4Address() + ); + } else if(arpHeader.IsReply()){ + arpHeader.SetReply( + bridge->ToMoose(arp_msrc).GetMacAddress(), + arpHeader.GetSourceIpv4Address(), + arpHeader.GetDestinationHardwareAddress(), + arpHeader.GetDestinationIpv4Address() + ); + } else { + NS_ASSERT (arpHeader.IsReply() || arpHeader.IsRequest()); + } + + NS_LOG_LOGIC("Rewriting ARP Header Source (from=" << arp_src48 << ",to=" << bridge->ToMoose(arp_msrc).GetMacAddress() << ")"); + } + + npacket->AddHeader(arpHeader); + } bridge->Learn(newsrc, this); - bridge->Forward(this, packet, protocol, newsrc.GetMacAddress(), dst, packetType); + bridge->Forward(this, npacket, protocol, newsrc.GetMacAddress(), dst, packetType); } else {