From adb677eddd7769743c22adc181a0f4fcd35a6dd6 Mon Sep 17 00:00:00 2001 From: Piotr Jurkiewicz Date: Tue, 19 Nov 2013 06:50:18 -0800 Subject: [PATCH] bug 1777: TapBridge address learning patch (modified by Tom Goff) --- src/tap-bridge/model/tap-bridge.cc | 56 +++++++++++++++++++----------- src/tap-bridge/model/tap-bridge.h | 2 +- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/src/tap-bridge/model/tap-bridge.cc b/src/tap-bridge/model/tap-bridge.cc index 83f9c9144..311d55978 100644 --- a/src/tap-bridge/model/tap-bridge.cc +++ b/src/tap-bridge/model/tap-bridge.cc @@ -39,29 +39,13 @@ #include #include #include +#include +#include #include #include #include #include -// -// Sometimes having a tap-creator is actually more trouble than solution. In -// these cases you can uncomment the define of TAP_CREATOR below and the -// simulation will just use a device you have preconfigured. This is useful -// if you are running in an environment where you have got to run as root, -// such as ORBIT or CORE. -// - - -// #define NO_CREATOR - -#ifdef NO_CREATOR -#include -#include -#include -#include -#endif - NS_LOG_COMPONENT_DEFINE ("TapBridge"); namespace ns3 { @@ -631,7 +615,7 @@ TapBridge::CreateTap (void) int *rawSocket = (int*)CMSG_DATA (cmsg); NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket); m_sock = *rawSocket; - return; + break; } else { @@ -639,8 +623,40 @@ TapBridge::CreateTap (void) } } } - NS_FATAL_ERROR ("Did not get the raw socket from the socket creator"); + if (cmsg == NULL) + { + NS_FATAL_ERROR ("Did not get the raw socket from the socket creator"); + } + + if (m_mode == USE_LOCAL || m_mode == USE_BRIDGE) + { + // + // Set the ns-3 device's mac address to the overlying container's + // mac address + // + struct ifreq s; + strncpy (s.ifr_name, m_tapDeviceName.c_str (), sizeof (s.ifr_name)); + + NS_LOG_INFO ("Trying to get MacAddr of " << m_tapDeviceName); + int ioctlResult = ioctl (sock, SIOCGIFHWADDR, &s); + if (ioctlResult == 0) + { + Mac48Address learnedMac; + learnedMac.CopyFrom ((uint8_t *)s.ifr_hwaddr.sa_data); + NS_LOG_INFO ("Learned Tap device MacAddr is " << learnedMac << ": setting ns-3 device to use this address"); + m_bridgedDevice->SetAddress (learnedMac); + m_ns3AddressRewritten = true; + } + + if (!m_ns3AddressRewritten) + { + NS_LOG_INFO ("Cannot get MacAddr of Tap device: " << m_tapDeviceName << " while in USE_LOCAL/USE_BRIDGE mode: " << std::strerror (errno)); + NS_LOG_INFO ("Underlying ns-3 device will continue to use default address, what can lead to connectivity errors"); + } + } } + + close (sock); } void diff --git a/src/tap-bridge/model/tap-bridge.h b/src/tap-bridge/model/tap-bridge.h index 0d3cc7950..9a3087c45 100644 --- a/src/tap-bridge/model/tap-bridge.h +++ b/src/tap-bridge/model/tap-bridge.h @@ -448,7 +448,7 @@ private: * \internal * * Whether the MAC address of the underlying ns-3 device has already been - * rewritten is stored in this variable (for UseLocal mode only). + * rewritten is stored in this variable (for UseLocal/UseBridge mode only). */ bool m_ns3AddressRewritten;