src/node/node.cc
changeset 3460 99698bc858e8
parent 3450 73e90de6eb47
child 3513 fe3945d67a88
equal deleted inserted replaced
3459:a67df503c7bc 3460:99698bc858e8
    24 #include "application.h"
    24 #include "application.h"
    25 #include "ns3/packet.h"
    25 #include "ns3/packet.h"
    26 #include "ns3/simulator.h"
    26 #include "ns3/simulator.h"
    27 #include "ns3/object-vector.h"
    27 #include "ns3/object-vector.h"
    28 #include "ns3/uinteger.h"
    28 #include "ns3/uinteger.h"
       
    29 #include "ns3/log.h"
       
    30 
       
    31 NS_LOG_COMPONENT_DEFINE ("Node");
    29 
    32 
    30 namespace ns3{
    33 namespace ns3{
    31 
    34 
    32 NS_OBJECT_ENSURE_REGISTERED (Node);
    35 NS_OBJECT_ENSURE_REGISTERED (Node);
    33 
    36 
    93 {
    96 {
    94   uint32_t index = m_devices.size ();
    97   uint32_t index = m_devices.size ();
    95   m_devices.push_back (device);
    98   m_devices.push_back (device);
    96   device->SetNode (this);
    99   device->SetNode (this);
    97   device->SetIfIndex(index);
   100   device->SetIfIndex(index);
    98   device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this));
   101   device->SetReceiveCallback (MakeCallback (&Node::NonPromiscReceiveFromDevice, this));
    99   NotifyDeviceAdded (device);
   102   NotifyDeviceAdded (device);
   100   return index;
   103   return index;
   101 }
   104 }
   102 Ptr<NetDevice>
   105 Ptr<NetDevice>
   103 Node::GetDevice (uint32_t index) const
   106 Node::GetDevice (uint32_t index) const
   168 {}
   171 {}
   169 
   172 
   170 void
   173 void
   171 Node::RegisterProtocolHandler (ProtocolHandler handler, 
   174 Node::RegisterProtocolHandler (ProtocolHandler handler, 
   172                                uint16_t protocolType,
   175                                uint16_t protocolType,
   173                                Ptr<NetDevice> device)
   176                                Ptr<NetDevice> device,
       
   177                                bool promiscuous)
   174 {
   178 {
   175   struct Node::ProtocolHandlerEntry entry;
   179   struct Node::ProtocolHandlerEntry entry;
   176   entry.handler = handler;
   180   entry.handler = handler;
   177   entry.protocol = protocolType;
   181   entry.protocol = protocolType;
   178   entry.device = device;
   182   entry.device = device;
       
   183   entry.promiscuous = promiscuous;
       
   184 
       
   185   // On demand enable promiscuous mode in netdevices
       
   186   if (promiscuous)
       
   187     {
       
   188       if (device == 0)
       
   189         {
       
   190           for (std::vector<Ptr<NetDevice> >::iterator i = m_devices.begin ();
       
   191                i != m_devices.end (); i++)
       
   192             {
       
   193               Ptr<NetDevice> dev = *i;
       
   194               if (dev->SupportsPromiscuous ())
       
   195                 {
       
   196                   dev->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
       
   197                 }
       
   198             }
       
   199         }
       
   200       else
       
   201         {
       
   202           if (device->SupportsPromiscuous ())
       
   203             {
       
   204               device->SetPromiscReceiveCallback (MakeCallback (&Node::PromiscReceiveFromDevice, this));
       
   205             }
       
   206           else
       
   207             {
       
   208               NS_LOG_WARN ("Protocol handler request promiscuous mode for a specific netdevice,"
       
   209                            " but netdevice does not support promiscuous mode.");
       
   210             }
       
   211         }
       
   212     }
       
   213 
   179   m_handlers.push_back (entry);
   214   m_handlers.push_back (entry);
   180 }
   215 }
   181 
   216 
   182 void
   217 void
   183 Node::UnregisterProtocolHandler (ProtocolHandler handler)
   218 Node::UnregisterProtocolHandler (ProtocolHandler handler)
   192         }
   227         }
   193     }
   228     }
   194 }
   229 }
   195 
   230 
   196 bool
   231 bool
       
   232 Node::PromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet, uint16_t protocol,
       
   233                                 const Address &from, const Address &to, NetDevice::PacketType packetType)
       
   234 {
       
   235   NS_LOG_FUNCTION(device->GetName ());
       
   236   return ReceiveFromDevice (device, packet, protocol, from, to, packetType, true);
       
   237 }
       
   238 
       
   239 bool
       
   240 Node::NonPromiscReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet, uint16_t protocol,
       
   241                                    const Address &from)
       
   242 {
       
   243   NS_LOG_FUNCTION(device->GetName ());
       
   244   return ReceiveFromDevice (device, packet, protocol, from, from, NetDevice::PacketType (0), false);
       
   245 }
       
   246 
       
   247 bool
   197 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet, uint16_t protocol,
   248 Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet, uint16_t protocol,
   198                          const Address &from, const Address &to, NetDevice::PacketType packetType)
   249                          const Address &from, const Address &to, NetDevice::PacketType packetType, bool promiscuous)
   199 {
   250 {
       
   251   NS_LOG_FUNCTION(device->GetName ());
   200   bool found = false;
   252   bool found = false;
   201   // if there are (potentially) multiple handlers, we need to copy the
   253   // if there are (potentially) multiple handlers, we need to copy the
   202   // packet before passing it to each handler, because handlers may
   254   // packet before passing it to each handler, because handlers may
   203   // modify it.
   255   // modify it.
   204   bool copyNeeded = (m_handlers.size () > 1);
   256   bool copyNeeded = (m_handlers.size () > 1);
   210           (i->device != 0 && i->device == device))
   262           (i->device != 0 && i->device == device))
   211         {
   263         {
   212           if (i->protocol == 0 || 
   264           if (i->protocol == 0 || 
   213               i->protocol == protocol)
   265               i->protocol == protocol)
   214             {
   266             {
   215               i->handler (device, (copyNeeded ? packet->Copy () : packet), protocol, from, to, packetType);
   267               if (promiscuous == i->promiscuous)
   216               found = true;
   268                 {
       
   269                   i->handler (device, (copyNeeded ? packet->Copy () : packet), protocol, from, to, packetType);
       
   270                   found = true;
       
   271                 }
   217             }
   272             }
   218         }
   273         }
   219     }
   274     }
   220   return found;
   275   return found;
   221 }
   276 }