Simplify promiscuous mode API: NetDevices always operate in promiscuous mode, normal receive callbacks receive extra destination address and packet type.

This commit is contained in:
Gustavo J. A. M. Carneiro committed 2008-07-07 12:18:05 +01:00
1 parent 8f38c8907b
commit e525ff58ab
20 files changed
+115 -222

No files matched your search

+13 -28
View File
@@ -254,17 +254,28 @@ public:
*/
virtual bool NeedsArp (void) const = 0;
/** Packet types */
enum PacketType
{
PACKET_HOST = 1, /* To us */
PACKET_BROADCAST, /* To all */
PACKET_MULTICAST, /* To group */
PACKET_OTHERHOST, /* To someone else */
};
/**
* \param device a pointer to the net device which is calling this callback
* \param packet the packet received
* \param protocol the 16 bit protocol number associated with this packet.
* This protocol number is expected to be the same protocol number
* given to the Send method by the user on the sender side.
* \param address the address of the sender
* \param sender the address of the sender
* \param receiver the address of the receiver
* \returns true if the callback could handle the packet successfully, false
* otherwise.
*/
typedef Callback<bool,Ptr<NetDevice>,Ptr<Packet>,uint16_t,const Address &> ReceiveCallback;
typedef Callback<bool,Ptr<NetDevice>,Ptr<Packet>,uint16_t,const Address &,const Address &, PacketType> ReceiveCallback;
/**
* \param cb callback to invoke whenever a packet has been received and must
@@ -273,32 +284,6 @@ public:
*/
virtual void SetReceiveCallback (ReceiveCallback cb) = 0;
/**
* \param device a pointer to the net device which is calling this callback
* \param packet the packet received
* \param protocol the 16 bit protocol number associated with this packet.
* This protocol number is expected to be the same protocol number
* given to the Send method by the user on the sender side.
* \param sourceAddress source address
* \param destinationAddress destination address
* \param forMe true if the packet is normally picked up also for
* the non-promiscuous callback, false if it is received exclusively
* by the promiscuous callback.
* \returns true if the callback could handle the packet successfully, false
* otherwise.
*/
typedef Callback<bool,Ptr<NetDevice>,Ptr<Packet>,uint16_t,
const Address &, const Address &, bool> PromiscuousReceiveCallback;
/**
* \param cb callback to invoke whenever a packet has been received
* in promiscuous mode. Note that PromiscuousReceiveCallback
* handles both packets for the device and packets not for
* it. In that sense, it receives a superset of packets
* received by the normal ReceivedCallback.
*/
virtual void SetPromiscuousReceiveCallback (PromiscuousReceiveCallback cb) = 0;
};
} // namespace ns3
+3 -58
View File
@@ -96,7 +96,6 @@ Node::AddDevice (Ptr<NetDevice> device)
device->SetNode (this);
device->SetIfIndex(index);
device->SetReceiveCallback (MakeCallback (&Node::ReceiveFromDevice, this));
device->SetPromiscuousReceiveCallback (MakeCallback (&Node::PromiscuousReceiveFromDevice, this));
NotifyDeviceAdded (device);
return index;
}
@@ -182,35 +181,9 @@ Node::UnregisterProtocolHandler (ProtocolHandler handler)
}
}
void
Node::RegisterPromiscuousProtocolHandler (PromiscuousProtocolHandler handler,
uint16_t protocolType,
Ptr<NetDevice> device)
{
struct Node::PromiscuousProtocolHandlerEntry entry;
entry.handler = handler;
entry.protocol = protocolType;
entry.device = device;
m_promiscuousHandlers.push_back (entry);
}
void
Node::UnregisterPromiscuousProtocolHandler (PromiscuousProtocolHandler handler)
{
for (PromiscuousProtocolHandlerList::iterator i = m_promiscuousHandlers.begin ();
i != m_promiscuousHandlers.end (); i++)
{
if (i->handler.IsEqual (handler))
{
m_promiscuousHandlers.erase (i);
break;
}
}
}
bool
Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet,
uint16_t protocol, const Address &from)
Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet, uint16_t protocol,
const Address &from, const Address &to, NetDevice::PacketType packetType)
{
bool found = false;
// if there are (potentially) multiple handlers, we need to copy the
@@ -227,35 +200,7 @@ Node::ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet,
if (i->protocol == 0 ||
i->protocol == protocol)
{
i->handler (device, (copyNeeded ? packet->Copy () : packet), protocol, from);
found = true;
}
}
}
return found;
}
bool
Node::PromiscuousReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet> packet,
uint16_t protocol, const Address &from, const Address &to, bool forMe)
{
bool found = false;
// if there are (potentially) multiple handlers, we need to copy the
// packet before passing it to each handler, because handlers may
// modify it.
bool copyNeeded = (m_handlers.size () > 1);
for (PromiscuousProtocolHandlerList::iterator i = m_promiscuousHandlers.begin ();
i != m_promiscuousHandlers.end (); i++)
{
if (i->device == 0 ||
(i->device != 0 && i->device == device))
{
if (i->protocol == 0 ||
i->protocol == protocol)
{
i->handler (device, (copyNeeded ? packet->Copy () : packet), protocol, from, to, forMe);
i->handler (device, (copyNeeded ? packet->Copy () : packet), protocol, from, to, packetType);
found = true;
}
}
+5 -14
View File
@@ -26,10 +26,10 @@
#include "ns3/object.h"
#include "ns3/callback.h"
#include "ns3/ptr.h"
#include "ns3/net-device.h"
namespace ns3 {
class NetDevice;
class Application;
class Packet;
class Address;
@@ -130,7 +130,8 @@ public:
/**
* A protocol handler
*/
typedef Callback<void,Ptr<NetDevice>, Ptr<Packet>,uint16_t,const Address &> ProtocolHandler;
typedef Callback<void,Ptr<NetDevice>, Ptr<Packet>,uint16_t,const Address &,
const Address &, NetDevice::PacketType> ProtocolHandler;
/**
* \param handler the handler to register
* \param protocolType the type of protocol this handler is
@@ -199,10 +200,8 @@ private:
*/
virtual void NotifyDeviceAdded (Ptr<NetDevice> device);
bool ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet>,
uint16_t protocol, const Address &from);
bool PromiscuousReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet>,
uint16_t protocol, const Address &from, const Address &to, bool forMe);
bool ReceiveFromDevice (Ptr<NetDevice> device, Ptr<Packet>, uint16_t protocol,
const Address &from, const Address &to, NetDevice::PacketType packetType);
void Construct (void);
struct ProtocolHandlerEntry {
@@ -217,14 +216,6 @@ private:
std::vector<Ptr<Application> > m_applications;
ProtocolHandlerList m_handlers;
// promiscuous protocol handlers
struct PromiscuousProtocolHandlerEntry {
PromiscuousProtocolHandler handler;
uint16_t protocol;
Ptr<NetDevice> device;
};
typedef std::vector<struct Node::PromiscuousProtocolHandlerEntry> PromiscuousProtocolHandlerList;
PromiscuousProtocolHandlerList m_promiscuousHandlers;
};
} //namespace ns3
+7 -1
View File
@@ -345,13 +345,19 @@ PacketSocket::SendTo (Ptr<Packet> p, uint32_t flags, const Address &address)
void
PacketSocket::ForwardUp (Ptr<NetDevice> device, Ptr<Packet> packet,
uint16_t protocol, const Address &from)
uint16_t protocol, const Address &from,
const Address &to, NetDevice::PacketType packetType)
{
NS_LOG_FUNCTION_NOARGS ();
if (m_shutdownRecv)
{
return;
}
if (packetType != NetDevice::PACKET_HOST)
{
return;
}
PacketSocketAddress address;
address.SetPhysicalAddress (from);
+3 -1
View File
@@ -27,6 +27,7 @@
#include "ns3/traced-callback.h"
#include "ns3/ptr.h"
#include "ns3/socket.h"
#include "ns3/net-device.h"
namespace ns3 {
@@ -103,7 +104,8 @@ public:
private:
void ForwardUp (Ptr<NetDevice> device, Ptr<Packet> packet,
uint16_t protocol, const Address &from);
uint16_t protocol, const Address &from, const Address &to,
NetDevice::PacketType packetType);
int DoBind (const PacketSocketAddress &address);
uint32_t GetMinMtu (PacketSocketAddress ad) const;
virtual void DoDispose (void);
+12 -6
View File
@@ -46,10 +46,20 @@ void
SimpleNetDevice::Receive (Ptr<Packet> packet, uint16_t protocol,
Mac48Address to, Mac48Address from)
{
if (to == m_address || to == Mac48Address::GetBroadcast ())
NetDevice::PacketType packetType;
if (to == m_address)
{
m_rxCallback (this, packet, protocol, from);
packetType = NetDevice::PACKET_HOST;
}
else if (to == Mac48Address::GetBroadcast ())
{
packetType = NetDevice::PACKET_HOST;
}
else
{
NS_FATAL_ERROR ("Weird packet destination " << to);
}
m_rxCallback (this, packet, protocol, from, to, packetType);
}
void
@@ -180,10 +190,6 @@ SimpleNetDevice::SetReceiveCallback (NetDevice::ReceiveCallback cb)
{
m_rxCallback = cb;
}
void
SimpleNetDevice::SetPromiscuousReceiveCallback (NetDevice::PromiscuousReceiveCallback cb)
{
}
void
SimpleNetDevice::DoDispose (void)
-1
View File
@@ -68,7 +68,6 @@ public:
virtual void SetNode (Ptr<Node> node);
virtual bool NeedsArp (void) const;
virtual void SetReceiveCallback (NetDevice::ReceiveCallback cb);
virtual void SetPromiscuousReceiveCallback (PromiscuousReceiveCallback cb);
protected:
virtual void DoDispose (void);