src/devices/emu/emu-net-device.cc
author vincent@clarinet.u-strasbg.fr
Wed, 05 Nov 2008 14:49:21 -0800
changeset 3842 545ddf9398ed
parent 3836 e3aa8940f23b
child 3847 784c45b27326
permissions -rw-r--r--
apply patch for bug 294 (GetMulticastAddr) + emu device update + rescan
     1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
     2 /*
     3  * Copyright (c) 2008 University of Washington
     4  *
     5  * This program is free software; you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License version 2 as
     7  * published by the Free Software Foundation;
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program; if not, write to the Free Software
    16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    17  */
    18 
    19 #include "emu-net-device.h"
    20 #include "emu-encode-decode.h"
    21 
    22 #include "ns3/log.h"
    23 #include "ns3/queue.h"
    24 #include "ns3/simulator.h"
    25 #include "ns3/realtime-simulator-impl.h"
    26 #include "ns3/mac48-address.h"
    27 #include "ns3/ethernet-header.h"
    28 #include "ns3/ethernet-trailer.h"
    29 #include "ns3/llc-snap-header.h"
    30 #include "ns3/trace-source-accessor.h"
    31 #include "ns3/pointer.h"
    32 #include "ns3/channel.h"
    33 #include "ns3/system-thread.h"
    34 #include "ns3/string.h"
    35 #include "ns3/boolean.h"
    36 
    37 #include <sys/wait.h>
    38 #include <sys/stat.h>
    39 #include <sys/socket.h>
    40 #include <sys/un.h>
    41 #include <sys/ioctl.h>
    42 #include <net/ethernet.h>
    43 #include <net/if.h>
    44 #include <netinet/in.h>
    45 #include <netpacket/packet.h>
    46 #include <arpa/inet.h>
    47 #include <errno.h>
    48 
    49 NS_LOG_COMPONENT_DEFINE ("EmuNetDevice");
    50 
    51 namespace ns3 {
    52 
    53 NS_OBJECT_ENSURE_REGISTERED (EmuNetDevice);
    54 
    55 #define EMU_MAGIC 65867
    56 
    57 TypeId 
    58 EmuNetDevice::GetTypeId (void)
    59 {
    60   static TypeId tid = TypeId ("ns3::EmuNetDevice")
    61     .SetParent<NetDevice> ()
    62     .AddConstructor<EmuNetDevice> ()
    63     .AddAttribute ("Address", 
    64                    "The ns-3 MAC address of this (virtual) device.",
    65                    Mac48AddressValue (Mac48Address ("ff:ff:ff:ff:ff:ff")),
    66                    MakeMac48AddressAccessor (&EmuNetDevice::m_address),
    67                    MakeMac48AddressChecker ())
    68     .AddAttribute ("DeviceName", 
    69                    "The name of the underlying real device (e.g. eth1).",
    70                    StringValue ("eth1"),
    71                    MakeStringAccessor (&EmuNetDevice::m_deviceName),
    72                    MakeStringChecker ())
    73     .AddAttribute ("Start", 
    74                    "The simulation time at which to spin up the device thread.",
    75                    TimeValue (Seconds (0.)),
    76                    MakeTimeAccessor (&EmuNetDevice::m_tStart),
    77                    MakeTimeChecker ())
    78     .AddAttribute ("Stop", 
    79                    "The simulation time at which to tear down the device thread.",
    80                    TimeValue (Seconds (0.)),
    81                    MakeTimeAccessor (&EmuNetDevice::m_tStop),
    82                    MakeTimeChecker ())
    83     .AddAttribute ("TxQueue", 
    84                    "A queue to use as the transmit queue in the device.",
    85                    PointerValue (),
    86                    MakePointerAccessor (&EmuNetDevice::m_queue),
    87                    MakePointerChecker<Queue> ())
    88     .AddTraceSource ("Rx", 
    89                      "Trace source to fire on reception of a MAC packet.",
    90                      MakeTraceSourceAccessor (&EmuNetDevice::m_rxTrace))
    91     .AddTraceSource ("Drop", 
    92                      "Trace source to fire on when a MAC packet is dropped.",
    93                      MakeTraceSourceAccessor (&EmuNetDevice::m_dropTrace))
    94     ;
    95   return tid;
    96 }
    97 
    98 
    99 EmuNetDevice::EmuNetDevice () 
   100 : 
   101   m_startEvent (),
   102   m_stopEvent (),
   103   m_sock (-1),
   104   m_readThread (0),
   105   m_ifIndex (-1),
   106   m_sll_ifindex (-1),
   107   m_name ("Emu NetDevice")
   108 {
   109   NS_LOG_FUNCTION (this);
   110   Start (m_tStart);
   111 }
   112 
   113 EmuNetDevice::~EmuNetDevice ()
   114 {
   115 }
   116 
   117 void 
   118 EmuNetDevice::DoDispose()
   119 {
   120   NS_LOG_FUNCTION_NOARGS ();
   121   m_node = 0;
   122   NetDevice::DoDispose ();
   123 }
   124 
   125 void
   126 EmuNetDevice::Start (Time tStart)
   127 {
   128   NS_LOG_FUNCTION (tStart);
   129 
   130   //
   131   // Cancel any pending start event and schedule a new one at some relative time in the future.
   132   //
   133   Simulator::Cancel (m_startEvent);
   134   m_startEvent = Simulator::Schedule (tStart, &EmuNetDevice::StartDevice, this);
   135 }
   136 
   137   void
   138 EmuNetDevice::Stop (Time tStop)
   139 {
   140   NS_LOG_FUNCTION (tStop);
   141   //
   142   // Cancel any pending stop event and schedule a new one at some relative time in the future.
   143   //
   144   Simulator::Cancel (m_stopEvent);
   145   m_startEvent = Simulator::Schedule (tStop, &EmuNetDevice::StopDevice, this);
   146 }
   147 
   148   void
   149 EmuNetDevice::StartDevice (void)
   150 {
   151   NS_LOG_FUNCTION_NOARGS ();
   152 
   153   //
   154   // Spin up the emu net device and start receiving packets.
   155   //
   156   NS_ASSERT_MSG (m_sock == -1, "EmuNetDevice::StartDevice(): Device is already started");
   157 
   158   NS_LOG_LOGIC ("Creating socket");
   159   //
   160   // Call out to a separate process running as suid root in order to get a raw 
   161   // socket.  We do this to avoid having the entire simulation running as root.
   162   // If this method returns, we'll have a raw socket waiting for us in m_sock.
   163   //
   164   CreateSocket ();
   165 
   166   //
   167   // Figure out which interface index corresponds to the device name in the corresponding attribute.
   168   //
   169   struct ifreq ifr;
   170   bzero (&ifr, sizeof(ifr));
   171   strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   172 
   173   NS_LOG_LOGIC ("Getting interface index");
   174   int32_t rc = ioctl (m_sock, SIOCGIFINDEX, &ifr);
   175   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::StartDevice(): Can't get interface index");
   176 
   177   //
   178   // Save the real interface index for later calls to sendto
   179   //
   180   m_sll_ifindex = ifr.ifr_ifindex;
   181 
   182   //
   183   // Bind the socket to the interface we just found.
   184   //
   185   struct sockaddr_ll ll;
   186   bzero (&ll, sizeof(ll));
   187 
   188   ll.sll_family = AF_PACKET;
   189   ll.sll_ifindex = m_sll_ifindex;
   190   ll.sll_protocol = htons(ETH_P_ALL); 
   191 
   192   NS_LOG_LOGIC ("Binding socket to interface");
   193 
   194   rc = bind (m_sock, (struct sockaddr *)&ll, sizeof (ll));
   195   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::StartDevice(): Can't bind to specified interface");
   196 
   197   rc = ioctl(m_sock, SIOCGIFFLAGS, &ifr);
   198   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::StartDevice(): Can't get interface flags");
   199   
   200   //
   201   // This device only works if the underlying interface is up in promiscuous 
   202   // mode.  We could have turned it on in the socket creator, but the situation
   203   // is that we expect these devices to be used in conjunction with virtual 
   204   // machines with connected host-only (simulated) networks, or in a testbed.
   205   // There is a lot of setup and configuration happening outside of this one 
   206   // issue, and we expect that configuration to include choosing a valid
   207   // interface (e.g, "ath1"), ensuring that the device supports promiscuous 
   208   // mode, and placing it in promiscuous mode.  We just make sure of the
   209   // end result.
   210   //
   211   if ((ifr.ifr_flags & IFF_PROMISC) == 0)
   212     {
   213       NS_FATAL_ERROR ("EmuNetDevice::StartDevice(): " << m_deviceName << " is not in promiscuous mode");
   214     }
   215 
   216   //
   217   // Now spin up a read thread to read packets.
   218   //
   219   NS_ASSERT_MSG (m_readThread == 0, "EmuNetDevice::StartDevice(): Receive thread is already running");
   220 
   221   NS_LOG_LOGIC ("Spinning up read thread");
   222 
   223   m_readThread = Create<SystemThread> (MakeCallback (&EmuNetDevice::ReadThread, this));
   224   m_readThread->Start ();
   225 
   226   NotifyLinkUp ();
   227 }
   228 
   229 void
   230 EmuNetDevice::CreateSocket (void)
   231 {
   232   NS_LOG_FUNCTION_NOARGS ();
   233   //
   234   // We want to create a raw socket for our net device.  Unfortunately for us
   235   // you have to have root privileges to do that.  Instead of running the 
   236   // entire simulation as root, we decided to make a small program who's whole
   237   // reason for being is to run as suid root and create a raw socket.  We're
   238   // going to fork and exec that program soon, but we need to have a socket
   239   // to talk to it with.  So we create a local interprocess (Unix) socket 
   240   // for that purpose.
   241   //
   242   int sock = socket (PF_UNIX, SOCK_DGRAM, 0);
   243   if (sock == -1)
   244     {
   245       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Unix socket creation error, errno = " << strerror (errno));
   246     }
   247 
   248   //
   249   // Bind to that socket and let the kernel allocate an endpoint
   250   //
   251   struct sockaddr_un un;
   252   memset (&un, 0, sizeof (un));
   253   un.sun_family = AF_UNIX;
   254   int status = bind (sock, (struct sockaddr*)&un, sizeof (sa_family_t));
   255   if (status == -1)
   256     {
   257       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Could not bind(): errno = " << strerror (errno));
   258     }
   259 
   260   NS_LOG_INFO ("Created Unix socket");
   261   NS_LOG_INFO ("sun_family = " << un.sun_family);
   262   NS_LOG_INFO ("sun_path = " << un.sun_path);
   263 
   264   //
   265   // We have a socket here, but we want to get it there -- to the program we're
   266   // going to exec.  What we'll do is to do a getsockname and then encode the
   267   // resulting address information as a string, and then send the string to the
   268   // program as an argument.  So we need to get the sock name.
   269   //
   270   socklen_t len = sizeof (un);
   271   status = getsockname (sock, (struct sockaddr*)&un, &len);
   272   if (status == -1)
   273     {
   274       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Could not getsockname(): errno = " << strerror (errno));
   275     }
   276 
   277   //
   278   // Now encode that socket name (family and path) as a string of hex digits
   279   //
   280   std::string path = EmuBufferToString((uint8_t *)&un, len);
   281   NS_LOG_INFO ("Encoded Unix socket as \"" << path << "\"");
   282   //
   283   // Fork and exec the process to create our socket.  If we're us (the parent)
   284   // we wait for the child (the socket creator) to complete and read the 
   285   // socket it created using the ancillary data mechanism.
   286   //
   287   pid_t pid = ::fork ();
   288   if (pid == 0)
   289     {
   290       NS_LOG_DEBUG ("Child process");
   291 
   292       //
   293       // build a command line argument from the encoded endpoint string that 
   294       // the socket creation process will use to figure out how to respond to
   295       // the (now) parent process.
   296       //
   297       std::ostringstream oss;
   298       oss << "-p" << path;
   299       NS_LOG_INFO ("Parameters set to \"" << oss.str () << "\"");
   300 
   301       //
   302       // Execute the socket creation process image.
   303       //
   304       status = ::execl (FindCreator ().c_str (), "emu-sock-creator", oss.str ().c_str (), (char *)NULL);
   305 
   306       //
   307       // If the execl successfully completes, it never returns.  If it returns it failed or the OS is
   308       // broken.  In either case, we bail.
   309       //
   310       NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Back from execl(), errno = " << ::strerror (errno));
   311     }
   312   else
   313     {
   314       NS_LOG_DEBUG ("Parent process");
   315       //
   316       // We're the process running the emu net device.  We need to wait for the
   317       // socket creator process to finish its job.
   318       //
   319       int st;
   320       pid_t waited = waitpid (pid, &st, 0);
   321       if (waited == -1)
   322 	{
   323 	  NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): waitpid() fails, errno = " << strerror (errno));
   324 	}
   325       NS_ASSERT_MSG (pid == waited, "EmuNetDevice::CreateSocket(): pid mismatch");
   326 
   327       //
   328       // Check to see if the socket creator exited normally and then take a 
   329       // look at the exit code.  If it bailed, so should we.  If it didn't
   330       // even exit normally, we bail too.
   331       //
   332       if (WIFEXITED (st))
   333 	{
   334           int exitStatus = WEXITSTATUS (st);
   335           if (exitStatus != 0)
   336             {
   337               NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): socket creator exited normally with status " << exitStatus);
   338             }
   339 	}
   340       else 
   341 	{
   342           NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): socket creator exited abnormally");
   343 	}
   344 
   345       //
   346       // At this point, the socket creator has run successfully and should 
   347       // have created our raw socket and sent it back to the socket address
   348       // we provided.  Our socket should be waiting on the Unix socket.  We've
   349       // got to do a bunch of grunto work to get at it, though.
   350       //
   351       // The struct iovec below is part of a scatter-gather list.  It describes a
   352       // buffer.  In this case, it describes a buffer (an integer) that will
   353       // get the data that comes back from the socket creator process.  It will
   354       // be a magic number that we use as a consistency/sanity check.
   355       // 
   356       struct iovec iov;
   357       uint32_t magic;
   358       iov.iov_base = &magic;
   359       iov.iov_len = sizeof(magic);
   360 
   361       //
   362       // The CMSG macros you'll see below are used to create and access control 
   363       // messages (which is another name for ancillary data).  The ancillary 
   364       // data is made up of pairs of struct cmsghdr structures and associated
   365       // data arrays.
   366       //
   367       // First, we're going to allocate a buffer on the stack to receive our 
   368       // data array (that contains the socket).  Sometimes you'll see this called
   369       // an "ancillary element" but the msghdr uses the control message termimology
   370       // so we call it "control."
   371       //
   372       size_t msg_size = sizeof(int);
   373       char control[CMSG_SPACE(msg_size)];
   374 
   375       //
   376       // There is a msghdr that is used to minimize the number of parameters
   377       // passed to recvmsg (which we will use to receive our ancillary data).  
   378       // This structure uses terminology corresponding to control messages, so
   379       // you'll see msg_control, which is the pointer to the ancillary data and 
   380       // controllen which is the size of the ancillary data array.
   381       //
   382       // So, initialize the message header that describes the ancillary/control
   383       // data we expect to receive and point it to buffer.
   384       //
   385       struct msghdr msg;
   386       msg.msg_name = 0;
   387       msg.msg_namelen = 0;
   388       msg.msg_iov = &iov;
   389       msg.msg_iovlen = 1;
   390       msg.msg_control = control;
   391       msg.msg_controllen = sizeof (control);
   392       msg.msg_flags = 0;
   393 
   394       //
   395       // Now we can actually receive the interesting bits from the socket
   396       // creator process.
   397       //
   398       ssize_t bytesRead = recvmsg (sock, &msg, 0);
   399       if (bytesRead != sizeof(int))
   400 	{
   401           NS_FATAL_ERROR ("EmuNetDevice::CreateSocket(): Wrong byte count from socket creator");
   402 	}
   403 
   404       //
   405       // There may be a number of message headers/ancillary data arrays coming in.
   406       // Let's look for the one with a type SCM_RIGHTS which indicates it' the
   407       // one we're interested in.
   408       //
   409       struct cmsghdr *cmsg;
   410       for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg, cmsg)) 
   411 	{
   412 	  if (cmsg->cmsg_level == SOL_SOCKET &&
   413 	      cmsg->cmsg_type == SCM_RIGHTS)
   414 	    {
   415               //
   416               // This is the type of message we want.  Check to see if the magic 
   417               // number is correct and then pull out the socket we care about if
   418               // it matches
   419               //
   420               if (magic == EMU_MAGIC)
   421                 {
   422                   NS_LOG_INFO ("Got SCM_RIGHTS with correct magic " << magic);
   423                   int *rawSocket = (int*)CMSG_DATA (cmsg);
   424                   NS_LOG_INFO ("Got the socket from the socket creator = " << *rawSocket);
   425                   m_sock = *rawSocket;
   426                   return;
   427                 }
   428               else
   429                 {
   430                   NS_LOG_INFO ("Got SCM_RIGHTS, but with bad magic " << magic);                  
   431                 }
   432 	    }
   433 	}
   434       NS_FATAL_ERROR ("Did not get the raw socket from the socket creator");
   435     }
   436 }
   437 
   438 std::string
   439 EmuNetDevice::FindCreator (void)
   440 {
   441   struct stat st;
   442   std::string debug = "./build/debug/src/devices/emu/emu-sock-creator";
   443   std::string optimized = "./build/optimized/src/devices/emu/emu-sock-creator";
   444 
   445   if (::stat (debug.c_str (), &st) == 0)
   446     {
   447       return debug;
   448     }
   449 
   450   if (::stat (optimized.c_str (), &st) == 0)
   451     {
   452       return optimized;
   453     }
   454 
   455   NS_FATAL_ERROR ("EmuNetDevice::FindCreator(): Couldn't find creator");
   456   return ""; // quiet compiler
   457 }
   458 
   459 void
   460 EmuNetDevice::StopDevice (void)
   461 {
   462   NS_LOG_FUNCTION_NOARGS ();
   463 
   464   close (m_sock);
   465   m_sock = -1;
   466 
   467   NS_ASSERT_MSG (m_readThread != 0, "EmuNetDevice::StopDevice(): Receive thread is not running");
   468 
   469   NS_LOG_LOGIC ("Joining read thread");
   470   m_readThread->Join ();
   471   m_readThread = 0;
   472 }
   473 
   474 void
   475 EmuNetDevice::ForwardUp (uint8_t *buf, uint32_t len)
   476 {
   477   NS_LOG_FUNCTION (buf << len);
   478 
   479   //
   480   // Create a packet out of the buffer we received and free that buffer.
   481   //
   482   Ptr<Packet> packet = Create<Packet> (reinterpret_cast<const uint8_t *> (buf), len);
   483   free (buf);
   484   buf = 0;
   485 
   486   //
   487   // Trace sinks will expect complete packets, not packets without some of the
   488   // headers.
   489   //
   490   Ptr<Packet> originalPacket = packet->Copy ();
   491 
   492   //
   493   // Checksum the packet
   494   //
   495   EthernetTrailer trailer;
   496   packet->RemoveTrailer (trailer);
   497   trailer.CheckFcs (packet);
   498 
   499   EthernetHeader header (false);
   500   packet->RemoveHeader (header);
   501 
   502   NS_LOG_LOGIC ("Pkt source is " << header.GetSource ());
   503   NS_LOG_LOGIC ("Pkt destination is " << header.GetDestination ());
   504 
   505   LlcSnapHeader llc;
   506   packet->RemoveHeader (llc);
   507   uint16_t protocol = llc.GetType ();
   508 
   509   PacketType packetType;
   510       
   511   if (header.GetDestination ().IsBroadcast ())
   512     {
   513       NS_LOG_LOGIC ("Pkt destination is PACKET_BROADCAST");
   514       packetType = NS3_PACKET_BROADCAST;
   515     }
   516   else if (header.GetDestination ().IsMulticast ())
   517     {
   518       NS_LOG_LOGIC ("Pkt destination is PACKET_MULTICAST");
   519       packetType = NS3_PACKET_MULTICAST;
   520     }
   521   else if (header.GetDestination () == m_address)
   522     {
   523       NS_LOG_LOGIC ("Pkt destination is PACKET_HOST");
   524       packetType = NS3_PACKET_HOST;
   525     }
   526   else
   527     {
   528       NS_LOG_LOGIC ("Pkt destination is PACKET_OTHERHOST");
   529       packetType = NS3_PACKET_OTHERHOST;
   530     }
   531 
   532   if (!m_promiscRxCallback.IsNull ())
   533     {
   534       NS_LOG_LOGIC ("calling m_promiscRxCallback");
   535       m_promiscRxCallback (this, packet->Copy (), protocol, header.GetSource (), header.GetDestination (), packetType);
   536     }
   537 
   538   if (packetType != NS3_PACKET_OTHERHOST)
   539     {
   540       m_rxTrace (originalPacket);
   541       NS_LOG_LOGIC ("calling m_rxCallback");
   542       m_rxCallback (this, packet, protocol, header.GetSource ());
   543     }
   544 }
   545 
   546 void
   547 EmuNetDevice::ReadThread (void)
   548 {
   549   NS_LOG_FUNCTION_NOARGS ();
   550 
   551   //
   552   // It's important to remember that we're in a completely different thread than the simulator is running in.  We
   553   // need to synchronize with that other thread to get the packet up into ns-3.  What we will need to do is to schedule 
   554   // a method to forward up the packet using the multithreaded simulator we are most certainly running.  However, I just 
   555   // said it -- we are talking about two threads here, so it is very, very dangerous to do any kind of reference couning
   556   // on a shared object.  Just don't do it.  So what we're going to do is to allocate a buffer on the heap and pass that
   557   // buffer into the ns-3 context thread where it will create the packet.
   558   //
   559 
   560   int32_t len = -1;
   561   struct sockaddr_ll addr;
   562   socklen_t addrSize = sizeof (addr);
   563 
   564   for (;;) 
   565     {
   566       uint32_t bufferSize = 65536;
   567       uint8_t *buf = (uint8_t *)malloc (bufferSize);
   568       NS_ASSERT_MSG (buf, "EmuNetDevice::ReadThread(): malloc packet buffer failed");
   569       NS_LOG_LOGIC ("Calling recvfrom");
   570       len = recvfrom (m_sock, buf, bufferSize, 0, (struct sockaddr *)&addr, &addrSize);
   571 
   572       if (len == -1)
   573         {
   574           free (buf);
   575           buf = 0;
   576           return;
   577         }
   578 
   579       NS_LOG_INFO ("EmuNetDevice::ReadThread(): Received packet");
   580       NS_LOG_INFO ("EmuNetDevice::ReadThread(): Scheduling handler");
   581       DynamicCast<RealtimeSimulatorImpl> (Simulator::GetImplementation ())->ScheduleRealtimeNow (
   582         MakeEvent (&EmuNetDevice::ForwardUp, this, buf, len));
   583       buf = 0;
   584     }
   585 }
   586 
   587 bool 
   588 EmuNetDevice::Send (Ptr<Packet> packet, const Address &dest, uint16_t protocolNumber)
   589 {
   590   NS_LOG_FUNCTION (packet << dest << protocolNumber);
   591   //
   592   // The immediate questions here are how are we going to encapsulate packets and what do we use as the MAC source and 
   593   // destination (hardware) addresses?
   594   //
   595   // If we return false from EmuNetDevice::NeedsArp, the ArpIpv4Interface will pass the broadcast address as the 
   596   // hardware (Ethernet) destination by default.  If we return true from EmuNetDevice::NeedsArp, then the hardware
   597   // destination is actually meaningful, but we'll have an ns-3 ARP running on this device.  There can also be an ARP
   598   // running on the underlying OS so we have to be very careful, both about multiple ARPs and also about TCP, UDP, etc.
   599   //
   600   // We are operating in promiscuous mode on the receive side (all ns-3 net devices are required to implement the 
   601   // promiscuous callback in a meaningful way), so we have an option regarding the hardware addresses.  We don't actually have
   602   // to use the real hardware addresses and IP addresses of the underlying system.  We can completely use MAC-spoofing to
   603   // fake out the OS by using the ns-3 assigned MAC address (and also the ns-3 assigned IP addresses).  Ns-3 starts its 
   604   // MAC address allocation using the OUI (vendor-code) 00:00:00 which is unassigned to any organization and is a globally
   605   // administered address, so there shouldn't be any collisions with real hardware.
   606   //
   607   // So what we do is we return true from EmuNetDevice::NeedsArp which tells ns-3 to use its own ARP.  We spoof the 
   608   // MAC address of the device and use promiscuous mode to receive traffic destined to that address.
   609   //
   610   return SendFrom (packet, m_address, dest, protocolNumber);
   611 }
   612 
   613 bool 
   614 EmuNetDevice::SendFrom (Ptr<Packet> packet, const Address &src, const Address &dest, uint16_t protocolNumber)
   615 {
   616   NS_LOG_FUNCTION (packet << src << dest << protocolNumber);
   617 
   618   if (IsLinkUp () == false)
   619     {
   620       NS_LOG_LOGIC ("Link is down, returning");
   621       return false;
   622     }
   623 
   624   Mac48Address destination = Mac48Address::ConvertFrom (dest);
   625   Mac48Address source = Mac48Address::ConvertFrom (src);
   626 
   627   NS_LOG_LOGIC ("Transmit packet with UID " << packet->GetUid ());
   628   NS_LOG_LOGIC ("Transmit packet from " << source);
   629   NS_LOG_LOGIC ("Transmit packet to " << destination);
   630 
   631 #if 0
   632   {
   633     struct ifreq ifr;
   634     bzero (&ifr, sizeof(ifr));
   635     strncpy ((char *)ifr.ifr_name, m_deviceName.c_str (), IFNAMSIZ);
   636 
   637     NS_LOG_LOGIC ("Getting MAC address");
   638     int32_t rc = ioctl (m_sock, SIOCGIFHWADDR, &ifr);
   639     NS_ASSERT_MSG (rc != -1, "EmuNetDevice::SendFrom(): Can't get MAC address");
   640 
   641     std::ostringstream oss;
   642     oss << std::hex <<
   643       (ifr.ifr_hwaddr.sa_data[0] & 0xff) << ":" <<
   644       (ifr.ifr_hwaddr.sa_data[1] & 0xff) << ":" <<
   645       (ifr.ifr_hwaddr.sa_data[2] & 0xff) << ":" <<
   646       (ifr.ifr_hwaddr.sa_data[3] & 0xff) << ":" <<
   647       (ifr.ifr_hwaddr.sa_data[4] & 0xff) << ":" <<
   648       (ifr.ifr_hwaddr.sa_data[5] & 0xff) << std::dec;
   649 
   650     NS_LOG_LOGIC ("Fixup source to HW MAC " << oss.str ());
   651     source = Mac48Address (oss.str ().c_str ());
   652     NS_LOG_LOGIC ("source now " << source);
   653   }
   654 #endif
   655 
   656   LlcSnapHeader llc;
   657   llc.SetType (protocolNumber);
   658   packet->AddHeader (llc);
   659 
   660   EthernetHeader header (false);
   661   header.SetSource (source);
   662   header.SetDestination (destination);
   663   header.SetLengthType (packet->GetSize ());
   664   packet->AddHeader (header);
   665 
   666   EthernetTrailer trailer;
   667   trailer.CalcFcs (packet);
   668   packet->AddTrailer (trailer);
   669 
   670   // 
   671   // Enqueue and dequeue the packet to hit the tracing hooks.
   672   //
   673   m_queue->Enqueue (packet);
   674   packet = m_queue->Dequeue ();
   675 
   676   struct sockaddr_ll ll;
   677   bzero (&ll, sizeof (ll));
   678 
   679   ll.sll_family = AF_PACKET;
   680   ll.sll_ifindex = m_sll_ifindex;
   681   ll.sll_protocol = htons(ETH_P_ALL); 
   682 
   683   NS_LOG_LOGIC ("calling sendto");
   684 
   685   int32_t rc;
   686   rc = sendto (m_sock, packet->PeekData (), packet->GetSize (), 0, reinterpret_cast<struct sockaddr *> (&ll), sizeof (ll));
   687 
   688   NS_LOG_LOGIC ("sendto returns " << rc);
   689 
   690   return rc == -1 ? false : true;
   691 }
   692 
   693 void 
   694 EmuNetDevice::SetDataRate(DataRate bps)
   695 {
   696   NS_LOG_FUNCTION (this << bps);
   697   NS_ASSERT_MSG (false, "EmuNetDevice::SetDataRate():  Unable."); 
   698 }
   699 
   700 void
   701 EmuNetDevice::SetQueue (Ptr<Queue> q)
   702 {
   703   NS_LOG_FUNCTION (this << q);
   704   m_queue = q;
   705 }
   706 
   707 Ptr<Queue> 
   708 EmuNetDevice::GetQueue(void) const 
   709 { 
   710   NS_LOG_FUNCTION_NOARGS ();
   711   return m_queue;
   712 }
   713 
   714 void
   715 EmuNetDevice::NotifyLinkUp (void)
   716 {
   717   m_linkUp = true;
   718   if (!m_linkChangeCallback.IsNull ())
   719     {
   720       m_linkChangeCallback ();
   721     }
   722 }
   723 
   724 void 
   725 EmuNetDevice::SetName(const std::string name)
   726 {
   727   m_name = name;
   728 }
   729 
   730 std::string 
   731 EmuNetDevice::GetName(void) const
   732 {
   733   return m_name;
   734 }
   735 
   736 void 
   737 EmuNetDevice::SetIfIndex(const uint32_t index)
   738 {
   739   m_ifIndex = index;
   740 }
   741 
   742 uint32_t 
   743 EmuNetDevice::GetIfIndex(void) const
   744 {
   745   return m_ifIndex;
   746 }
   747 
   748 Ptr<Channel> 
   749 EmuNetDevice::GetChannel (void) const
   750 {
   751   NS_ASSERT_MSG (false, "EmuNetDevice::GetChannel():  Unable."); 
   752   return 0;
   753 }
   754 
   755 void 
   756 EmuNetDevice::SetAddress (Mac48Address addr)
   757 {
   758   NS_LOG_FUNCTION (addr);
   759   m_address = addr;
   760 }
   761 
   762 Address 
   763 EmuNetDevice::GetAddress (void) const
   764 {
   765   NS_LOG_FUNCTION_NOARGS ();
   766   return m_address;
   767 }
   768 
   769 bool 
   770 EmuNetDevice::SetMtu (const uint16_t mtu)
   771 {
   772   NS_ASSERT_MSG (false, "EmuNetDevice::SetMtu():  Unable."); 
   773   return false;
   774 }
   775 
   776 uint16_t 
   777 EmuNetDevice::GetMtu (void) const
   778 {
   779   struct ifreq ifr;
   780   bzero (&ifr, sizeof (ifr));
   781   strcpy(ifr.ifr_name, m_deviceName.c_str ());
   782 
   783   int32_t fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP);
   784 
   785   int32_t rc = ioctl(fd, SIOCGIFMTU, &ifr);
   786   NS_ASSERT_MSG (rc != -1, "EmuNetDevice::GetMtu(): Can't ioctl SIOCGIFMTU");
   787 
   788   close (fd);
   789 
   790   return ifr.ifr_mtu;
   791 }
   792 
   793 bool 
   794 EmuNetDevice::IsLinkUp (void) const
   795 {
   796   return m_linkUp;
   797 }
   798 
   799 void 
   800 EmuNetDevice::SetLinkChangeCallback (Callback<void> callback)
   801 {
   802   m_linkChangeCallback = callback;
   803 }
   804 
   805 bool 
   806 EmuNetDevice::IsBroadcast (void) const
   807 {
   808   return true;
   809 }
   810 
   811 Address
   812 EmuNetDevice::GetBroadcast (void) const
   813 {
   814   return Mac48Address ("ff:ff:ff:ff:ff:ff");
   815 }
   816 
   817 bool 
   818 EmuNetDevice::IsMulticast (void) const
   819 {
   820   return false;
   821 }
   822 
   823   Address 
   824 EmuNetDevice::GetMulticast (Ipv4Address multicastGroup) const
   825 {
   826   NS_LOG_FUNCTION (multicastGroup);
   827 
   828   Mac48Address ad = Mac48Address::GetMulticast (multicastGroup);
   829 
   830   //
   831   // Implicit conversion (operator Address ()) is defined for Mac48Address, so
   832   // use it by just returning the EUI-48 address which is automagically converted
   833   // to an Address.
   834   //
   835   NS_LOG_LOGIC ("multicast address is " << ad);
   836 
   837   return ad;
   838 }
   839 
   840 bool 
   841 EmuNetDevice::IsPointToPoint (void) const
   842 {
   843   return false;
   844 }
   845 
   846 void
   847 EmuNetDevice::SetPromiscReceiveCallback (PromiscReceiveCallback cb)
   848 {
   849   NS_ASSERT_MSG (false, "EmuNetDevice::SetPromiscReceiveCallback(): Not implemented");
   850 }
   851 
   852   bool 
   853 EmuNetDevice::SupportsSendFrom () const
   854 {
   855   NS_LOG_FUNCTION_NOARGS ();
   856   return true;
   857 }
   858 
   859 
   860 Ptr<Node> 
   861 EmuNetDevice::GetNode (void) const
   862 {
   863   return m_node;
   864 }
   865 
   866 void 
   867 EmuNetDevice::SetNode (Ptr<Node> node)
   868 {
   869   m_node = node;
   870 }
   871 
   872 bool 
   873 EmuNetDevice::NeedsArp (void) const
   874 {
   875   return true;
   876 }
   877 
   878 void 
   879 EmuNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
   880 {
   881   m_rxCallback = cb;
   882 }
   883 
   884 } // namespace ns3