|
|
| 39 |
#include <sys/stat.h> |
39 |
#include <sys/stat.h> |
| 40 |
#include <sys/socket.h> |
40 |
#include <sys/socket.h> |
| 41 |
#include <sys/un.h> |
41 |
#include <sys/un.h> |
|
|
42 |
#include <sys/ioctl.h> |
| 43 |
#include <net/if.h> |
| 42 |
#include <cerrno> |
44 |
#include <cerrno> |
| 43 |
#include <limits> |
45 |
#include <limits> |
| 44 |
#include <cstdlib> |
46 |
#include <cstdlib> |
| 45 |
#include <unistd.h> |
47 |
#include <unistd.h> |
| 46 |
|
48 |
|
| 47 |
// |
|
|
| 48 |
// Sometimes having a tap-creator is actually more trouble than solution. In |
| 49 |
// these cases you can uncomment the define of TAP_CREATOR below and the |
| 50 |
// simulation will just use a device you have preconfigured. This is useful |
| 51 |
// if you are running in an environment where you have got to run as root, |
| 52 |
// such as ORBIT or CORE. |
| 53 |
// |
| 54 |
|
| 55 |
|
| 56 |
// #define NO_CREATOR |
| 57 |
|
| 58 |
#ifdef NO_CREATOR |
| 59 |
#include <fcntl.h> |
| 60 |
#include <net/if.h> |
| 61 |
#include <linux/if_tun.h> |
| 62 |
#include <sys/ioctl.h> |
| 63 |
#endif |
| 64 |
|
| 65 |
NS_LOG_COMPONENT_DEFINE ("TapBridge"); |
49 |
NS_LOG_COMPONENT_DEFINE ("TapBridge"); |
| 66 |
|
50 |
|
| 67 |
namespace ns3 { |
51 |
namespace ns3 { |
|
|
| 630 |
int *rawSocket = (int*)CMSG_DATA (cmsg); |
614 |
int *rawSocket = (int*)CMSG_DATA (cmsg); |
| 631 |
NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket); |
615 |
NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket); |
| 632 |
m_sock = *rawSocket; |
616 |
m_sock = *rawSocket; |
| 633 |
return; |
617 |
break; |
| 634 |
} |
618 |
} |
| 635 |
else |
619 |
else |
| 636 |
{ |
620 |
{ |
|
|
| 638 |
} |
622 |
} |
| 639 |
} |
623 |
} |
| 640 |
} |
624 |
} |
| 641 |
NS_FATAL_ERROR ("Did not get the raw socket from the socket creator"); |
625 |
if (cmsg == NULL) |
|
|
626 |
{ |
| 627 |
NS_FATAL_ERROR ("Did not get the raw socket from the socket creator"); |
| 628 |
} |
| 629 |
|
| 630 |
if (m_mode == USE_LOCAL || m_mode == USE_BRIDGE) |
| 631 |
{ |
| 632 |
// |
| 633 |
// Set the ns-3 device's mac address to the overlying container's |
| 634 |
// mac address |
| 635 |
// |
| 636 |
int fd = socket (AF_INET, SOCK_DGRAM, 0); |
| 637 |
if (fd >= 0) |
| 638 |
{ |
| 639 |
struct ifreq s; |
| 640 |
strncpy (s.ifr_name, m_tapDeviceName.c_str (), sizeof (s.ifr_name)); |
| 641 |
|
| 642 |
NS_LOG_INFO ("Trying to get MacAddr of " << m_tapDeviceName); |
| 643 |
int ioctlResult = ioctl (fd, SIOCGIFHWADDR, &s); |
| 644 |
if (ioctlResult == 0) |
| 645 |
{ |
| 646 |
Mac48Address learnedMac; |
| 647 |
learnedMac.CopyFrom ((uint8_t *)s.ifr_hwaddr.sa_data); |
| 648 |
NS_LOG_INFO ("Learned Tap device MacAddr is " << learnedMac << ": setting ns-3 device to use this address"); |
| 649 |
m_bridgedDevice->SetAddress (learnedMac); |
| 650 |
m_ns3AddressRewritten = true; |
| 651 |
} |
| 652 |
|
| 653 |
close (fd); |
| 654 |
} |
| 655 |
|
| 656 |
if (!m_ns3AddressRewritten) |
| 657 |
{ |
| 658 |
NS_LOG_INFO ("Cannot get MacAddr of Tap device: " << m_tapDeviceName << " while in USE_LOCAL/USE_BRIDGE mode: " << std::strerror (errno)); |
| 659 |
NS_LOG_INFO ("Underlying ns-3 device will continue to use default address, what can lead to connectivity errors"); |
| 660 |
} |
| 661 |
} |
| 642 |
} |
662 |
} |
| 643 |
} |
663 |
} |
| 644 |
|
664 |
|